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