1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jarduino.gui;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Map.Entry;
22
23 import javax.swing.JComponent;
24 import javax.swing.JTable;
25 import javax.swing.table.AbstractTableModel;
26
27 import net.sourceforge.jarduino.message.ArduinoValues;
28
29
30
31
32 public class ArduinoTableValues {
33
34
35
36 private final JTable theTable;
37
38
39
40
41 private final ArduinoValuesModel theModel;
42
43
44
45
46 private final List<Number> theValues;
47
48
49
50
51 private final List<String> theNames;
52
53
54
55
56 private ArduinoValues theSelected;
57
58
59
60
61 ArduinoTableValues() {
62
63 theValues = new ArrayList<>();
64 theNames = new ArrayList<>();
65
66
67 theTable = new JTable();
68 theTable.setAutoCreateRowSorter(true);
69
70
71 theModel = new ArduinoValuesModel();
72 theTable.setModel(theModel);
73 }
74
75
76
77
78
79 JComponent getComponent() {
80 return theTable;
81 }
82
83
84
85
86
87
88 void configureTable(final ArduinoValues pSelected) {
89
90 theSelected = pSelected;
91
92
93 theModel.refresh();
94 }
95
96
97
98
99 private class ArduinoValuesModel
100 extends AbstractTableModel {
101
102
103
104 private static final long serialVersionUID = -5981115401584506612L;
105
106
107
108
109 private static final int COL_NAME = 0;
110
111
112
113
114 private static final int COL_VALUE = COL_NAME + 1;
115
116
117
118
119 ArduinoValuesModel() {
120 }
121
122
123
124
125 void refresh() {
126
127 theNames.clear();
128 theValues.clear();
129
130
131 for (Entry<Number, String> myEntry : theSelected.getValues().entrySet()) {
132
133 theNames.add(myEntry.getValue());
134 theValues.add(myEntry.getKey());
135 }
136
137
138 fireTableDataChanged();
139 }
140
141 @Override
142 public int getRowCount() {
143 return theNames.size();
144 }
145
146 @Override
147 public String getColumnName(final int pColIndex) {
148 switch (pColIndex) {
149 case COL_NAME:
150 return "Name";
151 case COL_VALUE:
152 return "Value";
153 default:
154 return null;
155 }
156 }
157
158 @Override
159 public int getColumnCount() {
160 return COL_VALUE + 1;
161 }
162
163 @Override
164 public boolean isCellEditable(final int pRowIndex,
165 final int pColIndex) {
166 return false;
167 }
168
169 @Override
170 public Object getValueAt(final int pRowIndex,
171 final int pColIndex) {
172 switch (pColIndex) {
173 case COL_NAME:
174 return theNames.get(pRowIndex);
175 case COL_VALUE:
176 return theValues.get(pRowIndex);
177 default:
178 return null;
179 }
180 }
181 }
182 }