]> git.gag.com Git - sw/motorsim/blob - gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorWorkbench.java
5e9652f1ed4f4e59a07ce53cddc8059fb8180297
[sw/motorsim] / gui / com / billkuker / rocketry / motorsim / visual / workbench / MotorWorkbench.java
1 package com.billkuker.rocketry.motorsim.visual.workbench;\r
2 \r
3 import java.awt.BorderLayout;\r
4 import java.awt.FileDialog;\r
5 import java.awt.event.ActionEvent;\r
6 import java.awt.event.ActionListener;\r
7 import java.io.File;\r
8 import java.io.FileInputStream;\r
9 import java.io.FileOutputStream;\r
10 import java.util.HashMap;\r
11 import java.util.Vector;\r
12 \r
13 import javax.swing.ButtonGroup;\r
14 import javax.swing.JFrame;\r
15 import javax.swing.JMenu;\r
16 import javax.swing.JMenuBar;\r
17 import javax.swing.JMenuItem;\r
18 import javax.swing.JOptionPane;\r
19 import javax.swing.JPanel;\r
20 import javax.swing.JRadioButtonMenuItem;\r
21 import javax.swing.JSeparator;\r
22 import javax.swing.JTabbedPane;\r
23 import javax.swing.WindowConstants;\r
24 \r
25 import com.billkuker.rocketry.motorsim.Burn;\r
26 import com.billkuker.rocketry.motorsim.Fuel;\r
27 import com.billkuker.rocketry.motorsim.Motor;\r
28 import com.billkuker.rocketry.motorsim.RocketScience.UnitPreference;\r
29 import com.billkuker.rocketry.motorsim.fuel.FuelResolver;\r
30 import com.billkuker.rocketry.motorsim.io.ENGExporter;\r
31 import com.billkuker.rocketry.motorsim.io.MotorIO;\r
32 \r
33 import fuel.FuelsEditor;\r
34 \r
35 public class MotorWorkbench extends JFrame {\r
36         private static final long serialVersionUID = 1L;\r
37         \r
38         private JPanel top;\r
39         private JTabbedPane motors;\r
40         private MultiBurnChart mb;\r
41         private JFrame allBurns;\r
42         \r
43         private JFrame fuelEditorFrame = new JFrame(){\r
44                 private static final long serialVersionUID = 1L;\r
45                 {\r
46                         setSize(1024, 768);\r
47                         add(fuelEditor = new FuelsEditor(this));\r
48                         JMenuBar b;\r
49                         setJMenuBar(b = new JMenuBar());\r
50                         b.add(fuelEditor.getMenu());\r
51                         setTitle("MotorSim - Fuel Editor");\r
52                 }\r
53         };\r
54         private FuelsEditor fuelEditor;\r
55 \r
56         private HashMap<MotorEditor, File> e2f = new HashMap<MotorEditor, File>();\r
57         private HashMap<File, MotorEditor> f2e = new HashMap<File, MotorEditor>();\r
58 \r
59         private HashMap<Motor, MotorEditor> m2e = new HashMap<Motor, MotorEditor>();\r
60         \r
61         public MotorWorkbench() {\r
62                 setTitle("MotorSim 1.0 RC1");\r
63                 addMenu();\r
64                 setSize(1024, 768);\r
65                 top = new JPanel(new BorderLayout());\r
66                 setContentPane(top);\r
67                 \r
68                 mb = new MultiBurnChart();\r
69                 allBurns = new JFrame();\r
70                 allBurns.setTitle("All Burns");\r
71                 allBurns.setSize(800, 600);\r
72                 setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);\r
73                 allBurns.add(mb);\r
74 \r
75                 motors = new JTabbedPane();\r
76                 \r
77                 top.add(motors, BorderLayout.CENTER);\r
78                 \r
79                 for ( Fuel f : FuelResolver.getFuelMap().values() ){\r
80                         addFuel(f);\r
81                 }\r
82 \r
83                 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);\r
84                 setVisible(true);\r
85 \r
86         }\r
87 \r
88         private void addMenu() {\r
89 \r
90                 setJMenuBar(new JMenuBar() {\r
91                         private static final long serialVersionUID = 1L;\r
92 \r
93                         {\r
94                                 add(new JMenu("File") {\r
95                                         private static final long serialVersionUID = 1L;\r
96 \r
97                                         {\r
98                                                 add(new JMenuItem("New Motor") {\r
99                                                         private static final long serialVersionUID = 1L;\r
100                                                         {\r
101                                                                 addActionListener(new ActionListener() {\r
102 \r
103                                                                         @Override\r
104                                                                         public void actionPerformed(ActionEvent arg0) {\r
105                                                                                 addMotor(MotorEditor.defaultMotor(), null);\r
106                                                                         }\r
107                                                                 });\r
108 \r
109                                                         }\r
110                                                 });\r
111                                                 add(new JMenuItem("Open...") {\r
112                                                         private static final long serialVersionUID = 1L;\r
113                                                         {\r
114                                                                 addActionListener(new ActionListener() {\r
115                                                                         @Override\r
116                                                                         public void actionPerformed(ActionEvent arg0) {\r
117 \r
118                                                                                 final FileDialog fd = new FileDialog(MotorWorkbench.this, "Open Motor", FileDialog.LOAD);\r
119                                                                                 fd.setVisible(true);\r
120                                                                                 if ( fd.getFile() != null ) {\r
121                                                                                         File file = new File(fd.getDirectory() + fd.getFile());\r
122                                                                                         if (f2e.get(file) != null) {\r
123                                                                                                 motors.setSelectedComponent(f2e\r
124                                                                                                                 .get(file));\r
125                                                                                                 return;\r
126                                                                                         }\r
127                                                                                         try {\r
128                                                                                                 Motor m = MotorIO\r
129                                                                                                                 .readMotor(new FileInputStream(file));\r
130                                                                                                 addMotor(m, file);\r
131                                                                                                 \r
132                                                                                         } catch (Exception e) {\r
133                                                                                                 JOptionPane.showMessageDialog(\r
134                                                                                                                 MotorWorkbench.this, e\r
135                                                                                                                                 .getMessage());\r
136                                                                                         }\r
137                                                                                 }\r
138                                                                         }\r
139                                                                 });\r
140                                                         }\r
141                                                 });\r
142                                                 \r
143                                                 add(new JSeparator());\r
144                                                 \r
145                                                 add(new JMenuItem("Close Motor") {\r
146                                                         private static final long serialVersionUID = 1L;\r
147                                                         {\r
148                                                                 addActionListener(new ActionListener() {\r
149                                                                         @Override\r
150                                                                         public void actionPerformed(ActionEvent ev) {\r
151                                                                                 MotorEditor e = (MotorEditor) motors\r
152                                                                                                 .getSelectedComponent();\r
153                                                                                 motors.remove(e);\r
154                                                                                 f2e.remove(e2f.get(e));\r
155                                                                                 e2f.remove(e);\r
156                                                                                 m2e.remove(e.getMotor());\r
157                                                                                 mb.removeBurn(e.burn);\r
158                                                                         }\r
159                                                                 });\r
160                                                         }\r
161                                                 });\r
162                                                 \r
163                                                 add(new JMenuItem("Save Motor") {\r
164                                                         private static final long serialVersionUID = 1L;\r
165                                                         {\r
166                                                                 addActionListener(new ActionListener() {\r
167                                                                         @Override\r
168                                                                         public void actionPerformed(ActionEvent e) {\r
169                                                                                 MotorEditor m = (MotorEditor) motors\r
170                                                                                                 .getSelectedComponent();\r
171                                                                                 File f = e2f.get(m);\r
172                                                                                 if (f != null)\r
173                                                                                         save(m.getMotor(), f);\r
174 \r
175                                                                         }\r
176                                                                 });\r
177                                                         }\r
178                                                 });\r
179                                                 add(new JMenuItem("Save Motor As...") {\r
180                                                         private static final long serialVersionUID = 1L;\r
181                                                         {\r
182                                                                 addActionListener(new ActionListener() {\r
183                                                                         @Override\r
184                                                                         public void actionPerformed(ActionEvent arg0) {\r
185 \r
186                                                                                 final FileDialog fd = new FileDialog(MotorWorkbench.this, "Save Motor As", FileDialog.SAVE);\r
187                                                                                 fd.setVisible(true);\r
188                                                                                 if (fd.getFile() != null ) {\r
189                                                                                         File file = new File(fd.getDirectory() + fd.getFile());\r
190                                                                                         MotorEditor m = (MotorEditor) motors\r
191                                                                                                         .getSelectedComponent();\r
192                                                                                         try {\r
193                                                                                                 save(m.getMotor(), file);\r
194                                                                                                 e2f.put(m, file);\r
195                                                                                                 f2e.put(file, m);\r
196                                                                                                 motors.setTitleAt(motors\r
197                                                                                                                 .getSelectedIndex(),\r
198                                                                                                                 file.getName());\r
199                                                                                                 // TODO Set tab title\r
200                                                                                         } catch (Exception e) {\r
201                                                                                                 JOptionPane.showMessageDialog(\r
202                                                                                                                 MotorWorkbench.this, e\r
203                                                                                                                                 .getMessage());\r
204                                                                                         }\r
205                                                                                 }\r
206                                                                         }\r
207                                                                 });\r
208                                                         }\r
209                                                 });\r
210                                                 \r
211 \r
212                                                 add(new JSeparator());\r
213                                                 add(new JMenuItem("Export .ENG"){\r
214                                                         private static final long serialVersionUID = 1L;\r
215 \r
216                                                         {\r
217                                                                 addActionListener(new ActionListener() {\r
218                                                                         @Override\r
219                                                                         public void actionPerformed(ActionEvent arg0) {\r
220 \r
221                                                                                 final FileDialog fd = new FileDialog(MotorWorkbench.this, "Export .ENG File", FileDialog.SAVE);\r
222                                                                                 fd.setFile("motorsim.eng");\r
223                                                                                 fd.setVisible(true);\r
224                                                                                 if (fd.getFile() != null ) {\r
225                                                                                         File file = new File(fd.getDirectory() + fd.getFile());\r
226                                                                                         Vector<Burn> bb = new Vector<Burn>();\r
227                                                                                         for( MotorEditor me : m2e.values() )\r
228                                                                                                 bb.add(me.burn);\r
229                                                                                         try{\r
230                                                                                                 ENGExporter.export(bb, file);\r
231                                                                                         } catch ( Exception e ){\r
232                                                                                                 e.printStackTrace();\r
233                                                                                         }\r
234                                                                                 }\r
235                                                                         }\r
236                                                                 });\r
237                                                         }\r
238                                                 });\r
239                                         }\r
240                                 });\r
241                                 add(new JMenu("Settings") {\r
242                                         private static final long serialVersionUID = 1L;\r
243                                         {\r
244                                                 ButtonGroup units = new ButtonGroup();\r
245                                                 JRadioButtonMenuItem sci = new JRadioButtonMenuItem(\r
246                                                                 "SI");\r
247                                                 JRadioButtonMenuItem nonsci = new JRadioButtonMenuItem(\r
248                                                                 "NonSI");\r
249                                                 units.add(sci);\r
250                                                 units.add(nonsci);\r
251                                                 sci.setSelected(UnitPreference.getUnitPreference() == UnitPreference.SI);\r
252                                                 nonsci.setSelected(UnitPreference.getUnitPreference() == UnitPreference.NONSI);\r
253                                                 sci.addActionListener(new ActionListener() {\r
254                                                         public void actionPerformed(ActionEvent arg0) {\r
255                                                                 UnitPreference\r
256                                                                                 .setUnitPreference(UnitPreference.SI);\r
257                                                         }\r
258                                                 });\r
259                                                 nonsci.addActionListener(new ActionListener() {\r
260                                                         public void actionPerformed(ActionEvent arg0) {\r
261                                                                 UnitPreference\r
262                                                                                 .setUnitPreference(UnitPreference.NONSI);\r
263                                                         }\r
264                                                 });\r
265                                                 add(sci);\r
266                                                 add(nonsci);\r
267                                         }\r
268                                 });\r
269                                 add(new JMenu("View") {\r
270                                         private static final long serialVersionUID = 1L;\r
271                                         {\r
272                                                 add(new JMenuItem("All Motors Graph") {\r
273                                                         private static final long serialVersionUID = 1L;\r
274                                                         {\r
275                                                                 addActionListener(new ActionListener() {\r
276                                                                         @Override\r
277                                                                         public void actionPerformed(ActionEvent arg0) {\r
278                                                                                 allBurns.setVisible(true);\r
279                                                                                 allBurns.toFront();\r
280                                                                         }\r
281                                                                 });\r
282                                                         }\r
283                                                 });\r
284                                                 add(new JMenuItem("Fuel Editor") {\r
285                                                         private static final long serialVersionUID = 1L;\r
286                                                         {\r
287                                                                 addActionListener(new ActionListener() {\r
288                                                                         @Override\r
289                                                                         public void actionPerformed(ActionEvent arg0) {\r
290                                                                                 fuelEditorFrame.setVisible(true);\r
291                                                                                 fuelEditorFrame.toFront();\r
292                                                                         }\r
293                                                                 });\r
294                                                         }\r
295                                                 });\r
296                                         }\r
297                                 });\r
298                         }\r
299                 });\r
300         }\r
301         \r
302         private void addFuel(Fuel f){\r
303                 for ( MotorEditor e : m2e.values() )\r
304                         e.addFuel(f);\r
305         }\r
306         \r
307 \r
308         private void save(Motor m, File f) {\r
309                 try {\r
310                         FileOutputStream fo;\r
311                         MotorIO.writeMotor(m, fo = new FileOutputStream(f));\r
312                         fo.close();\r
313                 } catch (Throwable t) {\r
314                         JOptionPane.showMessageDialog(MotorWorkbench.this, t.getMessage());\r
315                 }\r
316         }\r
317 \r
318         public void addMotor(Motor m, File f) {\r
319                 MotorEditor e = new MotorEditor(m, FuelResolver.getFuelMap().values());\r
320                 e.addBurnWatcher(mb);\r
321                 String title;\r
322                 if (f == null) {\r
323                         title = "New Motor";\r
324                 } else {\r
325                         title = f.getName();\r
326                         e2f.put(e, f);\r
327                         f2e.put(f, e);\r
328                 }\r
329                 m2e.put(m, e);\r
330                 motors.addTab(title, e);\r
331         }\r
332 \r
333         \r
334 }\r