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 / searchpanel / SearchConditionPanelSimplified.java @ 44764
History | View | Annotate | Download (11.7 KB)
1 | 44707 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.searchpanel; |
---|---|---|---|
2 | |||
3 | import java.awt.event.ActionEvent; |
||
4 | import java.util.ArrayList; |
||
5 | import java.util.List; |
||
6 | import javax.swing.JButton; |
||
7 | import javax.swing.JComboBox; |
||
8 | 44729 | omartinez | import javax.swing.JComponent; |
9 | 44707 | jjdelcerro | import javax.swing.JLabel; |
10 | 44750 | jjdelcerro | import javax.swing.event.ChangeListener; |
11 | 44707 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
12 | import org.gvsig.expressionevaluator.Code; |
||
13 | import org.gvsig.expressionevaluator.Expression; |
||
14 | import org.gvsig.expressionevaluator.ExpressionBuilder; |
||
15 | import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_AND; |
||
16 | import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_OR; |
||
17 | import org.gvsig.expressionevaluator.ExpressionUtils; |
||
18 | import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
||
19 | import org.gvsig.fmap.dal.complements.Search; |
||
20 | import org.gvsig.fmap.dal.exception.DataException; |
||
21 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
22 | import org.gvsig.fmap.dal.swing.DALSwingLocator; |
||
23 | import org.gvsig.fmap.dal.swing.DataSwingManager; |
||
24 | import org.gvsig.fmap.dal.swing.searchpanel.SearchConditionPanel; |
||
25 | import org.gvsig.tools.ToolsLocator; |
||
26 | import org.gvsig.tools.i18n.I18nManager; |
||
27 | 44750 | jjdelcerro | import org.gvsig.tools.swing.api.ChangeListenerHelper; |
28 | 44707 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
29 | import org.gvsig.tools.swing.api.windowmanager.Dialog; |
||
30 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
31 | import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
||
32 | import org.slf4j.Logger; |
||
33 | import org.slf4j.LoggerFactory; |
||
34 | |||
35 | /**
|
||
36 | *
|
||
37 | * @author jjdelcerro
|
||
38 | */
|
||
39 | public class SearchConditionPanelSimplified implements SearchConditionPanel { |
||
40 | |||
41 | private static final Logger LOGGER = LoggerFactory.getLogger(SearchConditionPanelSimplified.class); |
||
42 | |||
43 | 44729 | omartinez | @Override
|
44 | public JComponent asJComponent() { |
||
45 | return null; |
||
46 | } |
||
47 | |||
48 | 44712 | jjdelcerro | private static class SearchConditionPanelAdvancedFactory |
49 | implements SearchConditionPanelFactory
|
||
50 | { |
||
51 | |||
52 | @Override
|
||
53 | public boolean isApplicable(FeatureStore store) { |
||
54 | return true; |
||
55 | } |
||
56 | |||
57 | @Override
|
||
58 | public SearchConditionPanel create(Object... os) { |
||
59 | return null; |
||
60 | } |
||
61 | |||
62 | @Override
|
||
63 | public String getName() { |
||
64 | return "Simplified"; |
||
65 | } |
||
66 | |||
67 | } |
||
68 | |||
69 | private static final SearchConditionPanelFactory FACTORY = new SearchConditionPanelAdvancedFactory(); |
||
70 | |||
71 | 44707 | jjdelcerro | private final FeatureStore store; |
72 | 44713 | jjdelcerro | private final List<SearchConditionFieldController> searchFields; |
73 | 44707 | jjdelcerro | private int maxSearhFields = 4; |
74 | private String accumulatedFilter; |
||
75 | private final JButton btnAddAccumulatedFilter; |
||
76 | private final JButton btnRemoveAccumulatedFilter; |
||
77 | private final JButton btnViewAccumulatedFilter; |
||
78 | private final String baseToolTip; |
||
79 | 44712 | jjdelcerro | private SearchConditionPanelFactory factory;
|
80 | 44750 | jjdelcerro | private final ChangeListenerHelper changeListenerHelper; |
81 | 44707 | jjdelcerro | |
82 | public SearchConditionPanelSimplified(
|
||
83 | FeatureStore store, |
||
84 | JButton btnAddToAccumulatedFilter,
|
||
85 | JButton btnRemoveAccumulatedFilter,
|
||
86 | JButton btnViewAccumulatedFilter,
|
||
87 | JLabel lblField1,
|
||
88 | JLabel lblExtraFields1,
|
||
89 | JLabel lblRelationalOperator1,
|
||
90 | JComboBox cboValue1,
|
||
91 | JLabel lblLogicalOperators1,
|
||
92 | JLabel lblField2,
|
||
93 | JLabel lblExtraFields2,
|
||
94 | JLabel lblRelationalOperator2,
|
||
95 | JComboBox cboValue2,
|
||
96 | JLabel lblLogicalOperators2,
|
||
97 | JLabel lblField3,
|
||
98 | JLabel lblExtraFields3,
|
||
99 | JLabel lblRelationalOperator3,
|
||
100 | JComboBox cboValue3,
|
||
101 | JLabel lblLogicalOperators3,
|
||
102 | JLabel lblField4,
|
||
103 | JLabel lblExtraFields4,
|
||
104 | JLabel lblRelationalOperator4,
|
||
105 | JComboBox cboValue4,
|
||
106 | JLabel lblLogicalOperators4
|
||
107 | ) { |
||
108 | 44750 | jjdelcerro | this.changeListenerHelper = ToolsSwingLocator.getToolsSwingManager().createChangeListenerHelper();
|
109 | 44707 | jjdelcerro | this.store = store;
|
110 | this.searchFields = new ArrayList<>(); |
||
111 | 44713 | jjdelcerro | SearchConditionFieldController controller = new SearchConditionFieldController(
|
112 | 44707 | jjdelcerro | store, |
113 | lblField1, |
||
114 | lblExtraFields1, |
||
115 | lblRelationalOperator1, |
||
116 | cboValue1, |
||
117 | lblLogicalOperators1 |
||
118 | ); |
||
119 | this.searchFields.add(controller);
|
||
120 | 44713 | jjdelcerro | controller = new SearchConditionFieldController(
|
121 | 44707 | jjdelcerro | store, |
122 | lblField2, |
||
123 | lblExtraFields2, |
||
124 | lblRelationalOperator2, |
||
125 | cboValue2, |
||
126 | lblLogicalOperators2 |
||
127 | ); |
||
128 | this.searchFields.add(controller);
|
||
129 | 44713 | jjdelcerro | controller = new SearchConditionFieldController(
|
130 | 44707 | jjdelcerro | store, |
131 | lblField3, |
||
132 | lblExtraFields3, |
||
133 | lblRelationalOperator3, |
||
134 | cboValue3, |
||
135 | lblLogicalOperators3 |
||
136 | ); |
||
137 | this.searchFields.add(controller);
|
||
138 | 44713 | jjdelcerro | controller = new SearchConditionFieldController(
|
139 | 44707 | jjdelcerro | store, |
140 | lblField4, |
||
141 | lblExtraFields4, |
||
142 | lblRelationalOperator4, |
||
143 | cboValue4, |
||
144 | null
|
||
145 | ); |
||
146 | this.searchFields.add(controller);
|
||
147 | this.accumulatedFilter = null; |
||
148 | this.btnAddAccumulatedFilter = btnAddToAccumulatedFilter;
|
||
149 | this.btnRemoveAccumulatedFilter = btnRemoveAccumulatedFilter;
|
||
150 | this.btnViewAccumulatedFilter = btnViewAccumulatedFilter;
|
||
151 | this.baseToolTip = this.btnAddAccumulatedFilter.getToolTipText(); |
||
152 | initComponents(); |
||
153 | } |
||
154 | |||
155 | 44712 | jjdelcerro | @Override
|
156 | public SearchConditionPanelFactory getFactory() {
|
||
157 | return FACTORY;
|
||
158 | } |
||
159 | 44764 | jjdelcerro | |
160 | @Override
|
||
161 | public boolean isValid(StringBuilder message) { |
||
162 | return true; |
||
163 | } |
||
164 | 44712 | jjdelcerro | |
165 | 44707 | jjdelcerro | private void initComponents() { |
166 | try {
|
||
167 | Search search = (Search) ToolsLocator.getComplementsManager().get( |
||
168 | Search.COMPLEMENT_MANE, this.store.getDefaultFeatureType()
|
||
169 | ); |
||
170 | List<Search.OrderedAttribute> orderedAttributes = search.getOrderedAttributes(
|
||
171 | Search.BASIC_TYPES_FILTER, |
||
172 | Search.STR_INT_LONG_LABEL_ORDER, |
||
173 | 5
|
||
174 | ); |
||
175 | this.maxSearhFields = Integer.min(orderedAttributes.size(), 4); |
||
176 | int n = 0; |
||
177 | 44713 | jjdelcerro | for (SearchConditionFieldController searchField : searchFields) {
|
178 | 44707 | jjdelcerro | if (n < this.maxSearhFields) { |
179 | searchField.setAttribute(orderedAttributes.get(n++).getDescriptor().getName()); |
||
180 | } else {
|
||
181 | searchField.setEnabled(false);
|
||
182 | } |
||
183 | } |
||
184 | } catch (DataException ex) {
|
||
185 | LOGGER.warn("Can't determine order of attributes", ex);
|
||
186 | } |
||
187 | this.btnAddAccumulatedFilter.addActionListener((ActionEvent e) -> { |
||
188 | addToAccumulatedFilter(this.getCurrentFilter());
|
||
189 | }); |
||
190 | this.btnRemoveAccumulatedFilter.addActionListener((ActionEvent e) -> { |
||
191 | clearAccumulatedFilter(); |
||
192 | }); |
||
193 | this.btnViewAccumulatedFilter.addActionListener((ActionEvent e) -> { |
||
194 | showAccumulatedFilter(); |
||
195 | }); |
||
196 | this.btnRemoveAccumulatedFilter.setEnabled(false); |
||
197 | this.btnViewAccumulatedFilter.setEnabled(false); |
||
198 | this.btnAddAccumulatedFilter.setEnabled(true); |
||
199 | } |
||
200 | |||
201 | public void addToAccumulatedFilter(String filter) { |
||
202 | if( StringUtils.isBlank(filter) ) {
|
||
203 | return;
|
||
204 | } |
||
205 | String theAccumulatedFilter = this.accumulatedFilter; |
||
206 | this.clear();
|
||
207 | if( !StringUtils.isBlank(theAccumulatedFilter) ) {
|
||
208 | filter = "( " + theAccumulatedFilter + ") AND \n( " + filter + ")"; |
||
209 | } |
||
210 | this.btnAddAccumulatedFilter.setToolTipText("<html><b>"+this.baseToolTip + "</b><br><br>" + filter.replaceAll("\\n", "<br>")+"</html>"); |
||
211 | this.accumulatedFilter = filter;
|
||
212 | this.btnRemoveAccumulatedFilter.setEnabled(true); |
||
213 | this.btnViewAccumulatedFilter.setEnabled(true); |
||
214 | 44750 | jjdelcerro | this.changeListenerHelper.fireEvent();
|
215 | 44707 | jjdelcerro | } |
216 | |||
217 | public void clearAccumulatedFilter() { |
||
218 | this.accumulatedFilter = null; |
||
219 | this.btnRemoveAccumulatedFilter.setEnabled(false); |
||
220 | this.btnViewAccumulatedFilter.setEnabled(false); |
||
221 | 44714 | jjdelcerro | this.btnAddAccumulatedFilter.setToolTipText(this.baseToolTip); |
222 | 44750 | jjdelcerro | this.changeListenerHelper.fireEvent();
|
223 | 44707 | jjdelcerro | } |
224 | |||
225 | public void showAccumulatedFilter() { |
||
226 | WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
||
227 | DataSwingManager manager = DALSwingLocator.getDataSwingManager(); |
||
228 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
229 | |||
230 | JExpressionBuilder panel = manager.createQueryFilterExpresion(store); |
||
231 | panel.setExpression(ExpressionUtils.createExpression(this.getAccumulatedFilter()));
|
||
232 | Dialog dialog = winmanager.createDialog(
|
||
233 | panel.asJComponent(), |
||
234 | i18n.getTranslation("_Expression_builder"),
|
||
235 | null,
|
||
236 | WindowManager_v2.BUTTONS_OK_CANCEL |
||
237 | ); |
||
238 | dialog.addActionListener((ActionEvent e) -> {
|
||
239 | if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
||
240 | this.clearAccumulatedFilter();
|
||
241 | this.addToAccumulatedFilter(panel.getExpression().getPhrase());
|
||
242 | } |
||
243 | }); |
||
244 | dialog.show(WindowManager.MODE.DIALOG); |
||
245 | } |
||
246 | |||
247 | @Override
|
||
248 | public void setEnabled(boolean enabled) { |
||
249 | if (this.searchFields == null) { |
||
250 | initComponents(); |
||
251 | } |
||
252 | int n = 0; |
||
253 | 44713 | jjdelcerro | for (SearchConditionFieldController searchField : searchFields) {
|
254 | 44707 | jjdelcerro | if (n < this.maxSearhFields) { |
255 | searchField.setEnabled(enabled); |
||
256 | } else {
|
||
257 | searchField.setEnabled(false);
|
||
258 | } |
||
259 | n++; |
||
260 | } |
||
261 | } |
||
262 | |||
263 | @Override
|
||
264 | public void clear() { |
||
265 | if (this.searchFields == null) { |
||
266 | return;
|
||
267 | } |
||
268 | 44713 | jjdelcerro | for (SearchConditionFieldController searchField : searchFields) {
|
269 | 44707 | jjdelcerro | searchField.clear(); |
270 | } |
||
271 | this.clearAccumulatedFilter();
|
||
272 | } |
||
273 | |||
274 | @Override
|
||
275 | public Expression get() { |
||
276 | String currentFilter = this.getCurrentFilter(); |
||
277 | if( StringUtils.isBlank(accumulatedFilter) ) {
|
||
278 | if( StringUtils.isBlank(currentFilter) ) {
|
||
279 | return null; |
||
280 | } |
||
281 | return ExpressionUtils.createExpression(currentFilter);
|
||
282 | } |
||
283 | if( StringUtils.isBlank(currentFilter) ) {
|
||
284 | return ExpressionUtils.createExpression(accumulatedFilter);
|
||
285 | } |
||
286 | return ExpressionUtils.createExpression("( " + accumulatedFilter + " ) AND ( "+ currentFilter +" )"); |
||
287 | } |
||
288 | |||
289 | public String getAccumulatedFilter() { |
||
290 | return this.accumulatedFilter; |
||
291 | } |
||
292 | |||
293 | public String getCurrentFilter() { |
||
294 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
295 | String relational = OPERATOR_OR;
|
||
296 | 44713 | jjdelcerro | for (SearchConditionFieldController searchField : searchFields) {
|
297 | 44748 | jjdelcerro | ExpressionBuilder.Value cond = searchField.getFilter(); |
298 | if( cond != null ) { |
||
299 | 44707 | jjdelcerro | if (relational.equals(OPERATOR_AND)) {
|
300 | builder.and(cond); |
||
301 | } else {
|
||
302 | builder.or(cond); |
||
303 | } |
||
304 | relational = searchField.getLogicalOperator(); |
||
305 | } |
||
306 | } |
||
307 | if (builder.isEmpty()) {
|
||
308 | return null; |
||
309 | } |
||
310 | return builder.toString();
|
||
311 | } |
||
312 | |||
313 | @Override
|
||
314 | public boolean set(Expression filter) { |
||
315 | 44750 | jjdelcerro | try {
|
316 | Code code = filter.getCode(); |
||
317 | 44752 | jjdelcerro | if (code.code() == Code.CALLABLE) {
|
318 | 44750 | jjdelcerro | SearchConditionFieldController searchField = this.searchFields.get(0); |
319 | 44752 | jjdelcerro | Code.Callable caller = (Code.Callable) code; |
320 | 44750 | jjdelcerro | if (searchField.isAValidRelationOperator(caller.name())) {
|
321 | Code op1 = caller.parameters().get(0);
|
||
322 | Code op2 = caller.parameters().get(1);
|
||
323 | if (op1.code() == Code.IDENTIFIER && op2.code() == Code.CONSTANT) {
|
||
324 | if (searchField.setAttribute(((Code.Identifier) op1).name()) >= 0) { |
||
325 | searchField.setRelationalOperator(caller.name()); |
||
326 | searchField.setValue(((Code.Constant) op2).value()); |
||
327 | return true; |
||
328 | } |
||
329 | 44707 | jjdelcerro | } |
330 | } |
||
331 | } |
||
332 | 44750 | jjdelcerro | return false; |
333 | } finally {
|
||
334 | this.changeListenerHelper.fireEvent();
|
||
335 | 44707 | jjdelcerro | } |
336 | } |
||
337 | 44750 | jjdelcerro | |
338 | @Override
|
||
339 | public void addChangeListener(ChangeListener listener) { |
||
340 | this.changeListenerHelper.addChangeListener(listener);
|
||
341 | } |
||
342 | |||
343 | @Override
|
||
344 | public ChangeListener[] getChangeListeners() { |
||
345 | return this.changeListenerHelper.getChangeListeners(); |
||
346 | } |
||
347 | |||
348 | @Override
|
||
349 | public void removeChangeListener(ChangeListener listener) { |
||
350 | this.changeListenerHelper.removeChangeListener(listener);
|
||
351 | } |
||
352 | |||
353 | @Override
|
||
354 | public void removeAllChangeListener() { |
||
355 | this.changeListenerHelper.removeAllChangeListener();
|
||
356 | } |
||
357 | |||
358 | @Override
|
||
359 | public boolean hasChangeListeners() { |
||
360 | return this.changeListenerHelper.hasChangeListeners(); |
||
361 | } |
||
362 | 44707 | jjdelcerro | } |