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