1 package com.billkuker.rocketry.motorsim.visual.workbench;
\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
8 import java.io.FileInputStream;
\r
9 import java.io.FileOutputStream;
\r
10 import java.util.HashMap;
\r
11 import java.util.Vector;
\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
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
33 import fuel.FuelsEditor;
\r
35 public class MotorWorkbench extends JFrame {
\r
36 private static final long serialVersionUID = 1L;
\r
39 private JTabbedPane motors;
\r
40 private MultiBurnChart mb;
\r
41 private JFrame allBurns;
\r
43 private JFrame fuelEditorFrame = new JFrame(){
\r
44 private static final long serialVersionUID = 1L;
\r
47 add(fuelEditor = new FuelsEditor(this));
\r
49 setJMenuBar(b = new JMenuBar());
\r
50 b.add(fuelEditor.getMenu());
\r
51 setTitle("MotorSim - Fuel Editor");
\r
54 private FuelsEditor fuelEditor;
\r
56 private HashMap<MotorEditor, File> e2f = new HashMap<MotorEditor, File>();
\r
57 private HashMap<File, MotorEditor> f2e = new HashMap<File, MotorEditor>();
\r
59 private HashMap<Motor, MotorEditor> m2e = new HashMap<Motor, MotorEditor>();
\r
61 public MotorWorkbench() {
\r
62 setTitle("MotorSim 1.0 RC1");
\r
65 top = new JPanel(new BorderLayout());
\r
66 setContentPane(top);
\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
75 motors = new JTabbedPane();
\r
77 top.add(motors, BorderLayout.CENTER);
\r
79 for ( Fuel f : FuelResolver.getFuelMap().values() ){
\r
83 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
\r
88 private void addMenu() {
\r
90 setJMenuBar(new JMenuBar() {
\r
91 private static final long serialVersionUID = 1L;
\r
94 add(new JMenu("File") {
\r
95 private static final long serialVersionUID = 1L;
\r
98 add(new JMenuItem("New Motor") {
\r
99 private static final long serialVersionUID = 1L;
\r
101 addActionListener(new ActionListener() {
\r
104 public void actionPerformed(ActionEvent arg0) {
\r
105 addMotor(MotorEditor.defaultMotor(), null);
\r
111 add(new JMenuItem("Open...") {
\r
112 private static final long serialVersionUID = 1L;
\r
114 addActionListener(new ActionListener() {
\r
116 public void actionPerformed(ActionEvent arg0) {
\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
129 .readMotor(new FileInputStream(file));
\r
132 } catch (Exception e) {
\r
133 JOptionPane.showMessageDialog(
\r
134 MotorWorkbench.this, e
\r
143 add(new JSeparator());
\r
145 add(new JMenuItem("Close Motor") {
\r
146 private static final long serialVersionUID = 1L;
\r
148 addActionListener(new ActionListener() {
\r
150 public void actionPerformed(ActionEvent ev) {
\r
151 MotorEditor e = (MotorEditor) motors
\r
152 .getSelectedComponent();
\r
154 f2e.remove(e2f.get(e));
\r
156 m2e.remove(e.getMotor());
\r
157 mb.removeBurn(e.burn);
\r
163 add(new JMenuItem("Save Motor") {
\r
164 private static final long serialVersionUID = 1L;
\r
166 addActionListener(new ActionListener() {
\r
168 public void actionPerformed(ActionEvent e) {
\r
169 MotorEditor m = (MotorEditor) motors
\r
170 .getSelectedComponent();
\r
171 File f = e2f.get(m);
\r
173 save(m.getMotor(), f);
\r
179 add(new JMenuItem("Save Motor As...") {
\r
180 private static final long serialVersionUID = 1L;
\r
182 addActionListener(new ActionListener() {
\r
184 public void actionPerformed(ActionEvent arg0) {
\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
193 save(m.getMotor(), file);
\r
196 motors.setTitleAt(motors
\r
197 .getSelectedIndex(),
\r
199 // TODO Set tab title
\r
200 } catch (Exception e) {
\r
201 JOptionPane.showMessageDialog(
\r
202 MotorWorkbench.this, e
\r
212 add(new JSeparator());
\r
213 add(new JMenuItem("Export .ENG"){
\r
214 private static final long serialVersionUID = 1L;
\r
217 addActionListener(new ActionListener() {
\r
219 public void actionPerformed(ActionEvent arg0) {
\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
230 ENGExporter.export(bb, file);
\r
231 } catch ( Exception e ){
\r
232 e.printStackTrace();
\r
241 add(new JMenu("Settings") {
\r
242 private static final long serialVersionUID = 1L;
\r
244 ButtonGroup units = new ButtonGroup();
\r
245 JRadioButtonMenuItem sci = new JRadioButtonMenuItem(
\r
247 JRadioButtonMenuItem nonsci = new JRadioButtonMenuItem(
\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
256 .setUnitPreference(UnitPreference.SI);
\r
259 nonsci.addActionListener(new ActionListener() {
\r
260 public void actionPerformed(ActionEvent arg0) {
\r
262 .setUnitPreference(UnitPreference.NONSI);
\r
269 add(new JMenu("View") {
\r
270 private static final long serialVersionUID = 1L;
\r
272 add(new JMenuItem("All Motors Graph") {
\r
273 private static final long serialVersionUID = 1L;
\r
275 addActionListener(new ActionListener() {
\r
277 public void actionPerformed(ActionEvent arg0) {
\r
278 allBurns.setVisible(true);
\r
279 allBurns.toFront();
\r
284 add(new JMenuItem("Fuel Editor") {
\r
285 private static final long serialVersionUID = 1L;
\r
287 addActionListener(new ActionListener() {
\r
289 public void actionPerformed(ActionEvent arg0) {
\r
290 fuelEditorFrame.setVisible(true);
\r
291 fuelEditorFrame.toFront();
\r
302 private void addFuel(Fuel f){
\r
303 for ( MotorEditor e : m2e.values() )
\r
308 private void save(Motor m, File f) {
\r
310 FileOutputStream fo;
\r
311 MotorIO.writeMotor(m, fo = new FileOutputStream(f));
\r
313 } catch (Throwable t) {
\r
314 JOptionPane.showMessageDialog(MotorWorkbench.this, t.getMessage());
\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
323 title = "New Motor";
\r
325 title = f.getName();
\r
330 motors.addTab(title, e);
\r