1 package net.sf.openrocket.gui.main;
3 import java.awt.Dimension;
5 import java.awt.Toolkit;
6 import java.awt.Window;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.ComponentAdapter;
10 import java.awt.event.ComponentEvent;
11 import java.awt.event.KeyEvent;
12 import java.awt.event.MouseAdapter;
13 import java.awt.event.MouseEvent;
14 import java.awt.event.MouseListener;
15 import java.awt.event.WindowAdapter;
16 import java.awt.event.WindowEvent;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
22 import java.lang.reflect.InvocationTargetException;
24 import java.net.URISyntaxException;
26 import java.net.URLDecoder;
27 import java.util.ArrayList;
28 import java.util.concurrent.ExecutionException;
30 import javax.swing.Action;
31 import javax.swing.InputMap;
32 import javax.swing.JButton;
33 import javax.swing.JComponent;
34 import javax.swing.JFileChooser;
35 import javax.swing.JFrame;
36 import javax.swing.JMenu;
37 import javax.swing.JMenuBar;
38 import javax.swing.JMenuItem;
39 import javax.swing.JOptionPane;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JSeparator;
43 import javax.swing.JSplitPane;
44 import javax.swing.JTabbedPane;
45 import javax.swing.JTextField;
46 import javax.swing.KeyStroke;
47 import javax.swing.ListSelectionModel;
48 import javax.swing.ScrollPaneConstants;
49 import javax.swing.SwingUtilities;
50 import javax.swing.Timer;
51 import javax.swing.ToolTipManager;
52 import javax.swing.border.TitledBorder;
53 import javax.swing.event.TreeSelectionEvent;
54 import javax.swing.event.TreeSelectionListener;
55 import javax.swing.filechooser.FileFilter;
56 import javax.swing.tree.DefaultTreeSelectionModel;
57 import javax.swing.tree.TreePath;
58 import javax.swing.tree.TreeSelectionModel;
60 import net.miginfocom.swing.MigLayout;
61 import net.sf.openrocket.aerodynamics.WarningSet;
62 import net.sf.openrocket.communication.UpdateInfo;
63 import net.sf.openrocket.communication.UpdateInfoRetriever;
64 import net.sf.openrocket.database.Databases;
65 import net.sf.openrocket.document.OpenRocketDocument;
66 import net.sf.openrocket.file.GeneralRocketLoader;
67 import net.sf.openrocket.file.RocketLoadException;
68 import net.sf.openrocket.file.RocketLoader;
69 import net.sf.openrocket.file.RocketSaver;
70 import net.sf.openrocket.file.openrocket.OpenRocketSaver;
71 import net.sf.openrocket.gui.StorageOptionChooser;
72 import net.sf.openrocket.gui.configdialog.ComponentConfigDialog;
73 import net.sf.openrocket.gui.dialogs.AboutDialog;
74 import net.sf.openrocket.gui.dialogs.BugReportDialog;
75 import net.sf.openrocket.gui.dialogs.ComponentAnalysisDialog;
76 import net.sf.openrocket.gui.dialogs.ExampleDesignDialog;
77 import net.sf.openrocket.gui.dialogs.LicenseDialog;
78 import net.sf.openrocket.gui.dialogs.MotorDatabaseLoadingDialog;
79 import net.sf.openrocket.gui.dialogs.SwingWorkerDialog;
80 import net.sf.openrocket.gui.dialogs.UpdateInfoDialog;
81 import net.sf.openrocket.gui.dialogs.WarningDialog;
82 import net.sf.openrocket.gui.dialogs.preferences.PreferencesDialog;
83 import net.sf.openrocket.gui.scalefigure.RocketPanel;
84 import net.sf.openrocket.logging.LogHelper;
85 import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
86 import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
87 import net.sf.openrocket.rocketcomponent.Rocket;
88 import net.sf.openrocket.rocketcomponent.RocketComponent;
89 import net.sf.openrocket.rocketcomponent.Stage;
90 import net.sf.openrocket.startup.Application;
91 import net.sf.openrocket.util.BugException;
92 import net.sf.openrocket.util.GUIUtil;
93 import net.sf.openrocket.util.Icons;
94 import net.sf.openrocket.util.OpenFileWorker;
95 import net.sf.openrocket.util.Prefs;
96 import net.sf.openrocket.util.Reflection;
97 import net.sf.openrocket.util.SaveFileWorker;
98 import net.sf.openrocket.util.TestRockets;
100 public class BasicFrame extends JFrame {
101 private static final LogHelper log = Application.getLogger();
104 * The RocketLoader instance used for loading all rocket designs.
106 private static final RocketLoader ROCKET_LOADER = new GeneralRocketLoader();
108 private static final RocketSaver ROCKET_SAVER = new OpenRocketSaver();
111 // FileFilters for different types of rocket design files
112 private static final FileFilter ALL_DESIGNS_FILTER =
113 new SimpleFileFilter("All rocket designs (*.ork; *.rkt)",
114 ".ork", ".ork.gz", ".rkt", ".rkt.gz");
116 private static final FileFilter OPENROCKET_DESIGN_FILTER =
117 new SimpleFileFilter("OpenRocket designs (*.ork)", ".ork", ".ork.gz");
119 private static final FileFilter ROCKSIM_DESIGN_FILTER =
120 new SimpleFileFilter("RockSim designs (*.rkt)", ".rkt", ".rkt.gz");
125 public static final int COMPONENT_TAB = 0;
126 public static final int SIMULATION_TAB = 1;
130 * List of currently open frames. When the list goes empty
131 * it is time to exit the application.
133 private static final ArrayList<BasicFrame> frames = new ArrayList<BasicFrame>();
140 * Whether "New" and "Open" should replace this frame.
141 * Should be set to false on the first rocket modification.
143 private boolean replaceable = false;
147 private final OpenRocketDocument document;
148 private final Rocket rocket;
150 private JTabbedPane tabbedPane;
151 private RocketPanel rocketpanel;
152 private ComponentTree tree = null;
154 private final DocumentSelectionModel selectionModel;
155 private final TreeSelectionModel componentSelectionModel;
156 private final ListSelectionModel simulationSelectionModel;
158 /** Actions available for rocket modifications */
159 private final RocketActions actions;
164 * Sole constructor. Creates a new frame based on the supplied document
165 * and adds it to the current frames list.
167 * @param document the document to show.
169 public BasicFrame(OpenRocketDocument document) {
170 log.debug("Instantiating new BasicFrame");
172 this.document = document;
173 this.rocket = document.getRocket();
174 this.rocket.getDefaultConfiguration().setAllStages();
177 // Set replaceable flag to false at first modification
178 rocket.addComponentChangeListener(new ComponentChangeListener() {
179 public void componentChanged(ComponentChangeEvent e) {
181 BasicFrame.this.rocket.removeComponentChangeListener(this);
186 // Create the component tree selection model that will be used
187 componentSelectionModel = new DefaultTreeSelectionModel();
188 componentSelectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
190 // Obtain the simulation selection model that will be used
191 SimulationPanel simulationPanel = new SimulationPanel(document);
192 simulationSelectionModel = simulationPanel.getSimulationListSelectionModel();
194 // Combine into a DocumentSelectionModel
195 selectionModel = new DocumentSelectionModel(document);
196 selectionModel.attachComponentTreeSelectionModel(componentSelectionModel);
197 selectionModel.attachSimulationListSelectionModel(simulationSelectionModel);
200 actions = new RocketActions(document, selectionModel, this);
203 log.debug("Constructing the BasicFrame UI");
205 // The main vertical split pane
206 JSplitPane vertical = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true);
207 vertical.setResizeWeight(0.5);
211 // The top tabbed pane
212 tabbedPane = new JTabbedPane();
213 tabbedPane.addTab("Rocket design", null, designTab());
214 tabbedPane.addTab("Flight simulations", null, simulationPanel);
216 vertical.setTopComponent(tabbedPane);
220 // Bottom segment, rocket figure
222 rocketpanel = new RocketPanel(document);
223 vertical.setBottomComponent(rocketpanel);
225 rocketpanel.setSelectionModel(tree.getSelectionModel());
231 rocket.addComponentChangeListener(new ComponentChangeListener() {
232 public void componentChanged(ComponentChangeEvent e) {
240 Dimension size = Prefs.getWindowSize(this.getClass());
242 size = Toolkit.getDefaultToolkit().getScreenSize();
243 size.width = size.width * 9 / 10;
244 size.height = size.height * 9 / 10;
247 this.addComponentListener(new ComponentAdapter() {
249 public void componentResized(ComponentEvent e) {
250 Prefs.setWindowSize(BasicFrame.this.getClass(), BasicFrame.this.getSize());
253 this.setLocationByPlatform(true);
255 GUIUtil.setWindowIcons(this);
258 vertical.setDividerLocation(0.4);
259 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
260 addWindowListener(new WindowAdapter() {
262 public void windowClosing(WindowEvent e) {
268 log.debug("BasicFrame instantiation complete");
273 * Construct the "Rocket design" tab. This contains a horizontal split pane
274 * with the left component the design tree and the right component buttons
275 * for adding components.
277 private JComponent designTab() {
278 JSplitPane horizontal = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
279 horizontal.setResizeWeight(0.5);
282 // Upper-left segment, component tree
284 JPanel panel = new JPanel(new MigLayout("fill, flowy", "", "[grow]"));
286 tree = new ComponentTree(rocket);
287 tree.setSelectionModel(componentSelectionModel);
289 // Remove JTree key events that interfere with menu accelerators
290 InputMap im = SwingUtilities.getUIInputMap(tree, JComponent.WHEN_FOCUSED);
291 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK), null);
292 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK), null);
293 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK), null);
294 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK), null);
295 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK), null);
296 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK), null);
297 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK), null);
301 // Double-click opens config dialog
302 MouseListener ml = new MouseAdapter() {
304 public void mousePressed(MouseEvent e) {
305 int selRow = tree.getRowForLocation(e.getX(), e.getY());
306 TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
308 if ((e.getClickCount() == 2) && !ComponentConfigDialog.isDialogVisible()) {
310 RocketComponent c = (RocketComponent) selPath.getLastPathComponent();
311 ComponentConfigDialog.showDialog(BasicFrame.this,
312 BasicFrame.this.document, c);
317 tree.addMouseListener(ml);
319 // Update dialog when selection is changed
320 componentSelectionModel.addTreeSelectionListener(new TreeSelectionListener() {
321 public void valueChanged(TreeSelectionEvent e) {
322 // Scroll tree to the selected item
323 TreePath path = componentSelectionModel.getSelectionPath();
326 tree.scrollPathToVisible(path);
328 if (!ComponentConfigDialog.isDialogVisible())
330 RocketComponent c = (RocketComponent) path.getLastPathComponent();
331 ComponentConfigDialog.showDialog(BasicFrame.this,
332 BasicFrame.this.document, c);
336 // Place tree inside scroll pane
337 JScrollPane scroll = new JScrollPane(tree);
338 panel.add(scroll, "spany, grow, wrap");
342 JButton button = new JButton(actions.getMoveUpAction());
343 panel.add(button, "sizegroup buttons, aligny 65%");
345 button = new JButton(actions.getMoveDownAction());
346 panel.add(button, "sizegroup buttons, aligny 0%");
348 button = new JButton(actions.getEditAction());
349 panel.add(button, "sizegroup buttons");
351 button = new JButton(actions.getNewStageAction());
352 panel.add(button, "sizegroup buttons");
354 button = new JButton(actions.getDeleteAction());
355 button.setIcon(null);
356 button.setMnemonic(0);
357 panel.add(button, "sizegroup buttons");
359 horizontal.setLeftComponent(panel);
362 // Upper-right segment, component addition buttons
364 panel = new JPanel(new MigLayout("fill, insets 0", "[0::]"));
366 scroll = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
367 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
368 scroll.setViewportView(new ComponentAddButtons(document, componentSelectionModel,
369 scroll.getViewport()));
370 scroll.setBorder(null);
371 scroll.setViewportBorder(null);
373 TitledBorder border = new TitledBorder("Add new component");
374 border.setTitleFont(border.getTitleFont().deriveFont(Font.BOLD));
375 scroll.setBorder(border);
377 panel.add(scroll, "grow");
379 horizontal.setRightComponent(panel);
387 * Creates the menu for the window.
389 private void createMenu() {
390 JMenuBar menubar = new JMenuBar();
395 menu = new JMenu("File");
396 menu.setMnemonic(KeyEvent.VK_F);
397 menu.getAccessibleContext().setAccessibleDescription("File-handling related tasks");
400 item = new JMenuItem("New", KeyEvent.VK_N);
401 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
402 item.setMnemonic(KeyEvent.VK_N);
403 item.getAccessibleContext().setAccessibleDescription("Create a new rocket design");
404 item.setIcon(Icons.FILE_NEW);
405 item.addActionListener(new ActionListener() {
406 public void actionPerformed(ActionEvent e) {
414 item = new JMenuItem("Open...", KeyEvent.VK_O);
415 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
416 item.getAccessibleContext().setAccessibleDescription("Open a rocket design");
417 item.setIcon(Icons.FILE_OPEN);
418 item.addActionListener(new ActionListener() {
419 public void actionPerformed(ActionEvent e) {
425 item = new JMenuItem("Open example...");
426 item.getAccessibleContext().setAccessibleDescription("Open an example rocket design");
427 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
428 ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
429 item.setIcon(Icons.FILE_OPEN_EXAMPLE);
430 item.addActionListener(new ActionListener() {
431 public void actionPerformed(ActionEvent e) {
432 URL[] urls = ExampleDesignDialog.selectExampleDesigns(BasicFrame.this);
435 open(u, BasicFrame.this);
444 item = new JMenuItem("Save", KeyEvent.VK_S);
445 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
446 item.getAccessibleContext().setAccessibleDescription("Save the current rocket design");
447 item.setIcon(Icons.FILE_SAVE);
448 item.addActionListener(new ActionListener() {
449 public void actionPerformed(ActionEvent e) {
455 item = new JMenuItem("Save as...", KeyEvent.VK_A);
456 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
457 ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
458 item.getAccessibleContext().setAccessibleDescription("Save the current rocket design " +
460 item.setIcon(Icons.FILE_SAVE_AS);
461 item.addActionListener(new ActionListener() {
462 public void actionPerformed(ActionEvent e) {
468 // menu.addSeparator();
469 menu.add(new JSeparator());
471 item = new JMenuItem("Close", KeyEvent.VK_C);
472 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
473 item.getAccessibleContext().setAccessibleDescription("Close the current rocket design");
474 item.setIcon(Icons.FILE_CLOSE);
475 item.addActionListener(new ActionListener() {
476 public void actionPerformed(ActionEvent e) {
484 item = new JMenuItem("Quit", KeyEvent.VK_Q);
485 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
486 item.getAccessibleContext().setAccessibleDescription("Quit the program");
487 item.setIcon(Icons.FILE_QUIT);
488 item.addActionListener(new ActionListener() {
489 public void actionPerformed(ActionEvent e) {
498 menu = new JMenu("Edit");
499 menu.setMnemonic(KeyEvent.VK_E);
500 menu.getAccessibleContext().setAccessibleDescription("Rocket editing");
504 Action action = document.getUndoAction();
505 item = new JMenuItem(action);
506 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
507 item.setMnemonic(KeyEvent.VK_U);
508 item.getAccessibleContext().setAccessibleDescription("Undo the previous operation");
512 action = document.getRedoAction();
513 item = new JMenuItem(action);
514 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
515 item.setMnemonic(KeyEvent.VK_R);
516 item.getAccessibleContext().setAccessibleDescription("Redo the previously undone " +
523 item = new JMenuItem(actions.getCutAction());
526 item = new JMenuItem(actions.getCopyAction());
529 item = new JMenuItem(actions.getPasteAction());
532 item = new JMenuItem(actions.getDeleteAction());
537 item = new JMenuItem("Preferences");
538 item.setIcon(Icons.PREFERENCES);
539 item.getAccessibleContext().setAccessibleDescription("Setup the application " +
541 item.addActionListener(new ActionListener() {
542 public void actionPerformed(ActionEvent e) {
543 PreferencesDialog.showPreferences();
552 menu = new JMenu("Analyze");
553 menu.setMnemonic(KeyEvent.VK_A);
554 menu.getAccessibleContext().setAccessibleDescription("Analyzing the rocket");
557 item = new JMenuItem("Component analysis", KeyEvent.VK_C);
558 item.getAccessibleContext().setAccessibleDescription("Analyze the rocket components " +
560 item.addActionListener(new ActionListener() {
561 public void actionPerformed(ActionEvent e) {
562 ComponentAnalysisDialog.showDialog(rocketpanel);
569 // (shown if openrocket.debug.menu is defined)
570 if (System.getProperty("openrocket.debug.menu") != null) {
571 menubar.add(makeDebugMenu());
578 menu = new JMenu("Help");
579 menu.setMnemonic(KeyEvent.VK_H);
580 menu.getAccessibleContext().setAccessibleDescription("Information about OpenRocket");
585 item = new JMenuItem("License", KeyEvent.VK_L);
586 item.getAccessibleContext().setAccessibleDescription("OpenRocket license information");
587 item.addActionListener(new ActionListener() {
588 public void actionPerformed(ActionEvent e) {
589 new LicenseDialog(BasicFrame.this).setVisible(true);
594 item = new JMenuItem("Bug report", KeyEvent.VK_B);
595 item.getAccessibleContext().setAccessibleDescription("Information about reporting " +
596 "bugs in OpenRocket");
597 item.addActionListener(new ActionListener() {
598 public void actionPerformed(ActionEvent e) {
599 // new BugDialog(BasicFrame.this).setVisible(true);
600 BugReportDialog.showBugReportDialog(BasicFrame.this);
605 item = new JMenuItem("About", KeyEvent.VK_A);
606 item.getAccessibleContext().setAccessibleDescription("About OpenRocket");
607 item.addActionListener(new ActionListener() {
608 public void actionPerformed(ActionEvent e) {
609 new AboutDialog(BasicFrame.this).setVisible(true);
615 this.setJMenuBar(menubar);
619 private JMenu makeDebugMenu() {
624 menu = new JMenu("Debug");
625 menu.getAccessibleContext().setAccessibleDescription("OpenRocket debugging tasks");
627 item = new JMenuItem("What is this menu?");
628 item.addActionListener(new ActionListener() {
629 public void actionPerformed(ActionEvent e) {
630 JOptionPane.showMessageDialog(BasicFrame.this,
632 "The 'Debug' menu includes actions for testing and debugging " +
634 "The menu is made visible by defining the system property " +
635 "'openrocket.debug.menu' when starting OpenRocket.",
636 "It should not be visible by default." },
637 "Debug menu", JOptionPane.INFORMATION_MESSAGE);
644 item = new JMenuItem("Create test rocket");
645 item.addActionListener(new ActionListener() {
647 public void actionPerformed(ActionEvent e) {
648 JTextField field = new JTextField();
649 int sel = JOptionPane.showOptionDialog(BasicFrame.this, new Object[] {
650 "Input text key to generate random rocket:",
652 }, "Generate random test rocket", JOptionPane.DEFAULT_OPTION,
653 JOptionPane.QUESTION_MESSAGE, null, new Object[] {
659 r = new TestRockets(null).makeTestRocket();
660 } else if (sel == 1) {
661 r = new TestRockets(field.getText()).makeTestRocket();
666 OpenRocketDocument doc = new OpenRocketDocument(r);
668 BasicFrame frame = new BasicFrame(doc);
669 frame.setVisible(true);
676 item = new JMenuItem("Create 'Iso-Haisu'");
677 item.addActionListener(new ActionListener() {
679 public void actionPerformed(ActionEvent e) {
680 Rocket r = TestRockets.makeIsoHaisu();
681 OpenRocketDocument doc = new OpenRocketDocument(r);
683 BasicFrame frame = new BasicFrame(doc);
684 frame.setVisible(true);
690 item = new JMenuItem("Create 'Big Blue'");
691 item.addActionListener(new ActionListener() {
693 public void actionPerformed(ActionEvent e) {
694 Rocket r = TestRockets.makeBigBlue();
695 OpenRocketDocument doc = new OpenRocketDocument(r);
697 BasicFrame frame = new BasicFrame(doc);
698 frame.setVisible(true);
707 item = new JMenuItem("Exception here");
708 item.addActionListener(new ActionListener() {
709 public void actionPerformed(ActionEvent e) {
710 throw new RuntimeException("Testing exception from menu action listener");
715 item = new JMenuItem("Exception from EDT");
716 item.addActionListener(new ActionListener() {
717 public void actionPerformed(ActionEvent e) {
718 SwingUtilities.invokeLater(new Runnable() {
721 throw new RuntimeException("Testing exception from " +
722 "later invoked EDT thread");
729 item = new JMenuItem("Exception from other thread");
730 item.addActionListener(new ActionListener() {
731 public void actionPerformed(ActionEvent e) {
735 throw new RuntimeException("Testing exception from " +
736 "newly created thread");
751 * Select the tab on the main pane.
753 * @param tab one of {@link #COMPONENT_TAB} or {@link #SIMULATION_TAB}.
755 public void selectTab(int tab) {
756 tabbedPane.setSelectedIndex(tab);
761 private void openAction() {
762 JFileChooser chooser = new JFileChooser();
764 chooser.addChoosableFileFilter(ALL_DESIGNS_FILTER);
765 chooser.addChoosableFileFilter(OPENROCKET_DESIGN_FILTER);
766 chooser.addChoosableFileFilter(ROCKSIM_DESIGN_FILTER);
767 chooser.setFileFilter(ALL_DESIGNS_FILTER);
769 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
770 chooser.setMultiSelectionEnabled(true);
771 chooser.setCurrentDirectory(Prefs.getDefaultDirectory());
772 if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
775 Prefs.setDefaultDirectory(chooser.getCurrentDirectory());
777 File[] files = chooser.getSelectedFiles();
779 for (File file : files) {
780 System.out.println("Opening file: " + file);
781 if (open(file, this)) {
783 // Close previous window if replacing
784 if (replaceable && document.isSaved()) {
794 private static boolean open(URL url, BasicFrame parent) {
795 String filename = null;
797 // Try using URI.getPath();
799 URI uri = url.toURI();
800 filename = uri.getPath();
801 } catch (URISyntaxException ignore) {
804 // Try URL-decoding the URL
805 if (filename == null) {
807 filename = URLDecoder.decode(url.toString(), "UTF-8");
808 } catch (UnsupportedEncodingException ignore) {
813 if (filename == null) {
817 // Remove path from filename
818 if (filename.lastIndexOf('/') >= 0) {
819 filename = filename.substring(filename.lastIndexOf('/') + 1);
823 InputStream is = url.openStream();
824 if (open(is, filename, parent)) {
825 // Close previous window if replacing
826 if (parent.replaceable && parent.document.isSaved()) {
827 parent.closeAction();
828 parent.replaceable = false;
831 } catch (IOException e) {
832 JOptionPane.showMessageDialog(parent,
833 "An error occurred while opening the file " + filename,
834 "Error loading file", JOptionPane.ERROR_MESSAGE);
842 * Open the specified file from an InputStream in a new design frame. If an error
843 * occurs, an error dialog is shown and <code>false</code> is returned.
845 * @param stream the stream to load from.
846 * @param filename the file name to display in dialogs (not set to the document).
847 * @param parent the parent component for which a progress dialog is opened.
848 * @return whether the file was successfully loaded and opened.
850 private static boolean open(InputStream stream, String filename, Window parent) {
851 OpenFileWorker worker = new OpenFileWorker(stream, ROCKET_LOADER);
852 return open(worker, filename, null, parent);
857 * Open the specified file in a new design frame. If an error occurs, an error
858 * dialog is shown and <code>false</code> is returned.
860 * @param file the file to open.
861 * @param parent the parent component for which a progress dialog is opened.
862 * @return whether the file was successfully loaded and opened.
864 public static boolean open(File file, Window parent) {
865 OpenFileWorker worker = new OpenFileWorker(file, ROCKET_LOADER);
866 return open(worker, file.getName(), file, parent);
871 * Open the specified file using the provided worker.
873 * @param worker the OpenFileWorker that loads the file.
874 * @param filename the file name to display in dialogs.
875 * @param file the File to set the document to (may be null).
879 private static boolean open(OpenFileWorker worker, String filename, File file,
882 MotorDatabaseLoadingDialog.check(null);
884 // Open the file in a Swing worker thread
885 if (!SwingWorkerDialog.runWorker(parent, "Opening file",
886 "Reading " + filename + "...", worker)) {
888 // User cancelled the operation
893 // Handle the document
894 OpenRocketDocument doc = null;
899 } catch (ExecutionException e) {
901 Throwable cause = e.getCause();
903 if (cause instanceof FileNotFoundException) {
905 JOptionPane.showMessageDialog(parent,
906 "File not found: " + filename,
907 "Error opening file", JOptionPane.ERROR_MESSAGE);
910 } else if (cause instanceof RocketLoadException) {
912 JOptionPane.showMessageDialog(parent,
913 "Unable to open file '" + filename + "': "
914 + cause.getMessage(),
915 "Error opening file", JOptionPane.ERROR_MESSAGE);
920 throw new BugException("Unknown error when opening file", e);
924 } catch (InterruptedException e) {
925 throw new BugException("EDT was interrupted", e);
929 throw new BugException("BUG: Document loader returned null");
934 WarningSet warnings = worker.getRocketLoader().getWarnings();
935 if (!warnings.isEmpty()) {
936 WarningDialog.showWarnings(parent,
938 "The following problems were encountered while opening " + filename + ".",
939 "Some design features may not have been loaded correctly."
941 "Warnings while opening file", warnings);
945 // Set document state
950 BasicFrame frame = new BasicFrame(doc);
951 frame.setVisible(true);
960 private boolean saveAction() {
961 File file = document.getFile();
963 return saveAsAction();
966 // Saving RockSim designs is not supported
967 if (ROCKSIM_DESIGN_FILTER.accept(file)) {
968 file = new File(file.getAbsolutePath().replaceAll(".[rR][kK][tT](.[gG][zZ])?$",
971 int option = JOptionPane.showConfirmDialog(this, new Object[] {
972 "Saving designs in RockSim format is not supported.",
973 "Save in OpenRocket format instead (" + file.getName() + ")?"
974 }, "Save " + file.getName(), JOptionPane.YES_NO_OPTION,
975 JOptionPane.QUESTION_MESSAGE, null);
976 if (option != JOptionPane.YES_OPTION)
979 document.setFile(file);
985 private boolean saveAsAction() {
987 while (file == null) {
988 // TODO: HIGH: what if *.rkt chosen?
989 StorageOptionChooser storageChooser =
990 new StorageOptionChooser(document, document.getDefaultStorageOptions());
991 JFileChooser chooser = new JFileChooser();
992 chooser.setFileFilter(OPENROCKET_DESIGN_FILTER);
993 chooser.setCurrentDirectory(Prefs.getDefaultDirectory());
994 chooser.setAccessory(storageChooser);
995 if (document.getFile() != null)
996 chooser.setSelectedFile(document.getFile());
998 if (chooser.showSaveDialog(BasicFrame.this) != JFileChooser.APPROVE_OPTION)
1001 file = chooser.getSelectedFile();
1005 Prefs.setDefaultDirectory(chooser.getCurrentDirectory());
1006 storageChooser.storeOptions(document.getDefaultStorageOptions());
1008 if (file.getName().indexOf('.') < 0) {
1009 String name = file.getAbsolutePath();
1010 name = name + ".ork";
1011 file = new File(name);
1014 if (file.exists()) {
1015 int result = JOptionPane.showConfirmDialog(this,
1016 "File '" + file.getName() + "' exists. Do you want to overwrite it?",
1017 "File exists", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
1018 if (result != JOptionPane.YES_OPTION)
1027 private boolean saveAs(File file) {
1028 System.out.println("Saving to file: " + file.getName());
1029 boolean saved = false;
1031 if (!StorageOptionChooser.verifyStorageOptions(document, this)) {
1032 // User cancelled the dialog
1037 SaveFileWorker worker = new SaveFileWorker(document, file, ROCKET_SAVER);
1039 if (!SwingWorkerDialog.runWorker(this, "Saving file",
1040 "Writing " + file.getName() + "...", worker)) {
1042 // User cancelled the save
1049 document.setFile(file);
1050 document.setSaved(true);
1053 } catch (ExecutionException e) {
1055 Throwable cause = e.getCause();
1057 if (cause instanceof IOException) {
1058 JOptionPane.showMessageDialog(this, new String[] {
1059 "An I/O error occurred while saving:",
1060 e.getMessage() }, "Saving failed", JOptionPane.ERROR_MESSAGE);
1063 Reflection.handleWrappedException(e);
1066 } catch (InterruptedException e) {
1067 throw new BugException("EDT was interrupted", e);
1074 private boolean closeAction() {
1075 if (!document.isSaved()) {
1076 ComponentConfigDialog.hideDialog();
1077 int result = JOptionPane.showConfirmDialog(this,
1078 "Design '" + rocket.getName() + "' has not been saved. " +
1079 "Do you want to save it?",
1080 "Design not saved", JOptionPane.YES_NO_CANCEL_OPTION,
1081 JOptionPane.QUESTION_MESSAGE);
1082 if (result == JOptionPane.YES_OPTION) {
1085 return false; // If save was interrupted
1086 } else if (result == JOptionPane.NO_OPTION) {
1087 // Don't save: No-op
1094 // Rocket has been saved or discarded
1097 // TODO: LOW: Close only dialogs that have this frame as their parent
1098 ComponentConfigDialog.hideDialog();
1099 ComponentAnalysisDialog.hideDialog();
1101 frames.remove(this);
1102 if (frames.isEmpty())
1109 * Closes this frame if it is replaceable.
1111 public void closeIfReplaceable() {
1112 if (this.replaceable && document.isSaved()) {
1118 * Open a new design window with a basic rocket+stage.
1120 public static void newAction() {
1121 log.debug("New action initiated");
1122 Rocket rocket = new Rocket();
1123 Stage stage = new Stage();
1124 stage.setName("Sustainer");
1125 rocket.addChild(stage);
1126 OpenRocketDocument doc = new OpenRocketDocument(rocket);
1129 BasicFrame frame = new BasicFrame(doc);
1130 frame.replaceable = true;
1131 frame.setVisible(true);
1132 ComponentConfigDialog.showDialog(frame, doc, rocket);
1136 * Quit the application. Confirms saving unsaved designs. The action of File->Quit.
1138 public static void quitAction() {
1139 for (int i = frames.size() - 1; i >= 0; i--) {
1140 if (!frames.get(i).closeAction()) {
1145 // Should not be reached, but just in case
1151 * Set the title of the frame, taking into account the name of the rocket, file it
1152 * has been saved to (if any) and saved status.
1154 private void setTitle() {
1155 File file = document.getFile();
1156 boolean saved = document.isSaved();
1159 title = rocket.getName();
1161 title = title + " (" + file.getName() + ")";
1164 title = "*" + title;
1172 * Find a currently open BasicFrame containing the specified rocket. This method
1173 * can be used to map a Rocket to a BasicFrame from GUI methods.
1175 * @param rocket the Rocket.
1176 * @return the corresponding BasicFrame, or <code>null</code> if none found.
1178 public static BasicFrame findFrame(Rocket rocket) {
1179 for (BasicFrame f : frames) {
1180 if (f.rocket == rocket)
1187 * Find a currently open document by the rocket object. This method can be used
1188 * to map a Rocket to OpenRocketDocument from GUI methods.
1190 * @param rocket the Rocket.
1191 * @return the corresponding OpenRocketDocument, or <code>null</code> if not found.
1193 public static OpenRocketDocument findDocument(Rocket rocket) {
1194 for (BasicFrame f : frames) {
1195 if (f.rocket == rocket)
1202 public static void main(final String[] args) {
1204 // Run the actual startup method in the EDT since it can use progress dialogs etc.
1206 SwingUtilities.invokeAndWait(new Runnable() {
1212 } catch (InterruptedException e) {
1213 e.printStackTrace();
1214 } catch (InvocationTargetException e) {
1215 e.printStackTrace();
1221 private static void runMain(String[] args) {
1223 // Initialize the splash screen with version info
1227 // Start update info fetching
1228 final UpdateInfoRetriever updateInfo;
1229 if (Prefs.getCheckUpdates()) {
1230 updateInfo = new UpdateInfoRetriever();
1237 // Set the best available look-and-feel
1238 GUIUtil.setBestLAF();
1240 // Set tooltip delay time. Tooltips are used in MotorChooserDialog extensively.
1241 ToolTipManager.sharedInstance().setDismissDelay(30000);
1244 // Setup the uncaught exception handler
1245 ExceptionHandler.registerExceptionHandler();
1249 Prefs.loadDefaultUnits();
1253 Databases.fakeMethod();
1255 // Starting action (load files or open new document)
1256 if (!handleCommandLine(args)) {
1261 // Check whether update info has been fetched or whether it needs more time
1262 checkUpdateStatus(updateInfo);
1266 private static void checkUpdateStatus(final UpdateInfoRetriever updateInfo) {
1267 if (updateInfo == null)
1271 if (!updateInfo.isRunning())
1274 final Timer timer = new Timer(delay, null);
1276 ActionListener listener = new ActionListener() {
1277 private int count = 5;
1280 public void actionPerformed(ActionEvent e) {
1281 if (!updateInfo.isRunning()) {
1284 String current = Prefs.getVersion();
1285 String last = Prefs.getString(Prefs.LAST_UPDATE, "");
1287 UpdateInfo info = updateInfo.getUpdateInfo();
1288 if (info != null && info.getLatestVersion() != null &&
1289 !current.equals(info.getLatestVersion()) &&
1290 !last.equals(info.getLatestVersion())) {
1292 UpdateInfoDialog infoDialog = new UpdateInfoDialog(info);
1293 infoDialog.setVisible(true);
1294 if (infoDialog.isReminderSelected()) {
1295 Prefs.putString(Prefs.LAST_UPDATE, "");
1297 Prefs.putString(Prefs.LAST_UPDATE, info.getLatestVersion());
1306 timer.addActionListener(listener);
1312 * Handles arguments passed from the command line. This may be used either
1313 * when starting the first instance of OpenRocket or later when OpenRocket is
1314 * executed again while running.
1316 * @param args the command-line arguments.
1317 * @return whether a new frame was opened or similar user desired action was
1318 * performed as a result.
1320 public static boolean handleCommandLine(String[] args) {
1322 // Check command-line for files
1323 boolean opened = false;
1324 for (String file : args) {
1325 if (open(new File(file), null)) {