svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.swing / org.gvsig.expressionevaluator.swing.impl / src / main / java / org / gvsig / expressionevaluator / swing / impl / AbstractAutomaticExpressionChecker.java @ 44148
History | View | Annotate | Download (10.4 KB)
1 |
package org.gvsig.expressionevaluator.swing.impl; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.awt.event.KeyAdapter; |
8 |
import java.awt.event.KeyEvent; |
9 |
import java.util.Objects; |
10 |
import javax.swing.JEditorPane; |
11 |
import javax.swing.JMenuItem; |
12 |
import javax.swing.JOptionPane; |
13 |
import javax.swing.JPopupMenu; |
14 |
import javax.swing.JScrollPane; |
15 |
import javax.swing.Timer; |
16 |
import javax.swing.event.DocumentEvent; |
17 |
import javax.swing.event.DocumentListener; |
18 |
import javax.swing.text.JTextComponent; |
19 |
import org.apache.commons.lang3.StringUtils; |
20 |
import org.gvsig.expressionevaluator.Expression; |
21 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
22 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
23 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
24 |
import org.gvsig.expressionevaluator.ExpressionSyntaxException; |
25 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
26 |
import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig; |
27 |
import org.gvsig.tools.ToolsLocator; |
28 |
import org.gvsig.tools.i18n.I18nManager; |
29 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
30 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
31 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
32 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
33 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
34 |
|
35 |
/**
|
36 |
*
|
37 |
* @author jjdelcerro
|
38 |
*/
|
39 |
@SuppressWarnings("UseSpecificCatch") |
40 |
public abstract class AbstractAutomaticExpressionChecker { |
41 |
|
42 |
private static final Color COLOR_ERROR = new Color(255,225,232); |
43 |
|
44 |
private JTextComponent text; |
45 |
private ExpressionBuilderConfig config;
|
46 |
|
47 |
private Color color_ok; |
48 |
private Timer timerExpression; |
49 |
|
50 |
private JMenuItem menuItemShowPreview; |
51 |
private JMenuItem menuItemShowTip; |
52 |
private JMenuItem menuItemShowMessage; |
53 |
|
54 |
private String message; |
55 |
protected String tip; |
56 |
private String preview; |
57 |
private DocumentListener doclistener; |
58 |
private KeyAdapter keylistener; |
59 |
private ActionListener timelistener; |
60 |
|
61 |
public AbstractAutomaticExpressionChecker(ExpressionBuilderConfig config, JTextComponent theText) { |
62 |
this.text = theText;
|
63 |
this.config = config;
|
64 |
this.initComponents();
|
65 |
} |
66 |
|
67 |
private void initComponents() { |
68 |
final I18nManager i18n = ToolsLocator.getI18nManager();
|
69 |
final ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
|
70 |
|
71 |
this.color_ok = this.text.getBackground(); |
72 |
|
73 |
toolsSwingManager.setDefaultPopupMenu(this.text);
|
74 |
toolsSwingManager.addClearButton(this.text);
|
75 |
|
76 |
JPopupMenu popupMenu = this.text.getComponentPopupMenu(); |
77 |
|
78 |
this.menuItemShowTip = new JMenuItem(i18n.getTranslation("_Show_tip")); |
79 |
this.menuItemShowTip.addActionListener(new ActionListener() { |
80 |
@Override
|
81 |
public void actionPerformed(ActionEvent e) { |
82 |
if( StringUtils.isBlank(tip) ) {
|
83 |
return;
|
84 |
} |
85 |
JOptionPane.showMessageDialog(
|
86 |
text.getParent(), |
87 |
tip, |
88 |
i18n.getTranslation("Tip"),
|
89 |
JOptionPane.INFORMATION_MESSAGE
|
90 |
); |
91 |
} |
92 |
}); |
93 |
popupMenu.add(this.menuItemShowTip);
|
94 |
|
95 |
this.menuItemShowPreview = new JMenuItem(i18n.getTranslation("_Show_preview")); |
96 |
this.menuItemShowPreview.addActionListener(new ActionListener() { |
97 |
@Override
|
98 |
public void actionPerformed(ActionEvent e) { |
99 |
showMessageDialog( |
100 |
i18n.getTranslation("_Preview_value"),
|
101 |
preview |
102 |
); |
103 |
} |
104 |
}); |
105 |
popupMenu.add(this.menuItemShowPreview);
|
106 |
|
107 |
this.menuItemShowMessage = new JMenuItem(i18n.getTranslation("_Show_message")); |
108 |
this.menuItemShowMessage.addActionListener(new ActionListener() { |
109 |
@Override
|
110 |
public void actionPerformed(ActionEvent e) { |
111 |
showMessageDialog( |
112 |
i18n.getTranslation("_Message"),
|
113 |
message |
114 |
); |
115 |
} |
116 |
}); |
117 |
popupMenu.add(this.menuItemShowMessage);
|
118 |
|
119 |
this.timelistener = new ActionListener() { |
120 |
@Override
|
121 |
public void actionPerformed(ActionEvent e) { |
122 |
doCheckExpression(); |
123 |
} |
124 |
}; |
125 |
this.doclistener = new DocumentListener() { |
126 |
@Override
|
127 |
public void insertUpdate(DocumentEvent e) { |
128 |
timerExpression.restart(); |
129 |
} |
130 |
|
131 |
@Override
|
132 |
public void removeUpdate(DocumentEvent e) { |
133 |
timerExpression.restart(); |
134 |
} |
135 |
|
136 |
@Override
|
137 |
public void changedUpdate(DocumentEvent e) { |
138 |
timerExpression.restart(); |
139 |
} |
140 |
}; |
141 |
this.keylistener = new KeyAdapter() { |
142 |
@Override
|
143 |
public void keyPressed(KeyEvent e) { |
144 |
if (e.getKeyCode() == KeyEvent.VK_C && e.isControlDown()) { |
145 |
doCheckExpression(); |
146 |
} |
147 |
} |
148 |
}; |
149 |
this.timerExpression = new Timer(3000, this.timelistener); |
150 |
this.timerExpression.setRepeats(false); |
151 |
this.text.getDocument().addDocumentListener(this.doclistener); |
152 |
this.text.addKeyListener(this.keylistener); |
153 |
} |
154 |
|
155 |
private void doCheckExpression() { |
156 |
if( this.text == null || this.config == null ) { |
157 |
return;
|
158 |
} |
159 |
Expression expression = this.getExpression(); |
160 |
this.setTip(null); |
161 |
this.setPreview(null); |
162 |
this.setMessage(JOptionPane.INFORMATION_MESSAGE, null); |
163 |
if (expression == null) { |
164 |
return;
|
165 |
} |
166 |
try {
|
167 |
expression.getCode(); |
168 |
this.setMessage(JOptionPane.INFORMATION_MESSAGE, ""); |
169 |
} catch (ExpressionSyntaxException ex) {
|
170 |
this.setMessage(JOptionPane.ERROR_MESSAGE, ex.getDescription()); |
171 |
return;
|
172 |
} catch (Exception ex) { |
173 |
this.setMessage(JOptionPane.ERROR_MESSAGE, "Syntax error in expression"); |
174 |
return;
|
175 |
} |
176 |
if (this.config.getPreviewSymbolTable() == null) { |
177 |
try {
|
178 |
ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager(); |
179 |
MutableSymbolTable symbolTable = theManager.createSymbolTable(); |
180 |
Object theValue = expression.execute(symbolTable);
|
181 |
this.setPreview(theValue);
|
182 |
} catch (Exception ex) { |
183 |
this.setMessage(JOptionPane.INFORMATION_MESSAGE, ""); |
184 |
} |
185 |
} else {
|
186 |
try {
|
187 |
Object theValue = expression.execute(this.config.getPreviewSymbolTable()); |
188 |
this.setPreview(theValue);
|
189 |
} catch (ExpressionRuntimeException ex) {
|
190 |
this.setTip(ex.getTip());
|
191 |
this.setMessage(JOptionPane.ERROR_MESSAGE, "Runtime error. " + ex.getMessage()); |
192 |
} catch (Exception ex) { |
193 |
this.setMessage(JOptionPane.ERROR_MESSAGE, "Runtime error. " + ex.getMessage()); |
194 |
} |
195 |
} |
196 |
} |
197 |
|
198 |
protected boolean isTipEnabled() { |
199 |
return !StringUtils.isBlank(this.tip); |
200 |
} |
201 |
|
202 |
protected boolean isPreviewEnabled() { |
203 |
return !StringUtils.isBlank(this.preview); |
204 |
} |
205 |
|
206 |
protected boolean isMessageEnabled() { |
207 |
return !StringUtils.isBlank(this.message); |
208 |
} |
209 |
|
210 |
protected String getTip() { |
211 |
return this.tip; |
212 |
} |
213 |
|
214 |
protected String getMessage() { |
215 |
return this.message; |
216 |
} |
217 |
|
218 |
protected String getPreview() { |
219 |
return this.preview; |
220 |
} |
221 |
|
222 |
protected void showMessageDialog() { |
223 |
final I18nManager i18n = ToolsLocator.getI18nManager();
|
224 |
if( this.isMessageEnabled() ) { |
225 |
this.showMessageDialog(
|
226 |
i18n.getTranslation("_Message"),
|
227 |
this.getMessage()
|
228 |
); |
229 |
} else if( this.isPreviewEnabled()) { |
230 |
this.showMessageDialog(
|
231 |
i18n.getTranslation("_Preview_value"),
|
232 |
this.getPreview()
|
233 |
); |
234 |
} |
235 |
} |
236 |
|
237 |
protected void showMessageDialog(String title, String msg) { |
238 |
final I18nManager i18n = ToolsLocator.getI18nManager();
|
239 |
final ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
|
240 |
|
241 |
JEditorPane textMessage = new JEditorPane(); |
242 |
JScrollPane scroll = new JScrollPane(textMessage); |
243 |
scroll.setPreferredSize(new Dimension(500, 300)); |
244 |
toolsSwingManager.setDefaultPopupMenu(textMessage); |
245 |
textMessage.setContentType("text/html");
|
246 |
textMessage.setText("<html>"+msg+"</html>"); |
247 |
textMessage.setCaretPosition(0);
|
248 |
WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
249 |
Dialog dialog = winmgr.createDialog(
|
250 |
scroll, |
251 |
title, |
252 |
null,
|
253 |
WindowManager_v2.BUTTONS_OK |
254 |
); |
255 |
dialog.show(WindowManager.MODE.DIALOG); |
256 |
} |
257 |
|
258 |
protected void setPreview(Object value) { |
259 |
this.preview = Objects.toString(value, null); |
260 |
this.text.setToolTipText(
|
261 |
"<html>" +
|
262 |
StringUtils.abbreviate(Objects.toString(this.preview,""),100) + |
263 |
"</html>"
|
264 |
); |
265 |
this.menuItemShowPreview.setEnabled(this.isPreviewEnabled()); |
266 |
this.text.setBackground(color_ok);
|
267 |
} |
268 |
|
269 |
protected void setMessage(int mode, String text) { |
270 |
this.message = text;
|
271 |
if( StringUtils.isBlank(text) ) {
|
272 |
this.text.setToolTipText(""); |
273 |
this.menuItemShowMessage.setEnabled(false); |
274 |
} else {
|
275 |
this.text.setToolTipText(
|
276 |
"<html>" +
|
277 |
StringUtils.abbreviate(Objects.toString(text,""),100) + |
278 |
"</html>"
|
279 |
); |
280 |
this.menuItemShowMessage.setEnabled(true); |
281 |
} |
282 |
switch (mode) {
|
283 |
case JOptionPane.INFORMATION_MESSAGE: |
284 |
this.text.setBackground(color_ok);
|
285 |
break;
|
286 |
case JOptionPane.WARNING_MESSAGE: |
287 |
case JOptionPane.ERROR_MESSAGE: |
288 |
default:
|
289 |
this.text.setBackground(COLOR_ERROR);
|
290 |
break;
|
291 |
} |
292 |
} |
293 |
|
294 |
protected void setTip(String theTip) { |
295 |
this.tip = theTip;
|
296 |
this.menuItemShowTip.setEnabled(this.isTipEnabled()); |
297 |
} |
298 |
|
299 |
protected abstract Expression getExpression(); |
300 |
} |