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