1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
package ca.spaz.cron.ui; |
5 |
|
|
6 |
|
import java.awt.*; |
7 |
|
import java.awt.event.*; |
8 |
|
import java.util.*; |
9 |
|
import java.util.List; |
10 |
|
|
11 |
|
import javax.swing.*; |
12 |
|
import javax.swing.event.*; |
13 |
|
import javax.swing.table.*; |
14 |
|
|
15 |
|
import ca.spaz.cron.database.*; |
16 |
|
import ca.spaz.gui.PrettyTable; |
17 |
|
import ca.spaz.util.ImageFactory; |
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
public class MeasureEditor extends JPanel { |
25 |
|
|
26 |
|
private WeightTableModel model; |
27 |
|
|
28 |
|
private JTable weightTable; |
29 |
|
|
30 |
|
private Food food; |
31 |
|
|
32 |
|
private ArrayList curWeights; |
33 |
|
|
34 |
|
private JButton delBtn; |
35 |
|
|
36 |
|
private JButton addBtn; |
37 |
|
|
38 |
|
private JScrollPane weightScrollPane; |
39 |
|
|
40 |
|
private JToolBar toolBar; |
41 |
|
|
42 |
|
private JPanel buttonPanel; |
43 |
|
|
44 |
|
private boolean dirty = false; |
45 |
|
|
46 |
|
private Vector listeners; |
47 |
|
|
48 |
|
public MeasureEditor(Food f) { |
49 |
|
this.food = f; |
50 |
|
resetMeasures(); |
51 |
|
initialize(); |
52 |
|
} |
53 |
|
|
54 |
|
private void resetMeasures() { |
55 |
|
this.curWeights = new ArrayList(); |
56 |
|
this.curWeights.addAll(food.getMeasures()); |
57 |
|
this.curWeights.remove(Measure.GRAM); |
58 |
|
} |
59 |
|
|
60 |
|
public void addChangeListener(ChangeListener cl) { |
61 |
|
getListeners().add(cl); |
62 |
|
} |
63 |
|
|
64 |
|
public void removeChangeListener(ChangeListener cl) { |
65 |
|
getListeners().remove(cl); |
66 |
|
} |
67 |
|
|
68 |
|
private Vector getListeners() { |
69 |
|
if (listeners == null) { |
70 |
|
listeners = new Vector(); |
71 |
|
} |
72 |
|
return listeners; |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
|
public void fireChangeEvent() { |
77 |
|
ChangeEvent ce = new ChangeEvent(this); |
78 |
|
Iterator iter = listeners.iterator(); |
79 |
|
while (iter.hasNext()) { |
80 |
|
((ChangeListener)iter.next()).stateChanged(ce); |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
private void initialize() { |
88 |
|
this.setLayout(new BorderLayout(4, 4)); |
89 |
|
this.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); |
90 |
|
this.add(getToolBar(), BorderLayout.WEST); |
91 |
|
this.add(getWeightScrollTable(), BorderLayout.CENTER); |
92 |
|
|
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
protected List getMeasures() { |
97 |
|
List measures = new ArrayList(); |
98 |
|
measures.add(Measure.GRAM); |
99 |
|
measures.addAll(curWeights); |
100 |
|
return measures; |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
private JScrollPane getWeightScrollTable() { |
107 |
|
if (null == weightScrollPane) { |
108 |
|
weightScrollPane = new JScrollPane(); |
109 |
|
weightScrollPane.setViewportView(getWeightTable()); |
110 |
|
weightScrollPane.getViewport().setBackground(Color.WHITE); |
111 |
|
weightScrollPane.setPreferredSize(new Dimension(300, 120)); |
112 |
|
} |
113 |
|
return weightScrollPane; |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
private JToolBar getToolBar() { |
121 |
|
if (null == toolBar) { |
122 |
|
toolBar = new JToolBar(); |
123 |
|
toolBar.setOrientation(JToolBar.VERTICAL); |
124 |
|
toolBar.setFloatable(false); |
125 |
|
toolBar.add(getAddButton()); |
126 |
|
toolBar.add(getDeleteButton()); |
127 |
|
toolBar.add(Box.createGlue()); |
128 |
|
} |
129 |
|
return toolBar; |
130 |
|
} |
131 |
|
|
132 |
|
private JButton getAddButton() { |
133 |
|
if (null == addBtn) { |
134 |
|
ImageIcon icon = new ImageIcon(ImageFactory.getInstance().loadImage("/img/Add24.gif")); |
135 |
|
addBtn = new JButton(icon); |
136 |
|
addBtn.setToolTipText("Add a new measurement."); |
137 |
|
addBtn.addActionListener(new ActionListener() { |
138 |
|
public void actionPerformed(ActionEvent e) { |
139 |
|
addMeasure(); |
140 |
|
} |
141 |
|
}); |
142 |
|
} |
143 |
|
return addBtn; |
144 |
|
} |
145 |
|
|
146 |
|
private JButton getDeleteButton() { |
147 |
|
if (null == delBtn) { |
148 |
|
ImageIcon icon = new ImageIcon(ImageFactory.getInstance().loadImage("/img/Delete24.gif")); |
149 |
|
delBtn = new JButton(icon); |
150 |
|
delBtn.setToolTipText("Delete the selected measurement."); |
151 |
|
delBtn.addActionListener(new ActionListener() { |
152 |
0 |
public void actionPerformed(ActionEvent e) { |
153 |
0 |
deleteSelectedWeight(); |
154 |
0 |
} |
155 |
|
}); |
156 |
|
} |
157 |
|
return delBtn; |
158 |
|
} |
159 |
|
|
160 |
|
private void addMeasure() { |
161 |
|
model.getWeights().add(new Measure(1.0, "Serving", 1.0)); |
162 |
|
model.fireTableDataChanged(); |
163 |
|
int n = model.getWeights().size() - 1; |
164 |
|
weightTable.getSelectionModel().setSelectionInterval(n, n); |
165 |
|
setDirty(true); |
166 |
|
} |
167 |
|
|
168 |
|
private void deleteSelectedWeight() { |
169 |
|
int row = weightTable.getSelectedRow(); |
170 |
|
if (row >= 0) { |
171 |
|
model.getWeights().remove(row); |
172 |
|
model.fireTableDataChanged(); |
173 |
|
setDirty(true); |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
|
private void setDirty(boolean val) { |
178 |
|
dirty = val; |
179 |
|
if (dirty) { |
180 |
|
fireChangeEvent(); |
181 |
|
} |
182 |
|
} |
183 |
|
|
184 |
|
private boolean isDirty() { |
185 |
|
return dirty; |
186 |
|
} |
187 |
|
|
188 |
|
private JComponent getWeightTable() { |
189 |
|
|
190 |
|
if (null == weightTable) { |
191 |
|
|
192 |
|
weightTable = new PrettyTable(); |
193 |
|
weightTable.setModel(getWeightTableModel()); |
194 |
|
weightTable.getSelectionModel().setSelectionMode( |
195 |
|
ListSelectionModel.SINGLE_SELECTION); |
196 |
|
weightTable |
197 |
|
.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS); |
198 |
|
weightTable.getTableHeader().setReorderingAllowed(false); |
199 |
|
weightTable.getSelectionModel().addListSelectionListener( |
200 |
|
new ListSelectionListener() { |
201 |
|
public void valueChanged(ListSelectionEvent e) { |
202 |
|
if (e.getValueIsAdjusting()) |
203 |
|
return; |
204 |
|
ListSelectionModel lsm = (ListSelectionModel) e |
205 |
|
.getSource(); |
206 |
|
if (!lsm.isSelectionEmpty()) { |
207 |
|
int selectedRow = lsm.getMinSelectionIndex(); |
208 |
|
getDeleteButton().setEnabled(true); |
209 |
|
} else { |
210 |
|
getDeleteButton().setEnabled(false); |
211 |
|
} |
212 |
|
} |
213 |
|
}); |
214 |
|
weightTable.getColumnModel().getColumn(0).setMaxWidth(60); |
215 |
|
weightTable.getColumnModel().getColumn(2).setMaxWidth(80); |
216 |
|
} |
217 |
|
return weightTable; |
218 |
|
} |
219 |
|
|
220 |
|
private TableModel getWeightTableModel() { |
221 |
|
if (null == model) { |
222 |
|
model = new WeightTableModel(curWeights); |
223 |
|
} |
224 |
|
return model; |
225 |
|
} |
226 |
|
|
227 |
|
public class WeightTableModel extends AbstractTableModel { |
228 |
|
List weights; |
229 |
|
|
230 |
|
public WeightTableModel(List weights) { |
231 |
|
this.weights = weights; |
232 |
|
} |
233 |
|
|
234 |
|
private String[] columnNames = { "Amount", "Measure", "Grams" }; |
235 |
|
|
236 |
|
public String getColumnName(int col) { |
237 |
|
return columnNames[col].toString(); |
238 |
|
} |
239 |
|
|
240 |
|
public int getRowCount() { |
241 |
|
return weights.size(); |
242 |
|
} |
243 |
|
|
244 |
|
public int getColumnCount() { |
245 |
|
return columnNames.length; |
246 |
|
} |
247 |
|
|
248 |
|
public List getWeights() { |
249 |
|
return weights; |
250 |
|
} |
251 |
|
|
252 |
|
public Measure getWeight(int i) { |
253 |
|
return (Measure) weights.get(i); |
254 |
|
} |
255 |
|
|
256 |
|
public Object getValueAt(int row, class="keyword">int col) { |
257 |
|
Measure w = getWeight(row); |
258 |
|
if (w != null) { |
259 |
|
switch (col) { |
260 |
|
case 0: |
261 |
|
return new Double(w.getAmount()); |
262 |
|
case 1: |
263 |
|
return w.getDescription(); |
264 |
|
case 2: |
265 |
|
return new Double(w.getGrams()); |
266 |
|
} |
267 |
|
} |
268 |
|
return ""; |
269 |
|
} |
270 |
|
|
271 |
|
public Class getColumnClass(int col) { |
272 |
|
Object o = getValueAt(0, col); |
273 |
|
if (o != null) { |
274 |
|
return o.getClass(); |
275 |
|
} |
276 |
|
return String.class; |
277 |
|
} |
278 |
|
|
279 |
|
public boolean isCellEditable(int row, class="keyword">int col) { |
280 |
|
if (getWeight(row) == Measure.GRAM) |
281 |
|
return false; |
282 |
|
return (true); |
283 |
|
} |
284 |
|
|
285 |
|
public void setValueAt(Object value, int row, class="keyword">int col) { |
286 |
|
Measure w = getWeight(row); |
287 |
|
if (w != null) { |
288 |
|
if (col == 0) { |
289 |
|
w.setAmount(((Double) value).doubleValue()); |
290 |
|
} else if (col == 1) { |
291 |
|
w.setDescription((String) value); |
292 |
|
} else if (col == 2) { |
293 |
|
w.setGrams(((Double) value).doubleValue()); |
294 |
|
} |
295 |
|
} |
296 |
|
setDirty(true); |
297 |
|
fireTableCellUpdated(row, col); |
298 |
|
} |
299 |
|
|
300 |
|
} |
301 |
|
} |