root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tables / filterVector / FilterVectorModelerParametersPanel.java @ 59
History | View | Annotate | Download (12.4 KB)
1 |
package es.unex.sextante.tables.filterVector; |
---|---|
2 |
|
3 |
import info.clearthought.layout.TableLayout; |
4 |
import info.clearthought.layout.TableLayoutConstants; |
5 |
|
6 |
import java.awt.event.MouseAdapter; |
7 |
import java.awt.event.MouseEvent; |
8 |
import java.awt.event.MouseListener; |
9 |
import java.util.HashMap; |
10 |
import java.util.Iterator; |
11 |
import java.util.Set; |
12 |
|
13 |
import javax.swing.BorderFactory; |
14 |
import javax.swing.DefaultComboBoxModel; |
15 |
import javax.swing.JComboBox; |
16 |
import javax.swing.JLabel; |
17 |
import javax.swing.JPanel; |
18 |
import javax.swing.JScrollPane; |
19 |
import javax.swing.JTextArea; |
20 |
import javax.swing.JTree; |
21 |
import javax.swing.ScrollPaneConstants; |
22 |
import javax.swing.border.BevelBorder; |
23 |
import javax.swing.tree.DefaultMutableTreeNode; |
24 |
import javax.swing.tree.DefaultTreeCellRenderer; |
25 |
import javax.swing.tree.DefaultTreeModel; |
26 |
import javax.swing.tree.TreePath; |
27 |
|
28 |
import org.nfunk.jep.JEP; |
29 |
|
30 |
import es.unex.sextante.core.ObjectAndDescription; |
31 |
import es.unex.sextante.core.OutputObjectsSet; |
32 |
import es.unex.sextante.core.Sextante; |
33 |
import es.unex.sextante.exceptions.WrongOutputIDException; |
34 |
import es.unex.sextante.gui.modeler.GeoAlgorithmModelerParametersPanel; |
35 |
import es.unex.sextante.gui.modeler.OutputLayerSettingsPanel; |
36 |
import es.unex.sextante.modeler.elements.ModelElementNumericalValue; |
37 |
import es.unex.sextante.modeler.elements.ModelElementTableField; |
38 |
import es.unex.sextante.modeler.elements.ModelElementVectorLayer; |
39 |
import es.unex.sextante.outputs.Output; |
40 |
import es.unex.sextante.parameters.Parameter; |
41 |
|
42 |
public class FilterVectorModelerParametersPanel |
43 |
extends
|
44 |
GeoAlgorithmModelerParametersPanel { |
45 |
|
46 |
private JTextArea jTextExpression; |
47 |
private JScrollPane jScrollPane; |
48 |
private JTree jTree; |
49 |
private JScrollPane jScrollPane2; |
50 |
private CalculatorKeysPanel m_KeysPanel;
|
51 |
private HashMap m_Constants; |
52 |
|
53 |
private OutputLayerSettingsPanel m_OutputLayerSettingsPanel;
|
54 |
private JPanel jPanel; |
55 |
private JComboBox comboBox; |
56 |
|
57 |
|
58 |
public FilterVectorModelerParametersPanel() {
|
59 |
|
60 |
super();
|
61 |
|
62 |
} |
63 |
|
64 |
|
65 |
@Override
|
66 |
protected void initGUI() { |
67 |
|
68 |
this.setPreferredSize(new java.awt.Dimension(570, 350)); |
69 |
final TableLayout thisLayout = new TableLayout(new double[][] { |
70 |
{ 10.0, TableLayoutConstants.FILL, TableLayoutConstants.FILL, TableLayoutConstants.FILL,
|
71 |
TableLayoutConstants.FILL, 10.0 },
|
72 |
{ 10.0, TableLayoutConstants.FILL, TableLayoutConstants.FILL, TableLayoutConstants.FILL, 50.0, 5.0, 50.0, 10.0 } }); |
73 |
thisLayout.setHGap(10);
|
74 |
thisLayout.setVGap(10);
|
75 |
this.setLayout(thisLayout);
|
76 |
this.setSize(new java.awt.Dimension(350, 350)); |
77 |
{ |
78 |
jScrollPane = new JScrollPane(); |
79 |
this.add(jScrollPane, "1, 4, 4, 4"); |
80 |
jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
|
81 |
{ |
82 |
try {
|
83 |
jTextExpression = new JTextArea(); |
84 |
jScrollPane.setViewportView(jTextExpression); |
85 |
jTextExpression.setPreferredSize(new java.awt.Dimension(0, 0)); |
86 |
jTextExpression.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); |
87 |
final Parameter param = m_Algorithm.getParameters().getParameter(FilterVectorAlgorithm.FORMULA);
|
88 |
final ObjectAndDescription oad = ((ObjectAndDescription) getParameterValue(param));
|
89 |
if (oad != null) { |
90 |
final String sFormula = (String) oad.getObject(); |
91 |
jTextExpression.setText(sFormula); |
92 |
} |
93 |
} |
94 |
catch (final Exception e) { |
95 |
Sextante.addErrorToLog(e); |
96 |
} |
97 |
|
98 |
|
99 |
} |
100 |
} |
101 |
{ |
102 |
m_KeysPanel = new CalculatorKeysPanel(jTextExpression);
|
103 |
this.add(m_KeysPanel, "3, 1, 4, 3"); |
104 |
} |
105 |
{ |
106 |
jPanel = new JPanel(); |
107 |
final TableLayout panelLayout = new TableLayout(new double[][] { { TableLayoutConstants.FILL }, |
108 |
{ 20, TableLayoutConstants.FILL } });
|
109 |
jPanel.setLayout(panelLayout); |
110 |
this.add(jPanel, "1, 1, 2, 3"); |
111 |
jScrollPane2 = new JScrollPane(); |
112 |
jPanel.add(jScrollPane2, "0,1");
|
113 |
jTree = new JTree(); |
114 |
final MouseListener ml = new MouseAdapter() { |
115 |
@Override
|
116 |
public void mousePressed(MouseEvent e) { |
117 |
int iRow = jTree.getRowForLocation(e.getX(), e.getY());
|
118 |
TreePath path = jTree.getPathForLocation(e.getX(), e.getY());
|
119 |
if ((iRow != -1) && (e.getClickCount() == 2)) { |
120 |
insertTextFromTree(path); |
121 |
} |
122 |
} |
123 |
}; |
124 |
jTree.addMouseListener(ml); |
125 |
jScrollPane2.setViewportView(jTree); |
126 |
jScrollPane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
|
127 |
jTree.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); |
128 |
|
129 |
comboBox = new JComboBox(); |
130 |
final ObjectAndDescription[] layers = getElementsOfClass(ModelElementVectorLayer.class, false); |
131 |
final DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(layers); |
132 |
comboBox.setModel(defaultModel); |
133 |
|
134 |
jPanel.add(comboBox, "0,0");
|
135 |
populateTree(); |
136 |
} |
137 |
{ |
138 |
final OutputObjectsSet oosetGlobal = this.m_GlobalAlgorithm.getOutputObjects(); |
139 |
m_OutputLayerSettingsPanel = new OutputLayerSettingsPanel();
|
140 |
String sDescription = Sextante.getText("Result"); |
141 |
final String sKey = FilterVectorAlgorithm.RESULT; |
142 |
|
143 |
if (!oosetGlobal.containsKey(sKey + this.m_sAlgorithmName)) { |
144 |
sDescription = "\"" + sDescription + "\" " + Sextante.getText("from") + " " + m_sAlgorithmDescription; |
145 |
m_OutputLayerSettingsPanel.setKeepAsFinalResult(false);
|
146 |
} |
147 |
else {
|
148 |
Output out; |
149 |
try {
|
150 |
out = oosetGlobal.getOutput(sKey + this.m_sAlgorithmName);
|
151 |
sDescription = out.getDescription(); |
152 |
m_OutputLayerSettingsPanel.setKeepAsFinalResult(true);
|
153 |
} |
154 |
catch (final WrongOutputIDException e) { |
155 |
//cannot reach this
|
156 |
} |
157 |
|
158 |
} |
159 |
m_OutputLayerSettingsPanel.setName(sDescription); |
160 |
|
161 |
this.add(m_OutputLayerSettingsPanel, "2, 6, 4, 6"); |
162 |
this.add(new JLabel(Sextante.getText("Result")), "1,6,2,6"); |
163 |
} |
164 |
|
165 |
} |
166 |
|
167 |
|
168 |
private void populateTree() { |
169 |
|
170 |
int i;
|
171 |
final int j; |
172 |
|
173 |
jTree.setModel(null);
|
174 |
|
175 |
final DefaultMutableTreeNode main = new DefaultMutableTreeNode(Sextante.getText("ELEMENTS")); |
176 |
|
177 |
//functions
|
178 |
final String sFunctions[] = { "sin", "cos", "tan", "asin", "acos", "atan", "atan2", "sinh", "cosh", "tanh", "asinh", |
179 |
"acosh", "atanh", "ln", "log", "exp", "abs", "rand", "mod", "sqrt", "if" }; |
180 |
DefaultMutableTreeNode node = new DefaultMutableTreeNode(Sextante.getText("Functions")); |
181 |
DefaultMutableTreeNode child;
|
182 |
for (i = 0; i < sFunctions.length; i++) { |
183 |
child = new DefaultMutableTreeNode(" " + sFunctions[i] + "() "); |
184 |
node.add(child); |
185 |
} |
186 |
main.add(node); |
187 |
|
188 |
//operators
|
189 |
final String sOperators[] = { "+", "-", "*", "/", "%", "!", "^", "&&", "||", "<", ">", "<=", ">=", "==", "!=" }; |
190 |
node = new DefaultMutableTreeNode(Sextante.getText("Operators")); |
191 |
for (i = 0; i < sOperators.length; i++) { |
192 |
final String s = " " + sOperators[i] + " "; |
193 |
child = new DefaultMutableTreeNode(new ObjectAndDescription(s, s)); |
194 |
node.add(child); |
195 |
} |
196 |
main.add(node); |
197 |
|
198 |
//numerical values
|
199 |
ObjectAndDescription[] values = getElementsOfClass(ModelElementNumericalValue.class, false); |
200 |
node = new DefaultMutableTreeNode(Sextante.getText("Numerical_values")); |
201 |
for (i = 0; i < values.length; i++) { |
202 |
final String sName = values[i].getDescription(); |
203 |
final String sVariableName = values[i].getObject().toString(); |
204 |
child = new DefaultMutableTreeNode(new ObjectAndDescription(sName, sVariableName)); |
205 |
node.add(child); |
206 |
} |
207 |
main.add(node); |
208 |
|
209 |
//fields
|
210 |
values = getElementsOfClass(ModelElementTableField.class, false);
|
211 |
node = new DefaultMutableTreeNode(Sextante.getText("Fields")); |
212 |
for (i = 0; i < values.length; i++) { |
213 |
final String sName = values[i].getDescription(); |
214 |
final String sVariableName = values[i].getObject().toString(); |
215 |
child = new DefaultMutableTreeNode(new ObjectAndDescription(sName, sVariableName)); |
216 |
node.add(child); |
217 |
} |
218 |
main.add(node); |
219 |
|
220 |
|
221 |
//constants
|
222 |
node = new DefaultMutableTreeNode(Sextante.getText("Constants")); |
223 |
m_Constants = new HashMap(); |
224 |
m_Constants.put("e", " " + Double.toString(Math.E) + " "); |
225 |
m_Constants.put("Pi", " " + Double.toString(Math.PI) + " "); |
226 |
|
227 |
final Set set = m_Constants.keySet(); |
228 |
final Iterator iter = set.iterator(); |
229 |
while (iter.hasNext()) {
|
230 |
final String s = (String) iter.next(); |
231 |
child = new DefaultMutableTreeNode(new ObjectAndDescription(s, s)); |
232 |
node.add(child); |
233 |
} |
234 |
|
235 |
main.add(node); |
236 |
|
237 |
jTree.setModel(new DefaultTreeModel(main)); |
238 |
final DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) jTree.getCellRenderer(); |
239 |
renderer.setOpenIcon(null);
|
240 |
renderer.setClosedIcon(null);
|
241 |
renderer.setLeafIcon(null);
|
242 |
|
243 |
} |
244 |
|
245 |
|
246 |
private void insertTextFromTree(final TreePath path) { |
247 |
|
248 |
final TreePath parent = path.getParentPath(); |
249 |
|
250 |
if ((parent != null) && !parent.toString().equals("[" + Sextante.getText("ELEMENTS") + "]")) { |
251 |
final String sParentName = parent.toString(); |
252 |
final DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); |
253 |
String s = (String) ((ObjectAndDescription) node.getUserObject()).getObject(); |
254 |
|
255 |
if (sParentName.equals("[" + Sextante.getText("ELEMENTS") + ", " + Sextante.getText("Constants]"))) { |
256 |
if (m_Constants.containsKey(s)) {
|
257 |
s = (String) m_Constants.get(s);
|
258 |
} |
259 |
else {
|
260 |
s = "";
|
261 |
} |
262 |
} |
263 |
|
264 |
jTextExpression.insert(s, jTextExpression.getCaretPosition()); |
265 |
|
266 |
if (sParentName.equals("[" + Sextante.getText("ELEMENTS") + ", " + Sextante.getText("Functions]"))) { |
267 |
jTextExpression.setCaretPosition(jTextExpression.getCaretPosition() - 2);
|
268 |
} |
269 |
} |
270 |
|
271 |
} |
272 |
|
273 |
|
274 |
@Override
|
275 |
public boolean assignParameters(final HashMap map) { |
276 |
|
277 |
map.put(FilterVectorAlgorithm.LAYER, ((ObjectAndDescription) comboBox.getSelectedItem()).getObject()); |
278 |
|
279 |
final String sFormulaKey = getInnerParameterKey(); |
280 |
map.put(FilterVectorAlgorithm.FORMULA, sFormulaKey); |
281 |
final String sFormula = jTextExpression.getText(); |
282 |
m_DataObjects.put(sFormulaKey, new ObjectAndDescription("String", sFormula)); |
283 |
|
284 |
final JEP jep = new JEP(); |
285 |
jep.addStandardConstants(); |
286 |
jep.addStandardFunctions(); |
287 |
|
288 |
ObjectAndDescription[] values = getElementsOfClass(ModelElementNumericalValue.class, false); |
289 |
for (int i = 0; i < values.length; i++) { |
290 |
if (sFormula.contains(values[i].getObject().toString())) {
|
291 |
jep.addVariable(values[i].getObject().toString(), 0);
|
292 |
map.put("DUMMY" + this.getInnerParameterKey(), values[i].getObject().toString()); |
293 |
} |
294 |
} |
295 |
values = getElementsOfClass(ModelElementTableField.class, false);
|
296 |
for (int i = 0; i < values.length; i++) { |
297 |
if (sFormula.contains(values[i].getObject().toString())) {
|
298 |
jep.addVariable(values[i].getObject().toString(), 0);
|
299 |
map.put("DUMMY" + this.getInnerParameterKey(), values[i].getObject().toString()); |
300 |
} |
301 |
} |
302 |
|
303 |
jep.setAllowUndeclared(true);
|
304 |
jep.parseExpression(sFormula); |
305 |
|
306 |
if (jep.hasError()) {
|
307 |
return false; |
308 |
} |
309 |
|
310 |
final OutputObjectsSet oosetGlobal = this.m_GlobalAlgorithm.getOutputObjects(); |
311 |
final OutputObjectsSet ooset = this.m_Algorithm.getOutputObjects(); |
312 |
|
313 |
final String sName = FilterVectorAlgorithm.RESULT + this.m_sAlgorithmName; |
314 |
if (m_OutputLayerSettingsPanel.getKeepAsFinalResult()) {
|
315 |
try {
|
316 |
final Output out = ooset.getOutput(FilterVectorAlgorithm.RESULT);
|
317 |
final Output outToAdd = out.getClass().newInstance();
|
318 |
outToAdd.setName(sName); |
319 |
outToAdd.setDescription(m_OutputLayerSettingsPanel.getName()); |
320 |
oosetGlobal.add(outToAdd); |
321 |
} |
322 |
catch (final Exception e) {} |
323 |
} |
324 |
else {
|
325 |
oosetGlobal.remove(sName); |
326 |
} |
327 |
|
328 |
return true; |
329 |
|
330 |
} |
331 |
|
332 |
} |