1 package net.sf.openrocket.gui.dialogs;
3 import java.awt.Dialog;
4 import java.awt.Window;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.MouseAdapter;
8 import java.awt.event.MouseEvent;
10 import java.io.FilenameFilter;
11 import java.io.IOException;
12 import java.net.MalformedURLException;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Enumeration;
17 import java.util.jar.JarEntry;
18 import java.util.jar.JarFile;
20 import javax.swing.JButton;
21 import javax.swing.JDialog;
22 import javax.swing.JLabel;
23 import javax.swing.JList;
24 import javax.swing.JOptionPane;
25 import javax.swing.JPanel;
26 import javax.swing.JScrollPane;
27 import javax.swing.ListSelectionModel;
29 import net.miginfocom.swing.MigLayout;
30 import net.sf.openrocket.gui.util.GUIUtil;
31 import net.sf.openrocket.l10n.Translator;
32 import net.sf.openrocket.startup.Application;
33 import net.sf.openrocket.util.BugException;
34 import net.sf.openrocket.util.JarUtil;
36 public class ExampleDesignDialog extends JDialog {
38 private static final String DIRECTORY = "datafiles/examples/";
39 private static final String PATTERN = ".*\\.[oO][rR][kK]$";
40 private static final Translator trans = Application.getTranslator();
42 private static final FilenameFilter FILTER = new FilenameFilter() {
44 public boolean accept(File dir, String name) {
45 return name.matches(PATTERN);
50 private boolean open = false;
51 private final JList designSelection;
53 private ExampleDesignDialog(ExampleDesign[] designs, Window parent) {
54 //// Open example design
55 super(parent, trans.get("exdesigndlg.lbl.Openexampledesign"), Dialog.ModalityType.APPLICATION_MODAL);
57 JPanel panel = new JPanel(new MigLayout("fill"));
59 //// Select example designs to open:
60 panel.add(new JLabel(trans.get("exdesigndlg.lbl.Selectexample")), "wrap");
62 designSelection = new JList(designs);
63 designSelection.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
64 designSelection.addMouseListener(new MouseAdapter() {
66 public void mouseClicked(MouseEvent e) {
67 if (e.getClickCount() >= 2) {
69 ExampleDesignDialog.this.setVisible(false);
73 panel.add(new JScrollPane(designSelection), "grow, wmin 300lp, wrap para");
76 JButton openButton = new JButton(trans.get("exdesigndlg.but.open"));
77 openButton.addActionListener(new ActionListener() {
79 public void actionPerformed(ActionEvent e) {
81 ExampleDesignDialog.this.setVisible(false);
84 panel.add(openButton, "split 2, sizegroup buttons, growx");
87 JButton cancelButton = new JButton(trans.get("dlg.but.cancel"));
88 cancelButton.addActionListener(new ActionListener() {
90 public void actionPerformed(ActionEvent e) {
92 ExampleDesignDialog.this.setVisible(false);
95 panel.add(cancelButton, "sizegroup buttons, growx");
99 this.setLocationByPlatform(true);
101 GUIUtil.setDisposableDialogOptions(this, openButton);
106 * Open a dialog to allow opening the example designs.
108 * @param parent the parent window of the dialog.
109 * @return an array of URL's to open, or <code>null</code> if the operation
112 public static URL[] selectExampleDesigns(Window parent) {
114 ExampleDesign[] designs;
116 designs = getJarFileNames();
117 if (designs == null || designs.length == 0) {
118 designs = getDirFileNames();
120 if (designs == null || designs.length == 0) {
121 //// Example designs could not be found.
122 JOptionPane.showMessageDialog(parent, trans.get("exdesigndlg.lbl.Exampledesignsnotfound"),
123 //// Examples not found
124 trans.get("exdesigndlg.lbl.Examplesnotfound"), JOptionPane.ERROR_MESSAGE);
128 Arrays.sort(designs);
130 ExampleDesignDialog dialog = new ExampleDesignDialog(designs, parent);
131 dialog.setVisible(true);
137 Object[] selected = dialog.designSelection.getSelectedValues();
138 URL[] urls = new URL[selected.length];
139 for (int i=0; i<selected.length; i++) {
140 urls[i] = ((ExampleDesign)selected[i]).getURL();
149 private static ExampleDesign[] getDirFileNames() {
151 // Try to find directory as a system resource
153 URL url = ClassLoader.getSystemResource(DIRECTORY);
156 dir = JarUtil.urlToFile(url);
157 } catch (Exception e1) {
158 dir = new File(DIRECTORY);
161 // Get the list of files
162 File[] files = dir.listFiles(FILTER);
166 ExampleDesign[] designs = new ExampleDesign[files.length];
168 for (int i=0; i<files.length; i++) {
169 String name = files[i].getName();
171 designs[i] = new ExampleDesign(files[i].toURI().toURL(),
172 name.substring(0, name.length()-4));
173 } catch (MalformedURLException e) {
174 throw new BugException(e);
182 private static ExampleDesign[] getJarFileNames() {
184 ArrayList<ExampleDesign> list = new ArrayList<ExampleDesign>();
185 int dirLength = DIRECTORY.length();
187 // Find and open the jar file this class is contained in
188 File file = JarUtil.getCurrentJarFile();
193 // Generate URL pointing to JAR file
196 fileUrl = file.toURI().toURL();
197 } catch (MalformedURLException e1) {
198 e1.printStackTrace();
199 throw new BugException(e1);
202 // Iterate over JAR entries searching for designs
203 JarFile jarFile = null;
205 jarFile = new JarFile(file);
207 // Loop through JAR entries searching for files to load
208 Enumeration<JarEntry> entries = jarFile.entries();
209 while (entries.hasMoreElements()) {
210 JarEntry entry = entries.nextElement();
211 String name = entry.getName();
212 if (name.startsWith(DIRECTORY) && FILTER.accept(null, name)) {
213 String urlName = "jar:" + fileUrl + "!/" + name;
214 URL url = new URL(urlName);
215 list.add(new ExampleDesign(url,
216 name.substring(dirLength, name.length()-4)));
220 } catch (IOException e) {
221 // Could be normal condition if not package in JAR
224 if (jarFile != null) {
227 } catch (IOException e) {
233 return list.toArray(new ExampleDesign[0]);
241 private static class ExampleDesign implements Comparable<ExampleDesign> {
243 private final URL url;
244 private final String name;
246 public ExampleDesign(URL url, String name) {
252 public String toString() {
256 public URL getURL() {
261 public int compareTo(ExampleDesign o) {
262 return this.name.compareTo(o.name);