1 package com.billkuker.rocketry.motorsim.fuel;
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.swing.ButtonGroup;
16 import javax.swing.JButton;
17 import javax.swing.JFrame;
18 import javax.swing.JPanel;
19 import javax.swing.JRadioButton;
20 import javax.swing.JScrollPane;
21 import javax.swing.JSplitPane;
22 import javax.swing.JTable;
23 import javax.swing.event.ChangeEvent;
24 import javax.swing.event.ChangeListener;
25 import javax.swing.table.AbstractTableModel;
27 import org.jscience.physics.amount.Amount;
29 import com.billkuker.rocketry.motorsim.RocketScience;
30 import com.billkuker.rocketry.motorsim.fuel.SaintRobertFuel.Type;
31 import com.billkuker.rocketry.motorsim.fuel.editable.EditablePiecewiseSaintRobertFuel;
33 public class SRFuelEditor extends AbstractFuelEditor {
34 private static final long serialVersionUID = 1L;
36 private static final NumberFormat nf = new DecimalFormat("##########.###");
38 private class Entry implements Comparable<Entry> {
39 Amount<Pressure> p = Amount.valueOf(0, RocketScience.UnitPreference.getUnitPreference().getPreferredUnit(RocketScience.PSI));
44 public int compareTo(Entry o) {
45 return p.compareTo(o.p);
49 private class TM extends AbstractTableModel {
50 private static final long serialVersionUID = 1L;
53 public int getColumnCount() {
58 public int getRowCount() {
59 return entries.size();
63 public String getColumnName(int col) {
68 return "Coefficient (a)";
70 return "Exponent (n)";
76 public Object getValueAt(int rowIndex, int columnIndex) {
77 Entry e = entries.get(rowIndex);
78 switch (columnIndex) {
80 //Format like 100 psi or 4.8 Mpa
81 return nf.format(e.p.doubleValue(e.p.getUnit())) + " " + e.p.getUnit();
90 public boolean isCellEditable(int row, int col) {
94 @SuppressWarnings("unchecked")
95 public void setValueAt(Object value, int row, int col) {
96 Entry e = entries.get(row);
101 e.p = (Amount<Pressure>) Amount.valueOf((String) value);
102 } catch ( Exception ee ){
103 double d = Double.parseDouble((String)value);
104 e.p = (Amount<Pressure>)Amount.valueOf(d, e.p.getUnit());
108 e.a = Double.valueOf((String) value);
111 e.n = Double.valueOf((String) value);
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) {
121 f.add(en.p, en.a, en.n);
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 EditablePiecewiseSaintRobertFuel f;
140 public SRFuelEditor(EditablePiecewiseSaintRobertFuel f) {
144 for ( Amount<Pressure> p : f.getAMap().keySet() ){
145 Entry e = new Entry();
146 e.a = f.getAMap().get(p);
147 e.n = f.getNMap().get(p);
150 Collections.sort(entries);
154 protected Component getBurnrateEditComponent(){
155 final TM tm = new TM();
157 JSplitPane editBottom = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
160 JTable table = new JTable(tm);
161 JScrollPane scrollpane = new JScrollPane(table);
162 scrollpane.setMinimumSize(new Dimension(200, 200));
163 editBottom.setTopComponent(scrollpane);
166 JButton add = new JButton("Add Data");
167 add.addActionListener(new ActionListener() {
169 public void actionPerformed(ActionEvent e) {
170 entries.add(new Entry());
171 tm.fireTableDataChanged();
174 controls = new JPanel();
175 controls.setPreferredSize(new Dimension(200, 50));
176 controls.setLayout(new FlowLayout());
181 final JRadioButton si, nonsi;
182 ButtonGroup type = new ButtonGroup();
183 JPanel radio = new JPanel();
184 radio.add(si = new JRadioButton("SI"));
185 radio.add(nonsi = new JRadioButton("NonSI"));
190 si.setSelected(true);
192 si.addChangeListener(new ChangeListener(){
194 public void stateChanged(ChangeEvent e) {
195 if ( si.isSelected() ){
198 f.setType(Type.NONSI);
203 editBottom.setBottomComponent(controls);
207 editBottom.setDividerLocation(.8);
212 public static void main(String args[]) {
213 JFrame f = new JFrame();
214 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
215 f.setContentPane(new SRFuelEditor(new EditablePiecewiseSaintRobertFuel()));