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 @ 44544
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 | 44148 | jjdelcerro | panel.setExpression(theValue); |
151 | 43983 | jjdelcerro | |
152 | WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
||
153 | Dialog dialog = windowManager.createDialog(
|
||
154 | panel.asJComponent(), |
||
155 | "Expression builder",
|
||
156 | null,
|
||
157 | WindowManager_v2.BUTTONS_OK_CANCEL |
||
158 | ); |
||
159 | dialog.show(WindowManager.MODE.DIALOG); |
||
160 | if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
||
161 | this.set(panel.getExpression());
|
||
162 | } |
||
163 | } |
||
164 | |||
165 | @Override
|
||
166 | public Expression get() { |
||
167 | 44263 | jjdelcerro | Expression expression = this.getWithOutHistory(); |
168 | ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager(); |
||
169 | 44536 | omartinez | if (expression != null && !expression.isEmpty()) { |
170 | theManager.getHistory().add(expression); |
||
171 | } |
||
172 | 44263 | jjdelcerro | return expression;
|
173 | } |
||
174 | |||
175 | @Override
|
||
176 | public Expression getWithOutHistory() { |
||
177 | 44152 | jjdelcerro | String s = this.text.getText(); |
178 | 44163 | jjdelcerro | if( this.returnNullWhenEmptyExpression ) { |
179 | if( ExpressionUtils.isEmpty(this.value) && StringUtils.isBlank(s) ) { |
||
180 | return null; |
||
181 | } |
||
182 | 44148 | jjdelcerro | } |
183 | 44163 | jjdelcerro | if( this.value == null ) { |
184 | this.value = ExpressionEvaluatorLocator.getManager().createExpression();
|
||
185 | 44152 | jjdelcerro | } |
186 | 44163 | jjdelcerro | this.value.setPhrase(StringUtils.defaultIfBlank(this.text.getText(),null)); |
187 | 43983 | jjdelcerro | return this.value; |
188 | } |
||
189 | |||
190 | @Override
|
||
191 | public void set(Expression value) { |
||
192 | 44079 | jjdelcerro | if( value==null ) { |
193 | this.text.setText(""); |
||
194 | 44148 | jjdelcerro | this.value = null; |
195 | 44163 | jjdelcerro | this.returnNullWhenEmptyExpression = true; |
196 | 44079 | jjdelcerro | } else {
|
197 | 44080 | jjdelcerro | this.value = value;
|
198 | 44079 | jjdelcerro | this.text.setText(this.value.getPhrase()); |
199 | 44163 | jjdelcerro | this.returnNullWhenEmptyExpression = false; |
200 | 44079 | jjdelcerro | } |
201 | 43983 | jjdelcerro | } |
202 | |||
203 | @Override
|
||
204 | public boolean isEmpty() { |
||
205 | boolean empty = StringUtils.isBlank(this.text.getText()); |
||
206 | return empty;
|
||
207 | } |
||
208 | |||
209 | |||
210 | @Override
|
||
211 | public void coerceAndSet(Object value) { |
||
212 | try {
|
||
213 | DataTypesManager.Coercion toExpression = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.EXPRESSION); |
||
214 | this.set((Expression) toExpression.coerce(value)); |
||
215 | } catch (CoercionException ex) {
|
||
216 | throw new IllegalArgumentException("Can't converto to color", ex); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | @Override
|
||
221 | public void setEnabled(boolean enabled) { |
||
222 | this.text.setEnabled(enabled);
|
||
223 | this.button.setEnabled(enabled);
|
||
224 | } |
||
225 | |||
226 | @Override
|
||
227 | public boolean isEnabled() { |
||
228 | return this.text.isEnabled(); |
||
229 | } |
||
230 | |||
231 | @Override
|
||
232 | public void setEditable(boolean editable) { |
||
233 | super.setEditable(editable);
|
||
234 | this.text.setEditable(editable);
|
||
235 | this.button.setEnabled(editable);
|
||
236 | } |
||
237 | |||
238 | @Override
|
||
239 | 44259 | jjdelcerro | public Preferences getPreferences() { |
240 | return this.config.getPreferences(); |
||
241 | 43983 | jjdelcerro | } |
242 | |||
243 | @Override
|
||
244 | public List<Element> getElements() { |
||
245 | return this.config.getElements(); |
||
246 | } |
||
247 | |||
248 | @Override
|
||
249 | 44034 | jjdelcerro | public void addElement(Element element) { |
250 | this.config.addElement(element);
|
||
251 | } |
||
252 | |||
253 | @Override
|
||
254 | 43983 | jjdelcerro | public Collection<SymbolTable> getSymbolTables() { |
255 | return this.config.getSymbolTables(); |
||
256 | } |
||
257 | 43987 | jjdelcerro | |
258 | @Override
|
||
259 | public void addSymbolTable(String name) { |
||
260 | this.config.addSymbolTable(name);
|
||
261 | } |
||
262 | 43983 | jjdelcerro | |
263 | @Override
|
||
264 | public void addSymbolTable(SymbolTable symbolTable) { |
||
265 | this.config.addSymbolTable(symbolTable);
|
||
266 | } |
||
267 | |||
268 | @Override
|
||
269 | public void removeAllSymbolTables() { |
||
270 | this.config.removeAllSymbolTables();
|
||
271 | } |
||
272 | |||
273 | 43984 | jjdelcerro | @Override
|
274 | public void removeSymbolTable(String name) { |
||
275 | this.config.removeSymbolTable(name);
|
||
276 | } |
||
277 | |||
278 | @Override
|
||
279 | 44098 | jjdelcerro | public SymbolTable getPreviewSymbolTable() {
|
280 | return this.config.getPreviewSymbolTable(); |
||
281 | } |
||
282 | |||
283 | @Override
|
||
284 | public void setPreviewSymbolTable(SymbolTable symbolTable) { |
||
285 | this.config.setPreviewSymbolTable(symbolTable);
|
||
286 | } |
||
287 | 44267 | jjdelcerro | |
288 | @Override
|
||
289 | public Object getProperty(String name) { |
||
290 | return this.config.getProperty(name); |
||
291 | } |
||
292 | |||
293 | @Override
|
||
294 | public void setProperty(String name, Object value) { |
||
295 | this.config.setProperty(name, value);
|
||
296 | } |
||
297 | |||
298 | @Override
|
||
299 | public Map<String, Object> getProperties() { |
||
300 | return this.config.getProperties(); |
||
301 | } |
||
302 | 43983 | jjdelcerro | } |