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

History | View | Annotate | Download (26.1 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.fmap.dal.DALLocator;
30
import org.gvsig.fmap.dal.exception.DataException;
31
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression;
32
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
33
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
34
import org.gvsig.fmap.dal.feature.FeatureQuery;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.FeatureType;
37
import org.gvsig.fmap.dal.swing.DALSwingLocator;
38
import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryCalculatedColumnsPanel;
39
import org.gvsig.fmap.dal.swing.featuretype.FeatureTypeAttributePanel;
40
import org.gvsig.fmap.dal.swing.featuretype.FeatureTypePanel;
41
import org.gvsig.tools.ToolsLocator;
42
import org.gvsig.tools.dataTypes.Coercion;
43
import org.gvsig.tools.dataTypes.CoercionException;
44
import org.gvsig.tools.dataTypes.DataType;
45
import org.gvsig.tools.dataTypes.DataTypes;
46
import org.gvsig.tools.i18n.I18nManager;
47
import org.gvsig.tools.swing.api.ListElement;
48
import org.gvsig.tools.swing.api.ToolsSwingLocator;
49
import org.gvsig.tools.swing.api.ToolsSwingManager;
50
import org.gvsig.tools.swing.api.pickercontroller.DataTypePickerController;
51
import org.gvsig.tools.swing.api.windowmanager.Dialog;
52
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
53
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
54
import org.gvsig.tools.swing.icontheme.IconTheme;
55
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
57

    
58
/**
59
 *
60
 * @author jjdelcerro
61
 */
62
public class DefaultFeatureQueryCalculatedColumnsPanel
63
        extends DefaultFeatureQueryCalculatedColumnsPanelView
64
        implements FeatureQueryCalculatedColumnsPanel {
65

    
66
    private class ColumnController implements DocumentListener, ChangeListener, ActionListener {
67

    
68
        private final JTextComponent txtName;
69
        private final JComboBox cboDataType;
70
        private final JButton btnDataType;
71
        private final JButton btnExpressionBookmarks;
72
        private final JButton btnExpressionHistory;
73
        private final JButton btnExpression;
74
        private final JTextComponent txtExpression;
75
        private ExpressionPickerController expPicker;
76
        private DataTypePickerController pickerDataType;
77
        private boolean hasChanges;
78
        private final JTextComponent txtSize;
79
        private final JTextComponent txtPrecision;
80
        private final JTextComponent txtScale;
81
        
82
        private final Logger LOGGER = LoggerFactory.getLogger(ColumnController.class);
83

    
84
        public ColumnController(
85
                JTextComponent txtName,
86
                JComboBox cboDataType,
87
                JButton btnDataType,
88
                JTextComponent txtExpression,
89
                JButton btnExpression,
90
                JButton btnExpressionHistory,
91
                JButton btnExpressionBookmarks,
92
                JTextComponent txtSize,
93
                JTextComponent txtPrecision,
94
                JTextComponent txtScale
95
        ) {
96
            this.txtName = txtName;
97
            this.cboDataType = cboDataType;
98
            this.btnDataType = btnDataType;
99
            this.txtExpression = txtExpression;
100
            this.btnExpression = btnExpression;
101
            this.btnExpressionHistory = btnExpressionHistory;
102
            this.btnExpressionBookmarks = btnExpressionBookmarks;
103
            this.txtSize = txtSize;
104
            this.txtPrecision = txtPrecision;
105
            this.txtScale = txtScale;
106
            this.hasChanges = false;
107
            this.initComponents();
108
            this.setEnabled(false);
109
        }
110

    
111
        public void initComponents() {
112
            this.pickerDataType = ToolsSwingLocator.getToolsSwingManager().createDataTypePickerController(this.cboDataType, this.btnDataType, false);
113

    
114
            ExpressionEvaluatorSwingManager managerExpSwing = ExpressionEvaluatorSwingLocator.getManager();
115
            this.expPicker = managerExpSwing.createExpressionPickerController(
116
                    txtExpression,
117
                    btnExpression,
118
                    btnExpressionBookmarks,
119
                    btnExpressionHistory
120
            );
121
            
122

    
123
            this.txtName.getDocument().addDocumentListener(this);
124
            this.cboDataType.addActionListener(this);
125
            this.txtExpression.getDocument().addDocumentListener(this);
126
            this.txtSize.getDocument().addDocumentListener(this);
127
            this.txtPrecision.getDocument().addDocumentListener(this);
128
            this.txtScale.getDocument().addDocumentListener(this);
129
            //Todo: que el listener expresion lo gestion el picker
130
            
131
            this.cboDataType.addActionListener(new ActionListener() {
132
                @Override
133
                public void actionPerformed(ActionEvent ae) {
134
                    doSetDefaultValuesForDataType();
135
                    doEnabledByDataType();
136
                }
137
            });
138

    
139
        }
140
        
141
        public void doEnabledByDataType() {
142
            JComboBox comboBox = cboDataType;
143
            if (comboBox.getSelectedItem()==null) {
144
                return;
145
            }
146
            DataType dataType = (DataType) comboBox.getSelectedItem();
147
            txtSize.setEnabled(dataType.supportSize());
148
            txtPrecision.setEnabled(dataType.supportPrecision());
149
            txtScale.setEnabled(dataType.supportScale());
150
        }
151
        
152
        public void doSetDefaultValuesForDataType() {
153
            if (this.cboDataType.getSelectedItem()==null) {
154
                return;
155
            }
156
            DataType dataType = (DataType) this.cboDataType.getSelectedItem();
157
            if (dataType.supportSize() && txtSize.getText().isEmpty()) {
158
                txtSize.setText(String.valueOf(dataType.getDefaultSize()));
159
            } else if (!dataType.supportSize()) {
160
                txtSize.setText("");
161
            }
162

    
163
            if (dataType.supportPrecision() && txtPrecision.getText().isEmpty()) {
164
                txtPrecision.setText(String.valueOf(dataType.getDefaultPrecision()));
165
            } else if (!dataType.supportPrecision()) {
166
                txtPrecision.setText("");
167
            }
168

    
169
            if (dataType.supportScale() && txtScale.getText().isEmpty()) {
170
                txtScale.setText(String.valueOf(dataType.getDefaultScale()));
171
            } else if (!dataType.supportScale()) {
172
                txtScale.setText("");
173
            }
174
        }
175

    
176
        public boolean hasChanges() {
177
            return this.hasChanges;
178
        }
179

    
180
        public void setEnabled(boolean enabled) {
181
            this.txtName.setEnabled(enabled);
182
            this.expPicker.setEnabled(enabled);
183
            this.cboDataType.setEnabled(enabled);
184
            this.txtSize.setEnabled(enabled);
185
            this.txtPrecision.setEnabled(enabled);
186
            this.txtScale.setEnabled(enabled);
187
        }
188

    
189
        public void clean() {
190
            this.txtName.setText("");
191
            this.expPicker.set(null);
192
            this.cboDataType.setSelectedIndex(0);
193
            this.txtSize.setText("");
194
            this.txtPrecision.setText("");
195
            this.txtScale.setText("");
196

    
197
        }
198

    
199
        public void put(EditableFeatureAttributeDescriptor attr) {
200
            this.clean();
201
            this.txtName.setText(attr.getName());
202
            FeatureAttributeEmulatorExpression emu = (FeatureAttributeEmulatorExpression) attr.getFeatureAttributeEmulator();
203
            if (emu != null) {
204
                this.expPicker.set(emu.getExpression());
205
            }
206
            this.pickerDataType.set(attr.getDataType());
207
            Coercion coerceString = ToolsLocator.getDataTypesManager().get(DataTypes.STRING).getCoercion();
208
            txtSize.setEnabled(attr.getDataType().supportSize());
209
            if (attr.getDataType().supportSize()) {
210
                try {
211
                    this.txtSize.setText((String) coerceString.coerce(attr.getSize()));
212
                } catch (CoercionException ex) {
213
                    LOGGER.warn("Not able to coerce text to integer from size box.", ex);
214
                }
215
            }
216
            txtPrecision.setEnabled(attr.getDataType().supportPrecision());
217
            if (attr.getDataType().supportPrecision()) {
218
                try {
219
                    this.txtPrecision.setText((String) coerceString.coerce(attr.getPrecision()));
220
                } catch (CoercionException ex) {
221
                    LOGGER.warn("Not able to coerce text to integer from precision box.", ex);
222
                }
223
            }
224
            txtScale.setEnabled(attr.getDataType().supportScale());
225
            if (attr.getDataType().supportScale()) {
226
                try {
227
                    this.txtScale.setText((String) coerceString.coerce(attr.getScale()));
228
                } catch (CoercionException ex) {
229
                    LOGGER.warn("Not able to coerce text to integer from scale box.", ex);
230
                }
231
            }
232
//            doEnabledByDataType();
233
            this.hasChanges = false;
234
        }
235

    
236
        public EditableFeatureAttributeDescriptor fetch(EditableFeatureAttributeDescriptor attr) {
237
            attr.setName(this.txtName.getText());
238
            attr.setFeatureAttributeEmulator(this.expPicker.get());
239
            attr.setDataType(this.pickerDataType.get());
240
            Coercion coerceInteger = ToolsLocator.getDataTypesManager().get(DataTypes.INTEGER).getCoercion();
241
            try {
242
                if (attr.getDataType().supportSize()) {
243
                    attr.setSize((int) coerceInteger.coerce(this.txtSize.getText()));
244
                }
245
            } catch (CoercionException ex) {
246
                LOGGER.warn("Not able to coerce text to integer from size box.", ex);
247
            }
248
            try {
249
                if (attr.getDataType().supportPrecision()) {
250
                    attr.setPrecision((int) coerceInteger.coerce(this.txtPrecision.getText()));
251
                }
252
            } catch (CoercionException ex) {
253
                LOGGER.warn("Not able to coerce text to integer from precision box.", ex);
254
            }
255
            try {
256
                if (attr.getDataType().supportScale()) {
257
                    attr.setScale((int) coerceInteger.coerce(this.txtScale.getText()));
258
                }
259
            } catch (CoercionException ex) {
260
                LOGGER.warn("Not able to coerce text to integer from scale box.", ex);
261
            }
262
            this.hasChanges = false;
263
            return attr;
264
        }
265

    
266
        private void doHasChanges() {
267
            this.hasChanges = true;
268
        }
269

    
270
        @Override
271
        public void insertUpdate(DocumentEvent de) {
272
            doHasChanges();
273
        }
274

    
275
        @Override
276
        public void removeUpdate(DocumentEvent de) {
277
            doHasChanges();
278
        }
279

    
280
        @Override
281
        public void changedUpdate(DocumentEvent de) {
282
            doHasChanges();
283
        }
284

    
285
        @Override
286
        public void stateChanged(ChangeEvent ce) {
287
            doHasChanges();
288
        }
289

    
290
        @Override
291
        public void actionPerformed(ActionEvent ae) {
292
            doHasChanges();
293
        }
294
        
295
        public void setStore(FeatureStore store) {
296
          DALSwingLocator.getManager().configureExpressionBuilder(expPicker, store);
297
//             FeatureStoreElement calculatedStoreElement = DALSwingLocator.getSwingManager()
298
//                    .createFeatureStoreElement();
299
//            calculatedStoreElement.setFeatureStore(store);
300
//            this.expPicker.removeAllSymbolTables();
301
//            this.expPicker.addElement(calculatedStoreElement);
302
        }
303
    }
304

    
305
    private ColumnController columnController;
306
    private FeatureStore store;
307
    private FeatureType featureType;
308
    private FeatureQuery query;
309
//    private JExpressionBuilder pckExpression;
310
    private static final String COLUMN_DEFAULT_NAME = "Field";
311
    private EditableFeatureAttributeDescriptor actualEditableAttribute;
312
    private DefaultListModel lstAttributesModel;
313
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureQueryCalculatedColumnsPanel.class);
314

    
315
    public DefaultFeatureQueryCalculatedColumnsPanel() {
316
        this.columnController = null;
317
        this.initComponents();
318
    }
319

    
320
    @Override
321
    public JComponent asJComponent() {
322
        return this;
323
    }
324

    
325
    @Override
326
    public ImageIcon loadImage(String imageName) {
327
        String name = FilenameUtils.getBaseName(imageName);
328
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault();
329
        if (theme.exists(name)) {
330
            return theme.get(name);
331
        }
332
        URL url = this.getClass().getResource(name + ".png");
333
        if (url == null) {
334
            return null;
335
        }
336
        return new ImageIcon(url);
337
    }
338

    
339
    @Override
340
    public void setStore(FeatureStore store) {
341
        try {
342
            this.featureType = store.getDefaultFeatureType();
343
            this.store = store;
344
            this.query = store.createFeatureQuery();
345
            this.columnController.setStore(store);
346
//            this.pckExpression = DALSwingLocator.getManager().createQueryFilterExpresion(store);
347
        } catch (DataException ex) {
348
            throw new RuntimeException("Can't assign store", ex);
349
        }
350
    }
351

    
352
    private void initComponents() {
353
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
354

    
355
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
356
        ImageIcon icon = theme.get("picker-datatype");
357
        this.btnColumnDataType.setIcon(icon);
358
        toolsSwingManager.translate(this.lblExpression);
359
        toolsSwingManager.translate(this.lblName);
360
        toolsSwingManager.translate(this.lblType);
361
        toolsSwingManager.translate(this.lblSize);
362
        toolsSwingManager.translate(this.lblPrecision);
363
        toolsSwingManager.translate(this.lblScale);
364
        toolsSwingManager.translate(this.btnApplyChanges);
365
        toolsSwingManager.translate(this.btnColumnMore);
366

    
367
        this.columnController = new ColumnController(
368
                this.txtColumnName,
369
                this.cboColumnDataType,
370
                this.btnColumnDataType,
371
                this.txtColumnExpression,
372
                this.btnColumnExpression,
373
                this.btnColumnExpressionHistory,
374
                this.btnColumnExpressionBookmarks,
375
                this.txtColumnSize,
376
                this.txtColumnPrecision,
377
                this.txtColumnScale
378
        );
379
//        this.lstAttributes.setCellRenderer(new FeatureAttributeListCellRenderer());
380

    
381
        this.lstAttributes.addListSelectionListener((ListSelectionEvent e) -> {
382
            if (e.getValueIsAdjusting()) {
383
                return;
384
            }
385
            doSelectAttribute();
386
        });
387

    
388
        Dimension sz = this.getPreferredSize();
389
        if (sz.width < DEFAULT_WIDTH) {
390
            sz.width = DEFAULT_WIDTH;
391
        }
392
        if (sz.height < DEFAULT_HEIGHT) {
393
            sz.height = DEFAULT_HEIGHT;
394
        }
395
        this.setPreferredSize(sz);
396

    
397
        this.lstAttributesModel = new DefaultListModel<ListElement<EditableFeatureAttributeDescriptor>>();
398
//        this.lstAttributesModel = ToolsSwingLocator.getToolsSwingManager().createFilteredListModel();
399
        this.lstAttributes.setModel(lstAttributesModel);
400

    
401
        this.lstAttributes.setSelectionModel(new DefaultListSelectionModel() {
402

    
403
            @Override
404
            public void setSelectionInterval(int index0, int index1) {
405
                if (checkColumnControllerHasChangesWithUser()) {
406
                    super.setSelectionInterval(index0, index1);
407
                    doSelectAttribute();
408
                }
409
            }
410

    
411
        });
412

    
413
        this.btnAdd.addActionListener((ActionEvent e) -> {
414
            doAdd();
415
        });
416

    
417
        this.btnRemove.addActionListener((ActionEvent e) -> {
418
            doRemove();
419
        });
420

    
421
        this.btnUp.addActionListener((ActionEvent e) -> {
422
            doUp(lstAttributes);
423
        });
424

    
425
        this.btnDown.addActionListener((ActionEvent e) -> {
426
            doDown(lstAttributes);
427
        });
428

    
429
        this.btnApplyChanges.addActionListener((ActionEvent e) -> {
430
            doApplyChanges();
431
        });
432

    
433
        this.btnColumnMore.addActionListener((ActionEvent e) -> {
434
            doColumnMore();
435
        });
436

    
437
    }
438

    
439
    private boolean checkColumnControllerHasChangesWithUser() {
440
        if (columnController.hasChanges()) {
441
            I18nManager i18n = ToolsLocator.getI18nManager();
442
            String message = i18n.getTranslation("_Changes_has_not_been_saved_Are_you_sure_you_want_to_continue");
443
            String title = i18n.getTranslation("_Changes_not_saved");
444
            int r = ToolsSwingLocator.getThreadSafeDialogsManager().confirmDialog(message, title,
445
                    JOptionPane.YES_NO_OPTION,
446
                    JOptionPane.QUESTION_MESSAGE);
447
            if (r == JOptionPane.NO_OPTION) {
448
                return false;
449
            }
450

    
451
        }
452
        return true;
453
    }
454

    
455
    private void doColumnMore() {
456
        if (this.columnController == null) {
457
            return;
458
        }
459
        final I18nManager i18n = ToolsLocator.getI18nManager();
460
        WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
461
        final FeatureTypeAttributePanel panel = DALSwingLocator.getSwingManager().createFeatureTypeAttributePanel();
462
        panel.clean();
463
//                panel.setNameAsNew(featureType);
464
        panel.put(actualEditableAttribute);
465
        panel.setMode(FeatureTypePanel.MODE_EDIT_ALL);
466
        final Dialog dialog = winManager.createDialog(
467
                panel.asJComponent(),
468
                i18n.getTranslation("_Extra_column"),
469
                null,
470
                WindowManager_v2.BUTTONS_OK_CANCEL
471
        );
472
        dialog.addActionListener(new ActionListener() {
473
            @Override
474
            public void actionPerformed(ActionEvent e) {
475
                if (dialog.getAction() != WindowManager_v2.BUTTONS_OK) {
476
                    return;
477
                }
478
                try {
479
                    FeatureAttributeDescriptor prev = actualEditableAttribute.getCopy();
480
                    panel.fetch(actualEditableAttribute);
481
                    columnController.put(actualEditableAttribute);
482
                    if (!prev.equals(actualEditableAttribute)) {
483
                        columnController.doHasChanges();
484
                    }
485
                } catch (Exception ex) {
486
                    LOGGER.warn("Problems updating feature descriptor.", ex);
487
                }
488
            }
489
        });
490
        dialog.show(WindowManager.MODE.DIALOG);
491
    }
492

    
493
    public boolean checkPanel(StringBuilder msg) {
494
        I18nManager i18n = ToolsLocator.getI18nManager();
495
        if (this.hasChanges()) {
496
            int r = ToolsSwingLocator.getThreadSafeDialogsManager().confirmDialog(
497
                    i18n.getTranslation("_Changes_has_not_been_saved_Are_you_sure_you_want_to_continue"),
498
                    i18n.getTranslation("_Changes_not_saved"),
499
                    JOptionPane.YES_NO_OPTION,
500
                    JOptionPane.QUESTION_MESSAGE);
501
            if (r == JOptionPane.NO_OPTION) {
502
                return false;
503
            }
504
        }
505
        return true;
506
    }
507

    
508
    public boolean hasChanges() {
509
        return this.columnController.hasChanges();
510
    }
511

    
512
    private void doSelectAttribute() {
513
        EditableFeatureAttributeDescriptor value = null;
514
        if (this.lstAttributes.getSelectedValue() != null) {
515
            ListElement<EditableFeatureAttributeDescriptor> selectedValue = (ListElement<EditableFeatureAttributeDescriptor>) this.lstAttributes.getSelectedValue();
516
            if (selectedValue != null) {
517
                value = selectedValue.getValue();
518
            }
519
        }
520
        if (value == null) {
521
            this.columnController.clean();
522
            this.columnController.setEnabled(false);
523
            this.actualEditableAttribute = null;
524

    
525
        } else {
526
            //EditableFeatureAttributeDescriptor value = node.getValue();
527
            this.actualEditableAttribute = value;
528
            columnController.setEnabled(true);
529
            columnController.put(value);
530
        }
531
    }
532

    
533
    @Override
534
    public FeatureQuery fetch(FeatureQuery query) {
535
        if (query == null) {
536
            return this.query.getCopy();
537
        }
538

    
539
        this.query.getExtraColumn().clear();
540
        for (int i = 0; i < lstAttributesModel.getSize(); i++) {
541
            ListElement<EditableFeatureAttributeDescriptor> element = (ListElement<EditableFeatureAttributeDescriptor>) lstAttributesModel.get(i);
542
            EditableFeatureAttributeDescriptor newExtraColumn = this.query.getExtraColumn().add(element.getValue().getName());
543
            newExtraColumn.copyFrom(element.getValue());
544
        }
545
        query.copyFrom(this.query);
546

    
547
        return query;
548
    }
549

    
550
    @Override
551
    public FeatureQuery fetch() {
552
        return this.fetch(null);
553
    }
554

    
555
    @Override
556
    public void put(FeatureQuery query) {
557
        this.query.copyFrom(query);
558
        addExtraColumns(this.query);
559
    }
560

    
561
    private void addExtraColumns(FeatureQuery query) {
562
        List<EditableFeatureAttributeDescriptor> cols = query.getExtraColumn().getColumns();
563
        if (cols == null || cols.isEmpty()) {
564
            return;
565
        }
566
        for (EditableFeatureAttributeDescriptor col : cols) {
567
            ListElement lf = new ListElement(col.getLabel(), col);
568
            this.lstAttributesModel.addElement(lf);
569
        }
570
        if (this.lstAttributesModel.getSize() > 0) {
571
            this.lstAttributes.setSelectedIndex(0);
572
        }
573
    }
574

    
575
    public static void selfRegister() {
576
        String[][] iconNames = new String[][]{
577
            new String[]{"dalswing", "common-applychanges"},
578
            new String[]{"dalswing", "common-more"},
579
            new String[]{"dalswing", "featurequery-column-add"},
580
            new String[]{"dalswing", "featurequery-column-remove"},
581
            new String[]{"dalswing", "featurequery-column-down"},
582
            new String[]{"dalswing", "featurequery-column-up"}
583
        };
584
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
585
        for (String[] icon : iconNames) {
586
            URL url = DefaultFeatureQueryOrderPanel.class.getResource(icon[1] + ".png");
587
            theme.registerDefault("DALSwing", icon[0], icon[1], null, url);
588
        }
589
    }
590

    
591
    private void doAdd() {
592
        EditableFeatureAttributeDescriptor newAttr = DALLocator.getDataManager().createFeatureAttributeDescriptor();
593
        newAttr.setName(COLUMN_DEFAULT_NAME);
594
        newAttr.setDataType(DataTypes.STRING);
595
        if (newAttr.getDataType().supportSize()) {
596
            newAttr.setSize(newAttr.getDataType().getDefaultSize());
597
        }
598
        int id = 0;
599
        while (checkIfValueExistInModel(newAttr.getName(), this.lstAttributesModel)) {
600
            String newName = COLUMN_DEFAULT_NAME + "_" + id;
601
            id += 1;
602
            newAttr.setName(newName);
603
        }
604
        this.columnController.put(newAttr);
605
        this.columnController.setEnabled(true);
606

    
607
        this.lstAttributesModel.addElement(new ListElement(newAttr.getName(), newAttr));
608
        ListElement.setSelected(this.lstAttributes, newAttr);
609
        this.actualEditableAttribute = newAttr;
610

    
611
    }
612

    
613
    private boolean checkIfValueExistInModel(String name, ListModel<ListElement<EditableFeatureAttributeDescriptor>> model) {
614
        for (int i = 0; i < model.getSize(); i++) {
615
            EditableFeatureAttributeDescriptor element = model.getElementAt(i).getValue();
616
            if (StringUtils.equals(element.getLabel(), name)) {
617
                return true;
618
            }
619

    
620
        }
621
        return false;
622
    }
623

    
624
    private void doRemove() {
625
        ListSelectionModel lsm = lstAttributes.getSelectionModel();
626
        DefaultListModel model = (DefaultListModel) this.lstAttributes.getModel();
627
        int actualIndex = lsm.getMinSelectionIndex();
628
        Object selectedValue = this.lstAttributes.getSelectedValue();
629
        this.lstAttributesModel.removeElement(selectedValue);
630
        this.columnController.clean();
631

    
632
        int size = model.size();
633
        if (size == 0) {
634
            //List is empty: disable delete, up, and down buttons.
635
        } else {
636
            //Adjust the selection.
637
            if (model.getSize() == 0) {
638

    
639
            } else if (actualIndex >= model.getSize()) {
640
                lstAttributes.setSelectedIndex(model.getSize() - 1);
641
            } else {
642
                lstAttributes.setSelectedIndex(actualIndex);
643
            }
644
        }
645
    }
646

    
647
    private void doApplyChanges() {
648
        this.columnController.fetch(this.actualEditableAttribute);
649
        this.lstAttributes.invalidate();
650
    }
651

    
652
    private void doUp(JList lstColumns) {
653
        int moveMe = lstColumns.getSelectedIndex();
654
        if (moveMe != 0) {
655
            //not already at top
656
            swap(lstColumns, moveMe, moveMe - 1);
657
            lstColumns.setSelectedIndex(moveMe - 1);
658
            lstColumns.ensureIndexIsVisible(moveMe - 1);
659
        }
660
    }
661

    
662
    private void doDown(JList lstColumns) {
663
        int moveMe = lstColumns.getSelectedIndex();
664
        if (moveMe != lstColumns.getModel().getSize() - 1) {
665
            //not already at bottom
666
            swap(lstColumns, moveMe, moveMe + 1);
667
            lstColumns.setSelectedIndex(moveMe + 1);
668
            lstColumns.ensureIndexIsVisible(moveMe + 1);
669
        }
670
    }
671

    
672
    private void swap(JList lstColumns, int a, int b) {
673
        DefaultListModel model = (DefaultListModel) lstColumns.getModel();
674
        Object aObject = model.getElementAt(a);
675
        Object bObject = model.getElementAt(b);
676
        model.set(a, bObject);
677
        model.set(b, aObject);
678
    }
679
}