svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / SelectByAttributes.java @ 45094
History | View | Annotate | Download (5.09 KB)
1 |
package org.gvsig.app.extension; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.FlowLayout; |
5 |
import java.awt.event.ActionEvent; |
6 |
import javax.swing.AbstractAction; |
7 |
import javax.swing.JButton; |
8 |
import javax.swing.JPanel; |
9 |
import org.gvsig.expressionevaluator.Expression; |
10 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
11 |
import org.gvsig.fmap.dal.DALLocator; |
12 |
import org.gvsig.fmap.dal.DataManager; |
13 |
import org.gvsig.fmap.dal.feature.Feature; |
14 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
15 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
16 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
17 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
18 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
19 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
20 |
import org.gvsig.tools.ToolsLocator; |
21 |
import org.gvsig.tools.i18n.I18nManager; |
22 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
23 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
24 |
import org.slf4j.Logger; |
25 |
import org.slf4j.LoggerFactory; |
26 |
|
27 |
/**
|
28 |
*
|
29 |
* @author jjdelcerro
|
30 |
*/
|
31 |
@SuppressWarnings("UseSpecificCatch") |
32 |
public class SelectByAttributes { |
33 |
|
34 |
private static final Logger LOGGER = LoggerFactory.getLogger(SelectByAttributes.class); |
35 |
|
36 |
public SelectByAttributes() {
|
37 |
|
38 |
} |
39 |
|
40 |
public void showWindow(String title, final FeatureStore store, WindowManager.MODE mode) { |
41 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
42 |
WindowManager windowManager = ToolsSwingLocator.getWindowManager(); |
43 |
DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager(); |
44 |
|
45 |
final JPanel panel = new JPanel(); |
46 |
panel.setLayout(new BorderLayout()); |
47 |
|
48 |
final JExpressionBuilder builder = dataSwingManager.createQueryFilterExpresion(store);
|
49 |
panel.add(builder.asJComponent(), BorderLayout.CENTER);
|
50 |
JPanel buttons = new JPanel(); |
51 |
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 4)); |
52 |
buttons.add(new JButton(new AbstractAction(i18n.getTranslation("_New_selection")) { |
53 |
@Override
|
54 |
public void actionPerformed(ActionEvent e) { |
55 |
setSelection(store, builder.getExpression()); |
56 |
} |
57 |
})); |
58 |
buttons.add(new JButton(new AbstractAction(i18n.getTranslation("_Add_to_selection")) { |
59 |
@Override
|
60 |
public void actionPerformed(ActionEvent e) { |
61 |
addToSelection(store, builder.getExpression()); |
62 |
} |
63 |
})); |
64 |
buttons.add(new JButton(new AbstractAction(i18n.getTranslation("_Filter_selection")) { |
65 |
@Override
|
66 |
public void actionPerformed(ActionEvent e) { |
67 |
filterSelection(store, builder.getExpression()); |
68 |
} |
69 |
})); |
70 |
buttons.add(new JButton(new AbstractAction(i18n.getTranslation("_Close")) { |
71 |
@Override
|
72 |
public void actionPerformed(ActionEvent e) { |
73 |
panel.setVisible(false);
|
74 |
} |
75 |
})); |
76 |
panel.add(buttons, BorderLayout.SOUTH);
|
77 |
|
78 |
windowManager.showWindow( |
79 |
panel, |
80 |
title, |
81 |
mode |
82 |
); |
83 |
|
84 |
} |
85 |
|
86 |
public void setSelection(FeatureStore store, Expression expression) { |
87 |
try {
|
88 |
DataManager manager = DALLocator.getDataManager(); |
89 |
|
90 |
FeatureSelection currentSelection = store.getFeatureSelection(); |
91 |
FeatureQuery query = store.createFeatureQuery(); |
92 |
query.setFilter(manager.createExpresion(expression)); |
93 |
FeatureSet selection = store.getFeatureSet(query); |
94 |
currentSelection.deselectAll(); |
95 |
currentSelection.select(selection); |
96 |
} catch (Exception ex) { |
97 |
LOGGER.warn("Can't build selecction from filter expression.", ex);
|
98 |
} |
99 |
} |
100 |
|
101 |
public void addToSelection(FeatureStore store, Expression expression) { |
102 |
try {
|
103 |
DataManager manager = DALLocator.getDataManager(); |
104 |
|
105 |
FeatureSelection currentSelection = store.getFeatureSelection(); |
106 |
FeatureQuery query = store.createFeatureQuery(); |
107 |
query.setFilter(manager.createExpresion(expression)); |
108 |
FeatureSet selection = store.getFeatureSet(query); |
109 |
currentSelection.select(selection); |
110 |
} catch (Exception ex) { |
111 |
LOGGER.warn("Can't build selecction from filter expression.", ex);
|
112 |
} |
113 |
} |
114 |
|
115 |
public void filterSelection(FeatureStore store, Expression expression) { |
116 |
try {
|
117 |
DataManager manager = DALLocator.getDataManager(); |
118 |
|
119 |
FeatureSelection currentSelection = store.getFeatureSelection(); |
120 |
FeatureQuery query = store.createFeatureQuery(); |
121 |
query.setFilter(manager.createExpresion(expression)); |
122 |
FeatureSet set = store.getFeatureSet(query); |
123 |
FeatureSelection newSelection = store.createFeatureSelection(); |
124 |
for (Feature feature : set) {
|
125 |
if( currentSelection.isSelected(feature) ) {
|
126 |
newSelection.select(feature); |
127 |
} |
128 |
} |
129 |
store.setSelection(newSelection); |
130 |
} catch (Exception ex) { |
131 |
LOGGER.warn("Can't build selecction from filter expression.", ex);
|
132 |
} |
133 |
} |
134 |
|
135 |
} |