Revision 44753 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

View differences:

DefaultFeatureQueryCalculatedColumnsPanel.java
1 1
package org.gvsig.fmap.dal.swing.impl.featurequery;
2 2

  
3 3
import java.awt.Dimension;
4
import java.awt.event.ActionEvent;
4 5
import java.net.URL;
6
import java.util.List;
7
import javax.swing.DefaultComboBoxModel;
8
import javax.swing.DefaultListModel;
5 9
import javax.swing.ImageIcon;
6 10
import javax.swing.JButton;
7 11
import javax.swing.JComboBox;
8 12
import javax.swing.JComponent;
13
import javax.swing.JList;
14
import javax.swing.ListModel;
9 15
import javax.swing.event.ListSelectionEvent;
10 16
import javax.swing.text.JTextComponent;
11 17
import org.apache.commons.io.FilenameUtils;
18
import org.apache.commons.lang.StringUtils;
19
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator;
20
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager;
21
import org.gvsig.expressionevaluator.swing.ExpressionPickerController;
12 22
import org.gvsig.expressionevaluator.swing.JExpressionBuilder;
23
import org.gvsig.fmap.dal.DALLocator;
13 24
import org.gvsig.fmap.dal.exception.DataException;
25
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression;
14 26
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
15 27
import org.gvsig.fmap.dal.feature.FeatureQuery;
16 28
import org.gvsig.fmap.dal.feature.FeatureStore;
......
18 30
import org.gvsig.fmap.dal.swing.DALSwingLocator;
19 31
import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryCalculatedColumnsPanel;
20 32
import org.gvsig.fmap.dal.swing.impl.featuretype.FeatureAttributeListCellRenderer;
33
import org.gvsig.tools.dataTypes.DataType;
34
import org.gvsig.tools.swing.api.FilteredListModel;
35
import org.gvsig.tools.swing.api.ListElement;
21 36
import org.gvsig.tools.swing.api.ToolsSwingLocator;
22 37
import org.gvsig.tools.swing.api.ToolsSwingManager;
23 38
import org.gvsig.tools.swing.icontheme.IconTheme;
39
import org.gvsig.tools.util.LabeledValueImpl;
24 40

  
25 41
/**
26 42
 *
......
30 46
        extends DefaultFeatureQueryCalculatedColumnsPanelView
31 47
        implements FeatureQueryCalculatedColumnsPanel {
32 48

  
33
  private static class ColumnController {
34 49
    
35
    public ColumnController(
36
            JTextComponent btnName,
37
            JComboBox cboDataType,
38
            JButton btnDataType,
39
            JTextComponent txtExpression,
40
            JButton btnExpression,
41
            JButton btnExpressionHistory,
42
            JButton btnExpressionBookmarks
43
      ) {
44
      
50
    private EditableFeatureAttributeDescriptor actualEditableAttribute;
51
    private DefaultListModel lstAttributesModel;
52

  
53
    private static class ColumnController {
54

  
55
        private final JTextComponent txtName;
56
        private final JComboBox cboDataType;
57
        private final JButton btnDataType;
58
        private final JButton btnExpressionBookmarks;
59
        private final JButton btnExpressionHistory;
60
        private final JButton btnExpression;
61
        private final JTextComponent txtExpression;
62
        private ExpressionPickerController expPicker;
63
        private EditableFeatureAttributeDescriptor attr;
64

  
65
        public ColumnController(
66
                JTextComponent txtName,
67
                JComboBox cboDataType,
68
                JButton btnDataType,
69
                JTextComponent txtExpression,
70
                JButton btnExpression,
71
                JButton btnExpressionHistory,
72
                JButton btnExpressionBookmarks
73
        ) {
74
            this.txtName = txtName;
75
            this.cboDataType = cboDataType;
76
            this.btnDataType = btnDataType;
77
            this.txtExpression = txtExpression;
78
            this.btnExpression = btnExpression;
79
            this.btnExpressionHistory = btnExpressionHistory;
80
            this.btnExpressionBookmarks = btnExpressionBookmarks;
81
            this.initComponents();
82
            this.setEnabled(false);
83
        }
84

  
85
        public void initComponents() {
86
            List<DataType> dataTypes = DALLocator.getDataManager().getDataTypes();
87
            DefaultComboBoxModel<LabeledValueImpl<DataType>> modelColumn = new DefaultComboBoxModel<LabeledValueImpl<DataType>>();
88
            for (DataType dataType : dataTypes) {
89
                LabeledValueImpl<DataType> labeled = new LabeledValueImpl<DataType>(dataType.getName(), dataType);
90
                modelColumn.addElement(labeled);
91
            }
92
            this.cboDataType.setModel(modelColumn);
93

  
94
            ExpressionEvaluatorSwingManager managerExpSwing = ExpressionEvaluatorSwingLocator.getManager();
95
            this.expPicker = managerExpSwing.createExpressionPickerController(
96
                    txtExpression,
97
                    btnExpression,
98
                    btnExpressionBookmarks,
99
                    btnExpressionHistory
100
            );
101

  
102
        }
103

  
104
        public void setEnabled(boolean enabled) {
105
            this.txtName.setEnabled(enabled);
106
            this.expPicker.setEnabled(enabled);
107
            this.cboDataType.setEnabled(enabled);
108

  
109
        }
110

  
111
        public void clean() {
112
            this.txtName.setText("");
113
            this.expPicker.set(null);
114
            this.cboDataType.setSelectedIndex(0);
115

  
116
        }
117

  
118
        public void put(EditableFeatureAttributeDescriptor attr) {
119
            this.attr = attr;
120
            this.txtName.setName(attr.getName());
121
            FeatureAttributeEmulatorExpression emu = (FeatureAttributeEmulatorExpression) attr.getFeatureAttributeEmulator();
122
            if (emu!=null) {
123
                this.expPicker.set(emu.getExpression());
124
            }
125
            this.cboDataType.setSelectedItem(attr.getDataType());
126

  
127
        }
128

  
129
        public EditableFeatureAttributeDescriptor fetch(EditableFeatureAttributeDescriptor attr) {
130
            this.attr.setName(this.txtName.getText());
131
            this.attr.setFeatureAttributeEmulator(this.expPicker.get());
132
            LabeledValueImpl<DataType> dataTypeValue = (LabeledValueImpl<DataType>) this.cboDataType.getSelectedItem();
133
            this.attr.setDataType(dataTypeValue.getValue());
134
            return null;
135
        }
45 136
    }
46
    
47
    public void setEnabled(boolean enabled) {
48
      
137

  
138
    private FeatureStore store;
139
    private FeatureType featureType;
140

  
141
    private FeatureQuery query;
142

  
143
    private ColumnController colummController;
144
    private JExpressionBuilder pckExpression;
145
    private static String COLUMN_DEFAULT_NAME = "Field";
146

  
147
    public DefaultFeatureQueryCalculatedColumnsPanel() {
148
        this.initComponents();
49 149
    }
50
    
51
    public void clean() {
52
      
150

  
151
    @Override
152
    public JComponent asJComponent() {
153
        return this;
53 154
    }
54
    
55
    public void put(EditableFeatureAttributeDescriptor attr) {
56
      
155

  
156
    @Override
157
    public ImageIcon loadImage(String imageName) {
158
        String name = FilenameUtils.getBaseName(imageName);
159
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault();
160
        if (theme.exists(name)) {
161
            return theme.get(name);
162
        }
163
        URL url = this.getClass().getResource(name + ".png");
164
        if (url == null) {
165
            return null;
166
        }
167
        return new ImageIcon(url);
57 168
    }
58
    
59
    public EditableFeatureAttributeDescriptor fetch(EditableFeatureAttributeDescriptor attr) {
60
      return null;
169

  
170
    @Override
171
    public void setStore(FeatureStore store) {
172
        try {
173
            this.featureType = store.getDefaultFeatureType();
174
            this.store = store;
175
            this.query = store.createFeatureQuery();
176
            this.pckExpression = DALSwingLocator.getManager().createQueryFilterExpresion(store);
177
        } catch (DataException ex) {
178
            throw new RuntimeException("Can't assign store", ex);
179
        }
61 180
    }
62
  }
63
  
64
  private FeatureStore store;
65
  private FeatureType featureType;
66 181

  
67
  private FeatureQuery query;
68
  
69
  private ColumnController colummController;
70
  private JExpressionBuilder pckExpression;
71
  
72
  public DefaultFeatureQueryCalculatedColumnsPanel() {
73
    this.initComponents();
74
  }
182
    private void initComponents() {
183
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
75 184

  
76
  @Override
77
  public JComponent asJComponent() {
78
    return this;
79
  }
185
        this.colummController = new ColumnController(
186
                this.txtColumnName,
187
                this.cboColumnDataType,
188
                this.btnColumnDataType,
189
                this.txtColumnExpression,
190
                this.btnColumnExpression,
191
                this.btnColumnExpressionHistory,
192
                this.btnColumnExpressionBookmarks
193
        );
194
//        this.lstAttributes.setCellRenderer(new FeatureAttributeListCellRenderer());
195
        this.lstAttributes.addListSelectionListener((ListSelectionEvent e) -> {
196
            if (e.getValueIsAdjusting()) {
197
                return;
198
            }
199
      doSelectAttribute();
200
        });
80 201

  
81
  @Override
82
  public ImageIcon loadImage(String imageName) {
83
    String name = FilenameUtils.getBaseName(imageName);
84
    IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault();
85
    if (theme.exists(name)) {
86
      return theme.get(name);
202
        Dimension sz = this.getPreferredSize();
203
        if (sz.width < DEFAULT_WIDTH) {
204
            sz.width = DEFAULT_WIDTH;
205
        }
206
        if (sz.height < DEFAULT_HEIGHT) {
207
            sz.height = DEFAULT_HEIGHT;
208
        }
209
        this.setPreferredSize(sz);
210

  
211
        this.lstAttributesModel = new DefaultListModel<ListElement<EditableFeatureAttributeDescriptor>>();
212
//        this.lstAttributesModel = ToolsSwingLocator.getToolsSwingManager().createFilteredListModel();
213
        this.lstAttributes.setModel(lstAttributesModel);
214

  
215
        this.btnAdd.addActionListener((ActionEvent e) -> {
216
            doAdd();
217
        });
218

  
219
        this.btnRemove.addActionListener((ActionEvent e) -> {
220
            doRemove();
221
        });
222

  
223
        this.btnUp.addActionListener((ActionEvent e) -> {
224
            doUp(lstAttributes);
225
        });
226

  
227
        this.btnDown.addActionListener((ActionEvent e) -> {
228
            doDown(lstAttributes);
229
        });
230

  
231
        this.btnApplyChanges.addActionListener((ActionEvent e) -> {
232
            doApplyChanges();
233
        });
234

  
235
        this.btnColumnMore.addActionListener((ActionEvent e) -> {
236
            //
237
        });
238

  
87 239
    }
88
    URL url = this.getClass().getResource(name + ".png");
89
    if (url == null) {
90
      return null;
240

  
241
    private void doSelectAttribute() {
242
        EditableFeatureAttributeDescriptor value = ((ListElement<EditableFeatureAttributeDescriptor>) this.lstAttributes.getSelectedValue()).getValue();
243
        if (value == null) {
244
            this.colummController.clean();
245
            this.colummController.setEnabled(false);
246
            this.actualEditableAttribute = null;
247

  
248
        } else {
249
            //EditableFeatureAttributeDescriptor value = node.getValue();
250
            this.actualEditableAttribute = value;
251
            colummController.put(value);
252
            colummController.setEnabled(true);
253
        }
91 254
    }
92
    return new ImageIcon(url);
93
  }
94 255

  
95
  @Override
96
  public void setStore(FeatureStore store) {
97
    try {
98
      this.featureType = store.getDefaultFeatureType();
99
      this.store = store;
100
      this.query = store.createFeatureQuery();
101
      this.pckExpression = DALSwingLocator.getManager().createQueryFilterExpresion(store);
102
    } catch (DataException ex) {
103
      throw new RuntimeException("Can't assign store", ex);
256
    @Override
257
    public FeatureQuery fetch(FeatureQuery query) {
258
        if (query == null) {
259
            return this.query.getCopy();
260
        }
261
        return query;
104 262
    }
105
  }
106 263

  
107
  private void initComponents() {
108
    ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
109
    
110
    this.colummController = new ColumnController(
111
            this.txtColumnName,
112
            this.cboColumnDataType, 
113
            this.btnColumnDataType,
114
            this.txtColumnExpression, 
115
            this.btnColumnExpression, 
116
            this.btnColumnExpressionHistory, 
117
            this.btnColumnExpressionBookmarks 
118
    );
119
    this.lstAttributes.setCellRenderer(new FeatureAttributeListCellRenderer());
120
    this.lstAttributes.addListSelectionListener((ListSelectionEvent e) -> {
121
      if (e.getValueIsAdjusting()) {
122
        return;
123
      }
124
      doSelectAttribute();
125
    });
264
    @Override
265
    public FeatureQuery fetch() {
266
        return this.fetch(null);
267
    }
126 268

  
127
    Dimension sz = this.getPreferredSize();
128
    if (sz.width < DEFAULT_WIDTH) {
129
      sz.width = DEFAULT_WIDTH;
269
    @Override
270
    public void put(FeatureQuery query) {
271
        this.query.copyFrom(query);
272
        addExtraColumns(this.query);
130 273
    }
131
    if (sz.height < DEFAULT_HEIGHT) {
132
      sz.height = DEFAULT_HEIGHT;
274

  
275
    private void addExtraColumns(FeatureQuery query) {
276
        List<EditableFeatureAttributeDescriptor> cols = query.getExtraColumn().getColumns();
277
        if (cols==null || cols.isEmpty()) {
278
            return;
279
        }
280
//        model = this.lstAttributes.getModel();
281
        for (EditableFeatureAttributeDescriptor col : cols) {
282
            //LabeledValueImpl<EditableFeatureAttributeDescriptor> lf = new LabeledValueImpl<EditableFeatureAttributeDescriptor>(col.getLabel(), col);
283
            this.lstAttributesModel.addElement(col);
284
        }
285
        if (this.lstAttributesModel.getSize() > 0) {
286
            this.lstAttributes.setSelectedIndex(0);
287
        }
133 288
    }
134
    this.setPreferredSize(sz);
135
  }
136 289

  
137
  private void doSelectAttribute() {
138
  }
290
    public static void selfRegister() {
291
        String[][] iconNames = new String[][]{
292
            new String[]{"dalswing", "common-applychanges"},
293
            new String[]{"dalswing", "common-more"},
294
            new String[]{"dalswing", "featurequery-column-add"},
295
            new String[]{"dalswing", "featurequery-column-remove"},
296
            new String[]{"dalswing", "featurequery-column-down"},
297
            new String[]{"dalswing", "featurequery-column-up"}
298
        };
299
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
300
        for (String[] icon : iconNames) {
301
            URL url = DefaultFeatureQueryOrderPanel.class.getResource(icon[1] + ".png");
302
            theme.registerDefault("DALSwing", icon[0], icon[1], null, url);
303
        }
304
    }
139 305

  
140
  @Override
141
  public FeatureQuery fetch(FeatureQuery query) {
142
    if( query == null ) {
143
      return this.query.getCopy();
306
    private void doAdd() {
307
        EditableFeatureAttributeDescriptor newAttr = DALLocator.getDataManager().createFeatureAttributeDescriptor();
308
        newAttr.setName(COLUMN_DEFAULT_NAME);
309
        int id = 0;
310
        while (checkIfValueExistInModel(newAttr.getName(), this.lstAttributesModel)) {
311
            String newName = COLUMN_DEFAULT_NAME + "_" + id;
312
            id+=1;
313
            newAttr.setName(newName);
314
        }
315
        this.colummController.put(newAttr);
316
        this.lstAttributesModel.addElement(new ListElement(newAttr.getName(), newAttr));
144 317
    }
145
    return query;
146
  }
147 318

  
148
  @Override
149
  public FeatureQuery fetch() {
150
    return this.fetch(null);
151
  }
319
    private boolean checkIfValueExistInModel(String name, ListModel<ListElement<EditableFeatureAttributeDescriptor>> model) {
320
        for (int i = 0; i < model.getSize(); i++) {
321
            EditableFeatureAttributeDescriptor element = model.getElementAt(i).getValue();
322
            if (StringUtils.equals(element.getLabel(), name)) {
323
                return true;
324
            }
152 325

  
153
  @Override
154
  public void put(FeatureQuery query) {
155
    this.query.copyFrom(query);
156
  }
326
        }
327
        return false;
328
    }
157 329

  
158
  public static void selfRegister() {
159
    String[][] iconNames = new String[][]{
160
      new String[]{"dalswing", "common-applychanges"},
161
      new String[]{"dalswing", "common-more"},
162
      new String[]{"dalswing", "featurequery-column-add"},
163
      new String[]{"dalswing", "featurequery-column-remove"},
164
      new String[]{"dalswing", "featurequery-column-down"},
165
      new String[]{"dalswing", "featurequery-column-up"}
166
    };
167
    IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
168
    for (String[] icon : iconNames) {
169
      URL url = DefaultFeatureQueryOrderPanel.class.getResource(icon[1] + ".png");
170
      theme.registerDefault("DALSwing", icon[0], icon[1], null, url);
330
    private void doRemove() {
331
        throw new UnsupportedOperationException("Not supported yet.");
171 332
    }
172
  }
173 333

  
334
    private void doApplyChanges() {
335
        this.colummController.fetch(this.actualEditableAttribute);
336
    }
337

  
338
    private void doUp(JList lstColumns) {
339
        int moveMe = lstColumns.getSelectedIndex();
340
        if (moveMe != 0) {
341
            //not already at top
342
            swap(lstColumns, moveMe, moveMe - 1);
343
            lstColumns.setSelectedIndex(moveMe - 1);
344
            lstColumns.ensureIndexIsVisible(moveMe - 1);
345
        }
346
    }
347

  
348
    private void doDown(JList lstColumns) {
349
        int moveMe = lstColumns.getSelectedIndex();
350
        if (moveMe != lstColumns.getModel().getSize() - 1) {
351
            //not already at bottom
352
            swap(lstColumns, moveMe, moveMe + 1);
353
            lstColumns.setSelectedIndex(moveMe + 1);
354
            lstColumns.ensureIndexIsVisible(moveMe + 1);
355
        }
356
    }
357

  
358
    private void swap(JList lstColumns, int a, int b) {
359
        DefaultListModel model = (DefaultListModel) lstColumns.getModel();
360
        Object aObject = model.getElementAt(a);
361
        Object bObject = model.getElementAt(b);
362
        model.set(a, bObject);
363
        model.set(b, aObject);
364
    }
174 365
}

Also available in: Unified diff