Only reset edit dialog position at startup

This commit is contained in:
SiboVG 2022-07-12 01:21:48 +02:00
parent 0b7b4d7bc7
commit a4b13a7ee3
4 changed files with 43 additions and 20 deletions

View File

@ -15,6 +15,7 @@ import java.util.List;
import javax.swing.JDialog; import javax.swing.JDialog;
import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.gui.main.BasicFrame;
import net.sf.openrocket.gui.util.GUIUtil; import net.sf.openrocket.gui.util.GUIUtil;
import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.gui.util.SwingPreferences;
import net.sf.openrocket.gui.util.WindowLocationUtil; import net.sf.openrocket.gui.util.WindowLocationUtil;
@ -261,7 +262,9 @@ public class ComponentConfigDialog extends JDialog implements ComponentChangeLis
dialog = new ComponentConfigDialog(parent, document, component); dialog = new ComponentConfigDialog(parent, document, component);
dialog.setVisible(true); dialog.setVisible(true);
WindowLocationUtil.moveIfOutsideOfParentMonitor(dialog, parent); if (parent instanceof BasicFrame && BasicFrame.getStartupFrame() == parent) {
WindowLocationUtil.moveIfOutsideOfParentMonitor(dialog, parent);
}
////Modify ////Modify
if (component.getConfigListeners().size() == 0) { if (component.getConfigListeners().size() == 0) {

View File

@ -116,6 +116,7 @@ public class BasicFrame extends JFrame {
* it is time to exit the application. * it is time to exit the application.
*/ */
private static final ArrayList<BasicFrame> frames = new ArrayList<BasicFrame>(); private static final ArrayList<BasicFrame> frames = new ArrayList<BasicFrame>();
private static BasicFrame startupFrame = null; // the frame that was created at startup
/** /**
@ -1200,6 +1201,19 @@ public class BasicFrame extends JFrame {
return menu; return menu;
} }
/**
* Return the frame that was created at the application's startup.
*/
public static BasicFrame getStartupFrame() {
return startupFrame;
}
/**
* Set the frame that is created at the application's startup.
*/
public static void setStartupFrame(BasicFrame startupFrame) {
BasicFrame.startupFrame = startupFrame;
}
/** /**
* Select the tab on the main pane. * Select the tab on the main pane.
@ -1242,7 +1256,7 @@ public class BasicFrame extends JFrame {
for (File file : files) { for (File file : files) {
log.info("Opening file: " + file); log.info("Opening file: " + file);
if (open(file, parent)) { if (open(file, parent) != null) {
MRUDesignFile opts = MRUDesignFile.getInstance(); MRUDesignFile opts = MRUDesignFile.getInstance();
opts.addFile(file.getAbsolutePath()); opts.addFile(file.getAbsolutePath());
} }
@ -1272,7 +1286,7 @@ public class BasicFrame extends JFrame {
for (File file : files) { for (File file : files) {
log.info("Opening file: " + file); log.info("Opening file: " + file);
if (open(file, this)) { if (open(file, this) != null) {
MRUDesignFile opts = MRUDesignFile.getInstance(); MRUDesignFile opts = MRUDesignFile.getInstance();
opts.addFile(file.getAbsolutePath()); opts.addFile(file.getAbsolutePath());
} }
@ -1342,9 +1356,9 @@ public class BasicFrame extends JFrame {
* *
* @param file the file to open. * @param file the file to open.
* @param parent the parent component for which a progress dialog is opened. * @param parent the parent component for which a progress dialog is opened.
* @return whether the file was successfully loaded and opened. * @return the BasicFrame that was created, or null if not created successfully.
*/ */
public static boolean open(File file, Window parent) { public static BasicFrame open(File file, Window parent) {
OpenFileWorker worker = new OpenFileWorker(file); OpenFileWorker worker = new OpenFileWorker(file);
return open(worker, file.getName(), parent, false); return open(worker, file.getName(), parent, false);
} }
@ -1357,15 +1371,15 @@ public class BasicFrame extends JFrame {
* @param displayName the file name to display in dialogs. * @param displayName the file name to display in dialogs.
* @param parent * @param parent
* @param openRocketConfigDialog if true, will open the configuration dialog of the rocket. This is useful for examples. * @param openRocketConfigDialog if true, will open the configuration dialog of the rocket. This is useful for examples.
* @return * @return the BasicFrame that was created, or null if not created successfully.
*/ */
private static boolean open(OpenFileWorker worker, String displayName, Window parent, boolean openRocketConfigDialog) { private static BasicFrame open(OpenFileWorker worker, String displayName, Window parent, boolean openRocketConfigDialog) {
//// Open the file in a Swing worker thread //// Open the file in a Swing worker thread
log.info("Starting OpenFileWorker"); log.info("Starting OpenFileWorker");
if (!SwingWorkerDialog.runWorker(parent, "Opening file", "Reading " + displayName + "...", worker)) { if (!SwingWorkerDialog.runWorker(parent, "Opening file", "Reading " + displayName + "...", worker)) {
// // User cancelled the operation // // User cancelled the operation
log.info("User cancelled the OpenFileWorker"); log.info("User cancelled the OpenFileWorker");
return false; return null;
} }
//// Handle the document //// Handle the document
@ -1384,7 +1398,7 @@ public class BasicFrame extends JFrame {
JOptionPane.showMessageDialog(parent, JOptionPane.showMessageDialog(parent,
"File not found: " + displayName, "File not found: " + displayName,
"Error opening file", JOptionPane.ERROR_MESSAGE); "Error opening file", JOptionPane.ERROR_MESSAGE);
return false; return null;
} else if (cause instanceof RocketLoadException) { } else if (cause instanceof RocketLoadException) {
@ -1393,7 +1407,7 @@ public class BasicFrame extends JFrame {
"Unable to open file '" + displayName + "': " "Unable to open file '" + displayName + "': "
+ cause.getMessage(), + cause.getMessage(),
"Error opening file", JOptionPane.ERROR_MESSAGE); "Error opening file", JOptionPane.ERROR_MESSAGE);
return false; return null;
} else { } else {
@ -1437,7 +1451,7 @@ public class BasicFrame extends JFrame {
ComponentConfigDialog.showDialog(frame, doc, doc.getRocket()); ComponentConfigDialog.showDialog(frame, doc, doc.getRocket());
} }
return true; return frame;
} }
@ -1743,24 +1757,27 @@ public class BasicFrame extends JFrame {
/** /**
* Opens a new design file or the last design file, if set in the preferences. * Opens a new design file or the last design file, if set in the preferences.
* Can be used for reopening the application or opening it the first time. * Can be used for reopening the application or opening it the first time.
* @return the BasicFrame that was created
*/ */
public static void reopen() { public static BasicFrame reopen() {
if (!Application.getPreferences().isAutoOpenLastDesignOnStartupEnabled()) { if (!Application.getPreferences().isAutoOpenLastDesignOnStartupEnabled()) {
BasicFrame.newAction(); return BasicFrame.newAction();
} else { } else {
String lastFile = MRUDesignFile.getInstance().getLastEditedDesignFile(); String lastFile = MRUDesignFile.getInstance().getLastEditedDesignFile();
if (lastFile != null) { if (lastFile != null) {
log.info("Opening last design file: " + lastFile); log.info("Opening last design file: " + lastFile);
if (!BasicFrame.open(new File(lastFile), null)) { BasicFrame frame = BasicFrame.open(new File(lastFile), null);
if (frame == null) {
MRUDesignFile.getInstance().removeFile(lastFile); MRUDesignFile.getInstance().removeFile(lastFile);
BasicFrame.newAction(); return BasicFrame.newAction();
} }
else { else {
MRUDesignFile.getInstance().addFile(lastFile); MRUDesignFile.getInstance().addFile(lastFile);
return frame;
} }
} }
else { else {
BasicFrame.newAction(); return BasicFrame.newAction();
} }
} }
} }
@ -1768,8 +1785,9 @@ public class BasicFrame extends JFrame {
/** /**
* Open a new design window with a basic rocket+stage. * Open a new design window with a basic rocket+stage.
* @return the BasicFrame that was created
*/ */
public static void newAction() { public static BasicFrame newAction() {
log.info("New action initiated"); log.info("New action initiated");
OpenRocketDocument doc = OpenRocketDocumentFactory.createNewRocket(); OpenRocketDocument doc = OpenRocketDocumentFactory.createNewRocket();
@ -1777,6 +1795,7 @@ public class BasicFrame extends JFrame {
BasicFrame frame = new BasicFrame(doc); BasicFrame frame = new BasicFrame(doc);
frame.replaceable = true; frame.replaceable = true;
frame.setVisible(true); frame.setVisible(true);
return frame;
} }

View File

@ -73,7 +73,7 @@ public final class MRUDesignFileAction extends JMenu {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand(); String command = e.getActionCommand();
if (BasicFrame.open(new File(command), parent)) { if (BasicFrame.open(new File(command), parent) != null) {
MRUDesignFile.getInstance().addFile(command); MRUDesignFile.getInstance().addFile(command);
} }
else { else {

View File

@ -223,7 +223,8 @@ public class SwingStartup {
// Starting action (load files or open new document) // Starting action (load files or open new document)
log.info("Opening main application window"); log.info("Opening main application window");
if (!handleCommandLine(args)) { if (!handleCommandLine(args)) {
BasicFrame.reopen(); BasicFrame startupFrame = BasicFrame.reopen();
BasicFrame.setStartupFrame(startupFrame);
} }
// Check whether update info has been fetched or whether it needs more time // Check whether update info has been fetched or whether it needs more time
@ -298,7 +299,7 @@ public class SwingStartup {
// Check command-line for files // Check command-line for files
boolean opened = false; boolean opened = false;
for (String file : args) { for (String file : args) {
if (BasicFrame.open(new File(file), null)) { if (BasicFrame.open(new File(file), null) != null) {
opened = true; opened = true;
} }
} }