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 @ 44753
History | View | Annotate | Download (13.2 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.featurequery; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.net.URL; |
6 |
import java.util.List; |
7 |
import javax.swing.DefaultComboBoxModel; |
8 |
import javax.swing.DefaultListModel; |
9 |
import javax.swing.ImageIcon; |
10 |
import javax.swing.JButton; |
11 |
import javax.swing.JComboBox; |
12 |
import javax.swing.JComponent; |
13 |
import javax.swing.JList; |
14 |
import javax.swing.ListModel; |
15 |
import javax.swing.event.ListSelectionEvent; |
16 |
import javax.swing.text.JTextComponent; |
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; |
22 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
23 |
import org.gvsig.fmap.dal.DALLocator; |
24 |
import org.gvsig.fmap.dal.exception.DataException; |
25 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
26 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
27 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
28 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
29 |
import org.gvsig.fmap.dal.feature.FeatureType; |
30 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
31 |
import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryCalculatedColumnsPanel; |
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; |
36 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
37 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
38 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
39 |
import org.gvsig.tools.util.LabeledValueImpl; |
40 |
|
41 |
/**
|
42 |
*
|
43 |
* @author jjdelcerro
|
44 |
*/
|
45 |
public class DefaultFeatureQueryCalculatedColumnsPanel |
46 |
extends DefaultFeatureQueryCalculatedColumnsPanelView
|
47 |
implements FeatureQueryCalculatedColumnsPanel {
|
48 |
|
49 |
|
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 |
} |
136 |
} |
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();
|
149 |
} |
150 |
|
151 |
@Override
|
152 |
public JComponent asJComponent() { |
153 |
return this; |
154 |
} |
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); |
168 |
} |
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 |
} |
180 |
} |
181 |
|
182 |
private void initComponents() { |
183 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
184 |
|
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 |
}); |
201 |
|
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 |
|
239 |
} |
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 |
} |
254 |
} |
255 |
|
256 |
@Override
|
257 |
public FeatureQuery fetch(FeatureQuery query) {
|
258 |
if (query == null) { |
259 |
return this.query.getCopy(); |
260 |
} |
261 |
return query;
|
262 |
} |
263 |
|
264 |
@Override
|
265 |
public FeatureQuery fetch() {
|
266 |
return this.fetch(null); |
267 |
} |
268 |
|
269 |
@Override
|
270 |
public void put(FeatureQuery query) { |
271 |
this.query.copyFrom(query);
|
272 |
addExtraColumns(this.query);
|
273 |
} |
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 |
} |
288 |
} |
289 |
|
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 |
} |
305 |
|
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)); |
317 |
} |
318 |
|
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 |
} |
325 |
|
326 |
} |
327 |
return false; |
328 |
} |
329 |
|
330 |
private void doRemove() { |
331 |
throw new UnsupportedOperationException("Not supported yet."); |
332 |
} |
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 |
} |
365 |
} |