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 @ 43983
History | View | Annotate | Download (5.52 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 | import javax.swing.ImageIcon; |
||
9 | import javax.swing.JButton; |
||
10 | import javax.swing.text.JTextComponent; |
||
11 | import org.apache.commons.lang3.StringUtils; |
||
12 | import org.gvsig.expressionevaluator.DataTypes; |
||
13 | import org.gvsig.expressionevaluator.Expression; |
||
14 | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
||
15 | import org.gvsig.expressionevaluator.SymbolTable; |
||
16 | import org.gvsig.expressionevaluator.swing.Element; |
||
17 | import org.gvsig.tools.ToolsLocator; |
||
18 | import org.gvsig.tools.dataTypes.CoercionException; |
||
19 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
20 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
21 | import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
||
22 | import org.gvsig.tools.swing.icontheme.IconTheme; |
||
23 | import org.gvsig.tools.swing.icontheme.IconThemeManager; |
||
24 | import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig; |
||
25 | import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
||
26 | import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
||
27 | import org.gvsig.tools.swing.api.windowmanager.Dialog; |
||
28 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
29 | import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
||
30 | |||
31 | /**
|
||
32 | *
|
||
33 | * @author jjdelcerro
|
||
34 | */
|
||
35 | public class DefaultExpressionPickerController |
||
36 | extends AbstractPickerController<Expression> |
||
37 | implements ExpressionPickerController
|
||
38 | { |
||
39 | |||
40 | public static void selfRegister() { |
||
41 | URL imageResource = DefaultExpressionPickerController.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-expression.png"); |
||
42 | if (imageResource != null) { |
||
43 | IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
||
44 | IconTheme theme = iconThemeManager.getCurrent(); |
||
45 | ImageIcon icon = new ImageIcon(imageResource); |
||
46 | theme.registerDefault("tools", "picker", "picker-expression", icon, imageResource); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | private final JButton button; |
||
51 | private final JTextComponent text; |
||
52 | private Expression value; |
||
53 | private ExpressionBuilderConfig config;
|
||
54 | private final ExpressionEvaluatorSwingManager manager; |
||
55 | |||
56 | public DefaultExpressionPickerController(ExpressionEvaluatorSwingManager manager, JTextComponent text, JButton button) { |
||
57 | this.manager = manager;
|
||
58 | this.config = new DefaultExpressionBuilderConfig(manager); |
||
59 | this.text = text;
|
||
60 | this.button = button;
|
||
61 | this.value = ExpressionEvaluatorLocator.getManager().createExpression();
|
||
62 | this.button.setIcon(this.getIcon("picker-expression")); |
||
63 | this.button.addActionListener(new ActionListener() { |
||
64 | @Override
|
||
65 | public void actionPerformed(ActionEvent e) { |
||
66 | doSelectExpression(); |
||
67 | } |
||
68 | }); |
||
69 | |||
70 | } |
||
71 | |||
72 | private void doSelectExpression() { |
||
73 | this.value.setPhrase(this.text.getText()); |
||
74 | |||
75 | DefaultJExpressionBuilder panel = new DefaultJExpressionBuilder(this.manager, this.config); |
||
76 | panel.setExpression(this.value);
|
||
77 | |||
78 | WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
||
79 | Dialog dialog = windowManager.createDialog(
|
||
80 | panel.asJComponent(), |
||
81 | "Expression builder",
|
||
82 | null,
|
||
83 | WindowManager_v2.BUTTONS_OK_CANCEL |
||
84 | ); |
||
85 | dialog.show(WindowManager.MODE.DIALOG); |
||
86 | if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
||
87 | this.set(panel.getExpression());
|
||
88 | } |
||
89 | } |
||
90 | |||
91 | @Override
|
||
92 | public Expression get() { |
||
93 | this.value.setPhrase(this.text.getText()); |
||
94 | return this.value; |
||
95 | } |
||
96 | |||
97 | @Override
|
||
98 | public void set(Expression value) { |
||
99 | this.value = value;
|
||
100 | this.text.setText(this.value.getPhrase()); |
||
101 | } |
||
102 | |||
103 | @Override
|
||
104 | public boolean isEmpty() { |
||
105 | boolean empty = StringUtils.isBlank(this.text.getText()); |
||
106 | return empty;
|
||
107 | } |
||
108 | |||
109 | |||
110 | @Override
|
||
111 | public void coerceAndSet(Object value) { |
||
112 | try {
|
||
113 | DataTypesManager.Coercion toExpression = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.EXPRESSION); |
||
114 | this.set((Expression) toExpression.coerce(value)); |
||
115 | } catch (CoercionException ex) {
|
||
116 | throw new IllegalArgumentException("Can't converto to color", ex); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | @Override
|
||
121 | public void setEnabled(boolean enabled) { |
||
122 | this.text.setEnabled(enabled);
|
||
123 | this.button.setEnabled(enabled);
|
||
124 | } |
||
125 | |||
126 | @Override
|
||
127 | public boolean isEnabled() { |
||
128 | return this.text.isEnabled(); |
||
129 | } |
||
130 | |||
131 | @Override
|
||
132 | public void setEditable(boolean editable) { |
||
133 | super.setEditable(editable);
|
||
134 | this.text.setEditable(editable);
|
||
135 | this.button.setEnabled(editable);
|
||
136 | } |
||
137 | |||
138 | @Override
|
||
139 | public int getSimpleElementsLimit() { |
||
140 | return this.config.getSimpleElementsLimit(); |
||
141 | } |
||
142 | |||
143 | @Override
|
||
144 | public void setSimpleElementsLimit(int limit) { |
||
145 | this.config.setSimpleElementsLimit(limit);
|
||
146 | } |
||
147 | |||
148 | @Override
|
||
149 | public List<Element> getElements() { |
||
150 | return this.config.getElements(); |
||
151 | } |
||
152 | |||
153 | @Override
|
||
154 | public Collection<SymbolTable> getSymbolTables() { |
||
155 | return this.config.getSymbolTables(); |
||
156 | } |
||
157 | |||
158 | @Override
|
||
159 | public void addSymbolTable(SymbolTable symbolTable) { |
||
160 | this.config.addSymbolTable(symbolTable);
|
||
161 | } |
||
162 | |||
163 | @Override
|
||
164 | public void removeAllSymbolTables() { |
||
165 | this.config.removeAllSymbolTables();
|
||
166 | } |
||
167 | |||
168 | |||
169 | } |