1 package com.billkuker.rocketry.motorsim.visual.workbench;
3 import java.awt.Component;
4 import java.awt.Dimension;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.beans.PropertyChangeEvent;
9 import java.text.DecimalFormat;
10 import java.text.NumberFormat;
11 import java.util.Collections;
12 import java.util.Vector;
14 import javax.measure.quantity.Pressure;
15 import javax.measure.quantity.Velocity;
16 import javax.measure.unit.SI;
17 import javax.swing.ButtonGroup;
18 import javax.swing.JButton;
19 import javax.swing.JFrame;
20 import javax.swing.JPanel;
21 import javax.swing.JRadioButton;
22 import javax.swing.JScrollPane;
23 import javax.swing.JSplitPane;
24 import javax.swing.JTable;
25 import javax.swing.event.ChangeEvent;
26 import javax.swing.event.ChangeListener;
27 import javax.swing.table.AbstractTableModel;
29 import org.jscience.physics.amount.Amount;
31 import com.billkuker.rocketry.motorsim.RocketScience;
32 import com.billkuker.rocketry.motorsim.fuel.PiecewiseLinearFuel;
33 import com.billkuker.rocketry.motorsim.fuel.SaintRobertFuel;
34 import com.billkuker.rocketry.motorsim.fuel.SaintRobertFuel.Type;
36 public class LinearFuelEditor extends AbstractFuelEditor {
37 private static final long serialVersionUID = 1L;
39 private static final NumberFormat nf = new DecimalFormat("##########.###");
41 private class Entry implements Comparable<Entry> {
42 Amount<Pressure> p = Amount.valueOf(0, RocketScience.UnitPreference.getUnitPreference().getPreferredUnit(RocketScience.PSI));
43 Amount<Velocity> v = Amount.valueOf(0, RocketScience.UnitPreference.getUnitPreference().getPreferredUnit(SI.METERS_PER_SECOND));
46 public int compareTo(Entry o) {
47 return p.compareTo(o.p);
51 private class TM extends AbstractTableModel {
52 private static final long serialVersionUID = 1L;
55 public int getColumnCount() {
60 public int getRowCount() {
61 return entries.size();
65 public String getColumnName(int col) {
76 public Object getValueAt(int rowIndex, int columnIndex) {
77 Entry e = entries.get(rowIndex);
78 switch (columnIndex) {
80 return RocketScience.ammountToString(e.p);
82 return RocketScience.ammountToString(e.v);
87 public boolean isCellEditable(int row, int col) {
91 @SuppressWarnings("unchecked")
92 public void setValueAt(Object value, int row, int col) {
93 Entry e = entries.get(row);
98 e.p = (Amount<Pressure>) Amount.valueOf((String) value);
99 } catch ( Exception ee ){
100 double d = Double.parseDouble((String)value);
101 e.p = (Amount<Pressure>)Amount.valueOf(d, e.p.getUnit());
106 e.v = (Amount<Velocity>) Amount.valueOf((String) value);
107 } catch ( Exception ee ){
108 double d = Double.parseDouble((String)value);
109 e.v = (Amount<Velocity>)Amount.valueOf(d, e.v.getUnit());
113 } catch (Exception ex) {
114 ex.printStackTrace();
116 Collections.sort(entries);
117 fireTableDataChanged();
118 //f = new EditablePSRFuel(SaintRobertFuel.Type.NONSI);
120 for (Entry en : entries) {
123 f.firePropertyChange(new PropertyChangeEvent(f,"entries", null, null));
130 public void fireTableDataChanged() {
131 super.fireTableDataChanged();
136 private Vector<Entry> entries = new Vector<Entry>();
138 final PiecewiseLinearFuel f;
140 public LinearFuelEditor(PiecewiseLinearFuel f) {
145 protected Component getBurnrateEditComponent(){
146 final TM tm = new TM();
148 JSplitPane editBottom = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
151 JTable table = new JTable(tm);
152 JScrollPane scrollpane = new JScrollPane(table);
153 scrollpane.setMinimumSize(new Dimension(200, 200));
154 editBottom.setTopComponent(scrollpane);
157 JButton add = new JButton("Add Data");
158 add.addActionListener(new ActionListener() {
160 public void actionPerformed(ActionEvent e) {
161 entries.add(new Entry());
162 tm.fireTableDataChanged();
165 controls = new JPanel();
166 controls.setPreferredSize(new Dimension(200, 50));
167 controls.setLayout(new FlowLayout());
171 editBottom.setBottomComponent(controls);
175 editBottom.setDividerLocation(.8);
180 public static void main(String args[]) {
181 JFrame f = new JFrame();
182 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
183 f.setContentPane(new LinearFuelEditor(new PiecewiseLinearFuel()));