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 @ 45739
History | View | Annotate | Download (8.32 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 boolean 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.itemController = itemController;
|
58 |
this.model = EMPTY_MODEL;
|
59 |
this.editingItem = false; |
60 |
this.newItem = false; |
61 |
this.currentRow = -1; |
62 |
|
63 |
initComponents(); |
64 |
} |
65 |
|
66 |
private void initComponents() { |
67 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
68 |
|
69 |
this.changeListenerHelper = swingManager.createChangeListenerHelper();
|
70 |
|
71 |
swingManager.translate(this.btnAccept);
|
72 |
swingManager.translate(this.btnDiscard);
|
73 |
swingManager.translate(this.btnModify);
|
74 |
swingManager.translate(this.btnNew);
|
75 |
swingManager.translate(this.btnDelete);
|
76 |
|
77 |
this.table.setModel(this.model); |
78 |
this.table.getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> { |
79 |
if (e.getValueIsAdjusting()) {
|
80 |
return;
|
81 |
} |
82 |
doChangeItemSelection(); |
83 |
}); |
84 |
this.btnModify.addActionListener((ActionEvent e) -> { |
85 |
modifyCurrentItem(); |
86 |
}); |
87 |
this.btnDiscard.addActionListener((ActionEvent e) -> { |
88 |
discardModificactions(); |
89 |
}); |
90 |
this.btnAccept.addActionListener((ActionEvent e) -> { |
91 |
acceptModifications(); |
92 |
}); |
93 |
this.btnNew.addActionListener((ActionEvent e) -> { |
94 |
newItem(); |
95 |
}); |
96 |
this.btnDelete.addActionListener((ActionEvent e) -> { |
97 |
deleteCurrentItem(); |
98 |
}); |
99 |
|
100 |
} |
101 |
|
102 |
@Override
|
103 |
public void setModel(AggregateModel<T> model) { |
104 |
this.model = model;
|
105 |
this.table.setModel(model);
|
106 |
this.currentRow = -1; |
107 |
this.newItem = false; |
108 |
this.setEditing(false); |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public AggregateModel<T> getModel() {
|
113 |
return this.model; |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public AggregateItemController<T> getItemController() {
|
118 |
return this.itemController; |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public int getCurrentRow() { |
123 |
return currentRow;
|
124 |
} |
125 |
|
126 |
private void doChangeItemSelection() { |
127 |
int row = this.table.getSelectedRow(); |
128 |
if (row < 0) { |
129 |
return;
|
130 |
} |
131 |
if (this.editingItem) { |
132 |
if (row != this.currentRow) { |
133 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
134 |
JOptionPane.showMessageDialog(
|
135 |
this.table,
|
136 |
i18n.getTranslation("_Before_changing_selected_item_accept_or_discard_the_changes"),
|
137 |
i18n.getTranslation("_Warning"),
|
138 |
JOptionPane.WARNING_MESSAGE
|
139 |
); |
140 |
this.setCurrentRow(this.currentRow); |
141 |
} |
142 |
return;
|
143 |
} |
144 |
this.setCurrentRow(row);
|
145 |
} |
146 |
|
147 |
@Override
|
148 |
public void newItem() { |
149 |
this.setCurrentRow(-1); |
150 |
this.setEditing(true); |
151 |
this.newItem = true; |
152 |
this.changeListenerHelper.fireEvent();
|
153 |
} |
154 |
|
155 |
@Override
|
156 |
public void modifyCurrentItem() { |
157 |
int row = this.currentRow; |
158 |
if (row < 0) { |
159 |
return;
|
160 |
} |
161 |
this.setCurrentRow(row);
|
162 |
this.setEditing(true); |
163 |
this.newItem = false; |
164 |
this.changeListenerHelper.fireEvent();
|
165 |
} |
166 |
|
167 |
@Override
|
168 |
public void deleteCurrentItem() { |
169 |
int row = this.currentRow; |
170 |
int[] rows = this.table.getSelectedRows(); |
171 |
|
172 |
int n = 0; |
173 |
Iterator<T> it = model.iterator();
|
174 |
while (it.hasNext()) {
|
175 |
if (ArrayUtils.contains(rows, n++)) {
|
176 |
it.remove(); |
177 |
} |
178 |
} |
179 |
this.setEditing(false); |
180 |
this.setCurrentRow(row);
|
181 |
this.table.updateUI();
|
182 |
this.changeListenerHelper.fireEvent();
|
183 |
} |
184 |
|
185 |
@Override
|
186 |
public void acceptModifications() { |
187 |
if( !this.editingItem ) { |
188 |
return;
|
189 |
} |
190 |
if (this.newItem) { |
191 |
T item = this.model.createItem();
|
192 |
if (this.itemController.fetch(item)) { |
193 |
this.model.addItem(item);
|
194 |
this.setEditing(false); |
195 |
this.setCurrentRow(this.model.getRowCount()); |
196 |
this.newItem = false; |
197 |
} |
198 |
} else {
|
199 |
int row = this.currentRow; |
200 |
if (row < 0) { |
201 |
return;
|
202 |
} |
203 |
T item = this.model.get(row);
|
204 |
if (this.itemController.fetch(item)) { |
205 |
this.setEditing(false); |
206 |
this.setCurrentRow(row);
|
207 |
} |
208 |
} |
209 |
this.changeListenerHelper.fireEvent();
|
210 |
} |
211 |
|
212 |
@Override
|
213 |
public void discardModificactions() { |
214 |
if( !this.editingItem ) { |
215 |
return;
|
216 |
} |
217 |
int row = this.currentRow; |
218 |
if (row < 0) { |
219 |
return;
|
220 |
} |
221 |
this.setEditing(false); |
222 |
this.setCurrentRow(row);
|
223 |
this.newItem = false; |
224 |
this.changeListenerHelper.fireEvent();
|
225 |
} |
226 |
|
227 |
private void setEditing(boolean editing) { |
228 |
this.editingItem = editing;
|
229 |
this.itemController.setEnabled(editing);
|
230 |
this.btnAccept.setEnabled(this.canModify() && editing); |
231 |
this.btnDiscard.setEnabled(editing);
|
232 |
this.btnModify.setEnabled(this.canModify() && !editing); |
233 |
this.btnDelete.setEnabled(this.canDelete() && !editing); |
234 |
if( this.currentRow>=0 ) { |
235 |
this.btnNew.setEnabled(this.canCreate() && !editing); |
236 |
} else {
|
237 |
this.btnNew.setEnabled(false); |
238 |
} |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public boolean isEditing() { |
243 |
return this.editingItem; |
244 |
} |
245 |
|
246 |
@Override
|
247 |
public void setCurrentRow(int row) { |
248 |
if( this.editingItem ) { |
249 |
return;
|
250 |
} |
251 |
if( row<0 ) { |
252 |
this.table.getSelectionModel().removeIndexInterval(0, this.model.getRowCount()-1); |
253 |
this.itemController.clean();
|
254 |
return;
|
255 |
} |
256 |
if (row >= this.table.getRowCount()) { |
257 |
row = this.table.getRowCount() - 1; |
258 |
} |
259 |
T item = this.model.get(row);
|
260 |
this.itemController.put(item);
|
261 |
this.currentRow = row;
|
262 |
this.table.getSelectionModel().setSelectionInterval(row, row);
|
263 |
this.table.scrollRectToVisible(this.table.getCellRect(row, 0, true)); |
264 |
} |
265 |
|
266 |
@Override
|
267 |
public void setCanCreate(boolean canCreate) { |
268 |
this.canCreate = canCreate;
|
269 |
} |
270 |
|
271 |
@Override
|
272 |
public void setCanModify(boolean canModify) { |
273 |
this.canModify = canModify;
|
274 |
} |
275 |
|
276 |
@Override
|
277 |
public void setCanDelete(boolean canDelete) { |
278 |
this.canDelete = canDelete;
|
279 |
} |
280 |
|
281 |
@Override
|
282 |
public boolean canCreate() { |
283 |
return this.canCreate; |
284 |
} |
285 |
|
286 |
@Override
|
287 |
public boolean canModify() { |
288 |
return this.canModify; |
289 |
} |
290 |
|
291 |
@Override
|
292 |
public boolean canDelete() { |
293 |
return this.canDelete; |
294 |
} |
295 |
|
296 |
} |