7216484f50aa8623da7772894187e5e7a8933b97
[debian/openrocket] / src / net / sf / openrocket / gui / main / BasicFrame.java
1 package net.sf.openrocket.gui.main;
2
3 import java.awt.Dimension;
4 import java.awt.Font;
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;
17 import java.io.File;
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;
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.net.URL;
26 import java.net.URLDecoder;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.concurrent.ExecutionException;
30
31 import javax.swing.Action;
32 import javax.swing.InputMap;
33 import javax.swing.JButton;
34 import javax.swing.JComponent;
35 import javax.swing.JFileChooser;
36 import javax.swing.JFrame;
37 import javax.swing.JMenu;
38 import javax.swing.JMenuBar;
39 import javax.swing.JMenuItem;
40 import javax.swing.JOptionPane;
41 import javax.swing.JPanel;
42 import javax.swing.JScrollPane;
43 import javax.swing.JSeparator;
44 import javax.swing.JSplitPane;
45 import javax.swing.JTabbedPane;
46 import javax.swing.KeyStroke;
47 import javax.swing.ListSelectionModel;
48 import javax.swing.LookAndFeel;
49 import javax.swing.ScrollPaneConstants;
50 import javax.swing.SwingUtilities;
51 import javax.swing.ToolTipManager;
52 import javax.swing.UIManager;
53 import javax.swing.border.TitledBorder;
54 import javax.swing.event.TreeSelectionEvent;
55 import javax.swing.event.TreeSelectionListener;
56 import javax.swing.filechooser.FileFilter;
57 import javax.swing.tree.DefaultTreeSelectionModel;
58 import javax.swing.tree.TreePath;
59 import javax.swing.tree.TreeSelectionModel;
60
61 import net.miginfocom.swing.MigLayout;
62 import net.sf.openrocket.aerodynamics.Warning;
63 import net.sf.openrocket.document.OpenRocketDocument;
64 import net.sf.openrocket.file.GeneralRocketLoader;
65 import net.sf.openrocket.file.OpenRocketSaver;
66 import net.sf.openrocket.file.RocketLoadException;
67 import net.sf.openrocket.file.RocketLoader;
68 import net.sf.openrocket.file.RocketSaver;
69 import net.sf.openrocket.gui.StorageOptionChooser;
70 import net.sf.openrocket.gui.configdialog.ComponentConfigDialog;
71 import net.sf.openrocket.gui.dialogs.BugDialog;
72 import net.sf.openrocket.gui.dialogs.ComponentAnalysisDialog;
73 import net.sf.openrocket.gui.dialogs.ExampleDesignDialog;
74 import net.sf.openrocket.gui.dialogs.LicenseDialog;
75 import net.sf.openrocket.gui.dialogs.PreferencesDialog;
76 import net.sf.openrocket.gui.dialogs.SwingWorkerDialog;
77 import net.sf.openrocket.gui.scalefigure.RocketPanel;
78 import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
79 import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
80 import net.sf.openrocket.rocketcomponent.Rocket;
81 import net.sf.openrocket.rocketcomponent.RocketComponent;
82 import net.sf.openrocket.rocketcomponent.Stage;
83 import net.sf.openrocket.util.Icons;
84 import net.sf.openrocket.util.OpenFileWorker;
85 import net.sf.openrocket.util.Prefs;
86 import net.sf.openrocket.util.SaveFileWorker;
87
88 public class BasicFrame extends JFrame {
89         private static final long serialVersionUID = 1L;
90
91         /**
92          * The RocketLoader instance used for loading all rocket designs.
93          */
94         private static final RocketLoader ROCKET_LOADER = new GeneralRocketLoader();
95         
96         private static final RocketSaver ROCKET_SAVER = new OpenRocketSaver();
97
98         
99         /**
100          * File filter for filtering only rocket designs.
101          */
102         private static final FileFilter ROCKET_DESIGN_FILTER = new FileFilter() {
103                 @Override
104                 public String getDescription() {
105                         return "OpenRocket designs (*.ork)";
106                 }
107                 @Override
108                 public boolean accept(File f) {
109                         if (f.isDirectory())
110                                 return true;
111                         String name = f.getName().toLowerCase();
112                         return name.endsWith(".ork") || name.endsWith(".ork.gz");
113                 }
114     };
115     
116     
117     
118     public static final int COMPONENT_TAB = 0;
119     public static final int SIMULATION_TAB = 1;
120     
121
122         /**
123          * List of currently open frames.  When the list goes empty
124          * it is time to exit the application.
125          */
126         private static final ArrayList<BasicFrame> frames = new ArrayList<BasicFrame>();
127         
128         
129         
130         
131         
132         /**
133          * Whether "New" and "Open" should replace this frame.
134          * Should be set to false on the first rocket modification.
135          */
136         private boolean replaceable = false;
137         
138         
139         
140         private final OpenRocketDocument document;
141         private final Rocket rocket;
142         
143         private JTabbedPane tabbedPane;
144         private RocketPanel rocketpanel;
145         private ComponentTree tree = null;
146         
147         private final DocumentSelectionModel selectionModel;
148         private final TreeSelectionModel componentSelectionModel;
149         private final ListSelectionModel simulationSelectionModel;
150         
151         /** Actions available for rocket modifications */
152         private final RocketActions actions;
153         
154         
155         
156         /**
157          * Sole constructor.  Creates a new frame based on the supplied document
158          * and adds it to the current frames list.
159          * 
160          * @param document      the document to show.
161          */
162         public BasicFrame(OpenRocketDocument document) {
163
164                 this.document = document;
165                 this.rocket = document.getRocket();
166                 this.rocket.getDefaultConfiguration().setAllStages();
167                 
168                 
169                 // Set replaceable flag to false at first modification
170                 rocket.addComponentChangeListener(new ComponentChangeListener() {
171                         public void componentChanged(ComponentChangeEvent e) {
172                                 replaceable = false;
173                                 BasicFrame.this.rocket.removeComponentChangeListener(this);
174                         }
175                 });
176                 
177                 
178                 // Create the component tree selection model that will be used
179                 componentSelectionModel = new DefaultTreeSelectionModel();
180                 componentSelectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
181                 
182                 // Obtain the simulation selection model that will be used
183                 SimulationPanel simulationPanel = new SimulationPanel(document);
184                 simulationSelectionModel = simulationPanel.getSimulationListSelectionModel();
185                 
186                 // Combine into a DocumentSelectionModel
187                 selectionModel = new DocumentSelectionModel(document);
188                 selectionModel.attachComponentTreeSelectionModel(componentSelectionModel);
189                 selectionModel.attachSimulationListSelectionModel(simulationSelectionModel);
190                 
191                 
192                 actions = new RocketActions(document, selectionModel, this);
193                 
194                 
195                 // The main vertical split pane         
196                 JSplitPane vertical = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true);
197                 vertical.setResizeWeight(0.5);
198                 this.add(vertical);
199
200                 
201                 // The top tabbed pane
202                 tabbedPane = new JTabbedPane();
203                 tabbedPane.addTab("Rocket design", null, designTab());
204                 tabbedPane.addTab("Flight simulations", null, simulationPanel);
205                 
206                 vertical.setTopComponent(tabbedPane);
207                 
208
209
210                 //  Bottom segment, rocket figure
211                 
212                 rocketpanel = new RocketPanel(document);
213                 vertical.setBottomComponent(rocketpanel);
214
215                 rocketpanel.setSelectionModel(tree.getSelectionModel());
216
217                                         
218                 createMenu();
219                 
220                 
221                 rocket.addComponentChangeListener(new ComponentChangeListener() {
222                         public void componentChanged(ComponentChangeEvent e) {
223                                 setTitle();
224                         }
225                 });
226                 
227                 setTitle();
228                 this.pack();
229
230                 Dimension size = Prefs.getWindowSize(this.getClass());
231                 if (size == null) {
232                         size = Toolkit.getDefaultToolkit().getScreenSize();
233                         size.width = size.width*9/10;
234                         size.height = size.height*9/10;
235                 }
236                 this.setSize(size);
237                 this.addComponentListener(new ComponentAdapter() {
238                         @Override
239                         public void componentResized(ComponentEvent e) {
240                                 Prefs.setWindowSize(BasicFrame.this.getClass(), BasicFrame.this.getSize());
241                         }
242                 });
243                 this.setLocationByPlatform(true);
244                                 
245                 this.validate();
246                 vertical.setDividerLocation(0.4);
247                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
248                 addWindowListener(new WindowAdapter() {
249                         @Override
250                         public void windowClosing(WindowEvent e) {
251                                 closeAction();
252                         }
253                 });
254                 frames.add(this);
255         }
256         
257         
258         /**
259          * Construct the "Rocket design" tab.  This contains a horizontal split pane
260          * with the left component the design tree and the right component buttons
261          * for adding components.
262          */
263         private JComponent designTab() {
264                 JSplitPane horizontal = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true);
265                 horizontal.setResizeWeight(0.5);
266
267
268                 //  Upper-left segment, component tree
269
270                 JPanel panel = new JPanel(new MigLayout("fill, flowy","","[grow]"));
271
272                 tree = new ComponentTree(rocket);
273                 tree.setSelectionModel(componentSelectionModel);
274
275                 // Remove JTree key events that interfere with menu accelerators
276                 InputMap im = SwingUtilities.getUIInputMap(tree, JComponent.WHEN_FOCUSED);
277                 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK), null);
278                 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK), null);
279                 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK), null);
280                 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK), null);
281                 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK), null);
282                 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK), null);
283                 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK), null);
284
285
286                 
287                 // Double-click opens config dialog
288                 MouseListener ml = new MouseAdapter() {
289                         @Override
290                         public void mousePressed(MouseEvent e) {
291                                 int selRow = tree.getRowForLocation(e.getX(), e.getY());
292                                 TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
293                                 if(selRow != -1) {
294                                         if(e.getClickCount() == 2) {
295                                                 // Double-click
296                                                 RocketComponent c = (RocketComponent)selPath.getLastPathComponent();
297                                                 ComponentConfigDialog.showDialog(BasicFrame.this, 
298                                                                 BasicFrame.this.document, c);
299                                         }
300                                 }
301                         }
302                 };
303                 tree.addMouseListener(ml);
304
305                 // Update dialog when selection is changed
306                 componentSelectionModel.addTreeSelectionListener(new TreeSelectionListener() {
307                         public void valueChanged(TreeSelectionEvent e) {
308                                 // Scroll tree to the selected item
309                                 TreePath path = componentSelectionModel.getSelectionPath();
310                                 if (path == null)
311                                         return;
312                                 tree.scrollPathToVisible(path);
313                                 
314                                 if (!ComponentConfigDialog.isDialogVisible())
315                                         return;
316                                 RocketComponent c = (RocketComponent)path.getLastPathComponent();
317                                 ComponentConfigDialog.showDialog(BasicFrame.this, 
318                                                 BasicFrame.this.document, c);
319                         }
320                 });
321
322                 // Place tree inside scroll pane
323                 JScrollPane scroll = new JScrollPane(tree);
324                 panel.add(scroll,"spany, grow, wrap");
325                 
326                 
327                 // Buttons
328                 JButton button = new JButton(actions.getMoveUpAction());
329                 panel.add(button,"sizegroup buttons, aligny 65%");
330                 
331                 button = new JButton(actions.getMoveDownAction());
332                 panel.add(button,"sizegroup buttons, aligny 0%");
333                 
334                 button = new JButton(actions.getEditAction());
335                 panel.add(button, "sizegroup buttons");
336                 
337                 button = new JButton(actions.getNewStageAction());
338                 panel.add(button,"sizegroup buttons");
339                 
340                 button = new JButton(actions.getDeleteAction());
341                 button.setIcon(null);
342                 button.setMnemonic(0);
343                 panel.add(button,"sizegroup buttons");
344
345                 horizontal.setLeftComponent(panel);
346
347
348                 //  Upper-right segment, component addition buttons
349
350                 panel = new JPanel(new MigLayout("fill, insets 0","[0::]"));
351
352                 scroll = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
353                                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
354                 scroll.setViewportView(new ComponentAddButtons(document, componentSelectionModel,
355                                 scroll.getViewport()));
356                 scroll.setBorder(null);
357                 scroll.setViewportBorder(null);
358
359                 TitledBorder border = new TitledBorder("Add new component");
360                 border.setTitleFont(border.getTitleFont().deriveFont(Font.BOLD));
361                 scroll.setBorder(border);
362
363                 panel.add(scroll,"grow");
364
365                 horizontal.setRightComponent(panel);
366
367                 return horizontal;
368         }
369         
370         
371         
372         /**
373          * Creates the menu for the window.
374          */
375         private void createMenu() {
376                 JMenuBar menubar = new JMenuBar();
377                 JMenu menu;
378                 JMenuItem item;
379                 
380                 ////  File
381                 menu = new JMenu("File");
382                 menu.setMnemonic(KeyEvent.VK_F);
383                 menu.getAccessibleContext().setAccessibleDescription("File-handling related tasks");
384                 menubar.add(menu);
385                 
386                 item = new JMenuItem("New",KeyEvent.VK_N);
387                 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
388                 item.setMnemonic(KeyEvent.VK_N);
389                 item.getAccessibleContext().setAccessibleDescription("Create a new rocket design");
390                 item.setIcon(Icons.FILE_NEW);
391                 item.addActionListener(new ActionListener() {
392                         public void actionPerformed(ActionEvent e) {
393                                 newAction();
394                                 if (replaceable)
395                                         closeAction();
396                         }
397                 });
398                 menu.add(item);
399                 
400                 item = new JMenuItem("Open...",KeyEvent.VK_O);
401                 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
402                 item.getAccessibleContext().setAccessibleDescription("Open a rocket design");
403                 item.setIcon(Icons.FILE_OPEN);
404                 item.addActionListener(new ActionListener() {
405                         public void actionPerformed(ActionEvent e) {
406                                 openAction();
407                         }
408                 });
409                 menu.add(item);
410                 
411                 item = new JMenuItem("Open example...");
412                 item.getAccessibleContext().setAccessibleDescription("Open an example rocket design");
413                 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, 
414                                 ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
415                 item.setIcon(Icons.FILE_OPEN_EXAMPLE);
416                 item.addActionListener(new ActionListener() {
417                         public void actionPerformed(ActionEvent e) {
418                                 URL[] urls = ExampleDesignDialog.selectExampleDesigns(BasicFrame.this);
419                                 if (urls != null) {
420                                         for (URL u: urls) {
421                                                 open(u, BasicFrame.this);
422                                         }
423                                 }
424                         }
425                 });
426                 menu.add(item);
427                 
428                 menu.addSeparator();
429                 
430                 item = new JMenuItem("Save",KeyEvent.VK_S);
431                 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
432                 item.getAccessibleContext().setAccessibleDescription("Save the current rocket design");
433                 item.setIcon(Icons.FILE_SAVE);
434                 item.addActionListener(new ActionListener() {
435                         public void actionPerformed(ActionEvent e) {
436                                 saveAction();
437                         }
438                 });
439                 menu.add(item);
440                 
441                 item = new JMenuItem("Save as...",KeyEvent.VK_A);
442                 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, 
443                                 ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
444                 item.getAccessibleContext().setAccessibleDescription("Save the current rocket design "+
445                                 "to a new file");
446                 item.setIcon(Icons.FILE_SAVE_AS);
447                 item.addActionListener(new ActionListener() {
448                         public void actionPerformed(ActionEvent e) {
449                                 saveAsAction();
450                         }
451                 });
452                 menu.add(item);
453                 
454 //              menu.addSeparator();
455                 menu.add(new JSeparator());
456                 
457                 item = new JMenuItem("Close",KeyEvent.VK_C);
458                 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
459                 item.getAccessibleContext().setAccessibleDescription("Close the current rocket design");
460                 item.setIcon(Icons.FILE_CLOSE);
461                 item.addActionListener(new ActionListener() {
462                         public void actionPerformed(ActionEvent e) {
463                                 closeAction();
464                         }
465                 });
466                 menu.add(item);
467                 
468                 menu.addSeparator();
469
470                 item = new JMenuItem("Quit",KeyEvent.VK_Q);
471                 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
472                 item.getAccessibleContext().setAccessibleDescription("Quit the program");
473                 item.setIcon(Icons.FILE_QUIT);
474                 item.addActionListener(new ActionListener() {
475                         public void actionPerformed(ActionEvent e) {
476                                 quitAction();
477                         }
478                 });
479                 menu.add(item);
480                 
481                 
482
483                 ////  Edit
484                 menu = new JMenu("Edit");
485                 menu.setMnemonic(KeyEvent.VK_E);
486                 menu.getAccessibleContext().setAccessibleDescription("Rocket editing");
487                 menubar.add(menu);
488                 
489                 
490                 Action action = document.getUndoAction();
491                 item = new JMenuItem(action);
492                 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
493                 item.setMnemonic(KeyEvent.VK_U);
494                 item.getAccessibleContext().setAccessibleDescription("Undo the previous operation");
495
496                 menu.add(item);
497
498                 action = document.getRedoAction();
499                 item = new JMenuItem(action);
500                 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
501                 item.setMnemonic(KeyEvent.VK_R);
502                 item.getAccessibleContext().setAccessibleDescription("Redo the previously undone " +
503                                 "operation");
504                 menu.add(item);
505                 
506                 menu.addSeparator();
507                 
508                 
509                 item = new JMenuItem(actions.getCutAction());
510                 menu.add(item);
511         
512                 item = new JMenuItem(actions.getCopyAction());
513                 menu.add(item);
514         
515                 item = new JMenuItem(actions.getPasteAction());
516                 menu.add(item);
517                 
518                 item = new JMenuItem(actions.getDeleteAction());
519                 menu.add(item);
520                 
521                 menu.addSeparator();
522                 
523                 item = new JMenuItem("Preferences");
524                 item.setIcon(Icons.PREFERENCES);
525                 item.getAccessibleContext().setAccessibleDescription("Setup the application "+
526                                 "preferences");
527                 item.addActionListener(new ActionListener() {
528                         public void actionPerformed(ActionEvent e) {
529                                 PreferencesDialog.showPreferences();
530                         }
531                 });
532                 menu.add(item);
533         
534                 
535
536
537                 ////  Analyze
538                 menu = new JMenu("Analyze");
539                 menu.setMnemonic(KeyEvent.VK_A);
540                 menu.getAccessibleContext().setAccessibleDescription("Analyzing the rocket");
541                 menubar.add(menu);
542                 
543                 item = new JMenuItem("Component analysis",KeyEvent.VK_C);
544                 item.getAccessibleContext().setAccessibleDescription("Analyze the rocket components " +
545                                 "separately");
546                 item.addActionListener(new ActionListener() {
547                         public void actionPerformed(ActionEvent e) {
548                                 ComponentAnalysisDialog.showDialog(rocketpanel);
549                         }
550                 });
551                 menu.add(item);
552                 
553                 
554                 
555                 ////  Help
556                 
557                 menu = new JMenu("Help");
558                 menu.setMnemonic(KeyEvent.VK_H);
559                 menu.getAccessibleContext().setAccessibleDescription("Information about OpenRocket");
560                 menubar.add(menu);
561                 
562                 
563                 
564                 item = new JMenuItem("License",KeyEvent.VK_L);
565                 item.getAccessibleContext().setAccessibleDescription("OpenRocket license information");
566                 item.addActionListener(new ActionListener() {
567                         public void actionPerformed(ActionEvent e) {
568                                 new LicenseDialog(BasicFrame.this).setVisible(true);
569                         }
570                 });
571                 menu.add(item);
572                 
573                 item = new JMenuItem("Bug report",KeyEvent.VK_B);
574                 item.getAccessibleContext().setAccessibleDescription("Information about reporting " +
575                                 "bugs in OpenRocket");
576                 item.addActionListener(new ActionListener() {
577                         public void actionPerformed(ActionEvent e) {
578                                 new BugDialog(BasicFrame.this).setVisible(true);
579                         }
580                 });
581                 menu.add(item);
582                 
583                 item = new JMenuItem("About",KeyEvent.VK_A);
584                 item.getAccessibleContext().setAccessibleDescription("About OpenRocket");
585                 item.addActionListener(new ActionListener() {
586                         public void actionPerformed(ActionEvent e) {
587                                 new AboutDialog(BasicFrame.this).setVisible(true);
588                         }
589                 });
590                 menu.add(item);
591                 
592                 
593                 this.setJMenuBar(menubar);
594         }
595         
596         
597         
598         /**
599          * Select the tab on the main pane.
600          * 
601          * @param tab   one of {@link #COMPONENT_TAB} or {@link #SIMULATION_TAB}.
602          */
603         public void selectTab(int tab) {
604                 tabbedPane.setSelectedIndex(tab);
605         }
606
607         
608         
609         private void openAction() {
610             JFileChooser chooser = new JFileChooser();
611             chooser.setFileFilter(ROCKET_DESIGN_FILTER);
612             chooser.setMultiSelectionEnabled(true);
613             chooser.setCurrentDirectory(Prefs.getDefaultDirectory());
614             if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
615                 return;
616             
617             Prefs.setDefaultDirectory(chooser.getCurrentDirectory());
618
619             File[] files = chooser.getSelectedFiles();
620             
621             for (File file: files) {
622                 System.out.println("Opening file: " + file);
623                 if (open(file, this)) {
624                         
625                         // Close previous window if replacing
626                         if (replaceable && document.isSaved()) {
627                                 closeAction();
628                                 replaceable = false;
629                         }
630                 }
631             }
632         }
633         
634         
635         
636         
637         private static boolean open(URL url, Window parent) {
638                 String filename = null;
639                 
640                 // Try using URI.getPath();
641                 try {
642                         URI uri = url.toURI();
643                         filename = uri.getPath();
644                 } catch (URISyntaxException ignore) { }
645
646                 // Try URL-decoding the URL
647                 if (filename == null) {
648                         try {
649                                 filename = URLDecoder.decode(url.toString(), "UTF-8");
650                         } catch (UnsupportedEncodingException ignore) { }
651                 }
652                 
653                 // Last resort
654                 if (filename == null) {
655                         filename = "";
656                 }
657                 
658                 // Remove path from filename
659                 if (filename.lastIndexOf('/') >= 0) {
660                         filename = filename.substring(filename.lastIndexOf('/')+1);
661                 }
662                 
663                 try {
664                         InputStream is = url.openStream();
665                         open(is, filename, parent);
666                 } catch (IOException e) {
667                         JOptionPane.showMessageDialog(parent, 
668                                         "An error occurred while opening the file " + filename,
669                                         "Error loading file", JOptionPane.ERROR_MESSAGE);
670                 }
671                 
672                 return false;
673         }
674         
675         
676         /**
677          * Open the specified file from an InputStream in a new design frame.  If an error
678          * occurs, an error dialog is shown and <code>false</code> is returned.
679          * 
680          * @param stream        the stream to load from.
681          * @param filename      the file name to display in dialogs (not set to the document).
682          * @param parent        the parent component for which a progress dialog is opened.
683          * @return                      whether the file was successfully loaded and opened.
684          */
685         private static boolean open(InputStream stream, String filename, Window parent) {
686                 OpenFileWorker worker = new OpenFileWorker(stream);
687                 return open(worker, filename, null, parent);
688         }
689         
690
691         /**
692          * Open the specified file in a new design frame.  If an error occurs, an error
693          * dialog is shown and <code>false</code> is returned.
694          * 
695          * @param file          the file to open.
696          * @param parent        the parent component for which a progress dialog is opened.
697          * @return                      whether the file was successfully loaded and opened.
698          */
699         private static boolean open(File file, Window parent) {
700                 OpenFileWorker worker = new OpenFileWorker(file);
701                 return open(worker, file.getName(), file, parent);
702         }
703         
704
705         /**
706          * Open the specified file using the provided worker.
707          * 
708          * @param worker        the OpenFileWorker that loads the file.
709          * @param filename      the file name to display in dialogs.
710          * @param file          the File to set the document to (may be null).
711          * @param parent
712          * @return
713          */
714         private static boolean open(OpenFileWorker worker, String filename, File file, 
715                         Window parent) {
716
717                 // Open the file in a Swing worker thread
718                 if (!SwingWorkerDialog.runWorker(parent, "Opening file", 
719                                 "Reading " + filename + "...", worker)) {
720
721                         // User cancelled the operation
722                         return false;
723                 }
724
725                 
726                 // Handle the document
727                 OpenRocketDocument doc = null;
728                 try {
729
730                         doc = worker.get();
731
732                 } catch (ExecutionException e) {
733
734                         Throwable cause = e.getCause();
735
736                         if (cause instanceof FileNotFoundException) {
737
738                                 JOptionPane.showMessageDialog(parent, 
739                                                 "File not found: " + filename,
740                                                 "Error opening file", JOptionPane.ERROR_MESSAGE);
741                                 return false;
742
743                         } else if (cause instanceof RocketLoadException) {
744
745                                 JOptionPane.showMessageDialog(parent, 
746                                                 "Unable to open file '" + filename +"': " 
747                                                 + cause.getMessage(),
748                                                 "Error opening file", JOptionPane.ERROR_MESSAGE);
749                                 return false;
750
751                         } else {
752
753                                 throw new RuntimeException("Unknown error when opening file", e);
754
755                         }
756
757                 } catch (InterruptedException e) {
758                         throw new RuntimeException("EDT was interrupted", e);
759                 }
760                 
761                 if (doc == null) {
762                         throw new RuntimeException("BUG: Document loader returned null");
763                 }
764                 
765                 
766             // Show warnings
767             Iterator<Warning> warns = ROCKET_LOADER.getWarnings().iterator();
768             System.out.println("Warnings:");
769             while (warns.hasNext()) {
770                 System.out.println("  "+warns.next());
771                 // TODO: HIGH: dialog
772             }
773             
774             // Set document state
775             doc.setFile(file);
776             doc.setSaved(true);
777
778             // Open the frame
779             BasicFrame frame = new BasicFrame(doc);
780             frame.setVisible(true);
781
782             return true;
783         }
784         
785         
786         
787         
788         
789         
790         
791         
792         
793         private boolean saveAction() {
794                 File file = document.getFile();
795                 if (file==null) {
796                         return saveAsAction();
797                 } else {
798                         return saveAs(file);
799                 }
800         }
801         
802         
803         private boolean saveAsAction() {
804                 File file = null;
805                 while (file == null) {
806                         StorageOptionChooser storageChooser = 
807                                 new StorageOptionChooser(document, document.getDefaultStorageOptions());
808                         JFileChooser chooser = new JFileChooser();
809                         chooser.setFileFilter(ROCKET_DESIGN_FILTER);
810                         chooser.setCurrentDirectory(Prefs.getDefaultDirectory());
811                         chooser.setAccessory(storageChooser);
812                         if (document.getFile() != null)
813                                 chooser.setSelectedFile(document.getFile());
814                         
815                         if (chooser.showSaveDialog(BasicFrame.this) != JFileChooser.APPROVE_OPTION)
816                                 return false;
817                         
818                         file = chooser.getSelectedFile();
819                         if (file == null)
820                                 return false;
821
822                         Prefs.setDefaultDirectory(chooser.getCurrentDirectory());
823                         storageChooser.storeOptions(document.getDefaultStorageOptions());
824                         
825                         if (file.getName().indexOf('.') < 0) {
826                                 String name = file.getAbsolutePath();
827                                 name = name + ".ork";
828                                 file = new File(name);
829                         }
830                         
831                         if (file.exists()) {
832                                 int result = JOptionPane.showConfirmDialog(this, 
833                                                 "File '"+file.getName()+"' exists.  Do you want to overwrite it?", 
834                                                 "File exists", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
835                                 if (result != JOptionPane.YES_OPTION)
836                                         return false;
837                         }
838                 }
839             saveAs(file);
840             return true;
841         }
842         
843         
844         private boolean saveAs(File file) {
845             System.out.println("Saving to file: " + file.getName());
846             boolean saved = false;
847             
848             if (!StorageOptionChooser.verifyStorageOptions(document, this)) {
849                 // User cancelled the dialog
850                 return false;
851             }
852
853
854             SaveFileWorker worker = new SaveFileWorker(document, file, ROCKET_SAVER);
855
856             if (!SwingWorkerDialog.runWorker(this, "Saving file", 
857                         "Writing " + file.getName() + "...", worker)) {
858                 
859                 // User cancelled the save
860                 file.delete();
861                 return false;
862             }
863             
864             try {
865                         worker.get();
866                         document.setFile(file);
867                         document.setSaved(true);
868                         saved = true;
869                     setTitle();
870                 } catch (ExecutionException e) {
871                         Throwable cause = e.getCause();
872                         
873                         if (cause instanceof IOException) {
874                         JOptionPane.showMessageDialog(this, new String[] { 
875                                         "An I/O error occurred while saving:",
876                                         e.getMessage() }, "Saving failed", JOptionPane.ERROR_MESSAGE);
877                         return false;
878                         } else {
879                                 throw new RuntimeException("Unknown error when saving file", e);
880                         }
881                         
882                 } catch (InterruptedException e) {
883                         throw new RuntimeException("EDT was interrupted", e);
884                 }
885             
886             return saved;
887         }
888         
889         
890         private boolean closeAction() {
891                 if (!document.isSaved()) {
892                         ComponentConfigDialog.hideDialog();
893                         int result = JOptionPane.showConfirmDialog(this, 
894                                         "Design '"+rocket.getName()+"' has not been saved.  " +
895                                                         "Do you want to save it?", 
896                                         "Design not saved", JOptionPane.YES_NO_CANCEL_OPTION, 
897                                         JOptionPane.QUESTION_MESSAGE);
898                         if (result == JOptionPane.YES_OPTION) {
899                                 // Save
900                                 if (!saveAction())
901                                         return false;  // If save was interrupted
902                         } else if (result == JOptionPane.NO_OPTION) {
903                                 // Don't save: No-op
904                         } else {
905                                 // Cancel or close
906                                 return false;
907                         }
908                 }
909                 
910                 // Rocket has been saved or discarded
911                 this.dispose();
912
913                 // TODO: LOW: Close only dialogs that have this frame as their parent
914                 ComponentConfigDialog.hideDialog();
915                 ComponentAnalysisDialog.hideDialog();
916                 
917                 frames.remove(this);
918                 if (frames.isEmpty())
919                         System.exit(0);
920                 return true;
921         }
922         
923         
924         /**
925          * Closes this frame if it is replaceable.
926          */
927         public void closeIfReplaceable() {
928                 if (this.replaceable && document.isSaved()) {
929                         closeAction();
930                 }
931         }
932         
933         /**
934          * Open a new design window with a basic rocket+stage.
935          */
936         public static void newAction() {
937                 Rocket rocket = new Rocket();
938                 Stage stage = new Stage();
939                 stage.setName("Sustainer");
940                 rocket.addChild(stage);
941                 OpenRocketDocument doc = new OpenRocketDocument(rocket);
942                 doc.setSaved(true);
943                 
944                 BasicFrame frame = new BasicFrame(doc);
945                 frame.replaceable = true;
946                 frame.setVisible(true);
947                 ComponentConfigDialog.showDialog(frame, doc, rocket);
948         }
949         
950         /**
951          * Quit the application.  Confirms saving unsaved designs.  The action of File->Quit.
952          */
953         public static void quitAction() {
954                 for (int i=frames.size()-1; i>=0; i--) {
955                         if (!frames.get(i).closeAction()) {
956                                 // Close canceled
957                                 return;
958                         }
959                 }
960                 // Should not be reached, but just in case
961                 System.exit(0);
962         }
963         
964         
965         /**
966          * Set the title of the frame, taking into account the name of the rocket, file it 
967          * has been saved to (if any) and saved status.
968          */
969         private void setTitle() {
970                 File file = document.getFile();
971                 boolean saved = document.isSaved();
972                 String title;
973                 
974                 title = rocket.getName();
975                 if (file!=null) {
976                         title = title + " ("+file.getName()+")";
977                 }
978                 if (!saved)
979                         title = "*" + title;
980                 
981                 setTitle(title);
982         }
983         
984         
985         
986         /**
987          * Find a currently open BasicFrame containing the specified rocket.  This method
988          * can be used to map a Rocket to a BasicFrame from GUI methods.
989          * 
990          * @param rocket the Rocket.
991          * @return               the corresponding BasicFrame, or <code>null</code> if none found.
992          */
993         public static BasicFrame findFrame(Rocket rocket) {
994                 for (BasicFrame f: frames) {
995                         if (f.rocket == rocket)
996                                 return f;
997                 }
998                 return null;
999         }
1000         
1001         /**
1002          * Find a currently open document by the rocket object.  This method can be used
1003          * to map a Rocket to OpenRocketDocument from GUI methods.
1004          * 
1005          * @param rocket the Rocket.
1006          * @return               the corresponding OpenRocketDocument, or <code>null</code> if not found.
1007          */
1008         public static OpenRocketDocument findDocument(Rocket rocket) {
1009                 for (BasicFrame f: frames) {
1010                         if (f.rocket == rocket)
1011                                 return f.document;
1012                 }
1013                 return null;
1014         }
1015         
1016         
1017         public static void main(final String[] args) {
1018                 
1019                 // Run the actual startup method in the EDT since it can use dialogs etc. 
1020                 try {
1021                         SwingUtilities.invokeAndWait(new Runnable() {
1022                                 @Override
1023                                 public void run() {
1024                                         runMain(args);
1025                                 }
1026                         });
1027                 } catch (InterruptedException e) {
1028                         e.printStackTrace();
1029                 } catch (InvocationTargetException e) {
1030                         e.printStackTrace();
1031                 }
1032                 
1033         }
1034         
1035         
1036         private static void runMain(String[] args) {
1037
1038                 /*
1039                  * Set the look-and-feel.  On Linux, Motif/Metal is sometimes incorrectly used 
1040                  * which is butt-ugly, so if the system l&f is Motif/Metal, we search for a few
1041                  * other alternatives.
1042                  */
1043                 try {
1044                         // Set system L&F
1045                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
1046                         
1047                         // Check whether we have an ugly L&F
1048                         LookAndFeel laf = UIManager.getLookAndFeel();
1049                         if (laf == null ||
1050                                         laf.getName().matches(".*[mM][oO][tT][iI][fF].*") ||
1051                                         laf.getName().matches(".*[mM][eE][tT][aA][lL].*")) {
1052                                 
1053                                 // Search for better LAF
1054                                 UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();
1055                                 String lafNames[] = {
1056                                                 ".*[gG][tT][kK].*",
1057                                                 ".*[wW][iI][nN].*",
1058                                                 ".*[mM][aA][cC].*",
1059                                                 ".*[aA][qQ][uU][aA].*",
1060                                                 ".*[nN][iI][mM][bB].*"
1061                                 };
1062                                 
1063                                 lf: for (String lafName: lafNames) {
1064                                         for (UIManager.LookAndFeelInfo l: info) {
1065                                                 if (l.getName().matches(lafName)) {
1066                                                         UIManager.setLookAndFeel(l.getClassName());
1067                                                         break lf;
1068                                                 }
1069                                         }                                       
1070                                 }
1071                         }
1072                 } catch (Exception e) {
1073                         System.err.println("Error setting LAF: " + e);
1074                 }
1075
1076                 // Set tooltip delay time.  Tooltips are used in MotorChooserDialog extensively.
1077                 ToolTipManager.sharedInstance().setDismissDelay(30000);
1078                 
1079                 
1080                 // Load defaults
1081                 Prefs.loadDefaultUnits();
1082
1083                 
1084                 // Check command-line for files
1085                 boolean opened = false;
1086                 for (String file: args) {
1087                         if (open(new File(file), null)) {
1088                                 opened = true;
1089                         }
1090                 }
1091                 
1092                 if (!opened) {
1093                         newAction();
1094                 }
1095         }
1096
1097 }