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 / featuretype / DefaultFeatureAttributesSelectionPanel.java @ 46485

History | View | Annotate | Download (17.6 KB)

1 44707 jjdelcerro
package org.gvsig.fmap.dal.swing.impl.featuretype;
2 44644 jjdelcerro
3
import java.awt.Dimension;
4
import java.awt.event.ActionEvent;
5
import java.awt.event.ActionListener;
6
import java.net.URL;
7 44707 jjdelcerro
import java.util.ArrayList;
8
import java.util.Collections;
9
import java.util.List;
10 44747 omartinez
import javax.swing.DefaultListCellRenderer;
11 44644 jjdelcerro
import javax.swing.ImageIcon;
12
import javax.swing.JComponent;
13 46485 fdiaz
import javax.swing.ListCellRenderer;
14 45155 omartinez
import javax.swing.event.DocumentEvent;
15
import javax.swing.event.DocumentListener;
16 44644 jjdelcerro
import javax.swing.event.ListSelectionEvent;
17
import org.apache.commons.io.FilenameUtils;
18 46485 fdiaz
import org.apache.commons.lang.StringUtils;
19 45155 omartinez
import org.gvsig.expressionevaluator.Expression;
20
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
21
import org.gvsig.expressionevaluator.swing.ExpressionPickerController;
22 46485 fdiaz
import org.gvsig.fmap.dal.DALLocator;
23
import org.gvsig.fmap.dal.DataTypes;
24 44644 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
25 44794 omartinez
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
26 44644 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
27
import org.gvsig.fmap.dal.feature.FeatureStore;
28
import org.gvsig.fmap.dal.feature.FeatureType;
29 45155 omartinez
import org.gvsig.fmap.dal.swing.DALSwingLocator;
30 46485 fdiaz
import org.gvsig.fmap.dal.swing.DataSwingManager;
31 44644 jjdelcerro
import org.gvsig.tools.swing.api.ActionListenerSupport;
32 44713 jjdelcerro
import org.gvsig.tools.swing.api.FilteredListController;
33 44644 jjdelcerro
import org.gvsig.tools.swing.api.FilteredListModel;
34
import org.gvsig.tools.swing.api.ToolsSwingLocator;
35
import org.gvsig.tools.swing.api.ToolsSwingManager;
36
import org.gvsig.tools.swing.icontheme.IconTheme;
37 44713 jjdelcerro
import org.gvsig.fmap.dal.swing.featuretype.FeatureAttributesSelectionPanel;
38 46238 jjdelcerro
import org.gvsig.tools.locator.ReferenceNotRegisteredException;
39 44747 omartinez
import org.gvsig.tools.util.LabeledValue;
40 46485 fdiaz
import org.gvsig.tools.util.LabeledValueImpl;
41 44644 jjdelcerro
42
/**
43
 *
44
 * @author jjdelcerro
45
 */
46 44712 jjdelcerro
public class DefaultFeatureAttributesSelectionPanel
47
        extends DefaultFeatureAttributesSelectionPanelView
48 44713 jjdelcerro
        implements FeatureAttributesSelectionPanel
49 44644 jjdelcerro
    {
50
51 46485 fdiaz
    private static final String ATTR_IS_EXPRESSION = "DefaultFeatureAttributesSelectionPanel_isExpression";
52
53 44644 jjdelcerro
    private FeatureType featureType;
54
    private final ActionListenerSupport actionListenerSupport;
55
    private FilteredListController availableFieldsController;
56 46485 fdiaz
    private final List<FeatureAttributeDescriptor> selecteds;
57 45155 omartinez
    private ExpressionPickerController expressionPicker = null;
58 45166 omartinez
    private boolean allowCalculatedAttributes = false;
59 45162 omartinez
    private List<EditableFeatureAttributeDescriptor> extraColumns;
60 44644 jjdelcerro
61 44712 jjdelcerro
    public DefaultFeatureAttributesSelectionPanel() {
62 44644 jjdelcerro
        this.actionListenerSupport = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
63 44707 jjdelcerro
        this.selecteds = new ArrayList<>();
64 44644 jjdelcerro
        this.initComponents();
65
    }
66
67
    private void initComponents() {
68 44713 jjdelcerro
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
69 46485 fdiaz
        ListCellRenderer renderer = DALSwingLocator.getDataSwingManager().createDefaultFeatureAttributeListCellRenderer();
70
//        DefaultListCellRenderer renderer = new DefaultListCellRenderer();
71 44644 jjdelcerro
        this.lstAvailableColumns.setCellRenderer(renderer);
72 44707 jjdelcerro
        this.lstSelectedColumns.setCellRenderer(renderer);
73 44644 jjdelcerro
74 44707 jjdelcerro
        this.lstAvailableColumns.addListSelectionListener((ListSelectionEvent e) -> {
75
          if( e.getValueIsAdjusting() ) {
76
            return;
77
          }
78
          doSelectAvailableColumn();
79 44644 jjdelcerro
        });
80 44707 jjdelcerro
        this.lstSelectedColumns.addListSelectionListener((ListSelectionEvent e) -> {
81
          if( e.getValueIsAdjusting() ) {
82
            return;
83
          }
84
          doSelectColumn();
85 44644 jjdelcerro
        });
86 45155 omartinez
        this.txtExpression.getDocument().addDocumentListener(new DocumentListener() {
87
            public void changedUpdate(DocumentEvent e) {
88
              doSelectAvailableColumn();
89
            }
90
            @Override
91
            public void insertUpdate(DocumentEvent de) {
92
                 doSelectAvailableColumn();
93
            }
94
95
            @Override
96
            public void removeUpdate(DocumentEvent de) {
97
                 doSelectAvailableColumn();
98
            }
99
        });
100 44707 jjdelcerro
        this.btnColumnAdd.addActionListener((ActionEvent e) -> {
101
          doColumnAdd();
102 44644 jjdelcerro
        });
103 44707 jjdelcerro
        this.btnColumnRemove.addActionListener((ActionEvent e) -> {
104
          doColumnRemove();
105 44644 jjdelcerro
        });
106 44707 jjdelcerro
        this.btnColumnDown.addActionListener((ActionEvent e) -> {
107
          doColumnDown();
108 44644 jjdelcerro
        });
109 44707 jjdelcerro
        this.btnColumnUp.addActionListener((ActionEvent e) -> {
110
          doColumnUp();
111 44644 jjdelcerro
        });
112 44713 jjdelcerro
        this.availableFieldsController = toolsSwingManager.createFilteredListController(
113 44644 jjdelcerro
                lstAvailableColumns,
114
                txtColumnsFilter,
115
                btnColumnsFilter
116 45155 omartinez
        );
117 44644 jjdelcerro
        Dimension sz = this.getPreferredSize();
118
        if( sz.width<450 ) {
119
            sz.width = 450;
120
        }
121
        if( sz.height < 320 ) {
122
            sz.height = 320;
123
        }
124
        this.setPreferredSize(sz);
125
    }
126
127 44707 jjdelcerro
    @Override
128
    public List<String> getSelectedNames() {
129 46485 fdiaz
        List<String> names = new ArrayList<>();
130
        for (FeatureAttributeDescriptor selected : selecteds) {
131
                names.add(selected.getName());
132
        }
133
      return names;
134 44707 jjdelcerro
    }
135
136
    @Override
137
    public void setSelectedNames(List<String> names) {
138 46485 fdiaz
        this.selecteds.clear();
139
        if (names != null) {
140
            for (String name : names) {
141
                FeatureAttributeDescriptor attr = this.getAttributeDescriptor(name);
142
                if(attr == null){
143
                    attr = createAttributeFromExpression(name);
144
                }
145
                if (selectedContains(attr)) {
146
                    continue;
147
                }
148
                this.selecteds.add(attr);
149
            }
150 44707 jjdelcerro
        }
151 46485 fdiaz
        updateControls();
152 44707 jjdelcerro
    }
153 46485 fdiaz
154
    private boolean selectedContains(FeatureAttributeDescriptor attr) {
155
        for (FeatureAttributeDescriptor selected : selecteds) {
156
            if(StringUtils.equalsIgnoreCase(selected.getName(), attr.getName())){
157
                return true;
158
            }
159
        }
160
        return false;
161
    }
162 44707 jjdelcerro
163 44644 jjdelcerro
    private void doSelectAvailableColumn() {
164 45155 omartinez
        if( this.lstAvailableColumns.getSelectedIndex()>=0 || !this.expressionPicker.isEmpty()) {
165 44644 jjdelcerro
            this.btnColumnAdd.setEnabled(true);
166
            return;
167
        }
168
        this.btnColumnAdd.setEnabled(false);
169
    }
170
171 44707 jjdelcerro
    private void doSelectColumn() {
172
        if( this.lstSelectedColumns.getSelectedIndex()>=0 ) {
173
//            FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstSelectedColumns.getSelectedValue();
174
//            if( attr != null ) {
175
//              this.selecteds.add(attr.getName());
176
              this.btnColumnRemove.setEnabled(true);
177
              this.btnColumnDown.setEnabled(true);
178
              this.btnColumnUp.setEnabled(true);
179
              return;
180
//            }
181 44644 jjdelcerro
        }
182
        this.btnColumnRemove.setEnabled(false);
183
        this.btnColumnDown.setEnabled(false);
184
        this.btnColumnUp.setEnabled(false);
185
    }
186
187
    private void doColumnUp() {
188 44747 omartinez
        LabeledValue attr = (LabeledValue) this.lstSelectedColumns.getSelectedValue();
189 44644 jjdelcerro
        if( attr == null ) {
190
            return;
191
        }
192 44747 omartinez
        int n = this.selecteds.indexOf(attr.getLabel());
193 44707 jjdelcerro
        if( n<1 ) {
194
          return;
195
        }
196
        Collections.swap(selecteds, n, n-1);
197
        this.lstSelectedColumns.setSelectedIndex(n-1);
198 44644 jjdelcerro
        this.updateControls();
199
    }
200
201
    private void doColumnDown() {
202 44747 omartinez
        LabeledValue attr = (LabeledValue) this.lstSelectedColumns.getSelectedValue();
203 44644 jjdelcerro
        if( attr == null ) {
204
            return;
205
        }
206 44747 omartinez
        int n = this.selecteds.indexOf(attr.getLabel());
207 45166 omartinez
        if( n<0 || n>=this.selecteds.size()-1 ) {
208 44707 jjdelcerro
          return;
209
        }
210
        Collections.swap(selecteds, n, n+1);
211
        this.lstSelectedColumns.setSelectedIndex(n+1);
212 44644 jjdelcerro
        this.updateControls();
213
    }
214 45155 omartinez
215
    private boolean checkIfAttributeInFeatureType(String myAttr) {
216
        for (FeatureAttributeDescriptor attr : featureType) {
217
            if(attr.getName().equalsIgnoreCase(myAttr) ) {
218
               return true;
219
            }
220
        }
221
        List<EditableFeatureAttributeDescriptor> columns = featureType.getExtraColumns().getColumns();
222
        if (columns!=null && !columns.isEmpty()) {
223
            for (EditableFeatureAttributeDescriptor extraCol : columns) {
224
                if(extraCol.getName().equalsIgnoreCase(myAttr)) {
225
                    return true;
226
                }
227
            }
228
        }
229 45166 omartinez
        if (this.extraColumns!=null && !extraColumns.isEmpty()) {
230
            for (EditableFeatureAttributeDescriptor extraCol : extraColumns) {
231
                if(extraCol.getName().equalsIgnoreCase(myAttr)) {
232
                    return true;
233
                }
234
            }
235
        }
236
237 45155 omartinez
        return false;
238
    }
239 44644 jjdelcerro
240
    private void doColumnRemove() {
241 46485 fdiaz
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstSelectedColumns.getSelectedValue();
242 44644 jjdelcerro
        if( attr == null ) {
243
            return;
244
        }
245 46485 fdiaz
        int n = this.selecteds.indexOf(attr);
246 44707 jjdelcerro
        if( n<0 ) {
247
          return;
248
        }
249 46485 fdiaz
        if(attr.getTags().getBoolean(ATTR_IS_EXPRESSION,false)){
250 45155 omartinez
            Expression toExp = ExpressionEvaluatorLocator.getManager().createExpression();
251 46485 fdiaz
            toExp.setPhrase(attr.getLabel());
252 45155 omartinez
            this.expressionPicker.set(toExp);
253 46485 fdiaz
        }
254 44707 jjdelcerro
        this.selecteds.remove(n);
255 44644 jjdelcerro
        this.updateControls();
256
    }
257
258
    private void doColumnAdd() {
259 45155 omartinez
260
        if (this.expressionPicker.isEmpty()) {
261 46485 fdiaz
            FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstAvailableColumns.getSelectedValue();
262 45155 omartinez
            if( attr == null ) {
263
                return;
264
            }
265
            int n = this.selecteds.indexOf(attr);
266
            if( n>=0 ) {
267
              return;
268
            }
269 46485 fdiaz
            this.selecteds.add(attr);
270 45155 omartinez
        } else {
271
            if (this.expressionPicker.isValid()) {
272
                Expression exp = this.expressionPicker.get();
273
                String expressionField = exp.getPhrase();
274 46485 fdiaz
                FeatureAttributeDescriptor attr = createAttributeFromExpression(expressionField);
275
                for (FeatureAttributeDescriptor selected : this.selecteds) {
276
                    if(StringUtils.equals(attr.getName(),selected.getName())){
277
                        return;
278
                    }
279 45155 omartinez
                }
280 46485 fdiaz
                this.selecteds.add(attr);
281 45155 omartinez
                this.expressionPicker.set(null);
282
            }
283 44644 jjdelcerro
        }
284
        this.updateControls();
285
    }
286 46485 fdiaz
287
    private FeatureAttributeDescriptor createAttributeFromExpression(String expressionField) {
288
        EditableFeatureAttributeDescriptor attr = DALLocator.getDataManager().createFeatureAttributeDescriptor(expressionField, DataTypes.STRING);
289
        attr.getTags().set(ATTR_IS_EXPRESSION, true);
290
        attr.setLabel(expressionField);
291
        return attr;
292
    }
293 44644 jjdelcerro
294
    @Override
295
    public void setStore(FeatureStore store) {
296
        try {
297
            this.setFeatureType(store.getDefaultFeatureType());
298
        } catch (DataException ex) {
299
            throw new RuntimeException("Can't assign store", ex);
300
        }
301
    }
302
303
    @Override
304
    public void setFeatureType(FeatureType featureType) {
305
        this.featureType = featureType;
306 44707 jjdelcerro
        if( !this.selecteds.isEmpty() ) {
307 44644 jjdelcerro
            return;
308
        }
309
        this.updateControls();
310
    }
311 45162 omartinez
    @Override
312
    public void setExtraColumns(List<EditableFeatureAttributeDescriptor> extraColumns) {
313
        this.extraColumns = extraColumns;
314
        this.updateControls();
315
    }
316 44644 jjdelcerro
317
    @Override
318
    public FeatureType getFeatureType() {
319
        return this.featureType;
320
    }
321
322
    @Override
323
    public JComponent asJComponent() {
324
        return this;
325
    }
326
327
    @Override
328
    public void addActionListener(ActionListener al) {
329
        this.actionListenerSupport.addActionListener(al);
330
    }
331
332
    @Override
333
    public ActionListener[] getActionListeners() {
334
        return this.actionListenerSupport.getActionListeners();
335
    }
336
337
    @Override
338
    public void removeActionListener(ActionListener al) {
339
        this.actionListenerSupport.removeActionListener(al);
340
    }
341
342
    @Override
343
    public void removeAllActionListener() {
344
        this.actionListenerSupport.removeAllActionListener();
345
    }
346
347
    @Override
348
    public void fireActionEvent(ActionEvent ae) {
349
        this.actionListenerSupport.fireActionEvent(ae);
350
    }
351
352
    @Override
353
    public boolean hasActionListeners() {
354
        return this.actionListenerSupport.hasActionListeners();
355
    }
356
357
    private void updateControls() {
358 45155 omartinez
359
        if (this.expressionPicker == null) {
360
                this.expressionPicker =
361
                DALSwingLocator.getDataSwingManager().createExpressionPickerController(
362
                        this.featureType.getStore(),
363
                        txtExpression,
364
                        btnExpression,
365
                        btnBookmarks,
366
                        btnHistory);
367
        }
368 44644 jjdelcerro
        int indexAvailables = this.lstAvailableColumns.getSelectedIndex();
369 44707 jjdelcerro
        int indexSelecteds = this.lstSelectedColumns.getSelectedIndex();
370 44644 jjdelcerro
        FilteredListModel modelAvailables = ToolsSwingLocator.getToolsSwingManager().createFilteredListModel();
371 44747 omartinez
        FilteredListModel modelSelecteds = ToolsSwingLocator.getToolsSwingManager().createFilteredListModel();
372 46485 fdiaz
        DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager();
373 44644 jjdelcerro
        for (FeatureAttributeDescriptor attr : featureType) {
374 46485 fdiaz
            if (!selectedContains(attr)) {
375
                modelAvailables.addElement(attr);
376 44644 jjdelcerro
            }
377 44794 omartinez
        }
378 44829 omartinez
        List<EditableFeatureAttributeDescriptor> columns = featureType.getExtraColumns().getColumns();
379 44794 omartinez
        if (columns!=null && !columns.isEmpty()) {
380
            for (EditableFeatureAttributeDescriptor extraCol : columns) {
381 46485 fdiaz
                if (!selectedContains(extraCol)) {
382
                    modelAvailables.addElement(extraCol);
383 44794 omartinez
                }
384
            }
385
        }
386 45162 omartinez
387
        if (this.extraColumns!=null && !this.extraColumns.isEmpty()) {
388
            for (FeatureAttributeDescriptor extraCol : this.extraColumns) {
389 46485 fdiaz
                if (!selectedContains(extraCol)) {
390
                    modelAvailables.addElement(extraCol);
391 45162 omartinez
                }
392
            }
393
        }
394
395 44719 jjdelcerro
        modelAvailables.setFilter(this.txtColumnsFilter.getText());
396 44707 jjdelcerro
        modelAvailables.sort(true);
397 46485 fdiaz
        for (FeatureAttributeDescriptor attr : this.selecteds) {
398
            modelSelecteds.addElement(attr);
399 44644 jjdelcerro
        }
400
        modelAvailables.sort(true);
401
        this.lstAvailableColumns.setModel(modelAvailables);
402 44707 jjdelcerro
        this.lstSelectedColumns.setModel(modelSelecteds);
403 44644 jjdelcerro
        if( indexAvailables >= 0 && modelAvailables.getSize()>0 ) {
404
            if( indexAvailables >= modelAvailables.getSize() ) {
405
                indexAvailables = modelAvailables.getSize()-1;
406
            }
407
            this.lstAvailableColumns.setSelectedIndex(indexAvailables);
408
            this.btnColumnAdd.setEnabled(true);
409
        } else {
410 45155 omartinez
            if(!this.expressionPicker.isEmpty()) {
411
                this.btnColumnAdd.setEnabled(true);
412
            } else {
413
                this.btnColumnAdd.setEnabled(false);
414
            }
415 44644 jjdelcerro
        }
416
417 44707 jjdelcerro
        if( indexSelecteds >= 0 && modelSelecteds.getSize()>0 ) {
418
            if( indexSelecteds >= modelSelecteds.getSize() ) {
419
                indexSelecteds = modelSelecteds.getSize()-1;
420 44644 jjdelcerro
            }
421 44707 jjdelcerro
            this.lstSelectedColumns.setSelectedIndex(indexSelecteds);
422 44644 jjdelcerro
            this.btnColumnRemove.setEnabled(true);
423
        } else {
424
            this.btnColumnRemove.setEnabled(false);
425
        }
426
    }
427
428
    @Override
429
    public ImageIcon loadImage(String imageName) {
430
        String name = FilenameUtils.getBaseName(imageName);
431
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault();
432
        if (theme.exists(name)) {
433
            return theme.get(name);
434
        }
435
        URL url = this.getClass().getResource(name + ".png");
436
        if (url == null) {
437
            return null;
438
        }
439
        return new ImageIcon(url);
440
    }
441
442
    public static void selfRegister() {
443 46238 jjdelcerro
        IconTheme theme;
444
        try {
445
            theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
446
        } catch(ReferenceNotRegisteredException ex) {
447
            return;
448
        }
449 44644 jjdelcerro
        String[][] iconNames = new String[][]{
450 44713 jjdelcerro
            new String[]{"dalswing", "featuretype-column-add-arrow"},
451
            new String[]{"dalswing", "featuretype-column-remove-arrow"},
452
            new String[]{"dalswing", "featuretype-column-down"},
453
            new String[]{"dalswing", "featuretype-column-up"}
454 44644 jjdelcerro
        };
455
        for (String[] icon : iconNames) {
456 44712 jjdelcerro
            URL url = DefaultFeatureAttributesSelectionPanel.class.getResource(icon[1] + ".png");
457 44644 jjdelcerro
            theme.registerDefault("DALSwing", icon[0], icon[1], null, url);
458
        }
459
    }
460 45166 omartinez
461
    @Override
462
    public void allowCalculatedAttributes(boolean allowCalculatedAttributes) {
463
        this.allowCalculatedAttributes = allowCalculatedAttributes;
464
        this.btnBookmarks.setVisible(this.allowCalculatedAttributes);
465
        this.btnHistory.setVisible(this.allowCalculatedAttributes);
466
        this.btnExpression.setVisible(this.allowCalculatedAttributes);
467
        this.txtExpression.setVisible(this.allowCalculatedAttributes);
468 45155 omartinez
    }
469 46485 fdiaz
470
    private FeatureAttributeDescriptor getAttributeDescriptor(String name) {
471
        if(this.featureType == null){
472
            return null;
473
        }
474
        FeatureAttributeDescriptor attr = this.featureType.getAttributeDescriptorFromAll(name);
475
        if(attr != null){
476
            return attr;
477
        }
478
        if(this.extraColumns == null){
479
            return null;
480
        }
481
        for (EditableFeatureAttributeDescriptor extraColumn : this.extraColumns) {
482
            if(StringUtils.equalsIgnoreCase(extraColumn.getName(), name)){
483
                return extraColumn;
484
            }
485
        }
486
        return null;
487
    }
488 44644 jjdelcerro
}