create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / dialogs / ExampleDesignDialog.java
1 package net.sf.openrocket.gui.dialogs;
2
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;
9 import java.io.File;
10 import java.io.FilenameFilter;
11 import java.io.IOException;
12 import java.net.MalformedURLException;
13 import java.net.URL;
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;
19
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;
28
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;
35
36 public class ExampleDesignDialog extends JDialog {
37         
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();
41
42         private static final FilenameFilter FILTER = new FilenameFilter() {
43                 @Override
44                 public boolean accept(File dir, String name) {
45                         return name.matches(PATTERN);
46                 }
47         };
48         
49
50         private boolean open = false;
51         private final JList designSelection;
52         
53         private ExampleDesignDialog(ExampleDesign[] designs, Window parent) {
54                 //// Open example design
55                 super(parent, trans.get("exdesigndlg.lbl.Openexampledesign"), Dialog.ModalityType.APPLICATION_MODAL);
56                 
57                 JPanel panel = new JPanel(new MigLayout("fill"));
58                 
59                 //// Select example designs to open:
60                 panel.add(new JLabel(trans.get("exdesigndlg.lbl.Selectexample")), "wrap");
61                 
62                 designSelection = new JList(designs);
63                 designSelection.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
64                 designSelection.addMouseListener(new MouseAdapter() {
65                         @Override
66                         public void mouseClicked(MouseEvent e) {
67                                 if (e.getClickCount() >= 2) {
68                                         open = true;
69                                         ExampleDesignDialog.this.setVisible(false);
70                                 }
71                         }
72                 });
73                 panel.add(new JScrollPane(designSelection), "grow, wmin 300lp, wrap para");
74                 
75                 //// Open button
76                 JButton openButton = new JButton(trans.get("exdesigndlg.but.open"));
77                 openButton.addActionListener(new ActionListener() {
78                         @Override
79                         public void actionPerformed(ActionEvent e) {
80                                 open = true;
81                                 ExampleDesignDialog.this.setVisible(false);
82                         }
83                 });
84                 panel.add(openButton, "split 2, sizegroup buttons, growx");
85                 
86                 //// Cancel button
87                 JButton cancelButton = new JButton(trans.get("dlg.but.cancel"));
88                 cancelButton.addActionListener(new ActionListener() {
89                         @Override
90                         public void actionPerformed(ActionEvent e) {
91                                 open = false;
92                                 ExampleDesignDialog.this.setVisible(false);
93                         }
94                 });
95                 panel.add(cancelButton, "sizegroup buttons, growx");
96
97                 this.add(panel);
98                 this.pack();
99                 this.setLocationByPlatform(true);
100                 
101                 GUIUtil.setDisposableDialogOptions(this, openButton);
102         }
103         
104         
105         /**
106          * Open a dialog to allow opening the example designs.
107          * 
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
110          *                                      was cancelled.
111          */
112         public static URL[] selectExampleDesigns(Window parent) {
113                 
114                 ExampleDesign[] designs;
115                 
116                 designs = getJarFileNames();
117                 if (designs == null || designs.length == 0) {
118                         designs = getDirFileNames();
119                 }
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);
125                         return null;
126                 }
127                 
128                 Arrays.sort(designs);
129                 
130                 ExampleDesignDialog dialog = new ExampleDesignDialog(designs, parent);
131                 dialog.setVisible(true);
132                 
133                 if (!dialog.open) {
134                         return null;
135                 }
136                 
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();
141                 }
142                 return urls;
143         }
144         
145         
146         
147         
148         
149         private static ExampleDesign[] getDirFileNames() {
150                 
151                 // Try to find directory as a system resource
152                 File dir;
153                 URL url = ClassLoader.getSystemResource(DIRECTORY);
154                 
155                 try {
156                         dir = JarUtil.urlToFile(url);
157                 } catch (Exception e1) {
158                         dir = new File(DIRECTORY);
159                 }
160
161                 // Get the list of files
162                 File[] files = dir.listFiles(FILTER);
163                 if (files == null)
164                         return null;
165                 
166                 ExampleDesign[] designs = new ExampleDesign[files.length];
167                 
168                 for (int i=0; i<files.length; i++) {
169                         String name = files[i].getName();
170                         try {
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);
175                         }
176                 }
177                 return designs;
178         }
179
180         
181         
182         private static ExampleDesign[] getJarFileNames() {
183                 
184                 ArrayList<ExampleDesign> list = new ArrayList<ExampleDesign>();
185                 int dirLength = DIRECTORY.length();
186
187                 // Find and open the jar file this class is contained in
188                 File file = JarUtil.getCurrentJarFile();
189                 if (file == null)
190                         return null;
191                 
192
193                 // Generate URL pointing to JAR file
194                 URL fileUrl;
195                 try {
196                         fileUrl = file.toURI().toURL();
197                 } catch (MalformedURLException e1) {
198                         e1.printStackTrace();
199                         throw new BugException(e1);
200                 }
201                 
202                 // Iterate over JAR entries searching for designs
203                 JarFile jarFile = null;
204                 try {
205                         jarFile = new JarFile(file);
206
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)));
217                                 }
218                         }
219
220                 } catch (IOException e) {
221                         // Could be normal condition if not package in JAR
222                         return null;
223                 } finally {
224                         if (jarFile != null) {
225                                 try {
226                                         jarFile.close();
227                                 } catch (IOException e) {
228                                         e.printStackTrace();
229                                 }
230                         }
231                 }
232                 
233                 return list.toArray(new ExampleDesign[0]);
234         }
235         
236         
237         
238         /**
239          * Data holder class.
240          */
241         private static class ExampleDesign implements Comparable<ExampleDesign> {
242                 
243                 private final URL url;
244                 private final String name;
245                 
246                 public ExampleDesign(URL url, String name) {
247                         this.url = url;
248                         this.name = name;
249                 }
250                 
251                 @Override
252                 public String toString() {
253                         return name;
254                 }
255                 
256                 public URL getURL() {
257                         return url;
258                 }
259
260                 @Override
261                 public int compareTo(ExampleDesign o) {
262                         return this.name.compareTo(o.name);
263                 }
264         }
265         
266 }