mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 19:58:00 +00:00
Refactor restart code so app can support restarting
Add Landscape Mode setting to app
This commit is contained in:
@@ -61,6 +61,7 @@ import forge.quest.io.QuestDataIO;
|
||||
import forge.screens.deckeditor.CDeckEditorUI;
|
||||
import forge.toolbox.FOptionPane;
|
||||
import forge.toolbox.FSkin;
|
||||
import forge.util.RestartUtil;
|
||||
import forge.util.gui.SOptionPane;
|
||||
import forge.view.FFrame;
|
||||
import forge.view.FView;
|
||||
@@ -191,6 +192,18 @@ public enum FControl implements KeyEventDispatcher {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean restartForge() {
|
||||
if (!canExitForge(true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (RestartUtil.prepareForRestart()) {
|
||||
System.exit(0);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean exitForge() {
|
||||
if (!canExitForge(false)) {
|
||||
return false;
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
package forge.control;
|
||||
|
||||
import forge.Singletons;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Restarts a java app.
|
||||
* Credit: http://leolewis.website.org/wordpress/2011/07/06/programmatically-restart-a-java-application/
|
||||
*/
|
||||
public class RestartUtil {
|
||||
/**
|
||||
* Sun property pointing the main class and its arguments.
|
||||
* Might not be defined on non Hotspot VM implementations.
|
||||
*/
|
||||
public static final String SUN_JAVA_COMMAND = "sun.java.command";
|
||||
|
||||
/**
|
||||
* Restart the current Java application.
|
||||
* @param runBeforeRestart some custom code to be run before restarting
|
||||
*/
|
||||
public static void restartApplication(final Runnable runBeforeRestart) {
|
||||
if (!Singletons.getControl().canExitForge(true)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// java binary
|
||||
final String java = System.getProperty("java.home")
|
||||
+ File.separator + "bin" + File.separator + "java";
|
||||
|
||||
// vm arguments
|
||||
final List<String> vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
|
||||
final StringBuffer vmArgsOneLine = new StringBuffer();
|
||||
for (final String arg : vmArguments) {
|
||||
// if it's the agent argument : we ignore it otherwise the
|
||||
// address of the old application and the new one will be in conflict
|
||||
if (!arg.contains("-agentlib")) {
|
||||
vmArgsOneLine.append(arg);
|
||||
vmArgsOneLine.append(" ");
|
||||
}
|
||||
}
|
||||
// init the command to execute, add the vm args
|
||||
final StringBuffer cmd = new StringBuffer("\"" + java + "\" " + vmArgsOneLine);
|
||||
|
||||
// program main and program arguments
|
||||
final String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(" ");
|
||||
// program main is a jar
|
||||
if (mainCommand[0].endsWith(".jar")) {
|
||||
// if it's a jar, add -jar mainJar
|
||||
cmd.append("-jar " + new File(mainCommand[0]).getPath());
|
||||
} else {
|
||||
// else it's a .class, add the classpath and mainClass
|
||||
cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]);
|
||||
}
|
||||
// finally add program arguments
|
||||
for (int i = 1; i < mainCommand.length; i++) {
|
||||
cmd.append(" ");
|
||||
cmd.append(mainCommand[i]);
|
||||
}
|
||||
// execute the command in a shutdown hook, to be sure that all the
|
||||
// resources have been disposed before restarting the application
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Runtime.getRuntime().exec(cmd.toString());
|
||||
} catch (final IOException e) {
|
||||
//e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
// execute some custom code before restarting
|
||||
if (runBeforeRestart != null) {
|
||||
runBeforeRestart.run();
|
||||
}
|
||||
// exit
|
||||
System.exit(0);
|
||||
} catch (final Exception ex) {
|
||||
//ErrorViewer.showError(ex, "Restart \"%s\" exception", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,6 @@ import javax.swing.event.PopupMenuEvent;
|
||||
import javax.swing.event.PopupMenuListener;
|
||||
|
||||
import forge.Singletons;
|
||||
import forge.control.RestartUtil;
|
||||
import forge.gui.GuiUtils;
|
||||
import forge.screens.home.online.OnlineMenu;
|
||||
import forge.util.ReflectionUtil;
|
||||
@@ -134,7 +133,7 @@ public final class ForgeMenu {
|
||||
return new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
RestartUtil.restartApplication(null);
|
||||
Singletons.getControl().restartForge();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import forge.Singletons;
|
||||
import forge.UiCommand;
|
||||
import forge.ai.AiProfileUtil;
|
||||
import forge.control.FControl.CloseAction;
|
||||
import forge.control.RestartUtil;
|
||||
import forge.game.GameLogEntryType;
|
||||
import forge.gui.framework.FScreen;
|
||||
import forge.gui.framework.ICDoc;
|
||||
@@ -213,7 +212,7 @@ public enum CSubmenuPreferences implements ICDoc {
|
||||
prefs.reset();
|
||||
prefs.save();
|
||||
update();
|
||||
RestartUtil.restartApplication(null);
|
||||
Singletons.getControl().restartForge();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,6 @@ import com.google.common.collect.Lists;
|
||||
|
||||
import forge.Singletons;
|
||||
import forge.assets.FSkinProp;
|
||||
import forge.control.RestartUtil;
|
||||
import forge.gui.ImportDialog;
|
||||
import forge.gui.SOverlayUtils;
|
||||
import forge.gui.framework.DragCell;
|
||||
@@ -228,9 +227,9 @@ public enum FView {
|
||||
btnOk.addActionListener(new ActionListener() {
|
||||
@Override public void actionPerformed(final ActionEvent e) {
|
||||
if (remainingFiles.isEmpty()) {
|
||||
RestartUtil.restartApplication(null);
|
||||
Singletons.getControl().restartForge();
|
||||
} else {
|
||||
System.exit(0);
|
||||
Singletons.getControl().exitForge();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user