Fix NullPointerException in SwingUtils.invokeAndWait

This commit is contained in:
huangyuhui 2016-01-01 10:51:44 +08:00
parent 6968809499
commit 1f7eb04215
6 changed files with 51 additions and 18 deletions

View File

@ -30,6 +30,7 @@ import org.jackhuang.hellominecraft.launcher.settings.Settings;
import org.jackhuang.hellominecraft.utils.NetUtils; import org.jackhuang.hellominecraft.utils.NetUtils;
import org.jackhuang.hellominecraft.utils.MessageBox; import org.jackhuang.hellominecraft.utils.MessageBox;
import org.jackhuang.hellominecraft.utils.StrUtils; import org.jackhuang.hellominecraft.utils.StrUtils;
import org.jackhuang.hellominecraft.utils.system.OS;
import org.jackhuang.hellominecraft.views.LogWindow; import org.jackhuang.hellominecraft.views.LogWindow;
/** /**
@ -96,7 +97,7 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
text += "\n Content: \n "; text += "\n Content: \n ";
text += s + "\n\n"; text += s + "\n\n";
text += "-- System Details --\n"; text += "-- System Details --\n";
text += " Operating System: " + System.getProperty("os.name") + " (" + System.getProperty("os.arch") + ") version " + System.getProperty("os.version") + "\n"; text += " Operating System: " + OS.getSystemVersion() + "\n";
text += " Java Version: " + System.getProperty("java.version") + ", " + System.getProperty("java.vendor") + "\n"; text += " Java Version: " + System.getProperty("java.version") + ", " + System.getProperty("java.vendor") + "\n";
text += " Java VM Version: " + System.getProperty("java.vm.name") + " (" + System.getProperty("java.vm.info") + "), " + System.getProperty("java.vm.vendor") + "\n"; text += " Java VM Version: " + System.getProperty("java.vm.name") + " (" + System.getProperty("java.vm.info") + "), " + System.getProperty("java.vm.vendor") + "\n";
if (enableLogger) if (enableLogger)

View File

@ -43,7 +43,7 @@ public class InstallerPanel extends AnimatedPanel implements Selectable {
/** /**
* Creates new form InstallerPanel * Creates new form InstallerPanel
* *
* @param gsp To get the minecraft version * @param gsp To get the minecraft version
* @param installerType load which installer * @param installerType load which installer
*/ */
public InstallerPanel(GameSettingsPanel gsp, InstallerType installerType) { public InstallerPanel(GameSettingsPanel gsp, InstallerType installerType) {
@ -56,10 +56,9 @@ public class InstallerPanel extends AnimatedPanel implements Selectable {
} }
/** /**
* This method is called from within the constructor to * This method is called from within the constructor to initialize the form.
* initialize the form. * WARNING: Do NOT modify this code. The content of this method is always
* WARNING: Do NOT modify this code. The content of this method is * regenerated by the Form Editor.
* always regenerated by the Form Editor.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
@ -125,7 +124,7 @@ public class InstallerPanel extends AnimatedPanel implements Selectable {
void refreshVersions() { void refreshVersions() {
if (TaskWindow.getInstance().addTask(new TaskRunnableArg1<>(C.i18n("install." + id.id + ".get_list"), list) if (TaskWindow.getInstance().addTask(new TaskRunnableArg1<>(C.i18n("install." + id.id + ".get_list"), list)
.registerPreviousResult(new DefaultPreviousResult<>(new String[] {gsp.getMinecraftVersionFormatted()}))) .registerPreviousResult(new DefaultPreviousResult<>(new String[]{gsp.getMinecraftVersionFormatted()})))
.start()) .start())
loadVersions(); loadVersions();
} }
@ -152,7 +151,8 @@ public class InstallerPanel extends AnimatedPanel implements Selectable {
SwingUtils.clearDefaultTable(lstInstallers); SwingUtils.clearDefaultTable(lstInstallers);
if (versions != null) if (versions != null)
for (InstallerVersionList.InstallerVersion v : versions) for (InstallerVersionList.InstallerVersion v : versions)
model.addRow(new Object[] {v.selfVersion == null ? "null" : v.selfVersion, v.mcVersion == null ? "null" : v.mcVersion}); if (v != null)
model.addRow(new Object[]{v.selfVersion == null ? "null" : v.selfVersion, v.mcVersion == null ? "null" : v.mcVersion});
} }
}); });
} }

View File

@ -87,6 +87,13 @@ public final class StrUtils {
return false; return false;
} }
public static boolean containsOne(String base, String... match) {
for (String s : match)
if (base.contains(s))
return true;
return false;
}
public static boolean containsOne(List<String> base, List<String> match) { public static boolean containsOne(List<String> base, List<String> match) {
for (String a : base) for (String a : base)
for (String b : match) for (String b : match)

View File

@ -26,6 +26,7 @@ import java.io.InputStreamReader;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import org.jackhuang.hellominecraft.HMCLog; import org.jackhuang.hellominecraft.HMCLog;
import org.jackhuang.hellominecraft.utils.StrUtils;
/** /**
* @author huangyuhui * @author huangyuhui
@ -108,4 +109,18 @@ public enum OS {
return result; return result;
} }
public static String getLinuxReleaseVersion() throws IOException {
return FileUtils.readFileToString(new File("/etc/issue"));
}
public static String getSystemVersion() {
if (os() == LINUX)
try {
return getLinuxReleaseVersion();
} catch (IOException e) {
HMCLog.warn("Failed to catch /etc/issue");
}
return System.getProperty("os.name") + " (" + System.getProperty("os.arch") + "), " + System.getProperty("os.version");
}
} }

View File

@ -22,6 +22,9 @@ import java.awt.FontMetrics;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListModel; import javax.swing.DefaultListModel;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JList; import javax.swing.JList;
@ -222,12 +225,19 @@ public class SwingUtils {
return builder.toString(); return builder.toString();
} }
private static final ThreadLocal<Object> THREAD_LOCAL = new ThreadLocal<>(); 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) { public static <T> T invokeAndWait(NonFunction<T> x) {
Runnable r = () -> THREAD_LOCAL.set(x.apply()); int id;
synchronized (INVOKE_AND_WAIT_LOCK) {
id = ++INVOKE_AND_WAIT_ID;
}
int fuck = id;
Runnable r = () -> INVOKE_AND_WAIT_MAP.put(fuck, x.apply());
invokeAndWait(r); invokeAndWait(r);
return (T) THREAD_LOCAL.get(); return (T) INVOKE_AND_WAIT_MAP.remove(id);
} }
public static void invokeAndWait(Runnable r) { public static void invokeAndWait(Runnable r) {

View File

@ -37,13 +37,13 @@ import java.awt.LinearGradientPaint;
*/ */
public class ProgressPainter extends SynthPainter { public class ProgressPainter extends SynthPainter {
private static final float[] NORMAL_BG_PTS = new float[] {0, 1}; private static final float[] NORMAL_BG_PTS = new float[]{0, 1};
private static final Color[] NORMAL_BG = new Color[] { private static final Color[] NORMAL_BG = new Color[]{
GraphicsUtils.getWebColor("c6c6c6"), GraphicsUtils.getWebColor("c6c6c6"),
GraphicsUtils.getWebColor("c6c6c6") GraphicsUtils.getWebColor("c6c6c6")
}; };
private static final float[] BAR_FG_PTS = new float[] {0, 1}; private static final float[] BAR_FG_PTS = new float[]{0, 1};
private static final Color[] BAR_FG = new Color[] { private static final Color[] BAR_FG = new Color[]{
GraphicsUtils.getWebColor("41B1E1"), GraphicsUtils.getWebColor("41B1E1"),
GraphicsUtils.getWebColor("41B1E1") GraphicsUtils.getWebColor("41B1E1")
}; };
@ -69,7 +69,7 @@ public class ProgressPainter extends SynthPainter {
Graphics2D g2 = (Graphics2D) g.create(); Graphics2D g2 = (Graphics2D) g.create();
g2.setPaint(new LinearGradientPaint(x, y + 2, x, y + h - 2, BAR_FG_PTS, BAR_FG)); g2.setPaint(new LinearGradientPaint(x, y + 2, x, y + h - 2, BAR_FG_PTS, BAR_FG));
if (x + 2 < w - 5 && y + 2 < h - 5) if (x + 2 < w - 5 && y + 2 < h - 5)
g2.drawRect(x + 2, y + 2, w - 5, h - 5); g2.fillRect(x + 2, y + 2, w - 5, h - 5);
} }
/** /**