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.util.Collections;
11 import java.util.Vector;
13 import javax.measure.quantity.Pressure;
14 import javax.measure.quantity.Velocity;
15 import javax.measure.unit.SI;
16 import javax.swing.JButton;
17 import javax.swing.JFrame;
18 import javax.swing.JPanel;
19 import javax.swing.JScrollPane;
20 import javax.swing.JSplitPane;
21 import javax.swing.JTable;
22 import javax.swing.table.AbstractTableModel;
24 import org.jscience.physics.amount.Amount;
26 import com.billkuker.rocketry.motorsim.RocketScience;
27 import com.billkuker.rocketry.motorsim.fuel.editable.EditablePiecewiseLinearFuel;
29 public class LinearFuelEditor extends AbstractFuelEditor {
30 private static final long serialVersionUID = 1L;
32 private class Entry implements Comparable<Entry> {
33 Amount<Pressure> p = Amount.valueOf(0, RocketScience.UnitPreference.getUnitPreference().getPreferredUnit(RocketScience.PSI));
34 Amount<Velocity> v = Amount.valueOf(0, RocketScience.UnitPreference.getUnitPreference().getPreferredUnit(SI.METERS_PER_SECOND));
37 public int compareTo(Entry o) {
38 return p.compareTo(o.p);
42 private class TM extends AbstractTableModel {
43 private static final long serialVersionUID = 1L;
46 public int getColumnCount() {
51 public int getRowCount() {
52 return entries.size();
56 public String getColumnName(int col) {
67 public Object getValueAt(int rowIndex, int columnIndex) {
68 Entry e = entries.get(rowIndex);
69 switch (columnIndex) {
71 return RocketScience.ammountToString(e.p);
73 return RocketScience.ammountToString(e.v);
78 public boolean isCellEditable(int row, int col) {
82 @SuppressWarnings("unchecked")
83 public void setValueAt(Object value, int row, int col) {
84 Entry e = entries.get(row);
89 e.p = (Amount<Pressure>) Amount.valueOf((String) value);
90 } catch ( Exception ee ){
91 double d = Double.parseDouble((String)value);
92 e.p = (Amount<Pressure>)Amount.valueOf(d, e.p.getUnit());
97 e.v = (Amount<Velocity>) Amount.valueOf((String) value);
98 } catch ( Exception ee ){
99 double d = Double.parseDouble((String)value);
100 e.v = (Amount<Velocity>)Amount.valueOf(d, e.v.getUnit());
104 } catch (Exception ex) {
105 ex.printStackTrace();
107 Collections.sort(entries);
108 fireTableDataChanged();
109 //f = new EditablePSRFuel(SaintRobertFuel.Type.NONSI);
111 for (Entry en : entries) {
114 f.firePropertyChange(new PropertyChangeEvent(f,"entries", null, null));
121 public void fireTableDataChanged() {
122 super.fireTableDataChanged();
127 private Vector<Entry> entries = new Vector<Entry>();
129 final EditablePiecewiseLinearFuel f;
131 public LinearFuelEditor(EditablePiecewiseLinearFuel f) {
134 for ( Map.Entry<Amount<Pressure>, Amount<Velocity>> e : f.getEntries().entrySet() ){
135 Entry n = new Entry();
140 Collections.sort(entries);
143 protected Component getBurnrateEditComponent(){
144 final TM tm = new TM();
146 JSplitPane editBottom = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
149 JTable table = new JTable(tm);
150 JScrollPane scrollpane = new JScrollPane(table);
151 scrollpane.setMinimumSize(new Dimension(200, 200));
152 editBottom.setTopComponent(scrollpane);
155 JButton add = new JButton("Add Data");
156 add.addActionListener(new ActionListener() {
158 public void actionPerformed(ActionEvent e) {
159 entries.add(new Entry());
160 tm.fireTableDataChanged();
163 controls = new JPanel();
164 controls.setPreferredSize(new Dimension(200, 50));
165 controls.setLayout(new FlowLayout());
169 editBottom.setBottomComponent(controls);
173 editBottom.setDividerLocation(.8);
178 public static void main(String args[]) {
179 JFrame f = new JFrame();
180 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
181 f.setContentPane(new LinearFuelEditor(new EditablePiecewiseLinearFuel()));