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