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 @ 44080
History | View | Annotate | Download (7.03 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 | 44080 | jjdelcerro | import javax.swing.AbstractAction; |
9 | import javax.swing.Action; |
||
10 | 43983 | jjdelcerro | import javax.swing.ImageIcon; |
11 | import javax.swing.JButton; |
||
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 | import org.gvsig.expressionevaluator.SymbolTable; |
||
19 | import org.gvsig.expressionevaluator.swing.Element; |
||
20 | import org.gvsig.tools.ToolsLocator; |
||
21 | import org.gvsig.tools.dataTypes.CoercionException; |
||
22 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
23 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
24 | import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
||
25 | import org.gvsig.tools.swing.icontheme.IconTheme; |
||
26 | import org.gvsig.tools.swing.icontheme.IconThemeManager; |
||
27 | import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig; |
||
28 | import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
||
29 | import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
||
30 | 44080 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
31 | import org.gvsig.tools.swing.api.ToolsSwingManager; |
||
32 | 43983 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.Dialog; |
33 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
34 | import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
||
35 | |||
36 | /**
|
||
37 | *
|
||
38 | * @author jjdelcerro
|
||
39 | */
|
||
40 | public class DefaultExpressionPickerController |
||
41 | extends AbstractPickerController<Expression> |
||
42 | implements ExpressionPickerController
|
||
43 | { |
||
44 | |||
45 | public static void selfRegister() { |
||
46 | URL imageResource = DefaultExpressionPickerController.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-expression.png"); |
||
47 | if (imageResource != null) { |
||
48 | IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
||
49 | IconTheme theme = iconThemeManager.getCurrent(); |
||
50 | ImageIcon icon = new ImageIcon(imageResource); |
||
51 | theme.registerDefault("tools", "picker", "picker-expression", icon, imageResource); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | private final JButton button; |
||
56 | private final JTextComponent text; |
||
57 | private Expression value; |
||
58 | private ExpressionBuilderConfig config;
|
||
59 | private final ExpressionEvaluatorSwingManager manager; |
||
60 | |||
61 | public DefaultExpressionPickerController(ExpressionEvaluatorSwingManager manager, JTextComponent text, JButton button) { |
||
62 | 44080 | jjdelcerro | ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
63 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
64 | |||
65 | 43983 | jjdelcerro | this.manager = manager;
|
66 | this.config = new DefaultExpressionBuilderConfig(manager); |
||
67 | this.text = text;
|
||
68 | this.button = button;
|
||
69 | this.value = ExpressionEvaluatorLocator.getManager().createExpression();
|
||
70 | this.button.setIcon(this.getIcon("picker-expression")); |
||
71 | this.button.addActionListener(new ActionListener() { |
||
72 | @Override
|
||
73 | public void actionPerformed(ActionEvent e) { |
||
74 | doSelectExpression(); |
||
75 | } |
||
76 | }); |
||
77 | |||
78 | 44080 | jjdelcerro | swingManager.addClearButton(this.text);
|
79 | swingManager.setDefaultPopupMenu(this.text);
|
||
80 | JPopupMenu popupMenu = this.text.getComponentPopupMenu(); |
||
81 | Action showExpressionBuilderAction = new AbstractAction(i18n.getTranslation("_Show_expression_builder")) { |
||
82 | @Override
|
||
83 | public void actionPerformed(ActionEvent e) { |
||
84 | doSelectExpression(); |
||
85 | } |
||
86 | }; |
||
87 | popupMenu.addSeparator(); |
||
88 | popupMenu.add(showExpressionBuilderAction); |
||
89 | 43983 | jjdelcerro | } |
90 | |||
91 | private void doSelectExpression() { |
||
92 | this.value.setPhrase(this.text.getText()); |
||
93 | |||
94 | DefaultJExpressionBuilder panel = new DefaultJExpressionBuilder(this.manager, this.config); |
||
95 | panel.setExpression(this.value);
|
||
96 | |||
97 | WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
||
98 | Dialog dialog = windowManager.createDialog(
|
||
99 | panel.asJComponent(), |
||
100 | "Expression builder",
|
||
101 | null,
|
||
102 | WindowManager_v2.BUTTONS_OK_CANCEL |
||
103 | ); |
||
104 | dialog.show(WindowManager.MODE.DIALOG); |
||
105 | if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
||
106 | this.set(panel.getExpression());
|
||
107 | } |
||
108 | } |
||
109 | |||
110 | @Override
|
||
111 | public Expression get() { |
||
112 | this.value.setPhrase(this.text.getText()); |
||
113 | return this.value; |
||
114 | } |
||
115 | |||
116 | @Override
|
||
117 | public void set(Expression value) { |
||
118 | 44079 | jjdelcerro | if( value==null ) { |
119 | this.text.setText(""); |
||
120 | 44080 | jjdelcerro | this.value.setPhrase(""); |
121 | 44079 | jjdelcerro | } else {
|
122 | 44080 | jjdelcerro | this.value = value;
|
123 | 44079 | jjdelcerro | this.text.setText(this.value.getPhrase()); |
124 | } |
||
125 | 43983 | jjdelcerro | } |
126 | |||
127 | @Override
|
||
128 | public boolean isEmpty() { |
||
129 | boolean empty = StringUtils.isBlank(this.text.getText()); |
||
130 | return empty;
|
||
131 | } |
||
132 | |||
133 | |||
134 | @Override
|
||
135 | public void coerceAndSet(Object value) { |
||
136 | try {
|
||
137 | DataTypesManager.Coercion toExpression = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.EXPRESSION); |
||
138 | this.set((Expression) toExpression.coerce(value)); |
||
139 | } catch (CoercionException ex) {
|
||
140 | throw new IllegalArgumentException("Can't converto to color", ex); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | @Override
|
||
145 | public void setEnabled(boolean enabled) { |
||
146 | this.text.setEnabled(enabled);
|
||
147 | this.button.setEnabled(enabled);
|
||
148 | } |
||
149 | |||
150 | @Override
|
||
151 | public boolean isEnabled() { |
||
152 | return this.text.isEnabled(); |
||
153 | } |
||
154 | |||
155 | @Override
|
||
156 | public void setEditable(boolean editable) { |
||
157 | super.setEditable(editable);
|
||
158 | this.text.setEditable(editable);
|
||
159 | this.button.setEnabled(editable);
|
||
160 | } |
||
161 | |||
162 | @Override
|
||
163 | public int getSimpleElementsLimit() { |
||
164 | return this.config.getSimpleElementsLimit(); |
||
165 | } |
||
166 | |||
167 | @Override
|
||
168 | public void setSimpleElementsLimit(int limit) { |
||
169 | this.config.setSimpleElementsLimit(limit);
|
||
170 | } |
||
171 | |||
172 | @Override
|
||
173 | public List<Element> getElements() { |
||
174 | return this.config.getElements(); |
||
175 | } |
||
176 | |||
177 | @Override
|
||
178 | 44034 | jjdelcerro | public void addElement(Element element) { |
179 | this.config.addElement(element);
|
||
180 | } |
||
181 | |||
182 | @Override
|
||
183 | 43983 | jjdelcerro | public Collection<SymbolTable> getSymbolTables() { |
184 | return this.config.getSymbolTables(); |
||
185 | } |
||
186 | 43987 | jjdelcerro | |
187 | @Override
|
||
188 | public void addSymbolTable(String name) { |
||
189 | this.config.addSymbolTable(name);
|
||
190 | } |
||
191 | 43983 | jjdelcerro | |
192 | @Override
|
||
193 | public void addSymbolTable(SymbolTable symbolTable) { |
||
194 | this.config.addSymbolTable(symbolTable);
|
||
195 | } |
||
196 | |||
197 | @Override
|
||
198 | public void removeAllSymbolTables() { |
||
199 | this.config.removeAllSymbolTables();
|
||
200 | } |
||
201 | |||
202 | 43984 | jjdelcerro | @Override
|
203 | public void removeSymbolTable(String name) { |
||
204 | this.config.removeSymbolTable(name);
|
||
205 | } |
||
206 | |||
207 | @Override
|
||
208 | public void setScriptsEnabled(boolean enabled) { |
||
209 | this.config.setScriptsEnabled(enabled);
|
||
210 | } |
||
211 | |||
212 | @Override
|
||
213 | public boolean getScriptsEnabled() { |
||
214 | return this.config.getScriptsEnabled(); |
||
215 | } |
||
216 | |||
217 | 43983 | jjdelcerro | |
218 | } |