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.util.GUIUtil;
31 import net.sf.openrocket.util.JarUtil;
33 public class ExampleDesignDialog extends JDialog {
35 private static final String DIRECTORY = "datafiles/examples/";
36 private static final String PATTERN = ".*\\.[oO][rR][kK]$";
37 private static final FilenameFilter FILTER = new FilenameFilter() {
39 public boolean accept(File dir, String name) {
40 return name.matches(PATTERN);
45 private boolean open = false;
46 private final JList designSelection;
48 private ExampleDesignDialog(ExampleDesign[] designs, Window parent) {
49 super(parent, "Open example design", Dialog.ModalityType.APPLICATION_MODAL);
51 JPanel panel = new JPanel(new MigLayout("fill"));
53 panel.add(new JLabel("Select example designs to open:"), "wrap");
55 designSelection = new JList(designs);
56 designSelection.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
57 designSelection.addMouseListener(new MouseAdapter() {
59 public void mouseClicked(MouseEvent e) {
60 if (e.getClickCount() >= 2) {
62 ExampleDesignDialog.this.setVisible(false);
66 panel.add(new JScrollPane(designSelection), "grow, wmin 300lp, wrap para");
68 JButton openButton = new JButton("Open");
69 openButton.addActionListener(new ActionListener() {
71 public void actionPerformed(ActionEvent e) {
73 ExampleDesignDialog.this.setVisible(false);
76 panel.add(openButton, "split 2, sizegroup buttons, growx");
78 JButton cancelButton = new JButton("Cancel");
79 cancelButton.addActionListener(new ActionListener() {
81 public void actionPerformed(ActionEvent e) {
83 ExampleDesignDialog.this.setVisible(false);
86 panel.add(cancelButton, "sizegroup buttons, growx");
90 this.setLocationByPlatform(true);
92 GUIUtil.setDisposableDialogOptions(this, openButton);
97 * Open a dialog to allow opening the example designs.
99 * @param parent the parent window of the dialog.
100 * @return an array of URL's to open, or <code>null</code> if the operation
103 public static URL[] selectExampleDesigns(Window parent) {
105 ExampleDesign[] designs;
107 designs = getJarFileNames();
108 if (designs == null || designs.length == 0) {
109 designs = getDirFileNames();
111 if (designs == null || designs.length == 0) {
112 JOptionPane.showMessageDialog(parent, "Example designs could not be found.",
113 "Examples not found", JOptionPane.ERROR_MESSAGE);
117 Arrays.sort(designs);
119 ExampleDesignDialog dialog = new ExampleDesignDialog(designs, parent);
120 dialog.setVisible(true);
126 Object[] selected = dialog.designSelection.getSelectedValues();
127 URL[] urls = new URL[selected.length];
128 for (int i=0; i<selected.length; i++) {
129 urls[i] = ((ExampleDesign)selected[i]).getURL();
138 private static ExampleDesign[] getDirFileNames() {
140 // Try to find directory as a system resource
142 URL url = ClassLoader.getSystemResource(DIRECTORY);
145 dir = JarUtil.urlToFile(url);
146 } catch (Exception e1) {
147 dir = new File(DIRECTORY);
150 // Get the list of files
151 File[] files = dir.listFiles(FILTER);
155 ExampleDesign[] designs = new ExampleDesign[files.length];
157 for (int i=0; i<files.length; i++) {
158 String name = files[i].getName();
160 designs[i] = new ExampleDesign(files[i].toURI().toURL(),
161 name.substring(0, name.length()-4));
162 } catch (MalformedURLException e) {
163 throw new RuntimeException(e);
171 private static ExampleDesign[] getJarFileNames() {
173 ArrayList<ExampleDesign> list = new ArrayList<ExampleDesign>();
174 int dirLength = DIRECTORY.length();
176 // Find and open the jar file this class is contained in
177 File file = JarUtil.getCurrentJarFile();
182 // Generate URL pointing to JAR file
185 fileUrl = file.toURI().toURL();
186 } catch (MalformedURLException e1) {
187 e1.printStackTrace();
188 throw new RuntimeException(e1);
191 // Iterate over JAR entries searching for designs
192 JarFile jarFile = null;
194 jarFile = new JarFile(file);
196 // Loop through JAR entries searching for files to load
197 Enumeration<JarEntry> entries = jarFile.entries();
198 while (entries.hasMoreElements()) {
199 JarEntry entry = entries.nextElement();
200 String name = entry.getName();
201 if (name.startsWith(DIRECTORY) && FILTER.accept(null, name)) {
202 String urlName = "jar:" + fileUrl + "!/" + name;
203 URL url = new URL(urlName);
204 list.add(new ExampleDesign(url,
205 name.substring(dirLength, name.length()-4)));
209 } catch (IOException e) {
210 // Could be normal condition if not package in JAR
213 if (jarFile != null) {
216 } catch (IOException e) {
222 return list.toArray(new ExampleDesign[0]);
230 private static class ExampleDesign implements Comparable<ExampleDesign> {
232 private final URL url;
233 private final String name;
235 public ExampleDesign(URL url, String name) {
241 public String toString() {
245 public URL getURL() {
250 public int compareTo(ExampleDesign o) {
251 return this.name.compareTo(o.name);