This commit is contained in:
huangyuhui
2017-01-27 16:26:02 +08:00
parent 68cfa3a5da
commit 809d7378e2
19 changed files with 117 additions and 234 deletions

View File

@@ -31,7 +31,7 @@ import org.jackhuang.hellominecraft.util.system.IOUtils;
/**
*
* @author huang
* @author huangyuhui
*/
public final class NetUtils {

View File

@@ -24,7 +24,7 @@ import java.util.Locale;
* @author huangyuhui
*/
public enum SupportedLocales {
def(Locale.getDefault(), "lang.default"), en(Locale.ENGLISH, null), zh_TW(Locale.TRADITIONAL_CHINESE, null), zh_CN(Locale.SIMPLIFIED_CHINESE, null);
def(Locale.getDefault(), "lang.default"), en(Locale.ENGLISH, null), zh_TW(Locale.TRADITIONAL_CHINESE, null), zh_CN(Locale.SIMPLIFIED_CHINESE, null), vi(new Locale("vi"), null);
public Locale self;
private String showString, customized;

View File

@@ -26,7 +26,7 @@ import java.util.HashSet;
*/
public class ParallelTask extends Task {
Collection<Task> dependsTask = new HashSet<>();
Collection<Task> tasks = new HashSet<>();
public ParallelTask() {
hidden = true;
@@ -43,11 +43,11 @@ public class ParallelTask extends Task {
@Override
public Collection<Task> getDependTasks() {
return dependsTask;
return tasks;
}
public void addDependsTask(Task t) {
dependsTask.add(t);
public void addTask(Task t) {
tasks.add(t);
}
}

View File

@@ -23,15 +23,13 @@ package org.jackhuang.hellominecraft.util.tasks;
*/
public abstract class TaskInfo extends Task {
String info;
public TaskInfo(String info) {
this.info = info;
this.tag = info;
}
@Override
public String getInfo() {
return info;
return tag;
}
}

View File

@@ -70,12 +70,10 @@ public class TaskWindow extends javax.swing.JDialog
return;
taskList = new TaskList();
taskList.addTaskListener(this);
taskList.doneEvent.register(() -> {
SwingUtilities.invokeLater(() -> {
dispose();
suc = true;
});
});
taskList.doneEvent.register(SwingUtils.invokeLater(() -> {
dispose();
suc = true;
}));
}
public static String downloadSource = "";

View File

@@ -1,38 +0,0 @@
/*
* Hello Minecraft!.
* Copyright (C) 2013 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hellominecraft.util.tasks.comm;
/**
*
* @author huangyuhui
* @param <T> the type of result.
*/
public class DefaultPreviousResult<T> implements PreviousResult<T> {
T a;
public DefaultPreviousResult(T a) {
this.a = a;
}
@Override
public T getResult() {
return a;
}
}

View File

@@ -30,7 +30,7 @@ public interface PreviousResultRegistrar<T> {
*
* @param pr previous task handler
*
* @return task self instance
* @return task self instance(factory mode!)
*/
Task registerPreviousResult(PreviousResult<T> pr);
}

View File

@@ -1,31 +0,0 @@
/*
* Hello Minecraft!.
* Copyright (C) 2013 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hellominecraft.util.tasks.download;
import org.jackhuang.hellominecraft.util.tasks.ProgressProviderListener;
/**
*
* @author huangyuhui
*/
public interface DownloadListener extends ProgressProviderListener {
boolean OnFailed();
void OnFailedMoreThan5Times(String url);
}

View File

@@ -130,12 +130,12 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
// Make sure response code is in the 200 range.
if (con.getResponseCode() / 100 != 2)
throw new NetException(C.i18n("download.not_200") + " " + con.getResponseCode());
throw new IOException(C.i18n("download.not_200") + " " + con.getResponseCode());
// Check for valid content length.
int contentLength = con.getContentLength();
if (contentLength < 1)
throw new NetException("The content length is invalid.");
throw new IOException("The content length is invalid.");
if (!filePath.getParentFile().mkdirs() && !filePath.getParentFile().isDirectory())
throw new IOException("Could not make directory");
@@ -210,8 +210,8 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
if (ppl != null)
ppl.onProgressProviderDone(this);
return;
} catch (IOException | IllegalStateException | NetException e) {
setFailReason(new NetException(C.i18n("download.failed") + " " + url, e));
} catch (IOException | IllegalStateException e) {
setFailReason(new IOException(C.i18n("download.failed") + " " + url, e));
} finally {
closeFiles();
}

View File

@@ -85,7 +85,7 @@ public class HTTPGetTask extends Task implements PreviousResult<String> {
doneEvent.execute(result);
return;
} catch (IOException ex) {
t = new NetException("Failed to get " + url, ex);
t = new IOException("Failed to get " + url, ex);
}
}
if (t != null)

View File

@@ -1,38 +0,0 @@
/*
* Hello Minecraft!.
* Copyright (C) 2013 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hellominecraft.util.tasks.download;
/**
*
* @author huangyuhui
*/
public class NetException extends RuntimeException {
public NetException(Exception message) {
super(message);
}
public NetException(String message) {
super(message);
}
public NetException(String message, Exception e) {
super(message, e);
}
}

View File

@@ -51,12 +51,9 @@ public class LogWindowOutputStream extends OutputStream {
}
private void append(final String str) {
try {
SwingUtilities.invokeLater(() -> {
txt.log(str, Level.guessLevel(str, sas));
});
} catch (Throwable ignore) {
}
SwingUtilities.invokeLater(() -> {
txt.log(str, Level.guessLevel(str, sas));
});
}
@Override

View File

@@ -41,6 +41,7 @@ import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import org.jackhuang.hellominecraft.util.C;
import org.jackhuang.hellominecraft.util.logging.HMCLog;
@@ -55,58 +56,58 @@ import org.jackhuang.hellominecraft.util.system.OS;
* @author huang
*/
public final class SwingUtils {
private SwingUtils() {
}
/**
* Make DefaultTableModel by overriding getColumnClass and isCellEditable of
* DefaultTableModel.
*
* @param titleA The title of each column.
* @param typesA The type of each column value.
* @param titleA The title of each column.
* @param typesA The type of each column value.
* @param canEditA Is column editable?
*
* @return
*/
public static DefaultTableModel makeDefaultTableModel(String[] titleA, final Class[] typesA, final boolean[] canEditA) {
return new DefaultTableModel(
new Object[][] {},
titleA) {
new Object[][] {},
titleA) {
Class[] types = typesA;
boolean[] canEdit = canEditA;
@Override
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit[columnIndex];
}
};
}
public static void openFolder(File f) {
f.mkdirs();
String path = f.getAbsolutePath();
switch (OS.os()) {
case OSX:
try {
Runtime.getRuntime().exec(new String[] { "/usr/bin/open", path });
} catch (IOException ex) {
HMCLog.err("Failed to open " + path + " through /usr/bin/open", ex);
}
break;
default:
try {
java.awt.Desktop.getDesktop().open(f);
} catch (Throwable ex) {
MessageBox.show(C.i18n("message.cannot_open_explorer") + ex.getMessage());
HMCLog.warn("Failed to open " + path + " through java.awt.Desktop.getDesktop().open()", ex);
}
break;
case OSX:
try {
Runtime.getRuntime().exec(new String[] { "/usr/bin/open", path });
} catch (IOException ex) {
HMCLog.err("Failed to open " + path + " through /usr/bin/open", ex);
}
break;
default:
try {
java.awt.Desktop.getDesktop().open(f);
} catch (Throwable ex) {
MessageBox.show(C.i18n("message.cannot_open_explorer") + ex.getMessage());
HMCLog.warn("Failed to open " + path + " through java.awt.Desktop.getDesktop().open()", ex);
}
break;
}
}
@@ -165,18 +166,18 @@ public final class SwingUtils {
/**
* Append new element to JList
*
* @param list the JList
* @param list the JList
* @param element the Element
*/
public static void appendLast(JList list, Object element) {
getDefaultListModel(list).addElement(element);
}
public static void replaceLast(JList list, Object element) {
DefaultListModel model = getDefaultListModel(list);
model.set(model.getSize() - 1, element);
}
public static void clear(JList list) {
list.setModel(new DefaultListModel());
}
@@ -194,17 +195,17 @@ public final class SwingUtils {
model.removeRow(0);
return model;
}
public static void appendLast(JTable table, Object... elements) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(elements);
}
public static void setValueAt(JTable table, Object element, int row, int col) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setValueAt(element, row, col);
}
public static Object[] getValueBySelectedRow(JTable table, int rows[], int col) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
Object[] ret = new Object[rows.length];
@@ -212,12 +213,12 @@ public final class SwingUtils {
ret[i] = model.getValueAt(rows[i], col);
return ret;
}
public static void removeRow(JTable table, int row) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.removeRow(row);
}
public static String getParsedJPanelText(JLabel jLabel1, String longString) {
if (StrUtils.isBlank(longString))
return longString;
@@ -239,11 +240,11 @@ public final class SwingUtils {
}
return builder.toString();
}
private static final Map<Integer, Object> INVOKE_AND_WAIT_MAP = Collections.synchronizedMap(new HashMap<>());
private static int INVOKE_AND_WAIT_ID = 0;
private static final Object INVOKE_AND_WAIT_LOCK = new Object();
public static <T> T invokeAndWait(NonFunction<T> x) {
int id;
synchronized (INVOKE_AND_WAIT_LOCK) {
@@ -254,7 +255,7 @@ public final class SwingUtils {
invokeAndWait(r);
return (T) INVOKE_AND_WAIT_MAP.remove(id);
}
public static void invokeAndWait(Runnable r) {
if (EventQueue.isDispatchThread())
r.run();
@@ -266,7 +267,7 @@ public final class SwingUtils {
r.run();
}
}
public static int select(String[] selList, String msg) {
Object msgs[] = new Object[2];
msgs[0] = msg;
@@ -276,7 +277,7 @@ public final class SwingUtils {
return -1;
return ((JComboBox) msgs[1]).getSelectedIndex();
}
public static void setEnabled(JComponent component, boolean t) {
synchronized (component.getTreeLock()) {
for (Component c : component.getComponents())
@@ -285,11 +286,11 @@ public final class SwingUtils {
}
component.setEnabled(t);
}
public static void exitIfNoWindow(Window thisFrame) {
exitIfNoWindow(thisFrame, false);
}
public static void exitIfNoWindow(Window thisFrame, boolean neededDispose) {
boolean flag = false;
for (Window f : Window.getWindows()) {
@@ -308,11 +309,11 @@ public final class SwingUtils {
else
thisFrame.dispose();
}
public static ImageIcon scaleImage(ImageIcon i, int x, int y) {
return new ImageIcon(i.getImage().getScaledInstance(x, y, Image.SCALE_SMOOTH));
}
public static ImageIcon searchBackgroundImage(ImageIcon init, String bgpath, int width, int height) {
Random r = new Random();
boolean loaded = false;
@@ -362,9 +363,15 @@ public final class SwingUtils {
HMCLog.log("Prepared background image in background.jpg.");
}
}
if (background == null)
return init;
return background;
}
public static Runnable invokeLater(Runnable r) {
return () -> {
SwingUtilities.invokeLater(r);
};
}
}