svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / featuretype / DefaultAggregateController.java @ 46876
History | View | Annotate | Download (10.9 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.featuretype; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.util.Iterator; |
5 |
import javax.swing.JButton; |
6 |
import javax.swing.JOptionPane; |
7 |
import javax.swing.JTable; |
8 |
import javax.swing.event.ListSelectionEvent; |
9 |
import org.apache.commons.lang3.ArrayUtils; |
10 |
import org.gvsig.tools.ToolsLocator; |
11 |
import org.gvsig.tools.i18n.I18nManager; |
12 |
import org.gvsig.tools.swing.api.ChangeListenerHelper; |
13 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
14 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
15 |
|
16 |
/**
|
17 |
*
|
18 |
* @author gvSIG Team
|
19 |
* @param <T>
|
20 |
*/
|
21 |
public class DefaultAggregateController<T> implements AggregateController<T> { |
22 |
|
23 |
private final JTable table; |
24 |
private final JButton btnModify; |
25 |
private final JButton btnDiscard; |
26 |
private final JButton btnAccept; |
27 |
private final JButton btnNew; |
28 |
private final JButton btnDelete; |
29 |
|
30 |
private final AggregateItemController<T> itemController; |
31 |
private AggregateModel<T> model;
|
32 |
private boolean canCreate; |
33 |
private boolean canModify; |
34 |
private boolean canDelete; |
35 |
private ChangeListenerHelper changeListenerHelper;
|
36 |
private int currentRow; |
37 |
private boolean editingItem; |
38 |
private T newItem;
|
39 |
|
40 |
|
41 |
public DefaultAggregateController(
|
42 |
JTable table,
|
43 |
JButton btnFormFieldModify,
|
44 |
JButton btnFormFieldDiscard,
|
45 |
JButton btnFormFieldAccept,
|
46 |
JButton btnNew,
|
47 |
JButton btnDelete,
|
48 |
AggregateItemController<T> itemController |
49 |
) { |
50 |
this.table = table;
|
51 |
this.btnModify = btnFormFieldModify;
|
52 |
this.btnDiscard = btnFormFieldDiscard;
|
53 |
this.btnAccept = btnFormFieldAccept;
|
54 |
this.btnNew = btnNew;
|
55 |
this.btnDelete = btnDelete;
|
56 |
|
57 |
this.canCreate = true; |
58 |
this.canDelete = true; |
59 |
this.canModify = true; |
60 |
|
61 |
this.itemController = itemController;
|
62 |
this.model = EMPTY_MODEL;
|
63 |
this.editingItem = false; |
64 |
this.newItem = null; |
65 |
this.currentRow = -1; |
66 |
|
67 |
initComponents(); |
68 |
} |
69 |
|
70 |
private void initComponents() { |
71 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
72 |
|
73 |
this.changeListenerHelper = swingManager.createChangeListenerHelper();
|
74 |
|
75 |
swingManager.translate(this.btnAccept);
|
76 |
swingManager.translate(this.btnDiscard);
|
77 |
swingManager.translate(this.btnModify);
|
78 |
swingManager.translate(this.btnNew);
|
79 |
swingManager.translate(this.btnDelete);
|
80 |
|
81 |
this.table.setModel(this.model); |
82 |
this.table.getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> { |
83 |
if (e.getValueIsAdjusting()) {
|
84 |
return;
|
85 |
} |
86 |
doChangeItemSelection(); |
87 |
}); |
88 |
this.btnModify.addActionListener((ActionEvent e) -> { |
89 |
modifyCurrentItem(); |
90 |
}); |
91 |
this.btnDiscard.addActionListener((ActionEvent e) -> { |
92 |
discardModificactions(); |
93 |
}); |
94 |
this.btnAccept.addActionListener((ActionEvent e) -> { |
95 |
acceptModifications(); |
96 |
}); |
97 |
this.btnNew.addActionListener((ActionEvent e) -> { |
98 |
newItem(); |
99 |
}); |
100 |
this.btnDelete.addActionListener((ActionEvent e) -> { |
101 |
deleteCurrentItem(); |
102 |
}); |
103 |
|
104 |
} |
105 |
|
106 |
@Override
|
107 |
public void setModel(AggregateModel<T> model) { |
108 |
this.model = model;
|
109 |
this.table.setModel(model);
|
110 |
this.currentRow = -1; |
111 |
this.newItem = null; |
112 |
this.setEditing(false); |
113 |
this.itemController.setAggregateModel(model);
|
114 |
} |
115 |
|
116 |
@Override
|
117 |
public AggregateModel<T> getModel() {
|
118 |
return this.model; |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public AggregateItemController<T> getItemController() {
|
123 |
return this.itemController; |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public int getCurrentRow() { |
128 |
return currentRow;
|
129 |
} |
130 |
|
131 |
private void doChangeItemSelection() { |
132 |
int row = this.table.getSelectedRow(); |
133 |
if (row < 0) { |
134 |
return;
|
135 |
} |
136 |
if (this.editingItem) { |
137 |
if (row != this.currentRow) { |
138 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
139 |
JOptionPane.showMessageDialog(
|
140 |
this.table,
|
141 |
i18n.getTranslation("_Before_changing_selected_item_accept_or_discard_the_changes"),
|
142 |
i18n.getTranslation("_Warning"),
|
143 |
JOptionPane.WARNING_MESSAGE
|
144 |
); |
145 |
this.setCurrentRow(this.currentRow); |
146 |
} |
147 |
return;
|
148 |
} |
149 |
this.setCurrentRow(row);
|
150 |
} |
151 |
|
152 |
@Override
|
153 |
public void newItem() { |
154 |
this.currentRow = -1; |
155 |
this.newItem = this.model.createItem(); |
156 |
this.itemController.put(this.newItem); |
157 |
this.table.getSelectionModel().clearSelection();
|
158 |
this.setEditing(true); |
159 |
this.changeListenerHelper.fireEvent();
|
160 |
} |
161 |
|
162 |
@Override
|
163 |
public void modifyCurrentItem() { |
164 |
int row = this.currentRow; |
165 |
if (row < 0) { |
166 |
return;
|
167 |
} |
168 |
this.setCurrentRow(row);
|
169 |
this.setEditing(true); |
170 |
this.newItem = null; |
171 |
this.changeListenerHelper.fireEvent();
|
172 |
} |
173 |
|
174 |
@Override
|
175 |
public void deleteCurrentItem() { |
176 |
int row = this.currentRow; |
177 |
int[] rows = this.table.getSelectedRows(); |
178 |
|
179 |
int n = 0; |
180 |
Iterator<T> it = model.iterator();
|
181 |
while (it.hasNext()) {
|
182 |
it.next(); |
183 |
if (ArrayUtils.contains(rows, n)) {
|
184 |
it.remove(); |
185 |
} |
186 |
n++; |
187 |
} |
188 |
this.setEditing(false); |
189 |
this.setCurrentRow(row);
|
190 |
this.table.updateUI();
|
191 |
this.changeListenerHelper.fireEvent();
|
192 |
} |
193 |
|
194 |
@Override
|
195 |
public void acceptModifications() { |
196 |
if( !this.editingItem ) { |
197 |
return;
|
198 |
} |
199 |
if (this.newItem!=null) { |
200 |
if (this.itemController.fetch(this.newItem)) { |
201 |
int row = this.model.addItem(this.newItem); |
202 |
this.setEditing(false); |
203 |
this.setCurrentRow(row);
|
204 |
this.newItem = null; |
205 |
} |
206 |
} else {
|
207 |
int row = this.currentRow; |
208 |
if (row < 0) { |
209 |
return;
|
210 |
} |
211 |
T item = this.model.get(row);
|
212 |
if (this.itemController.fetch(item)) { |
213 |
this.setEditing(false); |
214 |
this.setCurrentRow(row);
|
215 |
this.model.itemUpdated(row);
|
216 |
} |
217 |
} |
218 |
this.changeListenerHelper.fireEvent();
|
219 |
} |
220 |
|
221 |
@Override
|
222 |
public void discardModificactions() { |
223 |
if( !this.editingItem ) { |
224 |
return;
|
225 |
} |
226 |
if( this.newItem!=null ) { |
227 |
this.itemController.clean();
|
228 |
this.itemController.setEnabled(false); |
229 |
this.newItem = null; |
230 |
} |
231 |
this.setEditing(false); |
232 |
this.setCurrentRow(this.currentRow); |
233 |
this.changeListenerHelper.fireEvent();
|
234 |
} |
235 |
|
236 |
private void setEditing(boolean editing) { |
237 |
if( this.newItem==null ) { |
238 |
if( editing && this.currentRow<0 ) { |
239 |
// No se puede entrar en editing si no hay lineas en la tabla.
|
240 |
return;
|
241 |
} |
242 |
} |
243 |
this.editingItem = editing;
|
244 |
this.itemController.setEnabled(this.editingItem); |
245 |
this.updateButtons();
|
246 |
} |
247 |
|
248 |
private void updateButtons() { |
249 |
this.btnAccept.setEnabled(this.canModify() && this.editingItem); |
250 |
this.btnDiscard.setEnabled(this.editingItem); |
251 |
this.btnModify.setEnabled(this.canModify() && !this.editingItem && this.currentRow>=0); |
252 |
this.btnNew.setEnabled(this.canCreate() && !this.editingItem); |
253 |
if( this.currentRow>=0 ) { |
254 |
this.btnDelete.setEnabled(this.canDelete() && !this.editingItem); |
255 |
} else {
|
256 |
this.btnDelete.setEnabled(false); |
257 |
} |
258 |
} |
259 |
|
260 |
@Override
|
261 |
public boolean isEditing() { |
262 |
return this.editingItem; |
263 |
} |
264 |
|
265 |
@Override
|
266 |
public void setCurrentRow(int row) { |
267 |
if( this.editingItem ) { |
268 |
if( row!=this.currentRow ) { |
269 |
this.table.getSelectionModel().setSelectionInterval(
|
270 |
this.currentRow,
|
271 |
this.currentRow
|
272 |
); |
273 |
} |
274 |
return;
|
275 |
} |
276 |
int rowCount = this.table.getRowCount(); |
277 |
if( rowCount<1 ) { |
278 |
this.updateButtons();
|
279 |
return;
|
280 |
} |
281 |
if( row<0 ) { |
282 |
if( !this.table.getSelectionModel().isSelectionEmpty() ) { |
283 |
this.table.getSelectionModel().clearSelection();
|
284 |
} |
285 |
this.itemController.clean();
|
286 |
this.updateButtons();
|
287 |
return;
|
288 |
} |
289 |
if (row >= rowCount) {
|
290 |
row = rowCount- 1;
|
291 |
} |
292 |
T item = this.model.get(row);
|
293 |
this.itemController.put(item);
|
294 |
this.currentRow = row;
|
295 |
this.updateButtons();
|
296 |
this.table.scrollRectToVisible(this.table.getCellRect(row, 0, true)); |
297 |
if( this.table.getSelectedRow()!=row ) { |
298 |
this.table.getSelectionModel().setSelectionInterval(row, row);
|
299 |
} |
300 |
} |
301 |
|
302 |
@Override
|
303 |
public void setCanCreate(boolean canCreate) { |
304 |
this.canCreate = canCreate;
|
305 |
} |
306 |
|
307 |
@Override
|
308 |
public void setCanModify(boolean canModify) { |
309 |
this.canModify = canModify;
|
310 |
} |
311 |
|
312 |
@Override
|
313 |
public void setCanDelete(boolean canDelete) { |
314 |
this.canDelete = canDelete;
|
315 |
} |
316 |
|
317 |
@Override
|
318 |
public boolean canCreate() { |
319 |
return this.canCreate; |
320 |
} |
321 |
|
322 |
@Override
|
323 |
public boolean canModify() { |
324 |
return this.canModify; |
325 |
} |
326 |
|
327 |
@Override
|
328 |
public boolean canDelete() { |
329 |
return this.canDelete; |
330 |
} |
331 |
|
332 |
@Override
|
333 |
public void setEnabled(boolean enabled) { |
334 |
if( enabled ) {
|
335 |
if( this.currentRow<0 ) { |
336 |
this.itemController.setEnabled(false); |
337 |
this.btnAccept.setEnabled(false); |
338 |
this.btnDiscard.setEnabled(false); |
339 |
this.btnModify.setEnabled(false); |
340 |
this.btnDelete.setEnabled(false); |
341 |
} else {
|
342 |
this.itemController.setEnabled(true); |
343 |
this.btnAccept.setEnabled(this.canModify() && this.editingItem); |
344 |
this.btnDiscard.setEnabled(this.editingItem); |
345 |
this.btnModify.setEnabled(this.canModify() && !this.editingItem); |
346 |
this.btnDelete.setEnabled(this.canDelete() && !this.editingItem); |
347 |
} |
348 |
this.btnNew.setEnabled(this.canCreate() && !this.editingItem); |
349 |
} else {
|
350 |
this.itemController.setEnabled(false); |
351 |
this.btnAccept.setEnabled(false); |
352 |
this.btnDiscard.setEnabled(false); |
353 |
this.btnModify.setEnabled(false); |
354 |
this.btnDelete.setEnabled(false); |
355 |
this.btnNew.setEnabled(false); |
356 |
} |
357 |
} |
358 |
|
359 |
@Override
|
360 |
public boolean isEnabled() { |
361 |
return this.itemController.isEnabled(); |
362 |
} |
363 |
|
364 |
@Override
|
365 |
public void clean() { |
366 |
this.setModel(AggregateController.EMPTY_MODEL);
|
367 |
} |
368 |
} |