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 / DefaultExpressionEvaluatorSwingManager.java @ 46854
History | View | Annotate | Download (15.3 KB)
1 |
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.ArrayList; |
7 |
import java.util.Collection; |
8 |
import java.util.Collections; |
9 |
import java.util.Comparator; |
10 |
import java.util.HashMap; |
11 |
import java.util.List; |
12 |
import java.util.Map; |
13 |
import java.util.function.Predicate; |
14 |
import javax.swing.AbstractAction; |
15 |
import javax.swing.Action; |
16 |
import javax.swing.ImageIcon; |
17 |
import javax.swing.JButton; |
18 |
import javax.swing.JComboBox; |
19 |
import javax.swing.text.JTextComponent; |
20 |
import org.apache.commons.io.FilenameUtils; |
21 |
import org.apache.commons.lang3.StringUtils; |
22 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
23 |
import org.apache.commons.lang3.tuple.Pair; |
24 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
25 |
import org.gvsig.expressionevaluator.Function; |
26 |
import org.gvsig.expressionevaluator.SymbolTable; |
27 |
import org.gvsig.expressionevaluator.swing.CalculatorController; |
28 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
29 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
30 |
import org.gvsig.expressionevaluator.swing.Element; |
31 |
import org.gvsig.expressionevaluator.swing.Element.ConstantElement; |
32 |
import org.gvsig.expressionevaluator.swing.Element.FunctionElement; |
33 |
import org.gvsig.expressionevaluator.swing.Element.GroupElement; |
34 |
import org.gvsig.expressionevaluator.swing.ElementFactory; |
35 |
import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig; |
36 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
37 |
import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
38 |
import org.gvsig.expressionevaluator.swing.FunctionAdditionalPanelFactory; |
39 |
import org.gvsig.expressionevaluator.swing.JExpressionPicker; |
40 |
import org.gvsig.expressionevaluator.swing.JScriptEditor; |
41 |
import org.gvsig.expressionevaluator.swing.JTextComponentProxy; |
42 |
import org.gvsig.expressionevaluator.swing.impl.elements.DefaultAggregateElements; |
43 |
import org.gvsig.expressionevaluator.swing.impl.elements.DefaultConstantValueElement; |
44 |
import org.gvsig.expressionevaluator.swing.impl.elements.DefaultFunctionElement; |
45 |
import org.gvsig.expressionevaluator.swing.impl.elements.DefaultGroupElement; |
46 |
import org.gvsig.tools.ToolsLocator; |
47 |
import org.gvsig.tools.i18n.I18nManager; |
48 |
import org.gvsig.tools.script.Script; |
49 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
50 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
51 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
52 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
53 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
54 |
|
55 |
public class DefaultExpressionEvaluatorSwingManager |
56 |
implements ExpressionEvaluatorSwingManager {
|
57 |
|
58 |
private final Map<String, ElementFactory> elementsFactories; |
59 |
private final Map<String, FunctionAdditionalPanelFactory> functionAdditionalPanelFactories; |
60 |
|
61 |
public DefaultExpressionEvaluatorSwingManager() {
|
62 |
this.elementsFactories = new HashMap<>(); |
63 |
this.functionAdditionalPanelFactories = new HashMap<>(); |
64 |
} |
65 |
|
66 |
@Override
|
67 |
public List<Element> createElements(Collection<SymbolTable> symbolTables, Predicate<Function>isAllowed) { |
68 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
69 |
Map<String, Function> functions = new HashMap<>(); |
70 |
|
71 |
for (SymbolTable symbolTable : symbolTables) {
|
72 |
for (Function function : symbolTable.functions()) {
|
73 |
if( !function.isHidden() && isAllowed != null && isAllowed.test(function) ) { |
74 |
functions.put(function.name().toUpperCase(), function); |
75 |
} |
76 |
} |
77 |
} |
78 |
|
79 |
Map<String, GroupElement> functionGroups = new HashMap<>(); |
80 |
Map<String, GroupElement> operatorGroups = new HashMap<>(); |
81 |
for (Function function : functions.values()) {
|
82 |
if (function.isOperator()) {
|
83 |
String groupName = function.group();
|
84 |
GroupElement group = operatorGroups.get(groupName); |
85 |
if (group == null) { |
86 |
group = new DefaultGroupElement(groupName);
|
87 |
operatorGroups.put(groupName, group); |
88 |
} |
89 |
group.addElement(new DefaultFunctionElement(function));
|
90 |
} else {
|
91 |
String groupName = function.group();
|
92 |
GroupElement group = functionGroups.get(groupName); |
93 |
if (group == null) { |
94 |
group = new DefaultGroupElement(groupName);
|
95 |
functionGroups.put(groupName, group); |
96 |
} |
97 |
group.addElement(new DefaultFunctionElement(function));
|
98 |
} |
99 |
} |
100 |
GroupElement builtinFunctions = new DefaultAggregateElements(this, i18n.getTranslation("_Functions")); |
101 |
for (GroupElement group : functionGroups.values()) {
|
102 |
builtinFunctions.addElement(group); |
103 |
} |
104 |
GroupElement operatorFunctions = new DefaultAggregateElements(this, i18n.getTranslation("_Operators")); |
105 |
for (GroupElement group : operatorGroups.values()) {
|
106 |
operatorFunctions.addElement(group); |
107 |
} |
108 |
List<Element> elements = new ArrayList<>(); |
109 |
elements.add(operatorFunctions); |
110 |
elements.add(builtinFunctions); |
111 |
return elements;
|
112 |
} |
113 |
|
114 |
@Override
|
115 |
public void registerElement(ElementFactory factory) { |
116 |
this.elementsFactories.put(factory.getName().toLowerCase(), factory);
|
117 |
} |
118 |
|
119 |
@Override
|
120 |
public Collection<ElementFactory> getElementFactories() { |
121 |
List<ElementFactory> factories = new ArrayList<>(); |
122 |
factories.addAll(this.elementsFactories.values());
|
123 |
Collections.sort(factories, new Comparator<ElementFactory>() { |
124 |
@Override
|
125 |
public int compare(ElementFactory o1, ElementFactory o2) { |
126 |
return StringUtils.compare(o1.getName(), o2.getName());
|
127 |
} |
128 |
}); |
129 |
return factories;
|
130 |
} |
131 |
|
132 |
private static class CreateJExpressionBuilderAction extends AbstractAction { |
133 |
|
134 |
private JTextComponent jtext; |
135 |
|
136 |
public CreateJExpressionBuilderAction(JTextComponent jtext) { |
137 |
this.jtext = jtext;
|
138 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
139 |
this.putValue(
|
140 |
Action.ACTION_COMMAND_KEY,
|
141 |
"CreateJExpressionBuilderAction"
|
142 |
); |
143 |
this.putValue(
|
144 |
Action.NAME,
|
145 |
i18n.getTranslation("_Show_expression_builder")
|
146 |
); |
147 |
this.putValue(
|
148 |
Action.SMALL_ICON,
|
149 |
ToolsSwingLocator.getIconThemeManager().getCurrent().get("picker-expression")
|
150 |
); |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public void actionPerformed(ActionEvent e) { |
155 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
156 |
WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
157 |
final JExpressionBuilder panel = ExpressionEvaluatorSwingLocator.getManager().createJExpressionBuilder();
|
158 |
final Dialog dialog = winmgr.createDialog( |
159 |
panel.asJComponent(), |
160 |
i18n.getTranslation("_Expression_builder"),
|
161 |
null,
|
162 |
WindowManager_v2.BUTTONS_OK_CANCEL |
163 |
); |
164 |
dialog.addActionListener(new ActionListener() { |
165 |
@Override
|
166 |
public void actionPerformed(ActionEvent e2) { |
167 |
if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
168 |
if( ExpressionUtils.isDynamicText(jtext.getText())) {
|
169 |
jtext.replaceSelection( |
170 |
panel.getExpression().getPhrase() |
171 |
); |
172 |
} else {
|
173 |
jtext.replaceSelection( |
174 |
ExpressionUtils.surroundByDynamicTextTag( |
175 |
panel.getExpression().getPhrase(), |
176 |
true
|
177 |
) |
178 |
); |
179 |
} |
180 |
} |
181 |
} |
182 |
}); |
183 |
dialog.show(WindowManager.MODE.DIALOG); |
184 |
} |
185 |
|
186 |
} |
187 |
|
188 |
@Override
|
189 |
public Action createJExpressionBuilderAction(JTextComponent jtext) { |
190 |
Action action = new CreateJExpressionBuilderAction(jtext); |
191 |
return action;
|
192 |
} |
193 |
|
194 |
@Override
|
195 |
public Action createUniqueTemporalFileAction(JTextComponent jtext, String filename, String fileExtension) { |
196 |
return this.createUniqueTemporalFileAction(jtext, new ImmutablePair(filename, fileExtension)); |
197 |
} |
198 |
|
199 |
@Override
|
200 |
public Action createUniqueTemporalFileAction(JTextComponent jtext, Pair<String, String> nameAndExtension) { |
201 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
202 |
Action uniqueTemporalFileAction = new AbstractAction( |
203 |
i18nManager.getTranslation("_Temporal_file_path")) {
|
204 |
@Override
|
205 |
public void actionPerformed(ActionEvent e) { |
206 |
String fileExtension = nameAndExtension.getRight();
|
207 |
if( !StringUtils.startsWith(fileExtension, ".") ) { |
208 |
fileExtension = "."+fileExtension;
|
209 |
} |
210 |
if (jtext.isEditable() && jtext.isEnabled()) {
|
211 |
jtext.setText("<%=UNIQUETEMPORARYFILE('" + nameAndExtension.getLeft() + fileExtension + "')%>"); |
212 |
} |
213 |
} |
214 |
}; |
215 |
return uniqueTemporalFileAction;
|
216 |
} |
217 |
|
218 |
public JExpressionBuilder createJExpressionBuilder(ExpressionBuilderConfig config) {
|
219 |
DefaultJExpressionBuilder x = new DefaultJExpressionBuilder(
|
220 |
this,
|
221 |
config, |
222 |
new DefaultJExpressionBuilderView2() {
|
223 |
@Override
|
224 |
public ImageIcon loadImage(String imageName) { |
225 |
return loadImageFromIconTheme(imageName);
|
226 |
} |
227 |
} |
228 |
); |
229 |
return x;
|
230 |
} |
231 |
|
232 |
@Override
|
233 |
public JExpressionBuilder createJExpressionBuilder() {
|
234 |
DefaultJExpressionBuilder x = new DefaultJExpressionBuilder(
|
235 |
this,
|
236 |
new DefaultExpressionBuilderConfig(this), |
237 |
new DefaultJExpressionBuilderView2() {
|
238 |
@Override
|
239 |
public ImageIcon loadImage(String imageName) { |
240 |
return loadImageFromIconTheme(imageName);
|
241 |
} |
242 |
} |
243 |
); |
244 |
return x;
|
245 |
} |
246 |
|
247 |
@Override
|
248 |
public JExpressionBuilder createJExpressionBuilderAssistant(final JTextComponent expression) { |
249 |
return this.createJExpressionBuilderAssistant(() -> expression); |
250 |
} |
251 |
|
252 |
@Override
|
253 |
public JExpressionBuilder createJExpressionBuilderAssistant(JTextComponentProxy expression) {
|
254 |
DefaultJExpressionBuilder x = new DefaultJExpressionBuilder(
|
255 |
this,
|
256 |
new DefaultExpressionBuilderConfig(this), |
257 |
new DefaultJExpressionBuilderView3(expression) {
|
258 |
@Override
|
259 |
public ImageIcon loadImage(String imageName) { |
260 |
return loadImageFromIconTheme(imageName);
|
261 |
} |
262 |
} |
263 |
); |
264 |
return x;
|
265 |
} |
266 |
|
267 |
public ImageIcon loadImageFromIconTheme(String imageName) { |
268 |
String name = FilenameUtils.getBaseName(imageName);
|
269 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
270 |
if (theme.exists(name)) {
|
271 |
return theme.get(name);
|
272 |
} |
273 |
URL url = this.getClass().getResource(name + ".png"); |
274 |
if (url == null) { |
275 |
return null; |
276 |
} |
277 |
return new ImageIcon(url); |
278 |
} |
279 |
|
280 |
|
281 |
@Override
|
282 |
public Element createElement(String name, Object... parameters) { |
283 |
ElementFactory factory = this.elementsFactories.get(name.toLowerCase());
|
284 |
if( factory==null ) { |
285 |
throw new RuntimeException("Can't locate element factory '"+name+"'."); |
286 |
} |
287 |
return factory.createElement(parameters);
|
288 |
} |
289 |
|
290 |
@Override
|
291 |
public GroupElement createGroupElement(String name) { |
292 |
GroupElement e = new DefaultGroupElement(name);
|
293 |
return e;
|
294 |
} |
295 |
|
296 |
@Override
|
297 |
public GroupElement createGroupElement(String name, List<Element> elements) { |
298 |
GroupElement e = new DefaultGroupElement(this, name, elements); |
299 |
return e;
|
300 |
} |
301 |
|
302 |
@Override
|
303 |
public FunctionElement createFunctionElement(Function function) {
|
304 |
FunctionElement e = new DefaultFunctionElement(function);
|
305 |
return e;
|
306 |
} |
307 |
|
308 |
@Override
|
309 |
public ConstantElement createConstantElement(Object value, String label) { |
310 |
ConstantElement e = new DefaultConstantValueElement(
|
311 |
value, |
312 |
label, |
313 |
null
|
314 |
); |
315 |
return e;
|
316 |
} |
317 |
|
318 |
@Override
|
319 |
public ConstantElement createConstantElement(Object value, String label, String description) { |
320 |
ConstantElement e = new DefaultConstantValueElement(
|
321 |
value, |
322 |
StringUtils.defaultIfEmpty(label, ""),
|
323 |
description |
324 |
); |
325 |
return e;
|
326 |
} |
327 |
//
|
328 |
// @Override
|
329 |
// public VariableElement createVariableElement(ExpressionBuilderConfig config, String label, String source, String name, String typeName, String description) {
|
330 |
// VariableElement e = new DefaultVariableElement(config, label, source, name, typeName, description);
|
331 |
// return e;
|
332 |
// }
|
333 |
|
334 |
@Override
|
335 |
public ExpressionPickerController createExpressionPickerController(JTextComponent text, JButton button) { |
336 |
ExpressionPickerController c = new DefaultExpressionPickerController(this, text, button, null, null); |
337 |
return c;
|
338 |
} |
339 |
|
340 |
@Override
|
341 |
public ExpressionPickerController createExpressionPickerController(JTextComponent text, JButton button, JButton bookmarks, JButton history) { |
342 |
ExpressionPickerController c = new DefaultExpressionPickerController(this, text, button, bookmarks, history); |
343 |
return c;
|
344 |
} |
345 |
|
346 |
@Override
|
347 |
public ExpressionPickerController createExpressionPickerController(JComboBox combo, JButton button, JButton bookmarks, JButton history) { |
348 |
ExpressionPickerController c = new DefaultExpressionPickerController(this, combo, button, bookmarks, history); |
349 |
return c;
|
350 |
} |
351 |
|
352 |
@Override
|
353 |
public JExpressionPicker createJExpressionPicker() {
|
354 |
JExpressionPicker x = new DefaultJExpressionPicker(this); |
355 |
return x;
|
356 |
} |
357 |
|
358 |
@Override
|
359 |
public CalculatorController createCalculatorController(JTextComponent text, int dataType) { |
360 |
DefaultCalculatorController controller = new DefaultCalculatorController(this,text,dataType); |
361 |
return controller;
|
362 |
} |
363 |
|
364 |
@Override
|
365 |
public void registerAdditionalPanel(FunctionAdditionalPanelFactory factory) { |
366 |
if( factory == null ) { |
367 |
throw new IllegalArgumentException("factory can't be null"); |
368 |
} |
369 |
this.functionAdditionalPanelFactories.put(factory.getName().toUpperCase(),factory);
|
370 |
} |
371 |
|
372 |
@Override
|
373 |
public Collection<FunctionAdditionalPanelFactory> getFunctionAdditionalPanelFactories() { |
374 |
return this.functionAdditionalPanelFactories.values(); |
375 |
} |
376 |
|
377 |
@Override
|
378 |
public FunctionAdditionalPanelFactory getFunctionAdditionalPanelFactory(String functionName) { |
379 |
FunctionAdditionalPanelFactory factory = this.functionAdditionalPanelFactories.get(functionName.toUpperCase());
|
380 |
return factory;
|
381 |
} |
382 |
|
383 |
@Override
|
384 |
public JScriptEditor createJScriptEditor(Script script) {
|
385 |
JScriptEditorImpl editor = new JScriptEditorImpl(script);
|
386 |
return editor;
|
387 |
} |
388 |
} |