root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.gui / src / main / java / es / unex / sextante / gui / algorithm / AlgorithmDialog.java @ 338
History | View | Annotate | Download (16.6 KB)
1 |
package es.unex.sextante.gui.algorithm; |
---|---|
2 |
|
3 |
import info.clearthought.layout.TableLayout; |
4 |
import info.clearthought.layout.TableLayoutConstants; |
5 |
|
6 |
import java.awt.Font; |
7 |
import java.awt.event.ActionEvent; |
8 |
import java.awt.event.ActionListener; |
9 |
import java.util.ArrayList; |
10 |
|
11 |
import javax.swing.ImageIcon; |
12 |
import javax.swing.JButton; |
13 |
import javax.swing.JDialog; |
14 |
import javax.swing.JOptionPane; |
15 |
import javax.swing.JPanel; |
16 |
import javax.swing.JTabbedPane; |
17 |
import javax.swing.JTextField; |
18 |
|
19 |
import es.unex.sextante.core.AnalysisExtent; |
20 |
import es.unex.sextante.core.GeoAlgorithm; |
21 |
import es.unex.sextante.core.OutputObjectsSet; |
22 |
import es.unex.sextante.core.ParametersSet; |
23 |
import es.unex.sextante.core.Sextante; |
24 |
import es.unex.sextante.exceptions.WrongAnalysisExtentException; |
25 |
import es.unex.sextante.exceptions.WrongInputException; |
26 |
import es.unex.sextante.gui.core.IGUIFactory; |
27 |
import es.unex.sextante.gui.core.SextanteGUI; |
28 |
import es.unex.sextante.gui.exceptions.LayerCannotBeOverwrittenException; |
29 |
import es.unex.sextante.gui.exceptions.OverwrittingNotAllowedException; |
30 |
import es.unex.sextante.gui.exceptions.TooLargeGridExtentException; |
31 |
import es.unex.sextante.gui.history.DateAndCommand; |
32 |
import es.unex.sextante.gui.history.History; |
33 |
import es.unex.sextante.outputs.Output; |
34 |
import es.unex.sextante.outputs.Output3DRasterLayer; |
35 |
import es.unex.sextante.outputs.OutputRasterLayer; |
36 |
import es.unex.sextante.outputs.OutputTable; |
37 |
import es.unex.sextante.outputs.OutputVectorLayer; |
38 |
import es.unex.sextante.parameters.Parameter; |
39 |
|
40 |
|
41 |
/**
|
42 |
* A dialog used to introduce all the necessary input for a geoalgorithm (input parameters and raster extent if needed. see
|
43 |
* {@link es.unex.sextante.core.GeoAlgorithm#getUserCanDefineAnalysisExtent()}) for a given algorithm
|
44 |
*
|
45 |
* @author volaya
|
46 |
*
|
47 |
*/
|
48 |
public class AlgorithmDialog |
49 |
extends
|
50 |
JDialog {
|
51 |
|
52 |
protected GeoAlgorithm m_Algorithm;
|
53 |
protected JTabbedPane jTabbedPane1; |
54 |
private JPanel jPanelButtons; |
55 |
private JPanel jMainPanel; |
56 |
protected JButton jButtonCancel; |
57 |
protected JButton jButtonOK; |
58 |
|
59 |
private int m_iDialogReturn; |
60 |
|
61 |
protected GeoAlgorithmParametersPanel jPanelParametersMain = null; |
62 |
private AnalysisExtentPanel jAnalysisExtentPanel;
|
63 |
private JButton jButtonHelp; |
64 |
private String[] m_PreviousParameters; |
65 |
private String[] m_Extents; |
66 |
private JTextField jLabelCommand; |
67 |
private JButton jButtonPrevious; |
68 |
private JButton jButtonNext; |
69 |
private int m_iPreviousCommandIndex; |
70 |
private final ArrayList<DateAndCommand> m_sCommand; |
71 |
|
72 |
|
73 |
/**
|
74 |
* Creates a new dialog for a given algorithm.
|
75 |
*
|
76 |
* @param algorithm
|
77 |
* the algorithm
|
78 |
* @param parent
|
79 |
* the parent dialog
|
80 |
* @param panel
|
81 |
* the parameters panel to use. Doesn't have to be initialized using its init(Geoalgorithm) method. This
|
82 |
* constructor will initialize it
|
83 |
* @param commands
|
84 |
* a list of DateAndCommand objects to use as previous parameters set. Must include both the "runalg" commands
|
85 |
* and the "extent" ones, in case you want them to be used for algorithms generating new raster layers. If null,
|
86 |
* all suitable commands from history are used
|
87 |
*/
|
88 |
public AlgorithmDialog(final GeoAlgorithm algorithm, |
89 |
final JDialog parent, |
90 |
final GeoAlgorithmParametersPanel panel,
|
91 |
final ArrayList<DateAndCommand> commands) { |
92 |
|
93 |
super(parent, algorithm.getName(), true); |
94 |
|
95 |
setResizable(false);
|
96 |
|
97 |
m_Algorithm = algorithm; |
98 |
m_sCommand = commands; |
99 |
|
100 |
jPanelParametersMain = panel; |
101 |
jPanelParametersMain.init(m_Algorithm); |
102 |
|
103 |
initGUI(); |
104 |
setLocationRelativeTo(null);
|
105 |
|
106 |
} |
107 |
|
108 |
|
109 |
/**
|
110 |
* Creates a new dialog for a given algorithm. The main frame of the UI is used as the parent component
|
111 |
*
|
112 |
* @param algorithm
|
113 |
* the algorithm
|
114 |
* @param panel
|
115 |
* the parameters panel to use. Doesn't have to be initialized using its init(Geoalgorithm) method. This
|
116 |
* constructor will initialize it
|
117 |
* @param comands
|
118 |
* a list of DateAndCommand objects to use as previous parameters set. Must include both the "runalg" commands
|
119 |
* and the "extent" ones, in case you want them to be used for algorithms generating new raster layers. If null,
|
120 |
* all suitable commands from history are used
|
121 |
*/
|
122 |
public AlgorithmDialog(final GeoAlgorithm algorithm, |
123 |
final GeoAlgorithmParametersPanel panel,
|
124 |
final ArrayList<DateAndCommand> commands) { |
125 |
|
126 |
super(SextanteGUI.getMainFrame(), algorithm.getName(), true); |
127 |
|
128 |
setResizable(false);
|
129 |
|
130 |
m_sCommand = commands; |
131 |
m_Algorithm = algorithm; |
132 |
|
133 |
jPanelParametersMain = panel; |
134 |
jPanelParametersMain.init(m_Algorithm); |
135 |
|
136 |
initGUI(); |
137 |
setLocationRelativeTo(null);
|
138 |
|
139 |
} |
140 |
|
141 |
|
142 |
private void initGUI() { |
143 |
|
144 |
|
145 |
jMainPanel = new JPanel(); |
146 |
|
147 |
this.add(jMainPanel);
|
148 |
|
149 |
final TableLayout thisLayout = new TableLayout(new double[][] { { 10.0, TableLayoutConstants.FILL, 10. }, |
150 |
{ 1.0, 338.0, 37.0 } }); |
151 |
jMainPanel.setLayout(thisLayout); |
152 |
this.setSize(696, 446); |
153 |
{ |
154 |
jTabbedPane1 = new JTabbedPane(); |
155 |
jMainPanel.add(jTabbedPane1, "1, 1");
|
156 |
{ |
157 |
jTabbedPane1.addTab(Sextante.getText("Parameters"), null, jPanelParametersMain, null); |
158 |
} |
159 |
{ |
160 |
if (m_Algorithm.getUserCanDefineAnalysisExtent()) {
|
161 |
jTabbedPane1.addTab(Sextante.getText("Raster_output"), null, getAnalysisExtentPanel(), null); |
162 |
} |
163 |
} |
164 |
} |
165 |
{ |
166 |
jPanelButtons = new JPanel(); |
167 |
final TableLayout jPanelButtonsLayout = new TableLayout(new double[][] { |
168 |
{ 5.0, 45.0, 120.0, 120.0, 45.0, TableLayoutConstants.FILL, 90.0, 90.0, 25.0, 15.0 }, |
169 |
{ TableLayoutConstants.FILL, 25.0, TableLayoutConstants.FILL } });
|
170 |
jPanelButtonsLayout.setHGap(5);
|
171 |
jPanelButtonsLayout.setVGap(5);
|
172 |
jPanelButtons.setLayout(jPanelButtonsLayout); |
173 |
jMainPanel.add(jPanelButtons, "1, 2");
|
174 |
jPanelButtons.setFocusable(false);
|
175 |
{ |
176 |
jButtonOK = new JButton(); |
177 |
jPanelButtons.add(jButtonOK, "6, 1");
|
178 |
jButtonOK.setText(Sextante.getText("OK"));
|
179 |
jButtonOK.addActionListener(new java.awt.event.ActionListener() {
|
180 |
public void actionPerformed(final java.awt.event.ActionEvent e) { |
181 |
executeAlgorithm(); |
182 |
} |
183 |
}); |
184 |
} |
185 |
{ |
186 |
jButtonHelp = new JButton(); |
187 |
jPanelButtons.add(jButtonHelp, "8, 1");
|
188 |
jButtonHelp.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/info.gif"))); |
189 |
jButtonHelp.setPreferredSize(new java.awt.Dimension(33, 0)); |
190 |
jButtonHelp.addActionListener(new ActionListener() { |
191 |
public void actionPerformed(final ActionEvent evt) { |
192 |
showHelp(); |
193 |
} |
194 |
}); |
195 |
} |
196 |
{ |
197 |
jButtonCancel = new JButton(); |
198 |
jPanelButtons.add(jButtonCancel, "7, 1");
|
199 |
jButtonCancel.setText(Sextante.getText("Cancel"));
|
200 |
jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
|
201 |
public void actionPerformed(final java.awt.event.ActionEvent e) { |
202 |
m_iDialogReturn = IGUIFactory.CANCEL; |
203 |
dispose(); |
204 |
setVisible(false);
|
205 |
} |
206 |
}); |
207 |
} |
208 |
|
209 |
} |
210 |
|
211 |
retrievePreviouslyUsedParametersFromHistory(); |
212 |
if (m_PreviousParameters != null) { |
213 |
try {
|
214 |
{ |
215 |
jButtonPrevious = new JButton(); |
216 |
jPanelButtons.add(jButtonPrevious, "1, 1");
|
217 |
jButtonPrevious.setText("<");
|
218 |
jButtonPrevious.addActionListener(new ActionListener() { |
219 |
public void actionPerformed(final ActionEvent evt) { |
220 |
setPreviousSetOfPreviouslyUsedCommand(); |
221 |
} |
222 |
}); |
223 |
} |
224 |
{ |
225 |
jButtonNext = new JButton(); |
226 |
jPanelButtons.add(jButtonNext, "4, 1");
|
227 |
jButtonNext.setText(">");
|
228 |
jButtonNext.addActionListener(new ActionListener() { |
229 |
public void actionPerformed(final ActionEvent evt) { |
230 |
setNextSetOfPreviouslyUsedCommand(); |
231 |
} |
232 |
}); |
233 |
} |
234 |
{ |
235 |
jLabelCommand = new JTextField(); |
236 |
jLabelCommand.setFont(new Font("Monospaced", Font.PLAIN, 10)); |
237 |
jLabelCommand.setEditable(false);
|
238 |
jPanelButtons.add(jLabelCommand, "2, 1, 3, 1");
|
239 |
} |
240 |
|
241 |
m_iPreviousCommandIndex = m_PreviousParameters.length - 1;
|
242 |
setPreviouslyUsedParameters(m_iPreviousCommandIndex); |
243 |
} |
244 |
catch (final Exception e) { |
245 |
// do nothing
|
246 |
} |
247 |
} |
248 |
|
249 |
} |
250 |
|
251 |
|
252 |
private void setPreviouslyUsedParameters(final int iIndex) { |
253 |
|
254 |
int i;
|
255 |
Parameter param; |
256 |
String[] args; |
257 |
String sArg;
|
258 |
|
259 |
jLabelCommand.setText("[" + Integer.toString(iIndex + 1) + "] " + m_PreviousParameters[iIndex]); |
260 |
args = m_PreviousParameters[iIndex].split("\"");
|
261 |
|
262 |
for (int j = 0; j < 2; j++) { //twice to handle dependencies |
263 |
final ParametersSet ps = m_Algorithm.getParameters();
|
264 |
for (i = 0; i < m_Algorithm.getNumberOfParameters(); i++) { |
265 |
param = ps.getParameter(i); |
266 |
sArg = args[i * 2 + 3]; |
267 |
jPanelParametersMain.setParameterValue(param.getParameterName(), sArg.trim()); |
268 |
} |
269 |
int iOutputIndex = i * 2 + 3; |
270 |
final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
|
271 |
for (i = 0; i < ooSet.getOutputObjectsCount(); i++) { |
272 |
final Output out = ooSet.getOutput(i);
|
273 |
if ((out instanceof OutputRasterLayer) || (out instanceof Output3DRasterLayer) || (out instanceof OutputVectorLayer) |
274 |
|| (out instanceof OutputTable)) {
|
275 |
final String sValue = args[iOutputIndex].trim(); |
276 |
jPanelParametersMain.setOutputValue(out.getName(), sValue); |
277 |
iOutputIndex += 2;
|
278 |
} |
279 |
} |
280 |
} |
281 |
|
282 |
if (m_Algorithm.getUserCanDefineAnalysisExtent()) {
|
283 |
String sExtent = m_Extents[iIndex];
|
284 |
if (sExtent != null) { |
285 |
sExtent = sExtent.substring(sExtent.indexOf("(") + 1, sExtent.indexOf(")")); |
286 |
final AnalysisExtent ae = new AnalysisExtent(); |
287 |
final String[] sCoords = sExtent.split(","); |
288 |
final double dCellSize = Double.parseDouble(sCoords[6]); |
289 |
final double dCellSizeZ = Double.parseDouble(sCoords[7]); |
290 |
ae.setCellSize(dCellSize); |
291 |
ae.setCellSizeZ(dCellSizeZ); |
292 |
final double xMin = Double.parseDouble(sCoords[0]); |
293 |
final double xMax = Double.parseDouble(sCoords[3]); |
294 |
ae.setXRange(xMin, xMax, true);
|
295 |
final double yMin = Double.parseDouble(sCoords[1]); |
296 |
final double yMax = Double.parseDouble(sCoords[4]); |
297 |
ae.setYRange(yMin, yMax, true);
|
298 |
final double zMin = Double.parseDouble(sCoords[2]); |
299 |
final double zMax = Double.parseDouble(sCoords[5]); |
300 |
ae.setZRange(zMin, zMax, true);
|
301 |
getAnalysisExtentPanel().setExtent(ae); |
302 |
} |
303 |
else {
|
304 |
getAnalysisExtentPanel().setAutoExtent(); |
305 |
} |
306 |
} |
307 |
|
308 |
} |
309 |
|
310 |
|
311 |
private void retrievePreviouslyUsedParametersFromHistory() { |
312 |
|
313 |
ArrayList<DateAndCommand> dac;
|
314 |
if (m_sCommand != null) { |
315 |
dac = m_sCommand; |
316 |
} |
317 |
else {
|
318 |
dac = History.getHistory(); |
319 |
} |
320 |
final ArrayList<String> previousParameters = new ArrayList<String>(); |
321 |
final ArrayList<String> extents = new ArrayList<String>(); |
322 |
for (int i = 0; i < dac.size(); i++) { |
323 |
String command = dac.get(i).getCommand();
|
324 |
if (command.startsWith("runalg(\"" + m_Algorithm.getCommandLineName() + "\"")) { |
325 |
previousParameters.add(command); |
326 |
if (m_Algorithm.getUserCanDefineAnalysisExtent() && (i != 0)) { |
327 |
command = dac.get(i - 1).getCommand();
|
328 |
if (command.startsWith("extent")) { |
329 |
extents.add(command); |
330 |
} |
331 |
else {
|
332 |
extents.add(null);
|
333 |
} |
334 |
} |
335 |
} |
336 |
} |
337 |
|
338 |
if (previousParameters.size() == 0) { |
339 |
m_PreviousParameters = null;
|
340 |
m_Extents = null;
|
341 |
} |
342 |
else {
|
343 |
m_PreviousParameters = previousParameters.toArray(new String[0]); |
344 |
m_Extents = extents.toArray(new String[0]); |
345 |
|
346 |
} |
347 |
|
348 |
} |
349 |
|
350 |
|
351 |
public void setPreviousSetOfPreviouslyUsedCommand() { |
352 |
|
353 |
if (m_iPreviousCommandIndex <= 0) { |
354 |
//m_iPreviousCommandIndex = m_PreviousParameters.length - 1;
|
355 |
} |
356 |
else {
|
357 |
m_iPreviousCommandIndex--; |
358 |
setPreviouslyUsedParameters(m_iPreviousCommandIndex); |
359 |
} |
360 |
|
361 |
|
362 |
} |
363 |
|
364 |
|
365 |
public void setNextSetOfPreviouslyUsedCommand() { |
366 |
|
367 |
if (m_iPreviousCommandIndex >= m_PreviousParameters.length - 1) { |
368 |
//m_iPreviousCommandIndex = 0;
|
369 |
} |
370 |
else {
|
371 |
m_iPreviousCommandIndex++; |
372 |
setPreviouslyUsedParameters(m_iPreviousCommandIndex); |
373 |
} |
374 |
|
375 |
|
376 |
} |
377 |
|
378 |
|
379 |
protected void showHelp() { |
380 |
|
381 |
SextanteGUI.getGUIFactory().showHelpDialog(m_Algorithm); |
382 |
|
383 |
} |
384 |
|
385 |
|
386 |
protected void executeAlgorithm() { |
387 |
|
388 |
try {
|
389 |
try {
|
390 |
assignParameters(); |
391 |
} |
392 |
catch (final TooLargeGridExtentException e) { |
393 |
final int iRet = JOptionPane.showConfirmDialog(null, e.getMessage(), Sextante.getText("Warning"), |
394 |
JOptionPane.YES_NO_OPTION);
|
395 |
if (iRet != JOptionPane.YES_OPTION) { |
396 |
this.jTabbedPane1.setSelectedIndex(1); |
397 |
return;
|
398 |
} |
399 |
} |
400 |
|
401 |
m_iDialogReturn = IGUIFactory.OK; |
402 |
|
403 |
dispose(); |
404 |
setVisible(false);
|
405 |
|
406 |
} |
407 |
catch (final WrongInputException e) { |
408 |
JOptionPane.showMessageDialog(null, e.getMessage(), Sextante.getText("Warning"), JOptionPane.WARNING_MESSAGE); |
409 |
this.jTabbedPane1.setSelectedIndex(0); |
410 |
} |
411 |
catch (final WrongAnalysisExtentException e) { |
412 |
JOptionPane.showMessageDialog(null, e.getMessage(), Sextante.getText("Warning"), JOptionPane.WARNING_MESSAGE); |
413 |
this.jTabbedPane1.setSelectedIndex(1); |
414 |
} |
415 |
catch (final OverwrittingNotAllowedException e) { |
416 |
JOptionPane.showMessageDialog(null, e.getMessage(), Sextante.getText("Warning"), JOptionPane.WARNING_MESSAGE); |
417 |
this.jTabbedPane1.setSelectedIndex(0); |
418 |
} |
419 |
catch (final LayerCannotBeOverwrittenException e) { |
420 |
JOptionPane.showMessageDialog(null, e.getMessage(), Sextante.getText("Warning"), JOptionPane.WARNING_MESSAGE); |
421 |
this.jTabbedPane1.setSelectedIndex(0); |
422 |
} |
423 |
|
424 |
} |
425 |
|
426 |
|
427 |
protected void assignParameters() throws WrongInputException, OverwrittingNotAllowedException, |
428 |
LayerCannotBeOverwrittenException, WrongAnalysisExtentException, TooLargeGridExtentException { |
429 |
|
430 |
jPanelParametersMain.assignParameters(); |
431 |
if (m_Algorithm.getUserCanDefineAnalysisExtent()) {
|
432 |
getAnalysisExtentPanel().assignExtent(); |
433 |
} |
434 |
|
435 |
} |
436 |
|
437 |
|
438 |
private AnalysisExtentPanel getAnalysisExtentPanel() {
|
439 |
|
440 |
if (jAnalysisExtentPanel == null) { |
441 |
if (m_Algorithm.is3D()) {
|
442 |
jAnalysisExtentPanel = new TridimensionalAnalysisExtentPanel(m_Algorithm);
|
443 |
} |
444 |
else {
|
445 |
jAnalysisExtentPanel = new AnalysisExtentPanel(m_Algorithm);
|
446 |
} |
447 |
} |
448 |
return jAnalysisExtentPanel;
|
449 |
|
450 |
} |
451 |
|
452 |
|
453 |
/**
|
454 |
* Returns {@link es.unex.sextante.gui.core.IGUIFactory#OK} if the user selected "OK" and the algorithm should be executed
|
455 |
*
|
456 |
* @return IGUIFactory.OK if the user selected "OK" and the algorithm should be executed
|
457 |
*/
|
458 |
public int getDialogReturn() { |
459 |
|
460 |
return m_iDialogReturn;
|
461 |
|
462 |
} |
463 |
|
464 |
|
465 |
} |