Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.merge / src / main / java / org / gvsig / geoprocess / algorithm / merge / MergeParametersPanel.java @ 639

History | View | Annotate | Download (12.1 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.algorithm.merge;
25

    
26
import java.awt.Dimension;
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.Insets;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.util.ArrayList;
33

    
34
import javax.swing.JCheckBox;
35
import javax.swing.JComboBox;
36
import javax.swing.JLabel;
37
import javax.swing.JPanel;
38

    
39
import org.gvsig.geoprocess.lib.api.GeoProcessLocator;
40
import org.gvsig.geoprocess.sextante.gui.algorithm.AlgorithmOutputPanel;
41
import org.gvsig.gui.beans.table.TableContainer;
42
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
43

    
44
import es.unex.sextante.core.GeoAlgorithm;
45
import es.unex.sextante.core.ObjectAndDescription;
46
import es.unex.sextante.core.OutputObjectsSet;
47
import es.unex.sextante.core.ParametersSet;
48
import es.unex.sextante.core.Sextante;
49
import es.unex.sextante.dataObjects.IVectorLayer;
50
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
51
import es.unex.sextante.gui.algorithm.OutputChannelSelectionPanel;
52
import es.unex.sextante.gui.core.SextanteGUI;
53
import es.unex.sextante.outputs.Output;
54

    
55
/**
56
 * Panel for merge algorithm
57
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
58
 */
59
public class MergeParametersPanel extends GeoAlgorithmParametersPanel implements ActionListener {
60
        private static final long                serialVersionUID   = 1L;
61
        private GeoAlgorithm                     m_Algorithm        = null;
62
        private JComboBox                        fieldsCombo        = null;
63
        private JCheckBox                        allLayers          = null;
64
        //private AlgorithmOutputPanel             output             = null;
65
        private final String[]                   columnNames        = { GeoProcessLocator.getGeoProcessManager().getTranslation("Selected"),
66
                                                                                                                                        GeoProcessLocator.getGeoProcessManager().getTranslation("Layer") };
67
        private final int[]                      columnWidths       = { 35, 334 };
68
        private TableContainer                   table              = null;
69
        private ObjectAndDescription[]           layerList          = null;
70
        private AlgorithmOutputPanel             algorithmOutputPanel  = null;
71
        private OutputChannelSelectionPanel      outputChannelSelectionPanelPol;
72
        private OutputChannelSelectionPanel      outputChannelSelectionPanelLine;
73
        private OutputChannelSelectionPanel      outputChannelSelectionPanelPoint;
74
        private JPanel                           outputPanel;
75
        
76
        public MergeParametersPanel() {
77
                super();
78
        }
79

    
80
    public void init(GeoAlgorithm algorithm) {
81
            m_Algorithm = algorithm;
82
            initGUI();
83
    }
84

    
85
        private void initGUI() {
86
                GridBagLayout gbl = new GridBagLayout();
87
                this.setLayout(gbl);
88
                
89
                GridBagConstraints gbc = new GridBagConstraints();
90
                gbc.fill = GridBagConstraints.HORIZONTAL;
91
                gbc.weightx = 1.0;
92
                gbc.gridx = 0;
93
                gbc.gridy = 0;
94
                gbc.insets = new Insets(5, 5, 8, 5);
95
                this.add(getAllLayersCheck(), gbc);
96
                
97
                gbc.gridy = 1;
98
                gbc.fill = GridBagConstraints.BOTH;
99
                gbc.insets = new Insets(0, 0, 12, 0);
100
                gbc.weighty = 1.0;
101
                this.add(getCheckBoxTable(), gbc);
102
                
103
                gbc.gridy = 2;
104
                gbc.fill = GridBagConstraints.HORIZONTAL;
105
                gbc.insets = new Insets(5, 5, 8, 5);
106
                gbc.weightx = 1.0;
107
                gbc.weighty = 0;
108
                this.add(getFieldsComboPanel(GeoProcessLocator.getGeoProcessManager().getTranslation("Field"), getFieldsCombo()), gbc);
109
                
110
                gbc.gridy = 3;
111
                this.add(getOutputChannelSelectionPanel(), gbc);
112
                
113
                initTable();
114
        }
115
        
116
        /**
117
         * Gets the output panel (SEXTANTE)
118
         * @return
119
         */
120
        private JPanel getOutputChannelSelectionPanel() {
121
                if(outputPanel == null) {
122
                        try {
123
                                outputPanel = new JPanel();
124
                                outputPanel.setLayout(new GridBagLayout());
125
                                final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
126
                                final Output out_pol = ooSet.getOutput(MergeAlgorithm.RESULT_POL);
127
                                final Output out_line = ooSet.getOutput(MergeAlgorithm.RESULT_LINE);
128
                                final Output out_point = ooSet.getOutput(MergeAlgorithm.RESULT_POINT);
129
                                
130
                                outputChannelSelectionPanelPol = new OutputChannelSelectionPanel(out_pol, m_Algorithm.getParameters());
131
                                outputChannelSelectionPanelLine = new OutputChannelSelectionPanel(out_line, m_Algorithm.getParameters());
132
                                outputChannelSelectionPanelPoint = new OutputChannelSelectionPanel(out_point, m_Algorithm.getParameters());
133
                                
134
                                String label = GeoProcessLocator.getGeoProcessManager().getTranslation("Merge");
135
                                
136
                                GridBagConstraints gbc = new GridBagConstraints();
137
                                gbc.fill = GridBagConstraints.HORIZONTAL;
138
                                gbc.insets = new Insets(0, 0, 4, 0);
139
                                gbc.weightx = 1;
140
                                gbc.gridx = 0;
141
                                gbc.gridy = 0;
142
                                outputPanel.add(new JLabel(" " + label + " [" + 
143
                                                GeoProcessLocator.getGeoProcessManager().getTranslation("Polygon") + "]               "), gbc);
144
                                
145
                                gbc.gridx = 0;
146
                                gbc.gridy = 1;
147
                                outputPanel.add(new JLabel(" " + label + " [" +
148
                                                GeoProcessLocator.getGeoProcessManager().getTranslation("Line") + "]               "), gbc);
149
                                
150
                                gbc.gridx = 0;
151
                                gbc.gridy = 2;
152
                                outputPanel.add(new JLabel(" " + label + " [" +
153
                                                GeoProcessLocator.getGeoProcessManager().getTranslation("Point") + "]               "), gbc);
154
                                
155
                                gbc.fill = GridBagConstraints.HORIZONTAL;
156
                                gbc.weightx = 1;
157
                                gbc.gridx = 1;
158
                                gbc.gridy = 0;
159
                                outputPanel.add(outputChannelSelectionPanelPol, gbc);
160
                                
161
                                gbc.gridx = 1;
162
                                gbc.gridy = 1;
163
                                outputPanel.add(outputChannelSelectionPanelLine, gbc);
164
                                
165
                                gbc.gridx = 1;
166
                                gbc.gridy = 2;
167
                                outputPanel.add(outputChannelSelectionPanelPoint, gbc);
168
                        } catch (final Exception e) {
169
                                Sextante.addErrorToLog(e);
170
                        }
171
                }
172
                return outputPanel;
173
        }
174
        
175
        /**
176
         * Gets the output panel (DAL)
177
         * @return
178
         */
179
        @SuppressWarnings("unused")
180
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
181
                if(algorithmOutputPanel == null)
182
                    algorithmOutputPanel = new AlgorithmOutputPanel();
183
                return algorithmOutputPanel;
184
        }
185
        
186
        /**
187
         * Gets a new JPanel with the text and JComboBox 
188
         * @param text
189
         * @param combo
190
         * @return
191
         */
192
        public JPanel getFieldsComboPanel(String text, JComboBox combo) {
193
                JPanel panel = new JPanel();
194
                GridBagLayout gbl = new GridBagLayout();
195
                panel.setLayout(gbl);
196

    
197
                GridBagConstraints gbc = new GridBagConstraints();
198
                gbc.fill = GridBagConstraints.NONE;
199
                gbc.weightx = 0;
200
                gbc.gridx = 0;
201
                gbc.insets = new Insets(0, 2, 0, 50);
202
                JLabel label = new JLabel(text);
203
                label.setPreferredSize(new Dimension(80, 18));
204
                panel.add(label, gbc);
205

    
206
                gbc.fill = GridBagConstraints.HORIZONTAL;
207
                gbc.weightx = 1.0;
208
                gbc.gridx = 1;
209
                gbc.anchor = GridBagConstraints.EAST;
210
                gbc.insets = new Insets(0, 2, 0, 0);
211
                panel.add(combo, gbc);
212
                return panel;
213
        }
214
        
215
        /**
216
         * Gets a ComboBox
217
         * @return
218
         */
219
        public JComboBox getFieldsCombo() {
220
                if(fieldsCombo == null) {
221
                        fieldsCombo = new JComboBox();
222
                        fieldsCombo.setPreferredSize(new Dimension(0, 18));
223
                        ObjectAndDescription[] fieldList = getLayerList();
224
                        fieldsCombo.removeAllItems();
225
                        for (int i = 0; i < fieldList.length; i++) 
226
                                fieldsCombo.addItem(fieldList[i]);
227
                }
228
                return fieldsCombo;
229
        }
230
        
231
        /**
232
         * Gets a CheckBox
233
         * @return
234
         */
235
        public JCheckBox getAllLayersCheck() {
236
                if(allLayers == null) {
237
                        allLayers = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("select_all_layers"));
238
                        allLayers.addActionListener(this);
239
                }
240
                return allLayers;
241
        }
242

    
243
        /**
244
         * Gets the summary table
245
         * @return TableContainer
246
         */
247
        public TableContainer getCheckBoxTable() {
248
                if (table == null) {
249
                        table = new TableContainer(columnNames, columnWidths, null);
250
                        table.setModel("CheckBoxModel");
251
                        table.setControlVisible(false);
252
                        table.initialize();
253
                }
254
                return table;
255
        }
256
        
257
        //------------------------------------------------------------
258
        
259
        /*
260
         * (non-Javadoc)
261
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
262
         */
263
        public void actionPerformed(ActionEvent e) {
264
                if(e.getSource() == getAllLayersCheck()) {
265
                        //Selecci?n de todas las capas de la tabla
266
                        try {
267
                                for (int i = 0; i < getCheckBoxTable().getRowCount(); i++) {
268
                                        if(getAllLayersCheck().isSelected())
269
                                                getCheckBoxTable().getModel().setValueAt(new Boolean(true), i, 0);
270
                                        else
271
                                                getCheckBoxTable().getModel().setValueAt(new Boolean(false), i, 0);
272
                                }
273
                        } catch (NotInitializeException e1) {
274
                                Sextante.addErrorToLog(e1);
275
                        }
276
                }
277
        }
278
        
279
        /**
280
         * Adds to the table one entry for each field
281
         */
282
        private void initTable() {
283
                try {
284
                        getCheckBoxTable().removeAllRows();
285
                        ObjectAndDescription[] layerList = getLayerList();
286
                        for (int i = 0; i < layerList.length; i++)
287
                                addTableRow(layerList[i].getDescription());
288
                } catch (NotInitializeException e) {
289
                        Sextante.addErrorToLog(e);
290
                }
291
        }
292

    
293
        /**
294
         * Adds one entry to the table
295
         * @param layerName 
296
         *        Layer name
297
         * @throws NotInitializeException 
298
         */
299
        private void addTableRow(String layerName) throws NotInitializeException {
300
                Object[] row = {        new Boolean(false), 
301
                                                        layerName };
302
                getCheckBoxTable().addRow(row);
303
        }
304
        
305
        @Override
306
    public void assignParameters() {
307
                try {
308
                        ParametersSet params = m_Algorithm.getParameters();
309
                        params.getParameter(MergeAlgorithm.FIELDLAYER).setParameterValue(getSelectedVectorLayer());
310
                        params.getParameter(MergeAlgorithm.LAYERS).setParameterValue(getSelectedLayerList());
311
                        
312
                        OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
313
                        Output outPol = ooSet.getOutput(MergeAlgorithm.RESULT_POL);
314
                        Output outLine = ooSet.getOutput(MergeAlgorithm.RESULT_LINE);
315
                        Output outPoint = ooSet.getOutput(MergeAlgorithm.RESULT_POINT);
316
                        
317
                        //Reponer estas l?neas para cambiar el panel de salida y comentar la siguiente
318
                        //AlgorithmOutputPanel fsp = getAlgorithmOutputPanel();
319
                        //out.setOutputChannel(new CompositeSourceOutputChannel(fsp.getOutputParameters()));
320
                        outPol.setOutputChannel(outputChannelSelectionPanelPol.getOutputChannel());
321
                        outLine.setOutputChannel(outputChannelSelectionPanelLine.getOutputChannel());
322
                        outPoint.setOutputChannel(outputChannelSelectionPanelPoint.getOutputChannel());
323
                } catch (Exception e) {
324
                        Sextante.addErrorToLog(e);
325
                }
326
        }
327

    
328
        @Override
329
        public void setOutputValue(String arg0, String arg1) {
330
                
331
        }
332

    
333
        @Override
334
        public void setParameterValue(String arg0, String arg1) {
335
                
336
        }
337
        
338
        /**
339
         * Gets the input layer list
340
         * @return
341
         */
342
        private ObjectAndDescription[] getLayerList() {
343
                if(layerList == null) {
344
                        IVectorLayer[] layers = SextanteGUI.getInputFactory()
345
                        .getVectorLayers(IVectorLayer.SHAPE_TYPE_WRONG);
346
                        layerList = new ObjectAndDescription[layers.length];
347
                        for (int i = 0; i < layers.length; i++)
348
                                layerList[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
349
                }
350
                return layerList;
351
        }
352
        
353
        /**
354
         * Gets the list of selected layers
355
         * @return
356
         */
357
        private ArrayList<IVectorLayer> getSelectedLayerList() {
358
                ObjectAndDescription[] layerList = getLayerList();
359
                ArrayList<IVectorLayer> vLayer = new ArrayList<IVectorLayer>();
360
                for (int i = 0; i < layerList.length; i++) {
361
                        Boolean check = (Boolean)getCheckBoxTable().getModel().getValueAt(i, 0);
362
                        if(check.booleanValue())
363
                                vLayer.add((IVectorLayer)layerList[i].getObject());
364
                }
365
                return vLayer;
366
        }
367
        
368
        /**
369
         * Gets the selected vector layer in the JComboBox
370
         * @return
371
         */
372
        private IVectorLayer getSelectedVectorLayer() {
373
                if(getFieldsCombo().getSelectedItem() != null)
374
                        return (IVectorLayer)((ObjectAndDescription)getFieldsCombo().getSelectedItem()).getObject();
375
                return null;
376
        }
377
        
378
}