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 |
package org.gvsig.fmap.dal.swing.impl.featuretype; |
---|---|
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.ArrayList; |
8 |
import java.util.Collections; |
9 |
import java.util.List; |
10 |
import javax.swing.DefaultListCellRenderer; |
11 |
import javax.swing.ImageIcon; |
12 |
import javax.swing.JComponent; |
13 |
import javax.swing.ListCellRenderer; |
14 |
import javax.swing.event.DocumentEvent; |
15 |
import javax.swing.event.DocumentListener; |
16 |
import javax.swing.event.ListSelectionEvent; |
17 |
import org.apache.commons.io.FilenameUtils; |
18 |
import org.apache.commons.lang.StringUtils; |
19 |
import org.gvsig.expressionevaluator.Expression; |
20 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
21 |
import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
22 |
import org.gvsig.fmap.dal.DALLocator; |
23 |
import org.gvsig.fmap.dal.DataTypes; |
24 |
import org.gvsig.fmap.dal.exception.DataException; |
25 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
26 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
27 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
28 |
import org.gvsig.fmap.dal.feature.FeatureType; |
29 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
30 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
31 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
32 |
import org.gvsig.tools.swing.api.FilteredListController; |
33 |
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 |
import org.gvsig.fmap.dal.swing.featuretype.FeatureAttributesSelectionPanel; |
38 |
import org.gvsig.tools.locator.ReferenceNotRegisteredException; |
39 |
import org.gvsig.tools.util.LabeledValue; |
40 |
import org.gvsig.tools.util.LabeledValueImpl; |
41 |
|
42 |
/**
|
43 |
*
|
44 |
* @author jjdelcerro
|
45 |
*/
|
46 |
public class DefaultFeatureAttributesSelectionPanel |
47 |
extends DefaultFeatureAttributesSelectionPanelView
|
48 |
implements FeatureAttributesSelectionPanel
|
49 |
{ |
50 |
|
51 |
private static final String ATTR_IS_EXPRESSION = "DefaultFeatureAttributesSelectionPanel_isExpression"; |
52 |
|
53 |
private FeatureType featureType;
|
54 |
private final ActionListenerSupport actionListenerSupport; |
55 |
private FilteredListController availableFieldsController;
|
56 |
private final List<FeatureAttributeDescriptor> selecteds; |
57 |
private ExpressionPickerController expressionPicker = null; |
58 |
private boolean allowCalculatedAttributes = false; |
59 |
private List<EditableFeatureAttributeDescriptor> extraColumns; |
60 |
|
61 |
public DefaultFeatureAttributesSelectionPanel() {
|
62 |
this.actionListenerSupport = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
|
63 |
this.selecteds = new ArrayList<>(); |
64 |
this.initComponents();
|
65 |
} |
66 |
|
67 |
private void initComponents() { |
68 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
69 |
ListCellRenderer renderer = DALSwingLocator.getDataSwingManager().createDefaultFeatureAttributeListCellRenderer();
|
70 |
// DefaultListCellRenderer renderer = new DefaultListCellRenderer();
|
71 |
this.lstAvailableColumns.setCellRenderer(renderer);
|
72 |
this.lstSelectedColumns.setCellRenderer(renderer);
|
73 |
|
74 |
this.lstAvailableColumns.addListSelectionListener((ListSelectionEvent e) -> { |
75 |
if( e.getValueIsAdjusting() ) {
|
76 |
return;
|
77 |
} |
78 |
doSelectAvailableColumn(); |
79 |
}); |
80 |
this.lstSelectedColumns.addListSelectionListener((ListSelectionEvent e) -> { |
81 |
if( e.getValueIsAdjusting() ) {
|
82 |
return;
|
83 |
} |
84 |
doSelectColumn(); |
85 |
}); |
86 |
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 |
this.btnColumnAdd.addActionListener((ActionEvent e) -> { |
101 |
doColumnAdd(); |
102 |
}); |
103 |
this.btnColumnRemove.addActionListener((ActionEvent e) -> { |
104 |
doColumnRemove(); |
105 |
}); |
106 |
this.btnColumnDown.addActionListener((ActionEvent e) -> { |
107 |
doColumnDown(); |
108 |
}); |
109 |
this.btnColumnUp.addActionListener((ActionEvent e) -> { |
110 |
doColumnUp(); |
111 |
}); |
112 |
this.availableFieldsController = toolsSwingManager.createFilteredListController(
|
113 |
lstAvailableColumns, |
114 |
txtColumnsFilter, |
115 |
btnColumnsFilter |
116 |
); |
117 |
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 |
@Override
|
128 |
public List<String> getSelectedNames() { |
129 |
List<String> names = new ArrayList<>(); |
130 |
for (FeatureAttributeDescriptor selected : selecteds) {
|
131 |
names.add(selected.getName()); |
132 |
} |
133 |
return names;
|
134 |
} |
135 |
|
136 |
@Override
|
137 |
public void setSelectedNames(List<String> names) { |
138 |
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 |
} |
151 |
updateControls(); |
152 |
} |
153 |
|
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 |
|
163 |
private void doSelectAvailableColumn() { |
164 |
if( this.lstAvailableColumns.getSelectedIndex()>=0 || !this.expressionPicker.isEmpty()) { |
165 |
this.btnColumnAdd.setEnabled(true); |
166 |
return;
|
167 |
} |
168 |
this.btnColumnAdd.setEnabled(false); |
169 |
} |
170 |
|
171 |
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 |
} |
182 |
this.btnColumnRemove.setEnabled(false); |
183 |
this.btnColumnDown.setEnabled(false); |
184 |
this.btnColumnUp.setEnabled(false); |
185 |
} |
186 |
|
187 |
private void doColumnUp() { |
188 |
LabeledValue attr = (LabeledValue) this.lstSelectedColumns.getSelectedValue();
|
189 |
if( attr == null ) { |
190 |
return;
|
191 |
} |
192 |
int n = this.selecteds.indexOf(attr.getLabel()); |
193 |
if( n<1 ) { |
194 |
return;
|
195 |
} |
196 |
Collections.swap(selecteds, n, n-1); |
197 |
this.lstSelectedColumns.setSelectedIndex(n-1); |
198 |
this.updateControls();
|
199 |
} |
200 |
|
201 |
private void doColumnDown() { |
202 |
LabeledValue attr = (LabeledValue) this.lstSelectedColumns.getSelectedValue();
|
203 |
if( attr == null ) { |
204 |
return;
|
205 |
} |
206 |
int n = this.selecteds.indexOf(attr.getLabel()); |
207 |
if( n<0 || n>=this.selecteds.size()-1 ) { |
208 |
return;
|
209 |
} |
210 |
Collections.swap(selecteds, n, n+1); |
211 |
this.lstSelectedColumns.setSelectedIndex(n+1); |
212 |
this.updateControls();
|
213 |
} |
214 |
|
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 |
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 |
return false; |
238 |
} |
239 |
|
240 |
private void doColumnRemove() { |
241 |
FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstSelectedColumns.getSelectedValue();
|
242 |
if( attr == null ) { |
243 |
return;
|
244 |
} |
245 |
int n = this.selecteds.indexOf(attr); |
246 |
if( n<0 ) { |
247 |
return;
|
248 |
} |
249 |
if(attr.getTags().getBoolean(ATTR_IS_EXPRESSION,false)){ |
250 |
Expression toExp = ExpressionEvaluatorLocator.getManager().createExpression();
|
251 |
toExp.setPhrase(attr.getLabel()); |
252 |
this.expressionPicker.set(toExp);
|
253 |
} |
254 |
this.selecteds.remove(n);
|
255 |
this.updateControls();
|
256 |
} |
257 |
|
258 |
private void doColumnAdd() { |
259 |
|
260 |
if (this.expressionPicker.isEmpty()) { |
261 |
FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstAvailableColumns.getSelectedValue();
|
262 |
if( attr == null ) { |
263 |
return;
|
264 |
} |
265 |
int n = this.selecteds.indexOf(attr); |
266 |
if( n>=0 ) { |
267 |
return;
|
268 |
} |
269 |
this.selecteds.add(attr);
|
270 |
} else {
|
271 |
if (this.expressionPicker.isValid()) { |
272 |
Expression exp = this.expressionPicker.get(); |
273 |
String expressionField = exp.getPhrase();
|
274 |
FeatureAttributeDescriptor attr = createAttributeFromExpression(expressionField); |
275 |
for (FeatureAttributeDescriptor selected : this.selecteds) { |
276 |
if(StringUtils.equals(attr.getName(),selected.getName())){
|
277 |
return;
|
278 |
} |
279 |
} |
280 |
this.selecteds.add(attr);
|
281 |
this.expressionPicker.set(null); |
282 |
} |
283 |
} |
284 |
this.updateControls();
|
285 |
} |
286 |
|
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 |
|
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 |
if( !this.selecteds.isEmpty() ) { |
307 |
return;
|
308 |
} |
309 |
this.updateControls();
|
310 |
} |
311 |
@Override
|
312 |
public void setExtraColumns(List<EditableFeatureAttributeDescriptor> extraColumns) { |
313 |
this.extraColumns = extraColumns;
|
314 |
this.updateControls();
|
315 |
} |
316 |
|
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 |
|
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 |
int indexAvailables = this.lstAvailableColumns.getSelectedIndex(); |
369 |
int indexSelecteds = this.lstSelectedColumns.getSelectedIndex(); |
370 |
FilteredListModel modelAvailables = ToolsSwingLocator.getToolsSwingManager().createFilteredListModel(); |
371 |
FilteredListModel modelSelecteds = ToolsSwingLocator.getToolsSwingManager().createFilteredListModel(); |
372 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
373 |
for (FeatureAttributeDescriptor attr : featureType) {
|
374 |
if (!selectedContains(attr)) {
|
375 |
modelAvailables.addElement(attr); |
376 |
} |
377 |
} |
378 |
List<EditableFeatureAttributeDescriptor> columns = featureType.getExtraColumns().getColumns();
|
379 |
if (columns!=null && !columns.isEmpty()) { |
380 |
for (EditableFeatureAttributeDescriptor extraCol : columns) {
|
381 |
if (!selectedContains(extraCol)) {
|
382 |
modelAvailables.addElement(extraCol); |
383 |
} |
384 |
} |
385 |
} |
386 |
|
387 |
if (this.extraColumns!=null && !this.extraColumns.isEmpty()) { |
388 |
for (FeatureAttributeDescriptor extraCol : this.extraColumns) { |
389 |
if (!selectedContains(extraCol)) {
|
390 |
modelAvailables.addElement(extraCol); |
391 |
} |
392 |
} |
393 |
} |
394 |
|
395 |
modelAvailables.setFilter(this.txtColumnsFilter.getText());
|
396 |
modelAvailables.sort(true);
|
397 |
for (FeatureAttributeDescriptor attr : this.selecteds) { |
398 |
modelSelecteds.addElement(attr); |
399 |
} |
400 |
modelAvailables.sort(true);
|
401 |
this.lstAvailableColumns.setModel(modelAvailables);
|
402 |
this.lstSelectedColumns.setModel(modelSelecteds);
|
403 |
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 |
if(!this.expressionPicker.isEmpty()) { |
411 |
this.btnColumnAdd.setEnabled(true); |
412 |
} else {
|
413 |
this.btnColumnAdd.setEnabled(false); |
414 |
} |
415 |
} |
416 |
|
417 |
if( indexSelecteds >= 0 && modelSelecteds.getSize()>0 ) { |
418 |
if( indexSelecteds >= modelSelecteds.getSize() ) {
|
419 |
indexSelecteds = modelSelecteds.getSize()-1;
|
420 |
} |
421 |
this.lstSelectedColumns.setSelectedIndex(indexSelecteds);
|
422 |
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 |
IconTheme theme; |
444 |
try {
|
445 |
theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
446 |
} catch(ReferenceNotRegisteredException ex) {
|
447 |
return;
|
448 |
} |
449 |
String[][] iconNames = new String[][]{ |
450 |
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 |
}; |
455 |
for (String[] icon : iconNames) { |
456 |
URL url = DefaultFeatureAttributesSelectionPanel.class.getResource(icon[1] + ".png"); |
457 |
theme.registerDefault("DALSwing", icon[0], icon[1], null, url); |
458 |
} |
459 |
} |
460 |
|
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 |
} |
469 |
|
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 |
} |