root / org.gvsig.legend.vectorfilterexpression.app.mainplugin / trunk / org.gvsig.legend.vectorfilterexpression.app.mainplugin / src / main / java / org / gvsig / symbology / gui / layerproperties / ExpressionSymbolPanel.java @ 107
History | View | Annotate | Download (11.6 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2013 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.symbology.gui.layerproperties; |
24 |
|
25 |
import java.awt.Dimension; |
26 |
import java.awt.GridBagConstraints; |
27 |
import java.awt.GridBagLayout; |
28 |
import java.awt.Insets; |
29 |
import java.awt.event.ActionEvent; |
30 |
import java.awt.event.ActionListener; |
31 |
|
32 |
import javax.swing.BorderFactory; |
33 |
import javax.swing.JOptionPane; |
34 |
import javax.swing.JPanel; |
35 |
import javax.swing.JScrollPane; |
36 |
import javax.swing.JTextArea; |
37 |
import javax.swing.ScrollPaneConstants; |
38 |
|
39 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
40 |
import org.gvsig.andami.ui.mdiManager.MDIManager; |
41 |
import org.gvsig.andami.ui.mdiManager.WindowInfo; |
42 |
import org.gvsig.app.ApplicationLocator; |
43 |
import org.gvsig.app.project.documents.view.legend.gui.JSymbolPreviewButton; |
44 |
import org.gvsig.fmap.dal.feature.Feature; |
45 |
import org.gvsig.fmap.geom.Geometry; |
46 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
47 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
48 |
import org.gvsig.gui.beans.AcceptCancelPanel; |
49 |
import org.gvsig.i18n.Messages; |
50 |
import org.gvsig.symbology.fmap.rendering.filterexpression.EvaluatorCreator; |
51 |
import org.gvsig.tools.evaluator.EvaluatorException; |
52 |
import org.gvsig.tools.evaluator.EvaluatorWithDescriptions; |
53 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
54 |
import org.gvsig.tools.swing.api.evaluator.ComponentSwingManager; |
55 |
import org.gvsig.tools.swing.api.evaluator.EvaluatorPanel; |
56 |
|
57 |
|
58 |
/**
|
59 |
* This panel shows an expression creation panel (from the Tools
|
60 |
* library) and shows a symbol preview panel with a description
|
61 |
* so the user can associate a symbol to the expression (symbol
|
62 |
* will be used if expression evaluates to TRUE)
|
63 |
*
|
64 |
* @see EvaluatorPanel
|
65 |
*
|
66 |
*
|
67 |
* @author jldominguez
|
68 |
*
|
69 |
*/
|
70 |
public class ExpressionSymbolPanel extends JPanel |
71 |
implements ActionListener, IWindow { |
72 |
|
73 |
/**
|
74 |
*
|
75 |
*/
|
76 |
private static final long serialVersionUID = 6299256060002740452L; |
77 |
private Feature dummyFeature = null; |
78 |
private EvaluatorWithDescriptions evald = null; |
79 |
private int shptype = Geometry.TYPES.GEOMETRY; |
80 |
|
81 |
private JPanel symPanel = null; |
82 |
private JSymbolPreviewButton templateButton = null; |
83 |
private JTextArea descArea = null; |
84 |
private AcceptCancelPanel acPanel;
|
85 |
private boolean okPressed = false; |
86 |
private WindowInfo winfo = null; |
87 |
|
88 |
private EvaluatorPanel evapa = null; |
89 |
|
90 |
public ExpressionSymbolPanel(
|
91 |
Feature dummy_feat, EvaluatorWithDescriptions ed, int st, ISymbol sym) {
|
92 |
|
93 |
dummyFeature = dummy_feat; |
94 |
evald = ed; |
95 |
shptype = st; |
96 |
|
97 |
initialize(sym); |
98 |
} |
99 |
|
100 |
private EvaluatorPanel getEvaluatorPanel() {
|
101 |
if (evapa == null) { |
102 |
ComponentSwingManager esm = |
103 |
ToolsSwingLocator.getComponentSwingManager(); |
104 |
evapa = esm.getEvaluatorPanel( |
105 |
dummyFeature.getType(), |
106 |
evald); |
107 |
} |
108 |
return evapa;
|
109 |
} |
110 |
|
111 |
public String getExpression() { |
112 |
return getEvaluatorPanel().getExpression();
|
113 |
} |
114 |
|
115 |
public ISymbol getSymbol() {
|
116 |
ISymbol resp = getTemplateButton(null).getSymbol();
|
117 |
resp.setDescription(this.getDescArea(null).getText()); |
118 |
return resp;
|
119 |
} |
120 |
|
121 |
/**
|
122 |
*
|
123 |
*/
|
124 |
private void initialize(ISymbol sym) { |
125 |
|
126 |
this.setLayout(new GridBagLayout()); |
127 |
GridBagConstraints gbc = new GridBagConstraints(); |
128 |
|
129 |
gbc.gridx = 0;
|
130 |
gbc.gridy = 0;
|
131 |
gbc.anchor = GridBagConstraints.CENTER;
|
132 |
gbc.fill = GridBagConstraints.BOTH;
|
133 |
gbc.weightx = 1;
|
134 |
gbc.weighty = 1;
|
135 |
this.add(getEvaluatorPanel(), gbc);
|
136 |
gbc.gridy = 1;
|
137 |
gbc.weighty = 0;
|
138 |
this.add(getSymbolPanel(sym), gbc);
|
139 |
gbc.gridy = 2;
|
140 |
this.add(getAcceptCancel(), gbc);
|
141 |
} |
142 |
|
143 |
/**
|
144 |
* @return
|
145 |
*/
|
146 |
private JPanel getAcceptCancel() { |
147 |
|
148 |
if (acPanel == null) { |
149 |
acPanel = new AcceptCancelPanel(this, this); |
150 |
} |
151 |
return acPanel;
|
152 |
} |
153 |
|
154 |
/**
|
155 |
* @return
|
156 |
*/
|
157 |
private JPanel getSymbolPanel(ISymbol sym) { |
158 |
|
159 |
if (symPanel == null) { |
160 |
|
161 |
symPanel = new JPanel(); |
162 |
symPanel.setLayout(new GridBagLayout()); |
163 |
symPanel.setBorder(BorderFactory.createTitledBorder(
|
164 |
Messages.getText("_Symbol_and_description")));
|
165 |
|
166 |
GridBagConstraints gbc = new GridBagConstraints(); |
167 |
|
168 |
gbc.fill = GridBagConstraints.BOTH;
|
169 |
gbc.anchor = GridBagConstraints.NORTHWEST;
|
170 |
gbc.gridx = 0;
|
171 |
gbc.gridy = 0;
|
172 |
gbc.weightx = 0.2;
|
173 |
gbc.weighty = 0;
|
174 |
gbc.insets = new Insets(7, 7, 7, 7); |
175 |
symPanel.add(getTemplateButton(sym), gbc); |
176 |
|
177 |
gbc.gridx = 1;
|
178 |
gbc.weightx = 0.8;
|
179 |
|
180 |
JScrollPane scroll = new JScrollPane(getDescArea(sym)); |
181 |
scroll.setHorizontalScrollBarPolicy( |
182 |
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
183 |
scroll.setVerticalScrollBarPolicy( |
184 |
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
|
185 |
symPanel.add(scroll, gbc); |
186 |
} |
187 |
return symPanel;
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* @return
|
192 |
*/
|
193 |
private JTextArea getDescArea(ISymbol sym) { |
194 |
if (descArea == null) { |
195 |
|
196 |
descArea = new JTextArea(); |
197 |
descArea.setWrapStyleWord(true);
|
198 |
descArea.setLineWrap(true);
|
199 |
if (sym != null) { |
200 |
descArea.setText(sym.getDescription()); |
201 |
} |
202 |
} |
203 |
return descArea;
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* @return
|
208 |
*/
|
209 |
private JSymbolPreviewButton getTemplateButton(ISymbol sym) {
|
210 |
if (templateButton == null) { |
211 |
ISymbol inisym = sym; |
212 |
if (inisym == null) { |
213 |
inisym = MapContextLocator.getSymbolManager(). |
214 |
createSymbol(shptype); |
215 |
} |
216 |
templateButton = new JSymbolPreviewButton(inisym, shptype);
|
217 |
templateButton.setPreferredSize(new Dimension(100, 35)); |
218 |
templateButton.setMinimumSize(new Dimension(100, 35)); |
219 |
} |
220 |
return templateButton;
|
221 |
} |
222 |
|
223 |
/* (non-Javadoc)
|
224 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
|
225 |
*/
|
226 |
public void actionPerformed(ActionEvent e) { |
227 |
|
228 |
if (e.getActionCommand().compareToIgnoreCase("ok") == 0) { |
229 |
|
230 |
Class<?> return_class = null; |
231 |
try {
|
232 |
return_class = validateExpression(); |
233 |
} catch (EvaluatorException epe) {
|
234 |
|
235 |
if (epe.getCause() instanceof ArithmeticException) { |
236 |
|
237 |
if (!userTolerates(epe)) {
|
238 |
okPressed = false;
|
239 |
return;
|
240 |
} else {
|
241 |
// User said ok
|
242 |
okPressed = true;
|
243 |
closeThis(); |
244 |
return;
|
245 |
} |
246 |
|
247 |
} else {
|
248 |
String detail = epe.getMessageStack();
|
249 |
ApplicationLocator.getManager().messageDialog( |
250 |
Messages.getText("incorrect_expression")
|
251 |
+ "\n\n" + detail,
|
252 |
Messages.getText("error"),
|
253 |
JOptionPane.ERROR_MESSAGE);
|
254 |
okPressed = false;
|
255 |
return;
|
256 |
} |
257 |
} |
258 |
|
259 |
if (return_class != Boolean.class) { |
260 |
int usr_opt = JOptionPane.showConfirmDialog( |
261 |
this,
|
262 |
Messages.getText("_Result_not_boolean_so_zero_empty_string_null_are_false_Continue_question"),
|
263 |
Messages.getText("confirmation_dialog"),
|
264 |
JOptionPane.YES_NO_OPTION);
|
265 |
if (usr_opt == JOptionPane.NO_OPTION) { |
266 |
okPressed = false;
|
267 |
return;
|
268 |
} |
269 |
} |
270 |
okPressed = true;
|
271 |
closeThis(); |
272 |
|
273 |
} else {
|
274 |
if (e.getActionCommand().compareToIgnoreCase("cancel") == 0) { |
275 |
okPressed = false;
|
276 |
closeThis(); |
277 |
} |
278 |
} |
279 |
} |
280 |
|
281 |
|
282 |
/**
|
283 |
* @param epe
|
284 |
* @return
|
285 |
*/
|
286 |
private boolean userTolerates(EvaluatorException epe) { |
287 |
|
288 |
String detail = Messages.getText(
|
289 |
"_Expression_can_cause_arithmetic_issues_Continue_question");
|
290 |
detail = detail + "\n\nMessage: ";
|
291 |
detail = detail + epe.getMessageStack(); |
292 |
|
293 |
int usr_opt = JOptionPane.showConfirmDialog( |
294 |
this,
|
295 |
detail, |
296 |
Messages.getText("confirmation_dialog"),
|
297 |
JOptionPane.YES_NO_OPTION);
|
298 |
|
299 |
return usr_opt == JOptionPane.YES_OPTION; |
300 |
} |
301 |
|
302 |
/**
|
303 |
* @return class of the return value
|
304 |
*/
|
305 |
private Class<? extends Object> validateExpression() throws EvaluatorException { |
306 |
|
307 |
String expr = this.getExpression(); |
308 |
EvaluatorWithDescriptions evde = EvaluatorCreator.getEvaluator(expr); |
309 |
Object resp_class = null; |
310 |
|
311 |
try {
|
312 |
/*
|
313 |
* Try evaluate feature with default values
|
314 |
*/
|
315 |
resp_class = evde.evaluate(dummyFeature.getEvaluatorData()); |
316 |
} catch (EvaluatorException e) {
|
317 |
/*
|
318 |
* Parse exception or field not found or
|
319 |
* divison by zero, etc.
|
320 |
* Malformed expression, not valid
|
321 |
*/
|
322 |
throw e;
|
323 |
} catch (Exception e) { |
324 |
/*
|
325 |
* All other exceptions.
|
326 |
* Should not happen often.
|
327 |
* We assume a strange
|
328 |
* exception (NPE, etc) and decide it's not a valid
|
329 |
* expression
|
330 |
*
|
331 |
*/
|
332 |
throw new EvaluatorException(e); |
333 |
} |
334 |
|
335 |
if (resp_class == null) { |
336 |
return null; |
337 |
} else {
|
338 |
return resp_class.getClass();
|
339 |
} |
340 |
} |
341 |
|
342 |
private void closeThis() { |
343 |
MDIManager mm = ApplicationLocator.getManager().getUIManager(); |
344 |
mm.closeWindow(this);
|
345 |
} |
346 |
|
347 |
public boolean isOkPressed() { |
348 |
return okPressed;
|
349 |
} |
350 |
|
351 |
/* (non-Javadoc)
|
352 |
* @see org.gvsig.andami.ui.mdiManager.IWindow#getWindowInfo()
|
353 |
*/
|
354 |
public WindowInfo getWindowInfo() {
|
355 |
if (winfo == null) { |
356 |
winfo = new WindowInfo(
|
357 |
WindowInfo.MODALDIALOG | |
358 |
WindowInfo.RESIZABLE); |
359 |
|
360 |
winfo.setTitle(Messages.getText("expression_creator"));
|
361 |
winfo.setWidth(675);
|
362 |
winfo.setHeight(400);
|
363 |
} |
364 |
return winfo;
|
365 |
} |
366 |
|
367 |
/* (non-Javadoc)
|
368 |
* @see org.gvsig.andami.ui.mdiManager.IWindow#getWindowProfile()
|
369 |
*/
|
370 |
public Object getWindowProfile() { |
371 |
return WindowInfo.DIALOG_PROFILE;
|
372 |
} |
373 |
|
374 |
} |