Statistics
| Revision:

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 @ 47426

History | View | Annotate | Download (33 KB)

1 44712 jjdelcerro
package org.gvsig.fmap.dal.swing.impl.featurequery;
2 44644 jjdelcerro
3
import java.awt.Dimension;
4 44753 omartinez
import java.awt.event.ActionEvent;
5 44791 omartinez
import java.awt.event.ActionListener;
6 44753 omartinez
import java.util.List;
7
import javax.swing.DefaultListModel;
8 44791 omartinez
import javax.swing.DefaultListSelectionModel;
9 44644 jjdelcerro
import javax.swing.ImageIcon;
10
import javax.swing.JButton;
11 44712 jjdelcerro
import javax.swing.JComboBox;
12 44644 jjdelcerro
import javax.swing.JComponent;
13 44753 omartinez
import javax.swing.JList;
14 44791 omartinez
import javax.swing.JOptionPane;
15 44753 omartinez
import javax.swing.ListModel;
16 44791 omartinez
import javax.swing.ListSelectionModel;
17
import javax.swing.event.ChangeEvent;
18
import javax.swing.event.ChangeListener;
19
import javax.swing.event.DocumentEvent;
20
import javax.swing.event.DocumentListener;
21 44644 jjdelcerro
import javax.swing.event.ListSelectionEvent;
22
import javax.swing.text.JTextComponent;
23 44753 omartinez
import org.apache.commons.lang.StringUtils;
24 45351 omartinez
import org.gvsig.expressionevaluator.Expression;
25 44753 omartinez
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator;
26
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager;
27
import org.gvsig.expressionevaluator.swing.ExpressionPickerController;
28
import org.gvsig.fmap.dal.DALLocator;
29 44644 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
30 44753 omartinez
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression;
31 44712 jjdelcerro
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
32 44791 omartinez
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
33 44712 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQuery;
34 44644 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
35
import org.gvsig.fmap.dal.feature.FeatureType;
36 44713 jjdelcerro
import org.gvsig.fmap.dal.swing.DALSwingLocator;
37 44712 jjdelcerro
import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryCalculatedColumnsPanel;
38 44791 omartinez
import org.gvsig.fmap.dal.swing.featuretype.FeatureTypeAttributePanel;
39 46863 jjdelcerro
import static org.gvsig.fmap.dal.swing.impl.DefaultDALSwingLibrary.LIBRARY_NAME;
40 44791 omartinez
import org.gvsig.tools.ToolsLocator;
41 44810 omartinez
import org.gvsig.tools.dataTypes.Coercion;
42
import org.gvsig.tools.dataTypes.CoercionException;
43
import org.gvsig.tools.dataTypes.DataType;
44 44791 omartinez
import org.gvsig.tools.dataTypes.DataTypes;
45
import org.gvsig.tools.i18n.I18nManager;
46 44753 omartinez
import org.gvsig.tools.swing.api.ListElement;
47 44644 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingLocator;
48
import org.gvsig.tools.swing.api.ToolsSwingManager;
49 46863 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingUtils;
50 44791 omartinez
import org.gvsig.tools.swing.api.pickercontroller.DataTypePickerController;
51 46251 jjdelcerro
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
52 44791 omartinez
import org.gvsig.tools.swing.api.windowmanager.Dialog;
53
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
54
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
55 44644 jjdelcerro
import org.gvsig.tools.swing.icontheme.IconTheme;
56 44791 omartinez
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58 44644 jjdelcerro
59
/**
60
 *
61
 * @author jjdelcerro
62
 */
63 44712 jjdelcerro
public class DefaultFeatureQueryCalculatedColumnsPanel
64
        extends DefaultFeatureQueryCalculatedColumnsPanelView
65
        implements FeatureQueryCalculatedColumnsPanel {
66 45166 omartinez
67 46251 jjdelcerro
    private class AttributeListElement extends ListElement<FeatureAttributeDescriptor> {
68 45166 omartinez
69
        public AttributeListElement(FeatureAttributeDescriptor value) {
70
            super(value.getLabel(), value);
71
        }
72
73
        @Override
74
        public String getLabel() {
75
            return ((FeatureAttributeDescriptor) this.getValue()).getName();
76
        }
77
78
    }
79 44644 jjdelcerro
80 44791 omartinez
    private class ColumnController implements DocumentListener, ChangeListener, ActionListener {
81 44753 omartinez
82
        private final JTextComponent txtName;
83
        private final JComboBox cboDataType;
84
        private final JButton btnDataType;
85
        private final JButton btnExpressionBookmarks;
86
        private final JButton btnExpressionHistory;
87
        private final JButton btnExpression;
88
        private final JTextComponent txtExpression;
89
        private ExpressionPickerController expPicker;
90 44791 omartinez
        private DataTypePickerController pickerDataType;
91
        private boolean hasChanges;
92 44810 omartinez
        private final JTextComponent txtSize;
93
        private final JTextComponent txtPrecision;
94
        private final JTextComponent txtScale;
95
96
        private final Logger LOGGER = LoggerFactory.getLogger(ColumnController.class);
97 45166 omartinez
        private final JButton btnColumnMore;
98
        private final JButton btnApplyChanges;
99 44753 omartinez
100
        public ColumnController(
101
                JTextComponent txtName,
102
                JComboBox cboDataType,
103
                JButton btnDataType,
104
                JTextComponent txtExpression,
105
                JButton btnExpression,
106
                JButton btnExpressionHistory,
107 44810 omartinez
                JButton btnExpressionBookmarks,
108
                JTextComponent txtSize,
109
                JTextComponent txtPrecision,
110 45166 omartinez
                JTextComponent txtScale,
111
                JButton btnApplyChanges,
112
                JButton btnColumnMore
113 44753 omartinez
        ) {
114
            this.txtName = txtName;
115
            this.cboDataType = cboDataType;
116
            this.btnDataType = btnDataType;
117
            this.txtExpression = txtExpression;
118
            this.btnExpression = btnExpression;
119
            this.btnExpressionHistory = btnExpressionHistory;
120
            this.btnExpressionBookmarks = btnExpressionBookmarks;
121 44810 omartinez
            this.txtSize = txtSize;
122
            this.txtPrecision = txtPrecision;
123
            this.txtScale = txtScale;
124 45166 omartinez
            this.btnApplyChanges = btnApplyChanges;
125
            this.btnColumnMore = btnColumnMore;
126 44791 omartinez
            this.hasChanges = false;
127 44753 omartinez
            this.initComponents();
128
            this.setEnabled(false);
129
        }
130
131
        public void initComponents() {
132 44791 omartinez
            this.pickerDataType = ToolsSwingLocator.getToolsSwingManager().createDataTypePickerController(this.cboDataType, this.btnDataType, false);
133 44753 omartinez
134
            ExpressionEvaluatorSwingManager managerExpSwing = ExpressionEvaluatorSwingLocator.getManager();
135
            this.expPicker = managerExpSwing.createExpressionPickerController(
136
                    txtExpression,
137
                    btnExpression,
138
                    btnExpressionBookmarks,
139
                    btnExpressionHistory
140
            );
141 44801 omartinez
142 44753 omartinez
143 44791 omartinez
            this.txtName.getDocument().addDocumentListener(this);
144
            this.cboDataType.addActionListener(this);
145 44801 omartinez
            this.txtExpression.getDocument().addDocumentListener(this);
146 44810 omartinez
            this.txtSize.getDocument().addDocumentListener(this);
147
            this.txtPrecision.getDocument().addDocumentListener(this);
148
            this.txtScale.getDocument().addDocumentListener(this);
149 44801 omartinez
            //Todo: que el listener expresion lo gestion el picker
150 44810 omartinez
151
            this.cboDataType.addActionListener(new ActionListener() {
152
                @Override
153
                public void actionPerformed(ActionEvent ae) {
154
                    doSetDefaultValuesForDataType();
155
                    doEnabledByDataType();
156
                }
157
            });
158 44791 omartinez
159 44753 omartinez
        }
160 44810 omartinez
161
        public void doEnabledByDataType() {
162
            JComboBox comboBox = cboDataType;
163 44817 omartinez
            if (comboBox.getSelectedItem()==null) {
164
                return;
165
            }
166 44810 omartinez
            DataType dataType = (DataType) comboBox.getSelectedItem();
167
            txtSize.setEnabled(dataType.supportSize());
168
            txtPrecision.setEnabled(dataType.supportPrecision());
169
            txtScale.setEnabled(dataType.supportScale());
170
        }
171
172
        public void doSetDefaultValuesForDataType() {
173 44817 omartinez
            if (this.cboDataType.getSelectedItem()==null) {
174
                return;
175
            }
176 44810 omartinez
            DataType dataType = (DataType) this.cboDataType.getSelectedItem();
177
            if (dataType.supportSize() && txtSize.getText().isEmpty()) {
178
                txtSize.setText(String.valueOf(dataType.getDefaultSize()));
179
            } else if (!dataType.supportSize()) {
180
                txtSize.setText("");
181
            }
182 44753 omartinez
183 44810 omartinez
            if (dataType.supportPrecision() && txtPrecision.getText().isEmpty()) {
184
                txtPrecision.setText(String.valueOf(dataType.getDefaultPrecision()));
185
            } else if (!dataType.supportPrecision()) {
186
                txtPrecision.setText("");
187
            }
188
189
            if (dataType.supportScale() && txtScale.getText().isEmpty()) {
190
                txtScale.setText(String.valueOf(dataType.getDefaultScale()));
191
            } else if (!dataType.supportScale()) {
192
                txtScale.setText("");
193
            }
194
        }
195
196 44791 omartinez
        public boolean hasChanges() {
197
            return this.hasChanges;
198
        }
199
200 44753 omartinez
        public void setEnabled(boolean enabled) {
201
            this.txtName.setEnabled(enabled);
202
            this.expPicker.setEnabled(enabled);
203
            this.cboDataType.setEnabled(enabled);
204 44810 omartinez
            this.txtSize.setEnabled(enabled);
205
            this.txtPrecision.setEnabled(enabled);
206
            this.txtScale.setEnabled(enabled);
207 45166 omartinez
            this.btnApplyChanges.setEnabled(enabled);
208
            this.btnColumnMore.setEnabled(enabled);
209 44753 omartinez
        }
210
211
        public void clean() {
212
            this.txtName.setText("");
213
            this.expPicker.set(null);
214
            this.cboDataType.setSelectedIndex(0);
215 44810 omartinez
            this.txtSize.setText("");
216
            this.txtPrecision.setText("");
217
            this.txtScale.setText("");
218 45166 omartinez
            this.btnApplyChanges.setEnabled(false);
219
            this.btnColumnMore.setEnabled(false);
220
            this.hasChanges = false;
221 44753 omartinez
222
        }
223
224
        public void put(EditableFeatureAttributeDescriptor attr) {
225 44791 omartinez
            this.clean();
226
            this.txtName.setText(attr.getName());
227 44753 omartinez
            FeatureAttributeEmulatorExpression emu = (FeatureAttributeEmulatorExpression) attr.getFeatureAttributeEmulator();
228 44791 omartinez
            if (emu != null) {
229 45296 omartinez
                try {
230
                    this.expPicker.set(emu.getExpression().clone());
231
                } catch (CloneNotSupportedException ex) {
232
                    LOGGER.warn("Not able to clone expression.", ex);
233
                    throw new RuntimeException(ex);
234
                }
235 44753 omartinez
            }
236 44817 omartinez
            this.pickerDataType.set(attr.getDataType());
237 44810 omartinez
            Coercion coerceString = ToolsLocator.getDataTypesManager().get(DataTypes.STRING).getCoercion();
238
            txtSize.setEnabled(attr.getDataType().supportSize());
239
            if (attr.getDataType().supportSize()) {
240
                try {
241
                    this.txtSize.setText((String) coerceString.coerce(attr.getSize()));
242
                } catch (CoercionException ex) {
243
                    LOGGER.warn("Not able to coerce text to integer from size box.", ex);
244
                }
245
            }
246
            txtPrecision.setEnabled(attr.getDataType().supportPrecision());
247
            if (attr.getDataType().supportPrecision()) {
248
                try {
249
                    this.txtPrecision.setText((String) coerceString.coerce(attr.getPrecision()));
250
                } catch (CoercionException ex) {
251
                    LOGGER.warn("Not able to coerce text to integer from precision box.", ex);
252
                }
253
            }
254
            txtScale.setEnabled(attr.getDataType().supportScale());
255
            if (attr.getDataType().supportScale()) {
256
                try {
257
                    this.txtScale.setText((String) coerceString.coerce(attr.getScale()));
258
                } catch (CoercionException ex) {
259
                    LOGGER.warn("Not able to coerce text to integer from scale box.", ex);
260
                }
261
            }
262
//            doEnabledByDataType();
263 45166 omartinez
            this.btnColumnMore.setEnabled(true);
264
            this.btnApplyChanges.setEnabled(false);
265 44791 omartinez
            this.hasChanges = false;
266 44753 omartinez
        }
267
268
        public EditableFeatureAttributeDescriptor fetch(EditableFeatureAttributeDescriptor attr) {
269 44791 omartinez
            attr.setName(this.txtName.getText());
270 45296 omartinez
            try {
271 45351 omartinez
                Expression exp = this.expPicker.get();
272
                if (exp!=null) {
273
                    attr.setFeatureAttributeEmulator(exp.clone());
274
                } else {
275
                    attr.setFeatureAttributeEmulator((Expression) null);
276
                }
277 45296 omartinez
            } catch (CloneNotSupportedException ex) {
278
                LOGGER.warn("Not able to clone expression.", ex);
279
                throw new RuntimeException(ex);
280
            }
281 44791 omartinez
            attr.setDataType(this.pickerDataType.get());
282 44810 omartinez
            Coercion coerceInteger = ToolsLocator.getDataTypesManager().get(DataTypes.INTEGER).getCoercion();
283
            try {
284
                if (attr.getDataType().supportSize()) {
285
                    attr.setSize((int) coerceInteger.coerce(this.txtSize.getText()));
286
                }
287
            } catch (CoercionException ex) {
288
                LOGGER.warn("Not able to coerce text to integer from size box.", ex);
289
            }
290
            try {
291
                if (attr.getDataType().supportPrecision()) {
292
                    attr.setPrecision((int) coerceInteger.coerce(this.txtPrecision.getText()));
293
                }
294
            } catch (CoercionException ex) {
295
                LOGGER.warn("Not able to coerce text to integer from precision box.", ex);
296
            }
297
            try {
298
                if (attr.getDataType().supportScale()) {
299
                    attr.setScale((int) coerceInteger.coerce(this.txtScale.getText()));
300
                }
301
            } catch (CoercionException ex) {
302
                LOGGER.warn("Not able to coerce text to integer from scale box.", ex);
303
            }
304 44791 omartinez
            this.hasChanges = false;
305 45166 omartinez
            this.btnColumnMore.setEnabled(true);
306
            this.btnApplyChanges.setEnabled(false);
307 44791 omartinez
            return attr;
308 44753 omartinez
        }
309 44791 omartinez
310
        private void doHasChanges() {
311 46978 fdiaz
            this.btnColumnMore.setEnabled(false);
312 45166 omartinez
            this.btnApplyChanges.setEnabled(true);
313 44791 omartinez
            this.hasChanges = true;
314
        }
315
316
        @Override
317
        public void insertUpdate(DocumentEvent de) {
318
            doHasChanges();
319
        }
320
321
        @Override
322
        public void removeUpdate(DocumentEvent de) {
323
            doHasChanges();
324
        }
325
326
        @Override
327
        public void changedUpdate(DocumentEvent de) {
328
            doHasChanges();
329
        }
330
331
        @Override
332
        public void stateChanged(ChangeEvent ce) {
333
            doHasChanges();
334
        }
335
336
        @Override
337
        public void actionPerformed(ActionEvent ae) {
338
            doHasChanges();
339
        }
340 44801 omartinez
341
        public void setStore(FeatureStore store) {
342 46586 fdiaz
          DALSwingLocator.getManager().configureExpressionBuilder(expPicker.getConfig(), store);
343 44855 jjdelcerro
//             FeatureStoreElement calculatedStoreElement = DALSwingLocator.getSwingManager()
344
//                    .createFeatureStoreElement();
345
//            calculatedStoreElement.setFeatureStore(store);
346
//            this.expPicker.removeAllSymbolTables();
347
//            this.expPicker.addElement(calculatedStoreElement);
348 44801 omartinez
        }
349 44644 jjdelcerro
    }
350 44753 omartinez
351 44791 omartinez
    private ColumnController columnController;
352 44753 omartinez
    private FeatureStore store;
353
    private FeatureType featureType;
354
    private FeatureQuery query;
355 44855 jjdelcerro
//    private JExpressionBuilder pckExpression;
356
    private static final String COLUMN_DEFAULT_NAME = "Field";
357 44791 omartinez
    private EditableFeatureAttributeDescriptor actualEditableAttribute;
358
    private DefaultListModel lstAttributesModel;
359
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureQueryCalculatedColumnsPanel.class);
360 44753 omartinez
361
    public DefaultFeatureQueryCalculatedColumnsPanel() {
362 44791 omartinez
        this.columnController = null;
363 44753 omartinez
        this.initComponents();
364 44644 jjdelcerro
    }
365 44753 omartinez
366
    @Override
367
    public JComponent asJComponent() {
368
        return this;
369 44644 jjdelcerro
    }
370 44753 omartinez
371
    @Override
372
    public ImageIcon loadImage(String imageName) {
373 46863 jjdelcerro
        return ToolsSwingUtils.loadImage(this, imageName);
374 44644 jjdelcerro
    }
375 44753 omartinez
376
    @Override
377
    public void setStore(FeatureStore store) {
378
        try {
379
            this.featureType = store.getDefaultFeatureType();
380
            this.store = store;
381
            this.query = store.createFeatureQuery();
382 44801 omartinez
            this.columnController.setStore(store);
383 44855 jjdelcerro
//            this.pckExpression = DALSwingLocator.getManager().createQueryFilterExpresion(store);
384 44753 omartinez
        } catch (DataException ex) {
385
            throw new RuntimeException("Can't assign store", ex);
386
        }
387 44644 jjdelcerro
    }
388
389 44753 omartinez
    private void initComponents() {
390
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
391 44644 jjdelcerro
392 44791 omartinez
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
393
        ImageIcon icon = theme.get("picker-datatype");
394
        this.btnColumnDataType.setIcon(icon);
395
        toolsSwingManager.translate(this.lblExpression);
396
        toolsSwingManager.translate(this.lblName);
397
        toolsSwingManager.translate(this.lblType);
398 44810 omartinez
        toolsSwingManager.translate(this.lblSize);
399
        toolsSwingManager.translate(this.lblPrecision);
400
        toolsSwingManager.translate(this.lblScale);
401
        toolsSwingManager.translate(this.btnApplyChanges);
402
        toolsSwingManager.translate(this.btnColumnMore);
403 44791 omartinez
404
        this.columnController = new ColumnController(
405 44753 omartinez
                this.txtColumnName,
406
                this.cboColumnDataType,
407
                this.btnColumnDataType,
408
                this.txtColumnExpression,
409
                this.btnColumnExpression,
410
                this.btnColumnExpressionHistory,
411 44810 omartinez
                this.btnColumnExpressionBookmarks,
412
                this.txtColumnSize,
413
                this.txtColumnPrecision,
414 45166 omartinez
                this.txtColumnScale,
415
                this.btnApplyChanges,
416
                this.btnColumnMore
417 44753 omartinez
        );
418
//        this.lstAttributes.setCellRenderer(new FeatureAttributeListCellRenderer());
419 44791 omartinez
420 44753 omartinez
        this.lstAttributes.addListSelectionListener((ListSelectionEvent e) -> {
421
            if (e.getValueIsAdjusting()) {
422
                return;
423
            }
424 44791 omartinez
            doSelectAttribute();
425 44753 omartinez
        });
426 44644 jjdelcerro
427 44753 omartinez
        Dimension sz = this.getPreferredSize();
428
        if (sz.width < DEFAULT_WIDTH) {
429
            sz.width = DEFAULT_WIDTH;
430
        }
431
        if (sz.height < DEFAULT_HEIGHT) {
432
            sz.height = DEFAULT_HEIGHT;
433
        }
434
        this.setPreferredSize(sz);
435
436
        this.lstAttributesModel = new DefaultListModel<ListElement<EditableFeatureAttributeDescriptor>>();
437
//        this.lstAttributesModel = ToolsSwingLocator.getToolsSwingManager().createFilteredListModel();
438
        this.lstAttributes.setModel(lstAttributesModel);
439
440 44791 omartinez
        this.lstAttributes.setSelectionModel(new DefaultListSelectionModel() {
441
442
            @Override
443
            public void setSelectionInterval(int index0, int index1) {
444
                if (checkColumnControllerHasChangesWithUser()) {
445
                    super.setSelectionInterval(index0, index1);
446
                    doSelectAttribute();
447
                }
448
            }
449
450
        });
451
452 44753 omartinez
        this.btnAdd.addActionListener((ActionEvent e) -> {
453 45166 omartinez
            if (checkColumnControllerHasChangesWithUser()) {
454
                doAdd();
455
            }
456 44753 omartinez
        });
457
458
        this.btnRemove.addActionListener((ActionEvent e) -> {
459
            doRemove();
460
        });
461
462
        this.btnUp.addActionListener((ActionEvent e) -> {
463
            doUp(lstAttributes);
464
        });
465
466
        this.btnDown.addActionListener((ActionEvent e) -> {
467
            doDown(lstAttributes);
468
        });
469
470
        this.btnApplyChanges.addActionListener((ActionEvent e) -> {
471
            doApplyChanges();
472
        });
473
474
        this.btnColumnMore.addActionListener((ActionEvent e) -> {
475 44791 omartinez
            doColumnMore();
476 44753 omartinez
        });
477 45166 omartinez
478 44644 jjdelcerro
    }
479 44753 omartinez
480 44791 omartinez
    private boolean checkColumnControllerHasChangesWithUser() {
481 45166 omartinez
        if (this.hasChanges()) {
482 44791 omartinez
            I18nManager i18n = ToolsLocator.getI18nManager();
483
            String message = i18n.getTranslation("_Changes_has_not_been_saved_Are_you_sure_you_want_to_continue");
484
            String title = i18n.getTranslation("_Changes_not_saved");
485
            int r = ToolsSwingLocator.getThreadSafeDialogsManager().confirmDialog(message, title,
486
                    JOptionPane.YES_NO_OPTION,
487
                    JOptionPane.QUESTION_MESSAGE);
488
            if (r == JOptionPane.NO_OPTION) {
489
                return false;
490
            }
491
492
        }
493
        return true;
494
    }
495
496
    private void doColumnMore() {
497
        if (this.columnController == null) {
498
            return;
499
        }
500
        final I18nManager i18n = ToolsLocator.getI18nManager();
501
        WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
502
        final FeatureTypeAttributePanel panel = DALSwingLocator.getSwingManager().createFeatureTypeAttributePanel();
503
        panel.clean();
504 47403 fdiaz
        panel.setFeatureType(featureType);
505 44791 omartinez
        panel.put(actualEditableAttribute);
506 46978 fdiaz
        panel.setMode(FeatureTypeAttributePanel.MODE_VIRTUAL_FIELD);
507 44791 omartinez
        final Dialog dialog = winManager.createDialog(
508
                panel.asJComponent(),
509
                i18n.getTranslation("_Extra_column"),
510
                null,
511
                WindowManager_v2.BUTTONS_OK_CANCEL
512
        );
513 47403 fdiaz
        dialog.addActionListener((ActionEvent e) -> {
514
            if (dialog.getAction() != WindowManager_v2.BUTTONS_OK) {
515
                return;
516
            }
517
            try {
518
                FeatureAttributeDescriptor prev = actualEditableAttribute.getCopy();
519
                panel.fetch(actualEditableAttribute);
520
                columnController.put(actualEditableAttribute);
521
                if (!prev.equals(actualEditableAttribute)) {
522
                    columnController.doHasChanges();
523 44791 omartinez
                }
524 47403 fdiaz
            } catch (Exception ex) {
525
                LOGGER.warn("Problems updating feature descriptor.", ex);
526 44791 omartinez
            }
527
        });
528
        dialog.show(WindowManager.MODE.DIALOG);
529
    }
530
531
    public boolean checkPanel(StringBuilder msg) {
532
        I18nManager i18n = ToolsLocator.getI18nManager();
533
        if (this.hasChanges()) {
534
            int r = ToolsSwingLocator.getThreadSafeDialogsManager().confirmDialog(
535
                    i18n.getTranslation("_Changes_has_not_been_saved_Are_you_sure_you_want_to_continue"),
536
                    i18n.getTranslation("_Changes_not_saved"),
537
                    JOptionPane.YES_NO_OPTION,
538
                    JOptionPane.QUESTION_MESSAGE);
539
            if (r == JOptionPane.NO_OPTION) {
540
                return false;
541
            }
542
        }
543
        return true;
544
    }
545
546
    public boolean hasChanges() {
547
        return this.columnController.hasChanges();
548
    }
549
550 44753 omartinez
    private void doSelectAttribute() {
551 46526 fdiaz
        int index = this.lstAttributes.getSelectedIndex();
552
553
        ListModel model = this.lstAttributesModel;
554
        this.btnUp.setEnabled(index >= 1);
555
        this.btnDown.setEnabled(index >= 0 && index < this.lstAttributesModel.getSize() - 1);
556
557 44791 omartinez
        EditableFeatureAttributeDescriptor value = null;
558
        if (this.lstAttributes.getSelectedValue() != null) {
559
            ListElement<EditableFeatureAttributeDescriptor> selectedValue = (ListElement<EditableFeatureAttributeDescriptor>) this.lstAttributes.getSelectedValue();
560
            if (selectedValue != null) {
561
                value = selectedValue.getValue();
562
            }
563
        }
564 44753 omartinez
        if (value == null) {
565 44791 omartinez
            this.columnController.clean();
566
            this.columnController.setEnabled(false);
567 44753 omartinez
            this.actualEditableAttribute = null;
568
569
        } else {
570
            //EditableFeatureAttributeDescriptor value = node.getValue();
571
            this.actualEditableAttribute = value;
572 44810 omartinez
            columnController.setEnabled(true);
573 44791 omartinez
            columnController.put(value);
574 44753 omartinez
        }
575 44644 jjdelcerro
    }
576
577 44753 omartinez
    @Override
578
    public FeatureQuery fetch(FeatureQuery query) {
579 45166 omartinez
        if (hasChanges()) {
580
            I18nManager i18n = ToolsLocator.getI18nManager();
581
            String message = i18n.getTranslation("_Changes_has_not_been_saved_Do_you_want_to_save_them_before_continue");
582
            String title = i18n.getTranslation("_Changes_not_saved");
583
            int r = ToolsSwingLocator.getThreadSafeDialogsManager().confirmDialog(message, title,
584
                    JOptionPane.YES_NO_OPTION,
585
                    JOptionPane.QUESTION_MESSAGE);
586
            if (r == JOptionPane.YES_NO_OPTION) {
587
                doApplyChanges();
588
            }
589
        }
590
591 44753 omartinez
        if (query == null) {
592
            return this.query.getCopy();
593
        }
594 44791 omartinez
595
        this.query.getExtraColumn().clear();
596
        for (int i = 0; i < lstAttributesModel.getSize(); i++) {
597
            ListElement<EditableFeatureAttributeDescriptor> element = (ListElement<EditableFeatureAttributeDescriptor>) lstAttributesModel.get(i);
598 46012 jjdelcerro
            EditableFeatureAttributeDescriptor newExtraColumn = this.query.getExtraColumn().add(element.getValue().getName(), element.getValue().getType());
599 44791 omartinez
            newExtraColumn.copyFrom(element.getValue());
600
        }
601
        query.copyFrom(this.query);
602
603 44753 omartinez
        return query;
604 44644 jjdelcerro
    }
605
606 44753 omartinez
    @Override
607
    public FeatureQuery fetch() {
608
        return this.fetch(null);
609
    }
610 44644 jjdelcerro
611 44753 omartinez
    @Override
612
    public void put(FeatureQuery query) {
613
        this.query.copyFrom(query);
614
        addExtraColumns(this.query);
615 44644 jjdelcerro
    }
616 44753 omartinez
617
    private void addExtraColumns(FeatureQuery query) {
618
        List<EditableFeatureAttributeDescriptor> cols = query.getExtraColumn().getColumns();
619 44791 omartinez
        if (cols == null || cols.isEmpty()) {
620 44753 omartinez
            return;
621
        }
622
        for (EditableFeatureAttributeDescriptor col : cols) {
623 45166 omartinez
            ListElement lf = new AttributeListElement(col);
624 44791 omartinez
            this.lstAttributesModel.addElement(lf);
625 44753 omartinez
        }
626
        if (this.lstAttributesModel.getSize() > 0) {
627
            this.lstAttributes.setSelectedIndex(0);
628
        }
629 44644 jjdelcerro
    }
630
631 44753 omartinez
    public static void selfRegister() {
632 47426 jjdelcerro
//        boolean n = ToolsSwingUtils.registerIcons(
633
//                DefaultFeatureQueryOrderPanel.class,
634
//                null,
635
//                LIBRARY_NAME,
636
//                new String[]{ "featurequery", "featurequery-column-add"},
637
//                new String[]{ "featurequery", "featurequery-column-remove"},
638
//                new String[]{ "featurequery", "featurequery-column-down"},
639
//                new String[]{ "featurequery", "featurequery-column-up"}
640
//        );
641 44753 omartinez
    }
642 44644 jjdelcerro
643 44753 omartinez
    private void doAdd() {
644 46012 jjdelcerro
        int id = 0;
645
        String newName = COLUMN_DEFAULT_NAME;
646
        while (checkIfValueExistInModel(newName, this.lstAttributesModel)) {
647
            newName = COLUMN_DEFAULT_NAME + "_" + id;
648
            id += 1;
649
        }
650
        EditableFeatureAttributeDescriptor newAttr = DALLocator.getDataManager().createFeatureAttributeDescriptor(
651
                newName,
652
                DataTypes.STRING
653
        );
654 44810 omartinez
        if (newAttr.getDataType().supportSize()) {
655
            newAttr.setSize(newAttr.getDataType().getDefaultSize());
656
        }
657 44791 omartinez
        this.columnController.put(newAttr);
658
        this.columnController.setEnabled(true);
659
660 45166 omartinez
        this.lstAttributesModel.addElement(new AttributeListElement(newAttr));
661 44794 omartinez
        ListElement.setSelected(this.lstAttributes, newAttr);
662 45166 omartinez
        this.lstAttributes.revalidate();
663
        this.lstAttributes.repaint();
664 44794 omartinez
        this.actualEditableAttribute = newAttr;
665
666 44644 jjdelcerro
    }
667
668 44753 omartinez
    private boolean checkIfValueExistInModel(String name, ListModel<ListElement<EditableFeatureAttributeDescriptor>> model) {
669
        for (int i = 0; i < model.getSize(); i++) {
670
            EditableFeatureAttributeDescriptor element = model.getElementAt(i).getValue();
671
            if (StringUtils.equals(element.getLabel(), name)) {
672
                return true;
673
            }
674 44644 jjdelcerro
675 44753 omartinez
        }
676
        return false;
677
    }
678 44644 jjdelcerro
679 44753 omartinez
    private void doRemove() {
680 46251 jjdelcerro
        AttributeListElement selectedValue = (AttributeListElement) this.lstAttributes.getSelectedValue();
681
        if( selectedValue == null ) {
682
            return;
683
        }
684
        FeatureAttributeDescriptor attr = selectedValue.getValue();
685
        if( attr == null ) {
686
            return;
687
        }
688
        boolean removeFromAggregateFunctions = false;
689
        boolean removeFromOrder = false;
690
        boolean removeFromGroupByColums = false;
691
        if( this.query.getAggregateFunctions().containsKey(attr.getName()) ) {
692
            ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
693
            int n = dialogs.confirmDialog(
694
                    "El campo '"+attr.getName()+"' se esta usando en una funcion de agregado. ? Dese borrarlo igualmente ?",
695
                    "Eliminar campo",
696
                    JOptionPane.YES_NO_OPTION,
697
                    JOptionPane.QUESTION_MESSAGE,
698
                    "_FeatureQueryCalculatedColumns_remove_used_column"
699
            );
700
            if( n != JOptionPane.YES_OPTION ) {
701
                return;
702
            }
703
            n = dialogs.confirmDialog(
704
                    "? Desea eliminar la funcion de agregado donde se esta usando el campo '"+attr.getName()+"' ?",
705
                    "Eliminar campo",
706
                    JOptionPane.YES_NO_OPTION,
707
                    JOptionPane.QUESTION_MESSAGE,
708
                    "_FeatureQueryCalculatedColumns_remove_aggregate_function_when_remove_column"
709
            );
710
            if( n == JOptionPane.YES_OPTION ) {
711
                removeFromAggregateFunctions = true;
712 46586 fdiaz
            } else {
713
                LOGGER.info("It has been decided not to delete the aggregate function of a deleted field");
714 46251 jjdelcerro
            }
715
        }
716
        if( this.query.getGroupByColumns().contains(attr.getName()) ) {
717
            ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
718
            int n = dialogs.confirmDialog(
719
                    "El campo '"+attr.getName()+"' se esta usando como campo de agrupacion. ? Dese borrarlo igualmente ?",
720
                    "Eliminar campo",
721
                    JOptionPane.YES_NO_OPTION,
722
                    JOptionPane.QUESTION_MESSAGE,
723
                    "_FeatureQueryCalculatedColumns_remove_used_column"
724
            );
725
            if( n != JOptionPane.YES_OPTION ) {
726
                return;
727
            }
728
            n = dialogs.confirmDialog(
729
                    "? Desea eliminar la agrupacion por el campo '"+attr.getName()+"' ?",
730
                    "Eliminar campo",
731
                    JOptionPane.YES_NO_OPTION,
732
                    JOptionPane.QUESTION_MESSAGE,
733
                    "_FeatureQueryCalculatedColumns_remove_group_by_column_when_remove_column"
734
            );
735
            if( n == JOptionPane.YES_OPTION ) {
736
                removeFromGroupByColums = true;
737 46586 fdiaz
            } else {
738
                LOGGER.info("It has been decided not to delete the group by a deleted field");
739 46251 jjdelcerro
            }
740
        }
741
        if( this.query.getOrder().contains(attr.getName()) ) {
742
            ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
743
            int n = dialogs.confirmDialog(
744
                    "El campo '"+attr.getName()+"' se esta usando para establecer el orden. ? Dese borrarlo igualmente ?",
745
                    "Eliminar campo",
746
                    JOptionPane.YES_NO_OPTION,
747
                    JOptionPane.QUESTION_MESSAGE,
748
                    "_FeatureQueryCalculatedColumns_remove_used_column"
749
            );
750
            if( n != JOptionPane.YES_OPTION ) {
751
                return;
752
            }
753
754
            n = dialogs.confirmDialog(
755
                    "? Desea eliminar el uso del campo '"+attr.getName()+"' en la ordenacion ?",
756
                    "Eliminar campo",
757
                    JOptionPane.YES_NO_OPTION,
758
                    JOptionPane.QUESTION_MESSAGE,
759
                    "_FeatureQueryCalculatedColumns_remove_order_when_remove_column"
760
            );
761
            if( n == JOptionPane.YES_OPTION ) {
762
                removeFromOrder = true;
763 46586 fdiaz
            } else {
764
                LOGGER.info("It has been decided not to delete the order by a deleted field");
765 46251 jjdelcerro
            }
766
        }
767
        if( removeFromAggregateFunctions ) {
768
           this.query.removeAggregateFunction(attr.getName());
769
        }
770
        if( removeFromOrder ) {
771
            int index = this.query.getOrder().getIndex(attr.getName());
772
            this.query.getOrder().remove(index);
773
        }
774
        if( removeFromGroupByColums ) {
775
            this.query.removeGroupByColumn(attr.getName());
776
        }
777
778 44791 omartinez
        ListSelectionModel lsm = lstAttributes.getSelectionModel();
779
        DefaultListModel model = (DefaultListModel) this.lstAttributes.getModel();
780
        int actualIndex = lsm.getMinSelectionIndex();
781
        this.lstAttributesModel.removeElement(selectedValue);
782
        this.columnController.clean();
783
784
        int size = model.size();
785
        if (size == 0) {
786
            //List is empty: disable delete, up, and down buttons.
787
        } else {
788
            //Adjust the selection.
789
            if (model.getSize() == 0) {
790
791
            } else if (actualIndex >= model.getSize()) {
792
                lstAttributes.setSelectedIndex(model.getSize() - 1);
793
            } else {
794
                lstAttributes.setSelectedIndex(actualIndex);
795
            }
796
        }
797 44644 jjdelcerro
    }
798
799 44753 omartinez
    private void doApplyChanges() {
800 44791 omartinez
        this.columnController.fetch(this.actualEditableAttribute);
801
        this.lstAttributes.invalidate();
802 45166 omartinez
        this.lstAttributes.repaint();
803 44753 omartinez
    }
804
805
    private void doUp(JList lstColumns) {
806
        int moveMe = lstColumns.getSelectedIndex();
807
        if (moveMe != 0) {
808
            //not already at top
809
            swap(lstColumns, moveMe, moveMe - 1);
810
            lstColumns.setSelectedIndex(moveMe - 1);
811
            lstColumns.ensureIndexIsVisible(moveMe - 1);
812
        }
813
    }
814
815
    private void doDown(JList lstColumns) {
816
        int moveMe = lstColumns.getSelectedIndex();
817
        if (moveMe != lstColumns.getModel().getSize() - 1) {
818
            //not already at bottom
819
            swap(lstColumns, moveMe, moveMe + 1);
820
            lstColumns.setSelectedIndex(moveMe + 1);
821
            lstColumns.ensureIndexIsVisible(moveMe + 1);
822
        }
823
    }
824
825
    private void swap(JList lstColumns, int a, int b) {
826
        DefaultListModel model = (DefaultListModel) lstColumns.getModel();
827
        Object aObject = model.getElementAt(a);
828
        Object bObject = model.getElementAt(b);
829
        model.set(a, bObject);
830
        model.set(b, aObject);
831
    }
832 44644 jjdelcerro
}