package net.sf.openrocket.gui.main;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.Toolkit;
+import java.awt.Window;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.KeyEvent;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+
+import javax.swing.Action;
+import javax.swing.BorderFactory;
+import javax.swing.InputMap;
+import javax.swing.JButton;
+import javax.swing.JComponent;
+import javax.swing.JFileChooser;
+import javax.swing.JFrame;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JPopupMenu;
+import javax.swing.JScrollPane;
+import javax.swing.JSpinner;
+import javax.swing.JSplitPane;
+import javax.swing.JTabbedPane;
+import javax.swing.JTextField;
+import javax.swing.KeyStroke;
+import javax.swing.ListSelectionModel;
+import javax.swing.ScrollPaneConstants;
+import javax.swing.SwingUtilities;
+import javax.swing.border.BevelBorder;
+import javax.swing.border.TitledBorder;
+import javax.swing.event.TreeSelectionEvent;
+import javax.swing.event.TreeSelectionListener;
+import javax.swing.tree.DefaultTreeSelectionModel;
+import javax.swing.tree.TreePath;
+import javax.swing.tree.TreeSelectionModel;
+
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.aerodynamics.WarningSet;
import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.util.Reflection;
import net.sf.openrocket.util.TestRockets;
-import javax.swing.*;
-import javax.swing.border.BevelBorder;
-import javax.swing.border.TitledBorder;
-import javax.swing.event.TreeSelectionEvent;
-import javax.swing.event.TreeSelectionListener;
-import javax.swing.tree.DefaultTreeSelectionModel;
-import javax.swing.tree.TreePath;
-import javax.swing.tree.TreeSelectionModel;
-import java.awt.*;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.awt.event.KeyEvent;
-import java.awt.event.MouseAdapter;
-import java.awt.event.MouseEvent;
-import java.awt.event.MouseListener;
-import java.awt.event.WindowAdapter;
-import java.awt.event.WindowEvent;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.net.URLDecoder;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.concurrent.ExecutionException;
-
public class BasicFrame extends JFrame {
private static final LogHelper log = Application.getLogger();
public static final int COMPONENT_TAB = 0;
public static final int SIMULATION_TAB = 1;
-
+
/**
* List of currently open frames. When the list goes empty
* it is time to exit the application.
*/
private static final ArrayList<BasicFrame> frames = new ArrayList<BasicFrame>();
-
+
/**
* Whether "New" and "Open" should replace this frame.
* Should be set to false on the first rocket modification.
*/
private boolean replaceable = false;
-
-
+
+
private final OpenRocketDocument document;
private final Rocket rocket;
private final RocketActions actions;
-
+
/**
* Sole constructor. Creates a new frame based on the supplied document
* and adds it to the current frames list.
this.rocket = document.getRocket();
this.rocket.getDefaultConfiguration().setAllStages();
-
+
// Set replaceable flag to false at first modification
rocket.addComponentChangeListener(new ComponentChangeListener() {
@Override
}
});
-
+
// Create the component tree selection model that will be used
componentSelectionModel = new DefaultTreeSelectionModel();
componentSelectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
selectionModel.attachComponentTreeSelectionModel(componentSelectionModel);
selectionModel.attachSimulationListSelectionModel(simulationSelectionModel);
-
+
actions = new RocketActions(document, selectionModel, this);
-
+
log.debug("Constructing the BasicFrame UI");
// The main vertical split pane
vertical.setResizeWeight(0.5);
this.add(vertical);
-
+
// The top tabbed pane
tabbedPane = new JTabbedPane();
//// Rocket design
vertical.setTopComponent(tabbedPane);
-
-
+
+
// Bottom segment, rocket figure
rocketpanel = new RocketPanel(document);
rocketpanel.setSelectionModel(tree.getSelectionModel());
-
+
createMenu();
-
+
rocket.addComponentChangeListener(new ComponentChangeListener() {
@Override
public void componentChanged(ComponentChangeEvent e) {
setTitle();
this.pack();
-
+
// Set initial window size
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
size.width = size.width * 9 / 10;
JSplitPane horizontal = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
horizontal.setResizeWeight(0.5);
-
+
// Upper-left segment, component tree
JPanel panel = new JPanel(new MigLayout("fill, flowy", "", "[grow]"));
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK), null);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK), null);
-
-
+
+
// Double-click opens config dialog
MouseListener ml = new MouseAdapter() {
@Override
JScrollPane scroll = new JScrollPane(tree);
panel.add(scroll, "spany, grow, wrap");
-
+
// Buttons
JButton button = new JButton(actions.getMoveUpAction());
panel.add(button, "sizegroup buttons, aligny 65%");
horizontal.setLeftComponent(panel);
-
+
// Upper-right segment, component addition buttons
panel = new JPanel(new MigLayout("fill, insets 0", "[0::]"));
}
-
+
/**
* Return the currently selected rocket component, or <code>null</code> if none selected.
*/
});
menu.add(item);
-
+
menu.addSeparator();
//// Close
});
menu.add(item);
-
-
+
+
//// Edit
menu = new JMenu(trans.get("main.menu.edit"));
menu.setMnemonic(KeyEvent.VK_E);
menu.getAccessibleContext().setAccessibleDescription(trans.get("BasicFrame.menu.Rocketedt"));
menubar.add(menu);
-
+
Action action = UndoRedoAction.newUndoAction(document);
item = new JMenuItem(action);
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
item.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.edit.undo.desc"));
menu.add(item);
-
+
action = UndoRedoAction.newRedoAction(document);
item = new JMenuItem(action);
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
menu.addSeparator();
-
+
item = new JMenuItem(actions.getCutAction());
menu.add(item);
menu.addSeparator();
-
-
+
+
item = new JMenuItem(trans.get("main.menu.edit.resize"));
item.setIcon(Icons.EDIT_SCALE);
item.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.edit.resize.desc"));
});
menu.add(item);
-
-
+
+
//// Preferences
item = new JMenuItem(trans.get("main.menu.edit.preferences"));
item.setIcon(Icons.PREFERENCES);
});
menu.add(item);
-
-
-
+
+
+
//// Analyze
menu = new JMenu(trans.get("main.menu.analyze"));
menu.setMnemonic(KeyEvent.VK_A);
});
menu.add(item);
-
+
item = new JMenuItem(trans.get("main.menu.analyze.optimization"), KeyEvent.VK_O);
item.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.analyze.optimization.desc"));
item.addActionListener(new ActionListener() {
});
menu.add(item);
-
-
+
+
//// Debug
// (shown if openrocket.debug.menu is defined)
if (System.getProperty("openrocket.debug.menu") != null) {
menubar.add(makeDebugMenu());
}
-
-
+
+
//// Help
menu = new JMenu(trans.get("main.menu.help"));
menu.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.help.desc"));
menubar.add(menu);
-
+
// Guided tours
item = new JMenuItem(trans.get("main.menu.help.tours"), KeyEvent.VK_L);
- // TODO: Icon
+ item.setIcon(Icons.HELP_TOURS);
item.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.help.tours.desc"));
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
log.user("Guided tours selected");
- // FIXME: Singleton
- new GuidedTourSelectionDialog(BasicFrame.this).setVisible(true);
- }
- });
- menu.add(item);
-
- menu.addSeparator();
-
-
- //// License
- item = new JMenuItem(trans.get("main.menu.help.license"), KeyEvent.VK_L);
- item.setIcon(Icons.HELP_LICENSE);
- item.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.help.license.desc"));
- item.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- log.user("License selected");
- new LicenseDialog(BasicFrame.this).setVisible(true);
+ GuidedTourSelectionDialog.showDialog(BasicFrame.this);
}
});
menu.add(item);
menu.addSeparator();
+
+ //// License
+ item = new JMenuItem(trans.get("main.menu.help.license"), KeyEvent.VK_L);
+ item.setIcon(Icons.HELP_LICENSE);
+ item.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.help.license.desc"));
+ item.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ log.user("License selected");
+ new LicenseDialog(BasicFrame.this).setVisible(true);
+ }
+ });
+ menu.add(item);
+
+
//// About
item = new JMenuItem(trans.get("main.menu.help.about"), KeyEvent.VK_A);
item.setIcon(Icons.HELP_ABOUT);
});
menu.add(item);
-
+
this.setJMenuBar(menubar);
}
/*
* This menu is intentionally left untranslated.
*/
-
+
//// Debug menu
menu = new JMenu("Debug");
//// OpenRocket debugging tasks
int sel = JOptionPane.showOptionDialog(BasicFrame.this, new Object[] {
"Input text key to generate random rocket:",
field
- }, "Generate random test rocket", JOptionPane.DEFAULT_OPTION,
+ }, "Generate random test rocket", JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new Object[] {
"Random", "OK"
- }, "OK");
+ }, "OK");
Rocket r;
if (sel == 0) {
});
menu.add(item);
-
-
+
+
item = new JMenuItem("Create 'Iso-Haisu'");
item.addActionListener(new ActionListener() {
@Override
});
menu.add(item);
-
+
item = new JMenuItem("Create 'Big Blue'");
item.addActionListener(new ActionListener() {
@Override
menu.addSeparator();
-
+
item = new JMenuItem("Memory statistics");
item.addActionListener(new ActionListener() {
@Override
stats[2] = String.format(" Used memory: %.1f MB (%.0f%%)", used / 1024.0 / 1024.0, 100.0 * used / max);
stats[3] = String.format(" Free memory: %.1f MB (%.0f%%)", free / 1024.0 / 1024.0, 100.0 * free / max);
-
+
DetailDialog.showDetailedMessageDialog(BasicFrame.this, stats, sb.toString(),
"Memory statistics", JOptionPane.INFORMATION_MESSAGE);
}
});
menu.add(item);
-
+
menu.addSeparator();
//// Exception here
});
menu.add(item);
-
+
menu.addSeparator();
-
+
item = new JMenuItem("Test popup");
item.addActionListener(new ActionListener() {
@Override
});
menu.add(item);
-
-
-
+
+
+
return menu;
}
}
-
+
private void openAction() {
JFileChooser chooser = new JFileChooser();
filename = filename.substring(filename.lastIndexOf('/') + 1);
}
-
+
// Open the file
log.info("Opening file from url=" + url + " filename=" + filename);
try {
return false;
}
-
+
// Handle the document
OpenRocketDocument doc = null;
try {
throw new BugException("Document loader returned null");
}
-
+
// Show warnings
WarningSet warnings = worker.getRocketLoader().getWarnings();
if (!warnings.isEmpty()) {
trans.get("BasicFrame.WarningDialog.title"), warnings);
}
-
+
// Set document state
doc.setFile(file);
doc.setSaved(true);
-
+
// Open the frame
log.debug("Opening new frame with the document");
BasicFrame frame = new BasicFrame(doc);
return true;
}
-
-
-
-
+ /**
+ * "Save" action. If the design is new, then this is identical to "Save As", with a default file filter for .ork.
+ * If the rocket being edited previously was opened from a .ork file, then it will be saved immediately to the same
+ * file. But clicking on 'Save' for an existing design file with a .rkt will bring up a confirmation dialog because
+ * it's potentially a destructive write (loss of some fidelity if it's truly an original Rocksim generated file).
+ *
+ * @return true if the file was saved, false otherwise
+ */
private boolean saveAction() {
File file = document.getFile();
if (file == null) {
}
log.info("Saving document to " + file);
- // Saving RockSim designs is not supported
if (FileHelper.ROCKSIM_DESIGN_FILTER.accept(file)) {
- file = new File(file.getAbsolutePath().replaceAll(".[rR][kK][tT](.[gG][zZ])?$",
- ".ork"));
-
- log.info("Attempting to save in RockSim format, renaming to " + file);
- int option = JOptionPane.showConfirmDialog(this, new Object[] {
- "Saving designs in RockSim format is not supported.",
- "Save in OpenRocket format instead (" + file.getName() + ")?"
- }, "Save " + file.getName(), JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE, null);
- if (option != JOptionPane.YES_OPTION) {
- log.user("User chose not to save");
- return false;
- }
-
- document.setFile(file);
+ return saveAsRocksim(file);
}
return saveAs(file);
}
-
+ /**
+ * "Save As" action.
+ *
+ * Never should a .rkt file contain an OpenRocket content, or an .ork file contain a Rocksim design. Regardless of
+ * what extension the user has chosen, it would violate the Principle of Least Astonishment to do otherwise
+ * (and we want to make doing the wrong thing really hard to do). So always force the appropriate extension.
+ *
+ * This can result in some odd looking filenames (MyDesign.rkt.ork, MyDesign.rkt.ork.rkt, etc.) if the user is
+ * not paying attention, but the user can control that by modifying the filename in the dialog.
+ *
+ * @return true if the file was saved, false otherwise
+ */
private boolean saveAsAction() {
File file = null;
- // TODO: HIGH: what if *.rkt chosen?
StorageOptionChooser storageChooser =
new StorageOptionChooser(document, document.getDefaultStorageOptions());
- JFileChooser chooser = new JFileChooser();
- chooser.addChoosableFileFilter(FileHelper.OPENROCKET_DESIGN_FILTER);
- chooser.addChoosableFileFilter(FileHelper.ROCKSIM_DESIGN_FILTER);
- chooser.setFileFilter(FileHelper.OPENROCKET_DESIGN_FILTER);
+ final JFileChooser chooser = new JFileChooser();
+ chooser.addChoosableFileFilter(FileHelper.OPENROCKET_DESIGN_FILTER);
+ chooser.addChoosableFileFilter(FileHelper.ROCKSIM_DESIGN_FILTER);
+
+ //Force the file filter to match the file extension that was opened. Will default to OR if the file is null.
+ if (FileHelper.ROCKSIM_DESIGN_FILTER.accept(document.getFile())) {
+ chooser.setFileFilter(FileHelper.ROCKSIM_DESIGN_FILTER);
+ }
+ else {
+ chooser.setFileFilter(FileHelper.OPENROCKET_DESIGN_FILTER);
+ }
chooser.setCurrentDirectory(((SwingPreferences) Application.getPreferences()).getDefaultDirectory());
chooser.setAccessory(storageChooser);
- if (document.getFile() != null)
+ if (document.getFile() != null) {
chooser.setSelectedFile(document.getFile());
+ }
int option = chooser.showSaveDialog(BasicFrame.this);
if (option != JFileChooser.APPROVE_OPTION) {
((SwingPreferences) Application.getPreferences()).setDefaultDirectory(chooser.getCurrentDirectory());
storageChooser.storeOptions(document.getDefaultStorageOptions());
- if (chooser.getFileFilter().equals(FileHelper.OPENROCKET_DESIGN_FILTER)) {
- file = FileHelper.ensureExtension(file, "ork");
- if (!FileHelper.confirmWrite(file, this)) {
- return false;
- }
-
- return saveAs(file);
- }
- else if (chooser.getFileFilter().equals(FileHelper.ROCKSIM_DESIGN_FILTER)) {
- file = FileHelper.ensureExtension(file, "rkt");
- if (!FileHelper.confirmWrite(file, this)) {
- return false;
- }
-
- try {
- new RocksimSaver().save(file, document);
- return true;
- } catch (IOException e) {
- return false;
- }
- }
- else {
- return false;
- }
+ if (chooser.getFileFilter().equals(FileHelper.ROCKSIM_DESIGN_FILTER)) {
+ return saveAsRocksim(file);
+ }
+ else {
+ file = FileHelper.forceExtension(file, "ork");
+ return FileHelper.confirmWrite(file, this) && saveAs(file);
+ }
}
+ /**
+ * Perform the writing of the design to the given file in Rocksim format.
+ *
+ * @param file the chosen file
+ *
+ * @return true if the file was written
+ */
+ private boolean saveAsRocksim(File file) {
+ file = FileHelper.forceExtension(file, "rkt");
+ if (!FileHelper.confirmWrite(file, this)) {
+ return false;
+ }
+
+ try {
+ new RocksimSaver().save(file, document);
+ return true;
+ } catch (IOException e) {
+ return false;
+ }
+ }
+
+ /**
+ * Perform the writing of the design to the given file in OpenRocket format.
+ *
+ * @param file the chosen file
+ *
+ * @return true if the file was written
+ */
private boolean saveAs(File file) {
log.info("Saving document as " + file);
boolean saved = false;
return false;
}
-
+
SaveFileWorker worker = new SaveFileWorker(document, file, ROCKET_SAVER);
if (!SwingWorkerDialog.runWorker(this, "Saving file",
}
-
+
/**
*
*/
}
-
+
/**
* Find a currently open BasicFrame containing the specified rocket. This method
* can be used to map a Rocket to a BasicFrame from GUI methods.