Big update to custom expression feature.
[debian/openrocket] / core / src / net / sf / openrocket / gui / customexpression / VariableTableModel.java
1 /**
2  * 
3  */
4 package net.sf.openrocket.gui.customexpression;
5
6 import java.awt.event.MouseAdapter;
7 import java.awt.event.MouseEvent;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.Vector;
11
12 import javax.swing.JTable;
13 import javax.swing.table.AbstractTableModel;
14 import javax.swing.table.TableColumn;
15 import javax.swing.table.TableColumnModel;
16
17 import net.sf.openrocket.document.OpenRocketDocument;
18 import net.sf.openrocket.l10n.Translator;
19 import net.sf.openrocket.simulation.customexpression.CustomExpression;
20 import net.sf.openrocket.simulation.FlightDataType;
21 import net.sf.openrocket.startup.Application;
22
23 /**
24  * @author Richard Graham
25  *
26  */
27 public class VariableTableModel extends AbstractTableModel {
28
29         private static final Translator trans = Application.getTranslator();
30
31         private ArrayList<FlightDataType> types = new ArrayList<FlightDataType>();
32         private static final String[] columnNames = {trans.get("customExpression.Name"), trans.get("customExpression.Symbol"), trans.get("customExpression.Units")};
33         
34         /*
35          * Table model will be constructed with all the built in variables and any custom variables defined
36          */
37         public VariableTableModel(OpenRocketDocument doc){
38                 
39                 Collections.addAll(types, FlightDataType.ALL_TYPES);
40                 
41                 for (CustomExpression expression : doc.getCustomExpressions()){
42                         types.add(expression.getType());
43                 }
44         }
45         
46         @Override
47         public int getColumnCount() {
48                 return 3;
49         }
50
51         @Override
52         public int getRowCount() {
53                 return types.size();
54         }
55
56         @Override
57         public Object getValueAt(int row, int col) {
58                 if (col == 0)
59                         return types.get(row).getName();
60                 else if (col == 1)
61                         return types.get(row).getSymbol();
62                 else if (col == 2)
63                         return types.get(row).getUnitGroup().getSIUnit().toString();
64                 
65                 return null;
66         }
67         
68         @Override
69         public String getColumnName(int col) {
70         return columnNames[col];
71     }
72         
73         public String getSymbolAt(int row) {
74                 if (row < 0 || row > types.size()){
75                         return "";
76                 }
77                 else { 
78                         return types.get(row).getSymbol();
79                 }
80         }
81 }