root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / es / unex / sextante / gridCalculus / gridCalculator / GridCalculatorParametersPanel.java @ 56
History | View | Annotate | Download (10.3 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.gridCalculus.gridCalculator; |
4 |
|
5 |
import info.clearthought.layout.TableLayout; |
6 |
import info.clearthought.layout.TableLayoutConstants; |
7 |
|
8 |
import java.awt.event.MouseAdapter; |
9 |
import java.awt.event.MouseEvent; |
10 |
import java.awt.event.MouseListener; |
11 |
import java.util.ArrayList; |
12 |
import java.util.Iterator; |
13 |
import java.util.List; |
14 |
import java.util.Set; |
15 |
import java.util.TreeMap; |
16 |
|
17 |
import javax.swing.BorderFactory; |
18 |
import javax.swing.JLabel; |
19 |
import javax.swing.JScrollPane; |
20 |
import javax.swing.JTextArea; |
21 |
import javax.swing.JTree; |
22 |
import javax.swing.ScrollPaneConstants; |
23 |
import javax.swing.border.BevelBorder; |
24 |
import javax.swing.tree.DefaultMutableTreeNode; |
25 |
import javax.swing.tree.DefaultTreeCellRenderer; |
26 |
import javax.swing.tree.DefaultTreeModel; |
27 |
import javax.swing.tree.TreePath; |
28 |
|
29 |
import es.unex.sextante.core.GeoAlgorithm; |
30 |
import es.unex.sextante.core.OutputObjectsSet; |
31 |
import es.unex.sextante.core.Sextante; |
32 |
import es.unex.sextante.dataObjects.IRasterLayer; |
33 |
import es.unex.sextante.exceptions.WrongInputException; |
34 |
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel; |
35 |
import es.unex.sextante.gui.algorithm.OutputChannelSelectionPanel; |
36 |
import es.unex.sextante.gui.core.SextanteGUI; |
37 |
import es.unex.sextante.outputs.Output; |
38 |
import es.unex.sextante.parameters.RasterLayerAndBand; |
39 |
|
40 |
|
41 |
public class GridCalculatorParametersPanel |
42 |
extends
|
43 |
GeoAlgorithmParametersPanel { |
44 |
|
45 |
private JTextArea jTextExpression; |
46 |
private JScrollPane jScrollPane; |
47 |
private JTree jTree; |
48 |
private JScrollPane jScrollPane1; |
49 |
private CalculatorKeysPanel m_KeysPanel;
|
50 |
private TreeMap m_Constants; |
51 |
private GeoAlgorithm m_Algorithm;
|
52 |
private OutputChannelSelectionPanel outputChannelSelectionPanel;
|
53 |
|
54 |
|
55 |
public GridCalculatorParametersPanel() {
|
56 |
|
57 |
super();
|
58 |
|
59 |
} |
60 |
|
61 |
|
62 |
@Override
|
63 |
public void init(final GeoAlgorithm algorithm) { |
64 |
|
65 |
m_Algorithm = algorithm; |
66 |
|
67 |
initGUI(); |
68 |
|
69 |
} |
70 |
|
71 |
|
72 |
private void initGUI() { |
73 |
|
74 |
this.setPreferredSize(new java.awt.Dimension(570, 350)); |
75 |
final TableLayout thisLayout = new TableLayout(new double[][] { |
76 |
{ 10.0, TableLayoutConstants.FILL, TableLayoutConstants.FILL, TableLayoutConstants.FILL,
|
77 |
TableLayoutConstants.FILL, 10.0 },
|
78 |
{ 10.0, TableLayoutConstants.FILL, TableLayoutConstants.FILL, TableLayoutConstants.FILL, 50.0, 5.0, 20.0, 5, 20, |
79 |
10.0 } });
|
80 |
thisLayout.setHGap(10);
|
81 |
thisLayout.setVGap(10);
|
82 |
this.setLayout(thisLayout);
|
83 |
this.setSize(new java.awt.Dimension(350, 350)); |
84 |
{ |
85 |
jTextExpression = new JTextArea(); |
86 |
jTextExpression.setLineWrap(true);
|
87 |
jScrollPane = new JScrollPane(jTextExpression); |
88 |
this.add(jScrollPane, "1, 4, 4, 4"); |
89 |
jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
|
90 |
jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
|
91 |
jTextExpression.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); |
92 |
} |
93 |
{ |
94 |
m_KeysPanel = new CalculatorKeysPanel(jTextExpression);
|
95 |
this.add(m_KeysPanel, "3, 1, 4, 3"); |
96 |
} |
97 |
{ |
98 |
jScrollPane1 = new JScrollPane(); |
99 |
this.add(jScrollPane1, "1, 1, 2, 3"); |
100 |
{ |
101 |
jTree = new JTree(); |
102 |
jScrollPane1.setViewportView(jTree); |
103 |
jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
|
104 |
jTree.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); |
105 |
final MouseListener ml = new MouseAdapter() { |
106 |
@Override
|
107 |
public void mousePressed(MouseEvent e) { |
108 |
int iRow = jTree.getRowForLocation(e.getX(), e.getY());
|
109 |
TreePath path = jTree.getPathForLocation(e.getX(), e.getY());
|
110 |
if ((iRow != -1) && (e.getClickCount() == 2)) { |
111 |
insertTextFromTree(path); |
112 |
} |
113 |
} |
114 |
}; |
115 |
jTree.addMouseListener(ml); |
116 |
populateTree(); |
117 |
} |
118 |
} |
119 |
try {
|
120 |
final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
|
121 |
final Output out = ooSet.getOutput(GridCalculatorAlgorithm.RESULT);
|
122 |
outputChannelSelectionPanel = new OutputChannelSelectionPanel(out, m_Algorithm.getParameters());
|
123 |
this.add(outputChannelSelectionPanel, "3,6,4,6"); |
124 |
this.add(new JLabel(Sextante.getText("Result")), "1,6,2,6"); |
125 |
} |
126 |
catch (final Exception e) { |
127 |
Sextante.addErrorToLog(e); |
128 |
} |
129 |
|
130 |
} |
131 |
|
132 |
|
133 |
private void populateTree() { |
134 |
|
135 |
int i, j;
|
136 |
double dCellsize;
|
137 |
String sName;
|
138 |
|
139 |
jTree.setModel(null);
|
140 |
|
141 |
//layers
|
142 |
final IRasterLayer[] layers = SextanteGUI.getInputFactory().getRasterLayers(); |
143 |
final DefaultMutableTreeNode main = new DefaultMutableTreeNode(Sextante.getText("ELEMENTS")); |
144 |
DefaultMutableTreeNode node = new DefaultMutableTreeNode(Sextante.getText("Layers")); |
145 |
DefaultMutableTreeNode child;
|
146 |
for (i = 0; i < layers.length; i++) { |
147 |
for (j = 0; j < layers[i].getBandsCount(); j++) { |
148 |
sName = layers[i].getName() + " Band " + Integer.toString(j + 1); |
149 |
child = new DefaultMutableTreeNode(sName); |
150 |
node.add(child); |
151 |
} |
152 |
} |
153 |
main.add(node); |
154 |
|
155 |
//functions
|
156 |
final String sFunctions[] = { "sin", "cos", "tan", "asin", "acos", "atan", "atan2", "sinh", "cosh", "tanh", "asinh", |
157 |
"acosh", "atanh", "ln", "log", "exp", "abs", "rand", "mod", "sqrt", "if" }; |
158 |
node = new DefaultMutableTreeNode(Sextante.getText("Functions")); |
159 |
for (i = 0; i < sFunctions.length; i++) { |
160 |
child = new DefaultMutableTreeNode(" " + sFunctions[i] + "() "); |
161 |
node.add(child); |
162 |
} |
163 |
main.add(node); |
164 |
|
165 |
//operators
|
166 |
final String sOperators[] = { "+", "-", "*", "/", "%", "!", "^", "&&", "||", "<", ">", "<=", ">=", "==", "!=" }; |
167 |
node = new DefaultMutableTreeNode(Sextante.getText("Operators")); |
168 |
for (i = 0; i < sOperators.length; i++) { |
169 |
child = new DefaultMutableTreeNode(" " + sOperators[i] + " "); |
170 |
node.add(child); |
171 |
} |
172 |
main.add(node); |
173 |
|
174 |
//constants
|
175 |
node = new DefaultMutableTreeNode(Sextante.getText("Constants")); |
176 |
m_Constants = new TreeMap(); |
177 |
m_Constants.put("e", " " + Double.toString(Math.E) + " "); |
178 |
m_Constants.put("Pi", " " + Double.toString(Math.PI) + " "); |
179 |
m_Constants.put("NODATA", " " + Double.toString(SextanteGUI.getOutputFactory().getDefaultNoDataValue()) + " "); |
180 |
for (i = 0; i < layers.length; i++) { |
181 |
sName = Sextante.getText("Cell_size_[") + layers[i].getName() + "]"; |
182 |
dCellsize = layers[i].getLayerCellSize(); |
183 |
m_Constants.put(sName, " " + Double.toString(dCellsize) + " "); |
184 |
} |
185 |
|
186 |
final Set set = m_Constants.keySet(); |
187 |
final Iterator iter = set.iterator(); |
188 |
while (iter.hasNext()) {
|
189 |
child = new DefaultMutableTreeNode(iter.next()); |
190 |
node.add(child); |
191 |
} |
192 |
|
193 |
main.add(node); |
194 |
|
195 |
jTree.setModel(new DefaultTreeModel(main)); |
196 |
final DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) jTree.getCellRenderer(); |
197 |
renderer.setOpenIcon(null);
|
198 |
renderer.setClosedIcon(null);
|
199 |
renderer.setLeafIcon(null);
|
200 |
|
201 |
} |
202 |
|
203 |
|
204 |
private void insertTextFromTree(final TreePath path) { |
205 |
|
206 |
final TreePath parent = path.getParentPath(); |
207 |
|
208 |
if ((parent != null) && !parent.toString().equals("[" + Sextante.getText("ELEMENTS") + "]")) { |
209 |
final String sParentName = parent.toString(); |
210 |
String s = path.getLastPathComponent().toString();
|
211 |
|
212 |
if (sParentName.equals("[" + Sextante.getText("ELEMENTS") + ", " + Sextante.getText("Constants]"))) { |
213 |
if (m_Constants.containsKey(s)) {
|
214 |
s = (String) m_Constants.get(s);
|
215 |
} |
216 |
else {
|
217 |
s = "";
|
218 |
} |
219 |
} |
220 |
|
221 |
jTextExpression.insert(s, jTextExpression.getCaretPosition()); |
222 |
|
223 |
if (sParentName.equals("[" + Sextante.getText("ELEMENTS") + ", " + Sextante.getText("Functions]"))) { |
224 |
jTextExpression.setCaretPosition(jTextExpression.getCaretPosition() - 2);
|
225 |
} |
226 |
} |
227 |
|
228 |
} |
229 |
|
230 |
|
231 |
@Override
|
232 |
public void assignParameters() throws WrongInputException { |
233 |
|
234 |
final IRasterLayer[] layers = SextanteGUI.getInputFactory().getRasterLayers(); |
235 |
final ArrayList layersList = new ArrayList(); |
236 |
for (final IRasterLayer element : layers) { |
237 |
layersList.add(element); |
238 |
} |
239 |
final List<RasterLayerAndBand> array = FormulaParser.getBandsFromFormula(jTextExpression.getText(), layersList); |
240 |
|
241 |
if (array == null) { |
242 |
throw new WrongInputException(); |
243 |
} |
244 |
|
245 |
layersList.clear(); |
246 |
|
247 |
for (int i = 0; i < array.size(); i++) { |
248 |
final RasterLayerAndBand rlab = (RasterLayerAndBand) array.get(i);
|
249 |
final IRasterLayer layer = rlab.getRasterLayer();
|
250 |
if (!layersList.contains(layer)) {
|
251 |
layersList.add(layer); |
252 |
} |
253 |
} |
254 |
|
255 |
try {
|
256 |
m_Algorithm.getParameters().getParameter(GridCalculatorAlgorithm.LAYERS).setParameterValue(layersList); |
257 |
m_Algorithm.getParameters().getParameter(GridCalculatorAlgorithm.FORMULA).setParameterValue(jTextExpression.getText()); |
258 |
final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
|
259 |
final Output out = ooSet.getOutput(GridCalculatorAlgorithm.RESULT);
|
260 |
out.setOutputChannel(outputChannelSelectionPanel.getOutputChannel()); |
261 |
} |
262 |
catch (final Exception e) { |
263 |
Sextante.addErrorToLog(e); |
264 |
throw new WrongInputException(); |
265 |
} |
266 |
|
267 |
} |
268 |
|
269 |
|
270 |
@Override
|
271 |
public void setOutputValue(final String outputName, |
272 |
final String value) { |
273 |
|
274 |
|
275 |
outputChannelSelectionPanel.setText(value); |
276 |
|
277 |
} |
278 |
|
279 |
|
280 |
@Override
|
281 |
public void setParameterValue(final String parameterName, |
282 |
final String value) { |
283 |
|
284 |
if (parameterName.equals(GridCalculatorAlgorithm.FORMULA)) {
|
285 |
jTextExpression.setText(value); |
286 |
} |
287 |
|
288 |
} |
289 |
|
290 |
} |