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 / featurequery / DefaultFeatureQueryCalculatedColumnsPanel.java @ 44817
History | View | Annotate | Download (26.2 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.featurequery; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.net.URL; |
7 |
import java.util.List; |
8 |
import javax.swing.DefaultListModel; |
9 |
import javax.swing.DefaultListSelectionModel; |
10 |
import javax.swing.ImageIcon; |
11 |
import javax.swing.JButton; |
12 |
import javax.swing.JComboBox; |
13 |
import javax.swing.JComponent; |
14 |
import javax.swing.JList; |
15 |
import javax.swing.JOptionPane; |
16 |
import javax.swing.ListModel; |
17 |
import javax.swing.ListSelectionModel; |
18 |
import javax.swing.event.ChangeEvent; |
19 |
import javax.swing.event.ChangeListener; |
20 |
import javax.swing.event.DocumentEvent; |
21 |
import javax.swing.event.DocumentListener; |
22 |
import javax.swing.event.ListSelectionEvent; |
23 |
import javax.swing.text.JTextComponent; |
24 |
import org.apache.commons.io.FilenameUtils; |
25 |
import org.apache.commons.lang.StringUtils; |
26 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
27 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
28 |
import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
29 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
30 |
import org.gvsig.fmap.dal.DALLocator; |
31 |
import org.gvsig.fmap.dal.exception.DataException; |
32 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
33 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
34 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
35 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
36 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
37 |
import org.gvsig.fmap.dal.feature.FeatureType; |
38 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
39 |
import org.gvsig.fmap.dal.swing.expressionevaluator.FeatureStoreElement; |
40 |
import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryCalculatedColumnsPanel; |
41 |
import org.gvsig.fmap.dal.swing.featuretype.FeatureTypeAttributePanel; |
42 |
import org.gvsig.fmap.dal.swing.featuretype.FeatureTypePanel; |
43 |
import org.gvsig.tools.ToolsLocator; |
44 |
import org.gvsig.tools.dataTypes.Coercion; |
45 |
import org.gvsig.tools.dataTypes.CoercionException; |
46 |
import org.gvsig.tools.dataTypes.DataType; |
47 |
import org.gvsig.tools.dataTypes.DataTypes; |
48 |
import org.gvsig.tools.i18n.I18nManager; |
49 |
import org.gvsig.tools.swing.api.ListElement; |
50 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
51 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
52 |
import org.gvsig.tools.swing.api.pickercontroller.DataTypePickerController; |
53 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
54 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
55 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
56 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
57 |
import org.slf4j.Logger; |
58 |
import org.slf4j.LoggerFactory; |
59 |
|
60 |
/**
|
61 |
*
|
62 |
* @author jjdelcerro
|
63 |
*/
|
64 |
public class DefaultFeatureQueryCalculatedColumnsPanel |
65 |
extends DefaultFeatureQueryCalculatedColumnsPanelView
|
66 |
implements FeatureQueryCalculatedColumnsPanel {
|
67 |
|
68 |
private class ColumnController implements DocumentListener, ChangeListener, ActionListener { |
69 |
|
70 |
private final JTextComponent txtName; |
71 |
private final JComboBox cboDataType; |
72 |
private final JButton btnDataType; |
73 |
private final JButton btnExpressionBookmarks; |
74 |
private final JButton btnExpressionHistory; |
75 |
private final JButton btnExpression; |
76 |
private final JTextComponent txtExpression; |
77 |
private ExpressionPickerController expPicker;
|
78 |
private DataTypePickerController pickerDataType;
|
79 |
private boolean hasChanges; |
80 |
private final JTextComponent txtSize; |
81 |
private final JTextComponent txtPrecision; |
82 |
private final JTextComponent txtScale; |
83 |
|
84 |
private final Logger LOGGER = LoggerFactory.getLogger(ColumnController.class); |
85 |
|
86 |
public ColumnController(
|
87 |
JTextComponent txtName,
|
88 |
JComboBox cboDataType,
|
89 |
JButton btnDataType,
|
90 |
JTextComponent txtExpression,
|
91 |
JButton btnExpression,
|
92 |
JButton btnExpressionHistory,
|
93 |
JButton btnExpressionBookmarks,
|
94 |
JTextComponent txtSize,
|
95 |
JTextComponent txtPrecision,
|
96 |
JTextComponent txtScale
|
97 |
) { |
98 |
this.txtName = txtName;
|
99 |
this.cboDataType = cboDataType;
|
100 |
this.btnDataType = btnDataType;
|
101 |
this.txtExpression = txtExpression;
|
102 |
this.btnExpression = btnExpression;
|
103 |
this.btnExpressionHistory = btnExpressionHistory;
|
104 |
this.btnExpressionBookmarks = btnExpressionBookmarks;
|
105 |
this.txtSize = txtSize;
|
106 |
this.txtPrecision = txtPrecision;
|
107 |
this.txtScale = txtScale;
|
108 |
this.hasChanges = false; |
109 |
this.initComponents();
|
110 |
this.setEnabled(false); |
111 |
} |
112 |
|
113 |
public void initComponents() { |
114 |
this.pickerDataType = ToolsSwingLocator.getToolsSwingManager().createDataTypePickerController(this.cboDataType, this.btnDataType, false); |
115 |
|
116 |
ExpressionEvaluatorSwingManager managerExpSwing = ExpressionEvaluatorSwingLocator.getManager(); |
117 |
this.expPicker = managerExpSwing.createExpressionPickerController(
|
118 |
txtExpression, |
119 |
btnExpression, |
120 |
btnExpressionBookmarks, |
121 |
btnExpressionHistory |
122 |
); |
123 |
|
124 |
|
125 |
this.txtName.getDocument().addDocumentListener(this); |
126 |
this.cboDataType.addActionListener(this); |
127 |
this.txtExpression.getDocument().addDocumentListener(this); |
128 |
this.txtSize.getDocument().addDocumentListener(this); |
129 |
this.txtPrecision.getDocument().addDocumentListener(this); |
130 |
this.txtScale.getDocument().addDocumentListener(this); |
131 |
//Todo: que el listener expresion lo gestion el picker
|
132 |
|
133 |
this.cboDataType.addActionListener(new ActionListener() { |
134 |
@Override
|
135 |
public void actionPerformed(ActionEvent ae) { |
136 |
doSetDefaultValuesForDataType(); |
137 |
doEnabledByDataType(); |
138 |
} |
139 |
}); |
140 |
|
141 |
} |
142 |
|
143 |
public void doEnabledByDataType() { |
144 |
JComboBox comboBox = cboDataType;
|
145 |
if (comboBox.getSelectedItem()==null) { |
146 |
return;
|
147 |
} |
148 |
DataType dataType = (DataType) comboBox.getSelectedItem(); |
149 |
txtSize.setEnabled(dataType.supportSize()); |
150 |
txtPrecision.setEnabled(dataType.supportPrecision()); |
151 |
txtScale.setEnabled(dataType.supportScale()); |
152 |
} |
153 |
|
154 |
public void doSetDefaultValuesForDataType() { |
155 |
if (this.cboDataType.getSelectedItem()==null) { |
156 |
return;
|
157 |
} |
158 |
DataType dataType = (DataType) this.cboDataType.getSelectedItem();
|
159 |
if (dataType.supportSize() && txtSize.getText().isEmpty()) {
|
160 |
txtSize.setText(String.valueOf(dataType.getDefaultSize()));
|
161 |
} else if (!dataType.supportSize()) { |
162 |
txtSize.setText("");
|
163 |
} |
164 |
|
165 |
if (dataType.supportPrecision() && txtPrecision.getText().isEmpty()) {
|
166 |
txtPrecision.setText(String.valueOf(dataType.getDefaultPrecision()));
|
167 |
} else if (!dataType.supportPrecision()) { |
168 |
txtPrecision.setText("");
|
169 |
} |
170 |
|
171 |
if (dataType.supportScale() && txtScale.getText().isEmpty()) {
|
172 |
txtScale.setText(String.valueOf(dataType.getDefaultScale()));
|
173 |
} else if (!dataType.supportScale()) { |
174 |
txtScale.setText("");
|
175 |
} |
176 |
} |
177 |
|
178 |
public boolean hasChanges() { |
179 |
return this.hasChanges; |
180 |
} |
181 |
|
182 |
public void setEnabled(boolean enabled) { |
183 |
this.txtName.setEnabled(enabled);
|
184 |
this.expPicker.setEnabled(enabled);
|
185 |
this.cboDataType.setEnabled(enabled);
|
186 |
this.txtSize.setEnabled(enabled);
|
187 |
this.txtPrecision.setEnabled(enabled);
|
188 |
this.txtScale.setEnabled(enabled);
|
189 |
} |
190 |
|
191 |
public void clean() { |
192 |
this.txtName.setText(""); |
193 |
this.expPicker.set(null); |
194 |
this.cboDataType.setSelectedIndex(0); |
195 |
this.txtSize.setText(""); |
196 |
this.txtPrecision.setText(""); |
197 |
this.txtScale.setText(""); |
198 |
|
199 |
} |
200 |
|
201 |
public void put(EditableFeatureAttributeDescriptor attr) { |
202 |
this.clean();
|
203 |
this.txtName.setText(attr.getName());
|
204 |
FeatureAttributeEmulatorExpression emu = (FeatureAttributeEmulatorExpression) attr.getFeatureAttributeEmulator(); |
205 |
if (emu != null) { |
206 |
this.expPicker.set(emu.getExpression());
|
207 |
} |
208 |
this.pickerDataType.set(attr.getDataType());
|
209 |
Coercion coerceString = ToolsLocator.getDataTypesManager().get(DataTypes.STRING).getCoercion(); |
210 |
txtSize.setEnabled(attr.getDataType().supportSize()); |
211 |
if (attr.getDataType().supportSize()) {
|
212 |
try {
|
213 |
this.txtSize.setText((String) coerceString.coerce(attr.getSize())); |
214 |
} catch (CoercionException ex) {
|
215 |
LOGGER.warn("Not able to coerce text to integer from size box.", ex);
|
216 |
} |
217 |
} |
218 |
txtPrecision.setEnabled(attr.getDataType().supportPrecision()); |
219 |
if (attr.getDataType().supportPrecision()) {
|
220 |
try {
|
221 |
this.txtPrecision.setText((String) coerceString.coerce(attr.getPrecision())); |
222 |
} catch (CoercionException ex) {
|
223 |
LOGGER.warn("Not able to coerce text to integer from precision box.", ex);
|
224 |
} |
225 |
} |
226 |
txtScale.setEnabled(attr.getDataType().supportScale()); |
227 |
if (attr.getDataType().supportScale()) {
|
228 |
try {
|
229 |
this.txtScale.setText((String) coerceString.coerce(attr.getScale())); |
230 |
} catch (CoercionException ex) {
|
231 |
LOGGER.warn("Not able to coerce text to integer from scale box.", ex);
|
232 |
} |
233 |
} |
234 |
// doEnabledByDataType();
|
235 |
this.hasChanges = false; |
236 |
} |
237 |
|
238 |
public EditableFeatureAttributeDescriptor fetch(EditableFeatureAttributeDescriptor attr) {
|
239 |
attr.setName(this.txtName.getText());
|
240 |
attr.setFeatureAttributeEmulator(this.expPicker.get());
|
241 |
attr.setDataType(this.pickerDataType.get());
|
242 |
Coercion coerceInteger = ToolsLocator.getDataTypesManager().get(DataTypes.INTEGER).getCoercion(); |
243 |
try {
|
244 |
if (attr.getDataType().supportSize()) {
|
245 |
attr.setSize((int) coerceInteger.coerce(this.txtSize.getText())); |
246 |
} |
247 |
} catch (CoercionException ex) {
|
248 |
LOGGER.warn("Not able to coerce text to integer from size box.", ex);
|
249 |
} |
250 |
try {
|
251 |
if (attr.getDataType().supportPrecision()) {
|
252 |
attr.setPrecision((int) coerceInteger.coerce(this.txtPrecision.getText())); |
253 |
} |
254 |
} catch (CoercionException ex) {
|
255 |
LOGGER.warn("Not able to coerce text to integer from precision box.", ex);
|
256 |
} |
257 |
try {
|
258 |
if (attr.getDataType().supportScale()) {
|
259 |
attr.setScale((int) coerceInteger.coerce(this.txtScale.getText())); |
260 |
} |
261 |
} catch (CoercionException ex) {
|
262 |
LOGGER.warn("Not able to coerce text to integer from scale box.", ex);
|
263 |
} |
264 |
this.hasChanges = false; |
265 |
return attr;
|
266 |
} |
267 |
|
268 |
private void doHasChanges() { |
269 |
this.hasChanges = true; |
270 |
} |
271 |
|
272 |
@Override
|
273 |
public void insertUpdate(DocumentEvent de) { |
274 |
doHasChanges(); |
275 |
} |
276 |
|
277 |
@Override
|
278 |
public void removeUpdate(DocumentEvent de) { |
279 |
doHasChanges(); |
280 |
} |
281 |
|
282 |
@Override
|
283 |
public void changedUpdate(DocumentEvent de) { |
284 |
doHasChanges(); |
285 |
} |
286 |
|
287 |
@Override
|
288 |
public void stateChanged(ChangeEvent ce) { |
289 |
doHasChanges(); |
290 |
} |
291 |
|
292 |
@Override
|
293 |
public void actionPerformed(ActionEvent ae) { |
294 |
doHasChanges(); |
295 |
} |
296 |
|
297 |
public void setStore(FeatureStore store) { |
298 |
FeatureStoreElement calculatedStoreElement = DALSwingLocator.getSwingManager() |
299 |
.createFeatureStoreElement(); |
300 |
calculatedStoreElement.setFeatureStore(store); |
301 |
this.expPicker.removeAllSymbolTables();
|
302 |
this.expPicker.addElement(calculatedStoreElement);
|
303 |
} |
304 |
} |
305 |
|
306 |
private ColumnController columnController;
|
307 |
private FeatureStore store;
|
308 |
private FeatureType featureType;
|
309 |
private FeatureQuery query;
|
310 |
private JExpressionBuilder pckExpression;
|
311 |
private static String COLUMN_DEFAULT_NAME = "Field"; |
312 |
private EditableFeatureAttributeDescriptor actualEditableAttribute;
|
313 |
private DefaultListModel lstAttributesModel; |
314 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureQueryCalculatedColumnsPanel.class); |
315 |
|
316 |
public DefaultFeatureQueryCalculatedColumnsPanel() {
|
317 |
this.columnController = null; |
318 |
this.initComponents();
|
319 |
} |
320 |
|
321 |
@Override
|
322 |
public JComponent asJComponent() { |
323 |
return this; |
324 |
} |
325 |
|
326 |
@Override
|
327 |
public ImageIcon loadImage(String imageName) { |
328 |
String name = FilenameUtils.getBaseName(imageName);
|
329 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
330 |
if (theme.exists(name)) {
|
331 |
return theme.get(name);
|
332 |
} |
333 |
URL url = this.getClass().getResource(name + ".png"); |
334 |
if (url == null) { |
335 |
return null; |
336 |
} |
337 |
return new ImageIcon(url); |
338 |
} |
339 |
|
340 |
@Override
|
341 |
public void setStore(FeatureStore store) { |
342 |
try {
|
343 |
this.featureType = store.getDefaultFeatureType();
|
344 |
this.store = store;
|
345 |
this.query = store.createFeatureQuery();
|
346 |
this.columnController.setStore(store);
|
347 |
this.pckExpression = DALSwingLocator.getManager().createQueryFilterExpresion(store);
|
348 |
} catch (DataException ex) {
|
349 |
throw new RuntimeException("Can't assign store", ex); |
350 |
} |
351 |
} |
352 |
|
353 |
private void initComponents() { |
354 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
355 |
|
356 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
357 |
ImageIcon icon = theme.get("picker-datatype"); |
358 |
this.btnColumnDataType.setIcon(icon);
|
359 |
toolsSwingManager.translate(this.lblExpression);
|
360 |
toolsSwingManager.translate(this.lblName);
|
361 |
toolsSwingManager.translate(this.lblType);
|
362 |
toolsSwingManager.translate(this.lblSize);
|
363 |
toolsSwingManager.translate(this.lblPrecision);
|
364 |
toolsSwingManager.translate(this.lblScale);
|
365 |
toolsSwingManager.translate(this.btnApplyChanges);
|
366 |
toolsSwingManager.translate(this.btnColumnMore);
|
367 |
|
368 |
this.columnController = new ColumnController( |
369 |
this.txtColumnName,
|
370 |
this.cboColumnDataType,
|
371 |
this.btnColumnDataType,
|
372 |
this.txtColumnExpression,
|
373 |
this.btnColumnExpression,
|
374 |
this.btnColumnExpressionHistory,
|
375 |
this.btnColumnExpressionBookmarks,
|
376 |
this.txtColumnSize,
|
377 |
this.txtColumnPrecision,
|
378 |
this.txtColumnScale
|
379 |
); |
380 |
// this.lstAttributes.setCellRenderer(new FeatureAttributeListCellRenderer());
|
381 |
|
382 |
this.lstAttributes.addListSelectionListener((ListSelectionEvent e) -> { |
383 |
if (e.getValueIsAdjusting()) {
|
384 |
return;
|
385 |
} |
386 |
doSelectAttribute(); |
387 |
}); |
388 |
|
389 |
Dimension sz = this.getPreferredSize(); |
390 |
if (sz.width < DEFAULT_WIDTH) {
|
391 |
sz.width = DEFAULT_WIDTH; |
392 |
} |
393 |
if (sz.height < DEFAULT_HEIGHT) {
|
394 |
sz.height = DEFAULT_HEIGHT; |
395 |
} |
396 |
this.setPreferredSize(sz);
|
397 |
|
398 |
this.lstAttributesModel = new DefaultListModel<ListElement<EditableFeatureAttributeDescriptor>>(); |
399 |
// this.lstAttributesModel = ToolsSwingLocator.getToolsSwingManager().createFilteredListModel();
|
400 |
this.lstAttributes.setModel(lstAttributesModel);
|
401 |
|
402 |
this.lstAttributes.setSelectionModel(new DefaultListSelectionModel() { |
403 |
|
404 |
@Override
|
405 |
public void setSelectionInterval(int index0, int index1) { |
406 |
if (checkColumnControllerHasChangesWithUser()) {
|
407 |
super.setSelectionInterval(index0, index1);
|
408 |
doSelectAttribute(); |
409 |
} |
410 |
} |
411 |
|
412 |
}); |
413 |
|
414 |
this.btnAdd.addActionListener((ActionEvent e) -> { |
415 |
doAdd(); |
416 |
}); |
417 |
|
418 |
this.btnRemove.addActionListener((ActionEvent e) -> { |
419 |
doRemove(); |
420 |
}); |
421 |
|
422 |
this.btnUp.addActionListener((ActionEvent e) -> { |
423 |
doUp(lstAttributes); |
424 |
}); |
425 |
|
426 |
this.btnDown.addActionListener((ActionEvent e) -> { |
427 |
doDown(lstAttributes); |
428 |
}); |
429 |
|
430 |
this.btnApplyChanges.addActionListener((ActionEvent e) -> { |
431 |
doApplyChanges(); |
432 |
}); |
433 |
|
434 |
this.btnColumnMore.addActionListener((ActionEvent e) -> { |
435 |
doColumnMore(); |
436 |
}); |
437 |
|
438 |
} |
439 |
|
440 |
private boolean checkColumnControllerHasChangesWithUser() { |
441 |
if (columnController.hasChanges()) {
|
442 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
443 |
String message = i18n.getTranslation("_Changes_has_not_been_saved_Are_you_sure_you_want_to_continue"); |
444 |
String title = i18n.getTranslation("_Changes_not_saved"); |
445 |
int r = ToolsSwingLocator.getThreadSafeDialogsManager().confirmDialog(message, title,
|
446 |
JOptionPane.YES_NO_OPTION,
|
447 |
JOptionPane.QUESTION_MESSAGE);
|
448 |
if (r == JOptionPane.NO_OPTION) { |
449 |
return false; |
450 |
} |
451 |
|
452 |
} |
453 |
return true; |
454 |
} |
455 |
|
456 |
private void doColumnMore() { |
457 |
if (this.columnController == null) { |
458 |
return;
|
459 |
} |
460 |
final I18nManager i18n = ToolsLocator.getI18nManager();
|
461 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
462 |
final FeatureTypeAttributePanel panel = DALSwingLocator.getSwingManager().createFeatureTypeAttributePanel();
|
463 |
panel.clean(); |
464 |
// panel.setNameAsNew(featureType);
|
465 |
panel.put(actualEditableAttribute); |
466 |
panel.setMode(FeatureTypePanel.MODE_EDIT_ALL); |
467 |
final Dialog dialog = winManager.createDialog( |
468 |
panel.asJComponent(), |
469 |
i18n.getTranslation("_Extra_column"),
|
470 |
null,
|
471 |
WindowManager_v2.BUTTONS_OK_CANCEL |
472 |
); |
473 |
dialog.addActionListener(new ActionListener() { |
474 |
@Override
|
475 |
public void actionPerformed(ActionEvent e) { |
476 |
if (dialog.getAction() != WindowManager_v2.BUTTONS_OK) {
|
477 |
return;
|
478 |
} |
479 |
try {
|
480 |
FeatureAttributeDescriptor prev = actualEditableAttribute.getCopy(); |
481 |
panel.fetch(actualEditableAttribute); |
482 |
columnController.put(actualEditableAttribute); |
483 |
if (!prev.equals(actualEditableAttribute)) {
|
484 |
columnController.doHasChanges(); |
485 |
} |
486 |
} catch (Exception ex) { |
487 |
LOGGER.warn("Problems updating feature descriptor.", ex);
|
488 |
} |
489 |
} |
490 |
}); |
491 |
dialog.show(WindowManager.MODE.DIALOG); |
492 |
} |
493 |
|
494 |
public boolean checkPanel(StringBuilder msg) { |
495 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
496 |
if (this.hasChanges()) { |
497 |
int r = ToolsSwingLocator.getThreadSafeDialogsManager().confirmDialog(
|
498 |
i18n.getTranslation("_Changes_has_not_been_saved_Are_you_sure_you_want_to_continue"),
|
499 |
i18n.getTranslation("_Changes_not_saved"),
|
500 |
JOptionPane.YES_NO_OPTION,
|
501 |
JOptionPane.QUESTION_MESSAGE);
|
502 |
if (r == JOptionPane.NO_OPTION) { |
503 |
return false; |
504 |
} |
505 |
} |
506 |
return true; |
507 |
} |
508 |
|
509 |
public boolean hasChanges() { |
510 |
return this.columnController.hasChanges(); |
511 |
} |
512 |
|
513 |
private void doSelectAttribute() { |
514 |
EditableFeatureAttributeDescriptor value = null;
|
515 |
if (this.lstAttributes.getSelectedValue() != null) { |
516 |
ListElement<EditableFeatureAttributeDescriptor> selectedValue = (ListElement<EditableFeatureAttributeDescriptor>) this.lstAttributes.getSelectedValue();
|
517 |
if (selectedValue != null) { |
518 |
value = selectedValue.getValue(); |
519 |
} |
520 |
} |
521 |
if (value == null) { |
522 |
this.columnController.clean();
|
523 |
this.columnController.setEnabled(false); |
524 |
this.actualEditableAttribute = null; |
525 |
|
526 |
} else {
|
527 |
//EditableFeatureAttributeDescriptor value = node.getValue();
|
528 |
this.actualEditableAttribute = value;
|
529 |
columnController.setEnabled(true);
|
530 |
columnController.put(value); |
531 |
} |
532 |
} |
533 |
|
534 |
@Override
|
535 |
public FeatureQuery fetch(FeatureQuery query) {
|
536 |
if (query == null) { |
537 |
return this.query.getCopy(); |
538 |
} |
539 |
|
540 |
this.query.getExtraColumn().clear();
|
541 |
for (int i = 0; i < lstAttributesModel.getSize(); i++) { |
542 |
ListElement<EditableFeatureAttributeDescriptor> element = (ListElement<EditableFeatureAttributeDescriptor>) lstAttributesModel.get(i); |
543 |
EditableFeatureAttributeDescriptor newExtraColumn = this.query.getExtraColumn().add(element.getValue().getName());
|
544 |
newExtraColumn.copyFrom(element.getValue()); |
545 |
} |
546 |
query.copyFrom(this.query);
|
547 |
|
548 |
return query;
|
549 |
} |
550 |
|
551 |
@Override
|
552 |
public FeatureQuery fetch() {
|
553 |
return this.fetch(null); |
554 |
} |
555 |
|
556 |
@Override
|
557 |
public void put(FeatureQuery query) { |
558 |
this.query.copyFrom(query);
|
559 |
addExtraColumns(this.query);
|
560 |
} |
561 |
|
562 |
private void addExtraColumns(FeatureQuery query) { |
563 |
List<EditableFeatureAttributeDescriptor> cols = query.getExtraColumn().getColumns();
|
564 |
if (cols == null || cols.isEmpty()) { |
565 |
return;
|
566 |
} |
567 |
for (EditableFeatureAttributeDescriptor col : cols) {
|
568 |
ListElement lf = new ListElement(col.getLabel(), col);
|
569 |
this.lstAttributesModel.addElement(lf);
|
570 |
} |
571 |
if (this.lstAttributesModel.getSize() > 0) { |
572 |
this.lstAttributes.setSelectedIndex(0); |
573 |
} |
574 |
} |
575 |
|
576 |
public static void selfRegister() { |
577 |
String[][] iconNames = new String[][]{ |
578 |
new String[]{"dalswing", "common-applychanges"}, |
579 |
new String[]{"dalswing", "common-more"}, |
580 |
new String[]{"dalswing", "featurequery-column-add"}, |
581 |
new String[]{"dalswing", "featurequery-column-remove"}, |
582 |
new String[]{"dalswing", "featurequery-column-down"}, |
583 |
new String[]{"dalswing", "featurequery-column-up"} |
584 |
}; |
585 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
586 |
for (String[] icon : iconNames) { |
587 |
URL url = DefaultFeatureQueryOrderPanel.class.getResource(icon[1] + ".png"); |
588 |
theme.registerDefault("DALSwing", icon[0], icon[1], null, url); |
589 |
} |
590 |
} |
591 |
|
592 |
private void doAdd() { |
593 |
EditableFeatureAttributeDescriptor newAttr = DALLocator.getDataManager().createFeatureAttributeDescriptor(); |
594 |
newAttr.setName(COLUMN_DEFAULT_NAME); |
595 |
newAttr.setDataType(DataTypes.STRING); |
596 |
if (newAttr.getDataType().supportSize()) {
|
597 |
newAttr.setSize(newAttr.getDataType().getDefaultSize()); |
598 |
} |
599 |
int id = 0; |
600 |
while (checkIfValueExistInModel(newAttr.getName(), this.lstAttributesModel)) { |
601 |
String newName = COLUMN_DEFAULT_NAME + "_" + id; |
602 |
id += 1;
|
603 |
newAttr.setName(newName); |
604 |
} |
605 |
this.columnController.put(newAttr);
|
606 |
this.columnController.setEnabled(true); |
607 |
|
608 |
this.lstAttributesModel.addElement(new ListElement(newAttr.getName(), newAttr)); |
609 |
ListElement.setSelected(this.lstAttributes, newAttr);
|
610 |
this.actualEditableAttribute = newAttr;
|
611 |
|
612 |
} |
613 |
|
614 |
private boolean checkIfValueExistInModel(String name, ListModel<ListElement<EditableFeatureAttributeDescriptor>> model) { |
615 |
for (int i = 0; i < model.getSize(); i++) { |
616 |
EditableFeatureAttributeDescriptor element = model.getElementAt(i).getValue(); |
617 |
if (StringUtils.equals(element.getLabel(), name)) {
|
618 |
return true; |
619 |
} |
620 |
|
621 |
} |
622 |
return false; |
623 |
} |
624 |
|
625 |
private void doRemove() { |
626 |
ListSelectionModel lsm = lstAttributes.getSelectionModel();
|
627 |
DefaultListModel model = (DefaultListModel) this.lstAttributes.getModel(); |
628 |
int actualIndex = lsm.getMinSelectionIndex();
|
629 |
Object selectedValue = this.lstAttributes.getSelectedValue(); |
630 |
this.lstAttributesModel.removeElement(selectedValue);
|
631 |
this.columnController.clean();
|
632 |
|
633 |
int size = model.size();
|
634 |
if (size == 0) { |
635 |
//List is empty: disable delete, up, and down buttons.
|
636 |
} else {
|
637 |
//Adjust the selection.
|
638 |
if (model.getSize() == 0) { |
639 |
|
640 |
} else if (actualIndex >= model.getSize()) { |
641 |
lstAttributes.setSelectedIndex(model.getSize() - 1);
|
642 |
} else {
|
643 |
lstAttributes.setSelectedIndex(actualIndex); |
644 |
} |
645 |
} |
646 |
} |
647 |
|
648 |
private void doApplyChanges() { |
649 |
this.columnController.fetch(this.actualEditableAttribute); |
650 |
this.lstAttributes.invalidate();
|
651 |
} |
652 |
|
653 |
private void doUp(JList lstColumns) { |
654 |
int moveMe = lstColumns.getSelectedIndex();
|
655 |
if (moveMe != 0) { |
656 |
//not already at top
|
657 |
swap(lstColumns, moveMe, moveMe - 1);
|
658 |
lstColumns.setSelectedIndex(moveMe - 1);
|
659 |
lstColumns.ensureIndexIsVisible(moveMe - 1);
|
660 |
} |
661 |
} |
662 |
|
663 |
private void doDown(JList lstColumns) { |
664 |
int moveMe = lstColumns.getSelectedIndex();
|
665 |
if (moveMe != lstColumns.getModel().getSize() - 1) { |
666 |
//not already at bottom
|
667 |
swap(lstColumns, moveMe, moveMe + 1);
|
668 |
lstColumns.setSelectedIndex(moveMe + 1);
|
669 |
lstColumns.ensureIndexIsVisible(moveMe + 1);
|
670 |
} |
671 |
} |
672 |
|
673 |
private void swap(JList lstColumns, int a, int b) { |
674 |
DefaultListModel model = (DefaultListModel) lstColumns.getModel(); |
675 |
Object aObject = model.getElementAt(a);
|
676 |
Object bObject = model.getElementAt(b);
|
677 |
model.set(a, bObject); |
678 |
model.set(b, aObject); |
679 |
} |
680 |
} |