diff --git a/core/src/net/sf/openrocket/gui/dialogs/ExampleDesignDialog.java b/core/src/net/sf/openrocket/gui/dialogs/ExampleDesignDialog.java index 95ad56dfe..c289ee76f 100644 --- a/core/src/net/sf/openrocket/gui/dialogs/ExampleDesignDialog.java +++ b/core/src/net/sf/openrocket/gui/dialogs/ExampleDesignDialog.java @@ -6,51 +6,31 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.io.File; -import java.io.FilenameFilter; -import java.io.IOException; -import java.net.MalformedURLException; import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Enumeration; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; +import java.util.List; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JList; -import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.ListSelectionModel; import net.miginfocom.swing.MigLayout; +import net.sf.openrocket.gui.main.ExampleDesignFile; import net.sf.openrocket.gui.util.GUIUtil; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.startup.Application; -import net.sf.openrocket.util.BugException; -import net.sf.openrocket.util.JarUtil; public class ExampleDesignDialog extends JDialog { - private static final String DIRECTORY = "datafiles/examples/"; - private static final String PATTERN = ".*\\.[oO][rR][kK]$"; private static final Translator trans = Application.getTranslator(); - private static final FilenameFilter FILTER = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.matches(PATTERN); - } - }; - - private boolean open = false; - private final JList designSelection; + private final JList designSelection; - private ExampleDesignDialog(ExampleDesign[] designs, Window parent) { + private ExampleDesignDialog(ExampleDesignFile[] designs, Window parent) { //// Open example design super(parent, trans.get("exdesigndlg.lbl.Openexampledesign"), Dialog.ModalityType.APPLICATION_MODAL); @@ -59,7 +39,7 @@ public class ExampleDesignDialog extends JDialog { //// Select example designs to open: panel.add(new JLabel(trans.get("exdesigndlg.lbl.Selectexample")), "wrap"); - designSelection = new JList(designs); + designSelection = new JList(designs); designSelection.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); designSelection.addMouseListener(new MouseAdapter() { @Override @@ -111,21 +91,7 @@ public class ExampleDesignDialog extends JDialog { */ public static URL[] selectExampleDesigns(Window parent) { - ExampleDesign[] designs; - - designs = getJarFileNames(); - if (designs == null || designs.length == 0) { - designs = getDirFileNames(); - } - if (designs == null || designs.length == 0) { - //// Example designs could not be found. - JOptionPane.showMessageDialog(parent, trans.get("exdesigndlg.lbl.Exampledesignsnotfound"), - //// Examples not found - trans.get("exdesigndlg.lbl.Examplesnotfound"), JOptionPane.ERROR_MESSAGE); - return null; - } - - Arrays.sort(designs); + ExampleDesignFile[] designs = ExampleDesignFile.getExampleDesigns(); ExampleDesignDialog dialog = new ExampleDesignDialog(designs, parent); dialog.setVisible(true); @@ -134,133 +100,13 @@ public class ExampleDesignDialog extends JDialog { return null; } - Object[] selected = dialog.designSelection.getSelectedValues(); - URL[] urls = new URL[selected.length]; - for (int i=0; i selected = dialog.designSelection.getSelectedValuesList(); + URL[] urls = new URL[selected.size()]; + int i = 0; + for (ExampleDesignFile file : selected) { + urls[i++] = file.getURL(); } return urls; } - - - - - private static ExampleDesign[] getDirFileNames() { - - // Try to find directory as a system resource - File dir; - URL url = ClassLoader.getSystemResource(DIRECTORY); - - try { - dir = JarUtil.urlToFile(url); - } catch (Exception e1) { - dir = new File(DIRECTORY); - } - - // Get the list of files - File[] files = dir.listFiles(FILTER); - if (files == null) - return null; - - ExampleDesign[] designs = new ExampleDesign[files.length]; - - for (int i=0; i list = new ArrayList(); - int dirLength = DIRECTORY.length(); - - // Find and open the jar file this class is contained in - File file = JarUtil.getCurrentJarFile(); - if (file == null) - return null; - - - // Generate URL pointing to JAR file - URL fileUrl; - try { - fileUrl = file.toURI().toURL(); - } catch (MalformedURLException e1) { - e1.printStackTrace(); - throw new BugException(e1); - } - - // Iterate over JAR entries searching for designs - JarFile jarFile = null; - try { - jarFile = new JarFile(file); - - // Loop through JAR entries searching for files to load - Enumeration entries = jarFile.entries(); - while (entries.hasMoreElements()) { - JarEntry entry = entries.nextElement(); - String name = entry.getName(); - if (name.startsWith(DIRECTORY) && FILTER.accept(null, name)) { - String urlName = "jar:" + fileUrl + "!/" + name; - URL url = new URL(urlName); - list.add(new ExampleDesign(url, - name.substring(dirLength, name.length()-4))); - } - } - - } catch (IOException e) { - // Could be normal condition if not package in JAR - return null; - } finally { - if (jarFile != null) { - try { - jarFile.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - return list.toArray(new ExampleDesign[0]); - } - - - - /** - * Data holder class. - */ - private static class ExampleDesign implements Comparable { - - private final URL url; - private final String name; - - public ExampleDesign(URL url, String name) { - this.url = url; - this.name = name; - } - - @Override - public String toString() { - return name; - } - - public URL getURL() { - return url; - } - - @Override - public int compareTo(ExampleDesign o) { - return this.name.compareTo(o.name); - } - } - } diff --git a/core/src/net/sf/openrocket/gui/main/BasicFrame.java b/core/src/net/sf/openrocket/gui/main/BasicFrame.java index c1a5d597c..a539762f0 100644 --- a/core/src/net/sf/openrocket/gui/main/BasicFrame.java +++ b/core/src/net/sf/openrocket/gui/main/BasicFrame.java @@ -436,6 +436,11 @@ public class BasicFrame extends JFrame { item.setIcon(Icons.FILE_OPEN); menu.add(item); + item = new ExampleDesignFileAction(trans.get("main.menu.file.openExample"), this); + item.getAccessibleContext().setAccessibleDescription(trans.get("BasicFrame.item.Openexamplerocketdesign")); + item.setIcon(Icons.FILE_OPEN_EXAMPLE); + menu.add(item); + //// Open example... item = new JMenuItem(trans.get("main.menu.file.openExample")); //// Open an example rocket design @@ -490,7 +495,7 @@ public class BasicFrame extends JFrame { } }); menu.add(item); - + //// Export decal... item = new JMenuItem("Export Decal"); item.addActionListener(new ActionListener() { @@ -1092,7 +1097,7 @@ public class BasicFrame extends JFrame { * @param parent the parent window for dialogs. * @return true if opened successfully. */ - private static boolean open(URL url, BasicFrame parent) { + public static boolean open(URL url, BasicFrame parent) { String displayName = null; // First figure out the file name from the URL @@ -1126,13 +1131,7 @@ public class BasicFrame extends JFrame { log.info("Opening file from url=" + url + " filename=" + displayName); try { InputStream is = url.openStream(); - if (open(is, displayName, url, parent)) { - // Close previous window if replacing - if (parent.replaceable && parent.document.isSaved()) { - parent.closeAction(); - parent.replaceable = false; - } - } + open(is, displayName, url, parent, true); } catch (IOException e) { log.warn("Error opening file" + e); JOptionPane.showMessageDialog(parent, @@ -1151,11 +1150,12 @@ public class BasicFrame extends JFrame { * @param stream the stream to load from. * @param displayName the file name to display in dialogs (not set to the document). * @param parent the parent component for which a progress dialog is opened. + * @param openRocketConfigDialog if true will open the rocket configuration dialog * @return whether the file was successfully loaded and opened. */ - private static boolean open(InputStream stream, String displayName, URL fileURL, Window parent) { + private static boolean open(InputStream stream, String displayName, URL fileURL, Window parent, boolean openRocketConfigDialog) { OpenFileWorker worker = new OpenFileWorker(stream, fileURL, ROCKET_LOADER); - return open(worker, displayName, parent); + return open(worker, displayName, parent, openRocketConfigDialog); } @@ -1169,7 +1169,7 @@ public class BasicFrame extends JFrame { */ public static boolean open(File file, Window parent) { OpenFileWorker worker = new OpenFileWorker(file, ROCKET_LOADER); - return open(worker, file.getName(), parent); + return open(worker, file.getName(), parent, false); } @@ -1180,9 +1180,10 @@ public class BasicFrame extends JFrame { * @param displayName the file name to display in dialogs. * @param file the File to set the document to (may be null). * @param parent + * @param openRocketConfigDialog if true, will open the configuration dialog of the rocket. This is useful for examples. * @return */ - private static boolean open(OpenFileWorker worker, String displayName, Window parent) { + private static boolean open(OpenFileWorker worker, String displayName, Window parent, boolean openRocketConfigDialog) { MotorDatabaseLoadingDialog.check(parent); @@ -1243,13 +1244,13 @@ public class BasicFrame extends JFrame { log.info("Warnings while reading file: " + warnings); WarningDialog.showWarnings(parent, new Object[] { - //// The following problems were encountered while opening - trans.get("BasicFrame.WarningDialog.txt1") + " " + displayName + ".", - //// Some design features may not have been loaded correctly. - trans.get("BasicFrame.WarningDialog.txt2") - }, - //// Warnings while opening file - trans.get("BasicFrame.WarningDialog.title"), warnings); + //// The following problems were encountered while opening + trans.get("BasicFrame.WarningDialog.txt1") + " " + displayName + ".", + //// Some design features may not have been loaded correctly. + trans.get("BasicFrame.WarningDialog.txt2") + }, + //// Warnings while opening file + trans.get("BasicFrame.WarningDialog.title"), warnings); } // Open the frame @@ -1260,6 +1261,10 @@ public class BasicFrame extends JFrame { if ( parent != null && parent instanceof BasicFrame ) { ((BasicFrame)parent).closeIfReplaceable(); } + if( openRocketConfigDialog ) { + ComponentConfigDialog.showDialog(frame, doc, doc.getRocket()); + } + return true; } diff --git a/core/src/net/sf/openrocket/gui/main/ExampleDesignFile.java b/core/src/net/sf/openrocket/gui/main/ExampleDesignFile.java new file mode 100644 index 000000000..bb8cd8545 --- /dev/null +++ b/core/src/net/sf/openrocket/gui/main/ExampleDesignFile.java @@ -0,0 +1,158 @@ +package net.sf.openrocket.gui.main; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import net.sf.openrocket.file.iterator.DirectoryIterator; +import net.sf.openrocket.file.iterator.FileIterator; +import net.sf.openrocket.gui.util.SimpleFileFilter; +import net.sf.openrocket.util.BugException; +import net.sf.openrocket.util.JarUtil; +import net.sf.openrocket.util.Pair; + +public class ExampleDesignFile implements Comparable { + + private final URL url; + private final String name; + + private ExampleDesignFile(URL url, String name) { + this.url = url; + this.name = name; + } + + @Override + public String toString() { + return name; + } + + public URL getURL() { + return url; + } + + @Override + public int compareTo(ExampleDesignFile o) { + return this.name.compareTo(o.name); + } + + public static ExampleDesignFile[] getExampleDesigns() { + + ExampleDesignFile[] designs = getJarFileNames(); + if (designs == null || designs.length == 0) { + designs = getDirFileNames(); + } + if (designs == null || designs.length == 0) { + return null; + } + + Arrays.sort(designs); + + return designs; + } + + private static final String DIRECTORY = "datafiles/examples/"; + private static final String PATTERN = ".*\\.[oO][rR][kK]$"; + private static final FilenameFilter FILTER = new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.matches(PATTERN); + } + }; + + private static ExampleDesignFile[] getDirFileNames() { + + // Try to find directory as a system resource + File dir; + URL url = ClassLoader.getSystemResource(DIRECTORY); + + try { + dir = JarUtil.urlToFile(url); + } catch (Exception e1) { + dir = new File(DIRECTORY); + } + + // Get the list of files + File[] files = dir.listFiles(FILTER); + if (files == null) + return null; + + ExampleDesignFile[] designs = new ExampleDesignFile[files.length]; + + for (int i=0; i list = new ArrayList(); + int dirLength = DIRECTORY.length(); + + // Find and open the jar file this class is contained in + File file = JarUtil.getCurrentJarFile(); + if (file == null) + return null; + + + // Generate URL pointing to JAR file + URL fileUrl; + try { + fileUrl = file.toURI().toURL(); + } catch (MalformedURLException e1) { + e1.printStackTrace(); + throw new BugException(e1); + } + + // Iterate over JAR entries searching for designs + JarFile jarFile = null; + try { + jarFile = new JarFile(file); + + // Loop through JAR entries searching for files to load + Enumeration entries = jarFile.entries(); + while (entries.hasMoreElements()) { + JarEntry entry = entries.nextElement(); + String name = entry.getName(); + if (name.startsWith(DIRECTORY) && FILTER.accept(null, name)) { + String urlName = "jar:" + fileUrl + "!/" + name; + URL url = new URL(urlName); + list.add(new ExampleDesignFile(url, + name.substring(dirLength, name.length()-4))); + } + } + + } catch (IOException e) { + // Could be normal condition if not package in JAR + return null; + } finally { + if (jarFile != null) { + try { + jarFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + return list.toArray(new ExampleDesignFile[0]); + } + +} \ No newline at end of file diff --git a/core/src/net/sf/openrocket/gui/main/ExampleDesignFileAction.java b/core/src/net/sf/openrocket/gui/main/ExampleDesignFileAction.java new file mode 100644 index 000000000..d1ea61b5e --- /dev/null +++ b/core/src/net/sf/openrocket/gui/main/ExampleDesignFileAction.java @@ -0,0 +1,67 @@ +package net.sf.openrocket.gui.main; + +import java.awt.event.ActionEvent; + +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.JMenu; +import javax.swing.JMenuItem; + +/** + * Implements a menu for the example Open Rocket design files. + */ +public final class ExampleDesignFileAction extends JMenu { + + /** + * The window to which an open design file action will be parented to (typically an instance of BasicFrame). + */ + private final BasicFrame parent; + + /** + * Constructor. + * + * @param s the I18N menu string + * @param theParent the window to which an open design file action will be parented to (typically an instance of + * BasicFrame). + */ + public ExampleDesignFileAction(String s, BasicFrame theParent) { + super(s); + + parent = theParent; + updateMenu(); + } + + /** + * Create menu items. + */ + private void updateMenu() { + removeAll(); + ExampleDesignFile[] examples = ExampleDesignFile.getExampleDesigns(); + for (ExampleDesignFile file : examples) { + Action action = createAction(file); + action.putValue(Action.NAME, file.toString()); + JMenuItem menuItem = new JMenuItem(action); + add(menuItem); + } + } + + /** + * When a user clicks on one of the recently used design files, open it. + * + * @param file the design file name (absolute path) + * + * @return the action to open a design file + */ + private Action createAction(final ExampleDesignFile example) { + Action action = new AbstractAction() { + public void actionPerformed(ActionEvent e) { + String command = e.getActionCommand(); + BasicFrame.open(example.getURL(), parent); + } + }; + + action.putValue(Action.ACTION_COMMAND_KEY, example.toString()); + return action; + } + +} \ No newline at end of file