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 @ 44191
History | View | Annotate | Download (10.5 KB)
1 | 44148 | jjdelcerro | 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 | 44191 | jjdelcerro | private final JTextComponent text; |
45 | private final ExpressionBuilderConfig config; |
||
46 | 44148 | jjdelcerro | |
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 | 44191 | jjdelcerro | expression.setSQLCompatible(this.config.isSQLCompatible());
|
177 | 44148 | jjdelcerro | if (this.config.getPreviewSymbolTable() == null) { |
178 | try {
|
||
179 | ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager(); |
||
180 | MutableSymbolTable symbolTable = theManager.createSymbolTable(); |
||
181 | Object theValue = expression.execute(symbolTable);
|
||
182 | this.setPreview(theValue);
|
||
183 | } catch (Exception ex) { |
||
184 | this.setMessage(JOptionPane.INFORMATION_MESSAGE, ""); |
||
185 | } |
||
186 | } else {
|
||
187 | try {
|
||
188 | Object theValue = expression.execute(this.config.getPreviewSymbolTable()); |
||
189 | this.setPreview(theValue);
|
||
190 | } catch (ExpressionRuntimeException ex) {
|
||
191 | this.setTip(ex.getTip());
|
||
192 | this.setMessage(JOptionPane.ERROR_MESSAGE, "Runtime error. " + ex.getMessage()); |
||
193 | } catch (Exception ex) { |
||
194 | this.setMessage(JOptionPane.ERROR_MESSAGE, "Runtime error. " + ex.getMessage()); |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | |||
199 | protected boolean isTipEnabled() { |
||
200 | return !StringUtils.isBlank(this.tip); |
||
201 | } |
||
202 | |||
203 | protected boolean isPreviewEnabled() { |
||
204 | return !StringUtils.isBlank(this.preview); |
||
205 | } |
||
206 | |||
207 | protected boolean isMessageEnabled() { |
||
208 | return !StringUtils.isBlank(this.message); |
||
209 | } |
||
210 | |||
211 | protected String getTip() { |
||
212 | return this.tip; |
||
213 | } |
||
214 | |||
215 | protected String getMessage() { |
||
216 | return this.message; |
||
217 | } |
||
218 | |||
219 | protected String getPreview() { |
||
220 | return this.preview; |
||
221 | } |
||
222 | |||
223 | protected void showMessageDialog() { |
||
224 | final I18nManager i18n = ToolsLocator.getI18nManager();
|
||
225 | if( this.isMessageEnabled() ) { |
||
226 | this.showMessageDialog(
|
||
227 | i18n.getTranslation("_Message"),
|
||
228 | this.getMessage()
|
||
229 | ); |
||
230 | } else if( this.isPreviewEnabled()) { |
||
231 | this.showMessageDialog(
|
||
232 | i18n.getTranslation("_Preview_value"),
|
||
233 | this.getPreview()
|
||
234 | ); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | protected void showMessageDialog(String title, String msg) { |
||
239 | final I18nManager i18n = ToolsLocator.getI18nManager();
|
||
240 | final ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
|
||
241 | |||
242 | JEditorPane textMessage = new JEditorPane(); |
||
243 | JScrollPane scroll = new JScrollPane(textMessage); |
||
244 | scroll.setPreferredSize(new Dimension(500, 300)); |
||
245 | toolsSwingManager.setDefaultPopupMenu(textMessage); |
||
246 | textMessage.setContentType("text/html");
|
||
247 | textMessage.setText("<html>"+msg+"</html>"); |
||
248 | textMessage.setCaretPosition(0);
|
||
249 | WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
||
250 | Dialog dialog = winmgr.createDialog(
|
||
251 | scroll, |
||
252 | title, |
||
253 | null,
|
||
254 | WindowManager_v2.BUTTONS_OK |
||
255 | ); |
||
256 | dialog.show(WindowManager.MODE.DIALOG); |
||
257 | } |
||
258 | |||
259 | protected void setPreview(Object value) { |
||
260 | this.preview = Objects.toString(value, null); |
||
261 | this.text.setToolTipText(
|
||
262 | "<html>" +
|
||
263 | StringUtils.abbreviate(Objects.toString(this.preview,""),100) + |
||
264 | "</html>"
|
||
265 | ); |
||
266 | this.menuItemShowPreview.setEnabled(this.isPreviewEnabled()); |
||
267 | this.text.setBackground(color_ok);
|
||
268 | } |
||
269 | |||
270 | protected void setMessage(int mode, String text) { |
||
271 | this.message = text;
|
||
272 | if( StringUtils.isBlank(text) ) {
|
||
273 | this.text.setToolTipText(""); |
||
274 | this.menuItemShowMessage.setEnabled(false); |
||
275 | } else {
|
||
276 | this.text.setToolTipText(
|
||
277 | "<html>" +
|
||
278 | StringUtils.abbreviate(Objects.toString(text,""),100) + |
||
279 | "</html>"
|
||
280 | ); |
||
281 | this.menuItemShowMessage.setEnabled(true); |
||
282 | } |
||
283 | switch (mode) {
|
||
284 | case JOptionPane.INFORMATION_MESSAGE: |
||
285 | this.text.setBackground(color_ok);
|
||
286 | break;
|
||
287 | case JOptionPane.WARNING_MESSAGE: |
||
288 | case JOptionPane.ERROR_MESSAGE: |
||
289 | default:
|
||
290 | this.text.setBackground(COLOR_ERROR);
|
||
291 | break;
|
||
292 | } |
||
293 | } |
||
294 | |||
295 | protected void setTip(String theTip) { |
||
296 | this.tip = theTip;
|
||
297 | this.menuItemShowTip.setEnabled(this.isTipEnabled()); |
||
298 | } |
||
299 | |||
300 | protected abstract Expression getExpression(); |
||
301 | } |