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 / DefaultExpressionPickerController.java @ 44644
History | View | Annotate | Download (10.7 KB)
1 | 43983 | jjdelcerro | package org.gvsig.expressionevaluator.swing.impl; |
---|---|---|---|
2 | |||
3 | import java.awt.event.ActionEvent; |
||
4 | import java.awt.event.ActionListener; |
||
5 | import java.net.URL; |
||
6 | import java.util.Collection; |
||
7 | import java.util.List; |
||
8 | 44267 | jjdelcerro | import java.util.Map; |
9 | 43983 | jjdelcerro | import javax.swing.ImageIcon; |
10 | import javax.swing.JButton; |
||
11 | 44094 | jjdelcerro | import javax.swing.JMenuItem; |
12 | 44080 | jjdelcerro | import javax.swing.JPopupMenu; |
13 | 43983 | jjdelcerro | import javax.swing.text.JTextComponent; |
14 | import org.apache.commons.lang3.StringUtils; |
||
15 | import org.gvsig.expressionevaluator.DataTypes; |
||
16 | import org.gvsig.expressionevaluator.Expression; |
||
17 | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
||
18 | 44263 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
19 | 44163 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionUtils; |
20 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.SymbolTable; |
21 | import org.gvsig.expressionevaluator.swing.Element; |
||
22 | 44259 | jjdelcerro | import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig; |
23 | 43983 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
24 | import org.gvsig.tools.dataTypes.CoercionException; |
||
25 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
26 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
27 | import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
||
28 | import org.gvsig.tools.swing.icontheme.IconTheme; |
||
29 | import org.gvsig.tools.swing.icontheme.IconThemeManager; |
||
30 | import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
||
31 | import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
||
32 | 44080 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
33 | import org.gvsig.tools.swing.api.ToolsSwingManager; |
||
34 | 44390 | jjdelcerro | import org.gvsig.tools.swing.api.bookmarkshistory.ActionEventWithCurrentValue; |
35 | import static org.gvsig.tools.swing.api.bookmarkshistory.ActionEventWithCurrentValue.ID_GETVALUE; |
||
36 | import static org.gvsig.tools.swing.api.bookmarkshistory.ActionEventWithCurrentValue.ID_SETVALUE; |
||
37 | import org.gvsig.tools.swing.api.bookmarkshistory.BookmarksController; |
||
38 | import org.gvsig.tools.swing.api.bookmarkshistory.BookmarksController.BookmarkEvent; |
||
39 | import org.gvsig.tools.swing.api.bookmarkshistory.HistoryController; |
||
40 | import org.gvsig.tools.swing.api.bookmarkshistory.HistoryController.HistoryEvent; |
||
41 | 43983 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.Dialog; |
42 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
43 | import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
||
44 | |||
45 | /**
|
||
46 | *
|
||
47 | * @author jjdelcerro
|
||
48 | */
|
||
49 | public class DefaultExpressionPickerController |
||
50 | extends AbstractPickerController<Expression> |
||
51 | implements ExpressionPickerController
|
||
52 | { |
||
53 | |||
54 | public static void selfRegister() { |
||
55 | URL imageResource = DefaultExpressionPickerController.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-expression.png"); |
||
56 | if (imageResource != null) { |
||
57 | IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
||
58 | IconTheme theme = iconThemeManager.getCurrent(); |
||
59 | ImageIcon icon = new ImageIcon(imageResource); |
||
60 | theme.registerDefault("tools", "picker", "picker-expression", icon, imageResource); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | private final JButton button; |
||
65 | private final JTextComponent text; |
||
66 | private Expression value; |
||
67 | 44098 | jjdelcerro | private final ExpressionBuilderConfig config; |
68 | 43983 | jjdelcerro | private final ExpressionEvaluatorSwingManager manager; |
69 | 44094 | jjdelcerro | private final ActionListener buttonAction; |
70 | private final JMenuItem menuItem; |
||
71 | 44148 | jjdelcerro | private final AbstractAutomaticExpressionChecker automaticExpressionChecker; |
72 | 44163 | jjdelcerro | private boolean returnNullWhenEmptyExpression; |
73 | 44390 | jjdelcerro | private final HistoryController<Expression> historyController; |
74 | private final BookmarksController<Expression> bookmarksController; |
||
75 | 43983 | jjdelcerro | |
76 | 44267 | jjdelcerro | public DefaultExpressionPickerController(ExpressionEvaluatorSwingManager manager,
|
77 | JTextComponent text,
|
||
78 | JButton button,
|
||
79 | JButton btnHistory,
|
||
80 | JButton btnBookmarks
|
||
81 | ) { |
||
82 | 44080 | jjdelcerro | ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
83 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
84 | |||
85 | 43983 | jjdelcerro | this.manager = manager;
|
86 | this.config = new DefaultExpressionBuilderConfig(manager); |
||
87 | this.text = text;
|
||
88 | this.button = button;
|
||
89 | 44533 | jjdelcerro | if( "...".equals(this.button.getText()) ) { |
90 | this.button.setText(""); |
||
91 | } |
||
92 | 44148 | jjdelcerro | this.value = null; |
93 | 44163 | jjdelcerro | this.returnNullWhenEmptyExpression = true; |
94 | 43983 | jjdelcerro | this.button.setIcon(this.getIcon("picker-expression")); |
95 | 44094 | jjdelcerro | this.buttonAction = new ActionListener() { |
96 | 43983 | jjdelcerro | @Override
|
97 | public void actionPerformed(ActionEvent e) { |
||
98 | doSelectExpression(); |
||
99 | } |
||
100 | 44094 | jjdelcerro | }; |
101 | this.button.addActionListener(this.buttonAction); |
||
102 | 43983 | jjdelcerro | |
103 | 44080 | jjdelcerro | swingManager.addClearButton(this.text);
|
104 | swingManager.setDefaultPopupMenu(this.text);
|
||
105 | JPopupMenu popupMenu = this.text.getComponentPopupMenu(); |
||
106 | 44094 | jjdelcerro | this.menuItem = new JMenuItem(i18n.getTranslation("_Show_expression_builder")); |
107 | this.menuItem.addActionListener(buttonAction);
|
||
108 | popupMenu.add(this.menuItem);
|
||
109 | 44148 | jjdelcerro | |
110 | 44390 | jjdelcerro | ActionListener historyAndBookmarkListener = new ActionListener() { |
111 | 44267 | jjdelcerro | @Override
|
112 | 44390 | jjdelcerro | public void actionPerformed(ActionEvent e) { |
113 | ActionEventWithCurrentValue<Expression> ee = (ActionEventWithCurrentValue<Expression>)e; |
||
114 | switch(ee.getID()) {
|
||
115 | case ID_SETVALUE:
|
||
116 | set(ee.getCurrentValue()); |
||
117 | break;
|
||
118 | case ID_GETVALUE:
|
||
119 | ee.setCurrentValue(get()); |
||
120 | break;
|
||
121 | } |
||
122 | 44267 | jjdelcerro | } |
123 | }; |
||
124 | 44390 | jjdelcerro | this.historyController = swingManager.createHistoryController(
|
125 | ExpressionEvaluatorLocator.getManager().getHistory(), |
||
126 | btnHistory |
||
127 | ); |
||
128 | this.historyController.addActionListener(historyAndBookmarkListener);
|
||
129 | 44267 | jjdelcerro | popupMenu.add(this.historyController.createMenu());
|
130 | 44390 | jjdelcerro | |
131 | this.bookmarksController = swingManager.createBookmarksController(
|
||
132 | ExpressionEvaluatorLocator.getManager().getBookmarks(), |
||
133 | btnBookmarks |
||
134 | ); |
||
135 | this.bookmarksController.addActionListener(historyAndBookmarkListener);
|
||
136 | 44267 | jjdelcerro | popupMenu.add(this.bookmarksController.createMenu());
|
137 | |||
138 | 44148 | jjdelcerro | this.automaticExpressionChecker = new AbstractAutomaticExpressionChecker(config, text) { |
139 | @Override
|
||
140 | protected Expression getExpression() { |
||
141 | 44263 | jjdelcerro | return getWithOutHistory();
|
142 | 44148 | jjdelcerro | } |
143 | }; |
||
144 | 43983 | jjdelcerro | } |
145 | |||
146 | private void doSelectExpression() { |
||
147 | 44148 | jjdelcerro | Expression theValue = this.get(); |
148 | 43983 | jjdelcerro | |
149 | DefaultJExpressionBuilder panel = new DefaultJExpressionBuilder(this.manager, this.config); |
||
150 | 44630 | omartinez | panel.setPreviewSymbolTable(this.getPreviewSymbolTable());
|
151 | 44148 | jjdelcerro | panel.setExpression(theValue); |
152 | 43983 | jjdelcerro | |
153 | WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
||
154 | Dialog dialog = windowManager.createDialog(
|
||
155 | panel.asJComponent(), |
||
156 | "Expression builder",
|
||
157 | null,
|
||
158 | WindowManager_v2.BUTTONS_OK_CANCEL |
||
159 | ); |
||
160 | dialog.show(WindowManager.MODE.DIALOG); |
||
161 | if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
||
162 | this.set(panel.getExpression());
|
||
163 | } |
||
164 | } |
||
165 | |||
166 | @Override
|
||
167 | public Expression get() { |
||
168 | 44263 | jjdelcerro | Expression expression = this.getWithOutHistory(); |
169 | ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager(); |
||
170 | 44536 | omartinez | if (expression != null && !expression.isEmpty()) { |
171 | theManager.getHistory().add(expression); |
||
172 | } |
||
173 | 44263 | jjdelcerro | return expression;
|
174 | } |
||
175 | |||
176 | @Override
|
||
177 | public Expression getWithOutHistory() { |
||
178 | 44152 | jjdelcerro | String s = this.text.getText(); |
179 | 44163 | jjdelcerro | if( this.returnNullWhenEmptyExpression ) { |
180 | if( ExpressionUtils.isEmpty(this.value) && StringUtils.isBlank(s) ) { |
||
181 | return null; |
||
182 | } |
||
183 | 44148 | jjdelcerro | } |
184 | 44163 | jjdelcerro | if( this.value == null ) { |
185 | this.value = ExpressionEvaluatorLocator.getManager().createExpression();
|
||
186 | 44152 | jjdelcerro | } |
187 | 44163 | jjdelcerro | this.value.setPhrase(StringUtils.defaultIfBlank(this.text.getText(),null)); |
188 | 43983 | jjdelcerro | return this.value; |
189 | } |
||
190 | |||
191 | @Override
|
||
192 | public void set(Expression value) { |
||
193 | 44079 | jjdelcerro | if( value==null ) { |
194 | this.text.setText(""); |
||
195 | 44148 | jjdelcerro | this.value = null; |
196 | 44163 | jjdelcerro | this.returnNullWhenEmptyExpression = true; |
197 | 44079 | jjdelcerro | } else {
|
198 | 44080 | jjdelcerro | this.value = value;
|
199 | 44079 | jjdelcerro | this.text.setText(this.value.getPhrase()); |
200 | 44163 | jjdelcerro | this.returnNullWhenEmptyExpression = false; |
201 | 44079 | jjdelcerro | } |
202 | 43983 | jjdelcerro | } |
203 | |||
204 | @Override
|
||
205 | public boolean isEmpty() { |
||
206 | boolean empty = StringUtils.isBlank(this.text.getText()); |
||
207 | return empty;
|
||
208 | } |
||
209 | |||
210 | |||
211 | @Override
|
||
212 | public void coerceAndSet(Object value) { |
||
213 | try {
|
||
214 | DataTypesManager.Coercion toExpression = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.EXPRESSION); |
||
215 | this.set((Expression) toExpression.coerce(value)); |
||
216 | } catch (CoercionException ex) {
|
||
217 | throw new IllegalArgumentException("Can't converto to color", ex); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | @Override
|
||
222 | public void setEnabled(boolean enabled) { |
||
223 | this.text.setEnabled(enabled);
|
||
224 | this.button.setEnabled(enabled);
|
||
225 | } |
||
226 | |||
227 | @Override
|
||
228 | public boolean isEnabled() { |
||
229 | return this.text.isEnabled(); |
||
230 | } |
||
231 | |||
232 | @Override
|
||
233 | public void setEditable(boolean editable) { |
||
234 | super.setEditable(editable);
|
||
235 | this.text.setEditable(editable);
|
||
236 | this.button.setEnabled(editable);
|
||
237 | } |
||
238 | |||
239 | @Override
|
||
240 | 44259 | jjdelcerro | public Preferences getPreferences() { |
241 | return this.config.getPreferences(); |
||
242 | 43983 | jjdelcerro | } |
243 | |||
244 | @Override
|
||
245 | public List<Element> getElements() { |
||
246 | return this.config.getElements(); |
||
247 | } |
||
248 | |||
249 | @Override
|
||
250 | 44034 | jjdelcerro | public void addElement(Element element) { |
251 | this.config.addElement(element);
|
||
252 | } |
||
253 | |||
254 | @Override
|
||
255 | 43983 | jjdelcerro | public Collection<SymbolTable> getSymbolTables() { |
256 | return this.config.getSymbolTables(); |
||
257 | } |
||
258 | 43987 | jjdelcerro | |
259 | @Override
|
||
260 | public void addSymbolTable(String name) { |
||
261 | this.config.addSymbolTable(name);
|
||
262 | } |
||
263 | 43983 | jjdelcerro | |
264 | @Override
|
||
265 | public void addSymbolTable(SymbolTable symbolTable) { |
||
266 | this.config.addSymbolTable(symbolTable);
|
||
267 | } |
||
268 | |||
269 | @Override
|
||
270 | public void removeAllSymbolTables() { |
||
271 | this.config.removeAllSymbolTables();
|
||
272 | } |
||
273 | |||
274 | 43984 | jjdelcerro | @Override
|
275 | public void removeSymbolTable(String name) { |
||
276 | this.config.removeSymbolTable(name);
|
||
277 | } |
||
278 | |||
279 | @Override
|
||
280 | 44098 | jjdelcerro | public SymbolTable getPreviewSymbolTable() {
|
281 | return this.config.getPreviewSymbolTable(); |
||
282 | } |
||
283 | |||
284 | @Override
|
||
285 | public void setPreviewSymbolTable(SymbolTable symbolTable) { |
||
286 | this.config.setPreviewSymbolTable(symbolTable);
|
||
287 | } |
||
288 | 44267 | jjdelcerro | |
289 | @Override
|
||
290 | public Object getProperty(String name) { |
||
291 | return this.config.getProperty(name); |
||
292 | } |
||
293 | |||
294 | @Override
|
||
295 | public void setProperty(String name, Object value) { |
||
296 | this.config.setProperty(name, value);
|
||
297 | } |
||
298 | |||
299 | @Override
|
||
300 | public Map<String, Object> getProperties() { |
||
301 | return this.config.getProperties(); |
||
302 | } |
||
303 | 43983 | jjdelcerro | } |