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