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 @ 44752
History | View | Annotate | Download (11.6 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.SearchConditionPanel; |
25 |
import org.gvsig.tools.ToolsLocator; |
26 |
import org.gvsig.tools.i18n.I18nManager; |
27 |
import org.gvsig.tools.swing.api.ChangeListenerHelper; |
28 |
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 |
@Override
|
44 |
public JComponent asJComponent() { |
45 |
return null; |
46 |
} |
47 |
|
48 |
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 |
private final FeatureStore store; |
72 |
private final List<SearchConditionFieldController> searchFields; |
73 |
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 |
private SearchConditionPanelFactory factory;
|
80 |
private final ChangeListenerHelper changeListenerHelper; |
81 |
|
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 |
this.changeListenerHelper = ToolsSwingLocator.getToolsSwingManager().createChangeListenerHelper();
|
109 |
this.store = store;
|
110 |
this.searchFields = new ArrayList<>(); |
111 |
SearchConditionFieldController controller = new SearchConditionFieldController(
|
112 |
store, |
113 |
lblField1, |
114 |
lblExtraFields1, |
115 |
lblRelationalOperator1, |
116 |
cboValue1, |
117 |
lblLogicalOperators1 |
118 |
); |
119 |
this.searchFields.add(controller);
|
120 |
controller = new SearchConditionFieldController(
|
121 |
store, |
122 |
lblField2, |
123 |
lblExtraFields2, |
124 |
lblRelationalOperator2, |
125 |
cboValue2, |
126 |
lblLogicalOperators2 |
127 |
); |
128 |
this.searchFields.add(controller);
|
129 |
controller = new SearchConditionFieldController(
|
130 |
store, |
131 |
lblField3, |
132 |
lblExtraFields3, |
133 |
lblRelationalOperator3, |
134 |
cboValue3, |
135 |
lblLogicalOperators3 |
136 |
); |
137 |
this.searchFields.add(controller);
|
138 |
controller = new SearchConditionFieldController(
|
139 |
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 |
@Override
|
156 |
public SearchConditionPanelFactory getFactory() {
|
157 |
return FACTORY;
|
158 |
} |
159 |
|
160 |
private void initComponents() { |
161 |
try {
|
162 |
Search search = (Search) ToolsLocator.getComplementsManager().get( |
163 |
Search.COMPLEMENT_MANE, this.store.getDefaultFeatureType()
|
164 |
); |
165 |
List<Search.OrderedAttribute> orderedAttributes = search.getOrderedAttributes(
|
166 |
Search.BASIC_TYPES_FILTER, |
167 |
Search.STR_INT_LONG_LABEL_ORDER, |
168 |
5
|
169 |
); |
170 |
this.maxSearhFields = Integer.min(orderedAttributes.size(), 4); |
171 |
int n = 0; |
172 |
for (SearchConditionFieldController searchField : searchFields) {
|
173 |
if (n < this.maxSearhFields) { |
174 |
searchField.setAttribute(orderedAttributes.get(n++).getDescriptor().getName()); |
175 |
} else {
|
176 |
searchField.setEnabled(false);
|
177 |
} |
178 |
} |
179 |
} catch (DataException ex) {
|
180 |
LOGGER.warn("Can't determine order of attributes", ex);
|
181 |
} |
182 |
this.btnAddAccumulatedFilter.addActionListener((ActionEvent e) -> { |
183 |
addToAccumulatedFilter(this.getCurrentFilter());
|
184 |
}); |
185 |
this.btnRemoveAccumulatedFilter.addActionListener((ActionEvent e) -> { |
186 |
clearAccumulatedFilter(); |
187 |
}); |
188 |
this.btnViewAccumulatedFilter.addActionListener((ActionEvent e) -> { |
189 |
showAccumulatedFilter(); |
190 |
}); |
191 |
this.btnRemoveAccumulatedFilter.setEnabled(false); |
192 |
this.btnViewAccumulatedFilter.setEnabled(false); |
193 |
this.btnAddAccumulatedFilter.setEnabled(true); |
194 |
} |
195 |
|
196 |
public void addToAccumulatedFilter(String filter) { |
197 |
if( StringUtils.isBlank(filter) ) {
|
198 |
return;
|
199 |
} |
200 |
String theAccumulatedFilter = this.accumulatedFilter; |
201 |
this.clear();
|
202 |
if( !StringUtils.isBlank(theAccumulatedFilter) ) {
|
203 |
filter = "( " + theAccumulatedFilter + ") AND \n( " + filter + ")"; |
204 |
} |
205 |
this.btnAddAccumulatedFilter.setToolTipText("<html><b>"+this.baseToolTip + "</b><br><br>" + filter.replaceAll("\\n", "<br>")+"</html>"); |
206 |
this.accumulatedFilter = filter;
|
207 |
this.btnRemoveAccumulatedFilter.setEnabled(true); |
208 |
this.btnViewAccumulatedFilter.setEnabled(true); |
209 |
this.changeListenerHelper.fireEvent();
|
210 |
} |
211 |
|
212 |
public void clearAccumulatedFilter() { |
213 |
this.accumulatedFilter = null; |
214 |
this.btnRemoveAccumulatedFilter.setEnabled(false); |
215 |
this.btnViewAccumulatedFilter.setEnabled(false); |
216 |
this.btnAddAccumulatedFilter.setToolTipText(this.baseToolTip); |
217 |
this.changeListenerHelper.fireEvent();
|
218 |
} |
219 |
|
220 |
public void showAccumulatedFilter() { |
221 |
WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
222 |
DataSwingManager manager = DALSwingLocator.getDataSwingManager(); |
223 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
224 |
|
225 |
JExpressionBuilder panel = manager.createQueryFilterExpresion(store); |
226 |
panel.setExpression(ExpressionUtils.createExpression(this.getAccumulatedFilter()));
|
227 |
Dialog dialog = winmanager.createDialog(
|
228 |
panel.asJComponent(), |
229 |
i18n.getTranslation("_Expression_builder"),
|
230 |
null,
|
231 |
WindowManager_v2.BUTTONS_OK_CANCEL |
232 |
); |
233 |
dialog.addActionListener((ActionEvent e) -> {
|
234 |
if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
235 |
this.clearAccumulatedFilter();
|
236 |
this.addToAccumulatedFilter(panel.getExpression().getPhrase());
|
237 |
} |
238 |
}); |
239 |
dialog.show(WindowManager.MODE.DIALOG); |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public void setEnabled(boolean enabled) { |
244 |
if (this.searchFields == null) { |
245 |
initComponents(); |
246 |
} |
247 |
int n = 0; |
248 |
for (SearchConditionFieldController searchField : searchFields) {
|
249 |
if (n < this.maxSearhFields) { |
250 |
searchField.setEnabled(enabled); |
251 |
} else {
|
252 |
searchField.setEnabled(false);
|
253 |
} |
254 |
n++; |
255 |
} |
256 |
} |
257 |
|
258 |
@Override
|
259 |
public void clear() { |
260 |
if (this.searchFields == null) { |
261 |
return;
|
262 |
} |
263 |
for (SearchConditionFieldController searchField : searchFields) {
|
264 |
searchField.clear(); |
265 |
} |
266 |
this.clearAccumulatedFilter();
|
267 |
} |
268 |
|
269 |
@Override
|
270 |
public Expression get() { |
271 |
String currentFilter = this.getCurrentFilter(); |
272 |
if( StringUtils.isBlank(accumulatedFilter) ) {
|
273 |
if( StringUtils.isBlank(currentFilter) ) {
|
274 |
return null; |
275 |
} |
276 |
return ExpressionUtils.createExpression(currentFilter);
|
277 |
} |
278 |
if( StringUtils.isBlank(currentFilter) ) {
|
279 |
return ExpressionUtils.createExpression(accumulatedFilter);
|
280 |
} |
281 |
return ExpressionUtils.createExpression("( " + accumulatedFilter + " ) AND ( "+ currentFilter +" )"); |
282 |
} |
283 |
|
284 |
public String getAccumulatedFilter() { |
285 |
return this.accumulatedFilter; |
286 |
} |
287 |
|
288 |
public String getCurrentFilter() { |
289 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
290 |
String relational = OPERATOR_OR;
|
291 |
for (SearchConditionFieldController searchField : searchFields) {
|
292 |
ExpressionBuilder.Value cond = searchField.getFilter(); |
293 |
if( cond != null ) { |
294 |
if (relational.equals(OPERATOR_AND)) {
|
295 |
builder.and(cond); |
296 |
} else {
|
297 |
builder.or(cond); |
298 |
} |
299 |
relational = searchField.getLogicalOperator(); |
300 |
} |
301 |
} |
302 |
if (builder.isEmpty()) {
|
303 |
return null; |
304 |
} |
305 |
return builder.toString();
|
306 |
} |
307 |
|
308 |
@Override
|
309 |
public boolean set(Expression filter) { |
310 |
try {
|
311 |
Code code = filter.getCode(); |
312 |
if (code.code() == Code.CALLABLE) {
|
313 |
SearchConditionFieldController searchField = this.searchFields.get(0); |
314 |
Code.Callable caller = (Code.Callable) code; |
315 |
if (searchField.isAValidRelationOperator(caller.name())) {
|
316 |
Code op1 = caller.parameters().get(0);
|
317 |
Code op2 = caller.parameters().get(1);
|
318 |
if (op1.code() == Code.IDENTIFIER && op2.code() == Code.CONSTANT) {
|
319 |
if (searchField.setAttribute(((Code.Identifier) op1).name()) >= 0) { |
320 |
searchField.setRelationalOperator(caller.name()); |
321 |
searchField.setValue(((Code.Constant) op2).value()); |
322 |
return true; |
323 |
} |
324 |
} |
325 |
} |
326 |
} |
327 |
return false; |
328 |
} finally {
|
329 |
this.changeListenerHelper.fireEvent();
|
330 |
} |
331 |
} |
332 |
|
333 |
@Override
|
334 |
public void addChangeListener(ChangeListener listener) { |
335 |
this.changeListenerHelper.addChangeListener(listener);
|
336 |
} |
337 |
|
338 |
@Override
|
339 |
public ChangeListener[] getChangeListeners() { |
340 |
return this.changeListenerHelper.getChangeListeners(); |
341 |
} |
342 |
|
343 |
@Override
|
344 |
public void removeChangeListener(ChangeListener listener) { |
345 |
this.changeListenerHelper.removeChangeListener(listener);
|
346 |
} |
347 |
|
348 |
@Override
|
349 |
public void removeAllChangeListener() { |
350 |
this.changeListenerHelper.removeAllChangeListener();
|
351 |
} |
352 |
|
353 |
@Override
|
354 |
public boolean hasChangeListeners() { |
355 |
return this.changeListenerHelper.hasChangeListeners(); |
356 |
} |
357 |
} |