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 @ 262

History | View | Annotate | Download (10.3 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.BorderLayout;
27
import java.awt.Dimension;
28
import java.awt.GridBagConstraints;
29
import java.awt.GridBagLayout;
30
import java.awt.Insets;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
import java.util.ArrayList;
34

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

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

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

    
57
/**
58
 * Panel for merge algorithm
59
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
60
 */
61
public class MergeParametersPanel extends GeoAlgorithmParametersPanel implements ActionListener {
62
        private static final long                serialVersionUID   = 1L;
63
        private GeoAlgorithm                     m_Algorithm        = null;
64
        private JComboBox                        fieldsCombo        = null;
65
        private JCheckBox                        allLayers          = null;
66
        //private AlgorithmOutputPanel             output             = null;
67
        private final String[]                   columnNames        = { GeoProcessLocator.getGeoProcessManager().getTranslation("Selected"),
68
                                                                                                                                        GeoProcessLocator.getGeoProcessManager().getTranslation("Layer") };
69
        private final int[]                      columnWidths       = { 35, 334 };
70
        private TableContainer                   table              = null;
71
        private ObjectAndDescription[]           layerList          = null;
72
        private AlgorithmOutputPanel             algorithmOutputPanel  = null;
73
        private OutputChannelSelectionPanel      outputChannelSelectionPanel;
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 BorderLayout());
125
                                final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
126
                                final Output out = ooSet.getOutput(GridCalculatorAlgorithm.RESULT);
127
                                outputChannelSelectionPanel = new OutputChannelSelectionPanel(out, m_Algorithm.getParameters());
128
                                outputPanel.add(new JLabel(" Dissolve [Vectorial]               "), BorderLayout.WEST);
129
                                outputPanel.add(outputChannelSelectionPanel, BorderLayout.CENTER);
130
                        } catch (final Exception e) {
131
                                Sextante.addErrorToLog(e);
132
                        }
133
                }
134
                return outputPanel;
135
        }
136
        
137
        /**
138
         * Gets the output panel (DAL)
139
         * @return
140
         */
141
        @SuppressWarnings("unused")
142
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
143
                if(algorithmOutputPanel == null)
144
                    algorithmOutputPanel = new AlgorithmOutputPanel();
145
                return algorithmOutputPanel;
146
        }
147
        
148
        /**
149
         * Gets a new JPanel with the text and JComboBox 
150
         * @param text
151
         * @param combo
152
         * @return
153
         */
154
        public JPanel getFieldsComboPanel(String text, JComboBox combo) {
155
                JPanel panel = new JPanel();
156
                GridBagLayout gbl = new GridBagLayout();
157
                panel.setLayout(gbl);
158

    
159
                GridBagConstraints gbc = new GridBagConstraints();
160
                gbc.fill = GridBagConstraints.NONE;
161
                gbc.weightx = 0;
162
                gbc.gridx = 0;
163
                gbc.insets = new Insets(0, 2, 0, 50);
164
                JLabel label = new JLabel(text);
165
                label.setPreferredSize(new Dimension(80, 18));
166
                panel.add(label, gbc);
167

    
168
                gbc.fill = GridBagConstraints.HORIZONTAL;
169
                gbc.weightx = 1.0;
170
                gbc.gridx = 1;
171
                gbc.anchor = GridBagConstraints.EAST;
172
                gbc.insets = new Insets(0, 2, 0, 0);
173
                panel.add(combo, gbc);
174
                return panel;
175
        }
176
        
177
        /**
178
         * Gets a ComboBox
179
         * @return
180
         */
181
        public JComboBox getFieldsCombo() {
182
                if(fieldsCombo == null) {
183
                        fieldsCombo = new JComboBox();
184
                        fieldsCombo.setPreferredSize(new Dimension(0, 18));
185
                        ObjectAndDescription[] fieldList = getLayerList();
186
                        fieldsCombo.removeAllItems();
187
                        for (int i = 0; i < fieldList.length; i++) 
188
                                fieldsCombo.addItem(fieldList[i]);
189
                }
190
                return fieldsCombo;
191
        }
192
        
193
        /**
194
         * Gets a CheckBox
195
         * @return
196
         */
197
        public JCheckBox getAllLayersCheck() {
198
                if(allLayers == null) {
199
                        allLayers = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("select_all_layers"));
200
                        allLayers.addActionListener(this);
201
                }
202
                return allLayers;
203
        }
204

    
205
        /**
206
         * Gets the summary table
207
         * @return TableContainer
208
         */
209
        public TableContainer getCheckBoxTable() {
210
                if (table == null) {
211
                        table = new TableContainer(columnNames, columnWidths, null);
212
                        table.setModel("CheckBoxModel");
213
                        table.setControlVisible(false);
214
                        table.initialize();
215
                }
216
                return table;
217
        }
218
        
219
        //------------------------------------------------------------
220
        
221
        /*
222
         * (non-Javadoc)
223
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
224
         */
225
        public void actionPerformed(ActionEvent e) {
226
                if(e.getSource() == getAllLayersCheck()) {
227
                        //Selecci?n de todas las capas de la tabla
228
                        try {
229
                                for (int i = 0; i < getCheckBoxTable().getRowCount(); i++) {
230
                                        if(getAllLayersCheck().isSelected())
231
                                                getCheckBoxTable().getModel().setValueAt(new Boolean(true), i, 0);
232
                                        else
233
                                                getCheckBoxTable().getModel().setValueAt(new Boolean(false), i, 0);
234
                                }
235
                        } catch (NotInitializeException e1) {
236
                                Sextante.addErrorToLog(e1);
237
                        }
238
                }
239
        }
240
        
241
        /**
242
         * Adds to the table one entry for each field
243
         */
244
        private void initTable() {
245
                try {
246
                        getCheckBoxTable().removeAllRows();
247
                        ObjectAndDescription[] layerList = getLayerList();
248
                        for (int i = 0; i < layerList.length; i++)
249
                                addTableRow(layerList[i].getDescription());
250
                } catch (NotInitializeException e) {
251
                        Sextante.addErrorToLog(e);
252
                }
253
        }
254

    
255
        /**
256
         * Adds one entry to the table
257
         * @param layerName 
258
         *        Layer name
259
         * @throws NotInitializeException 
260
         */
261
        private void addTableRow(String layerName) throws NotInitializeException {
262
                Object[] row = {        new Boolean(false), 
263
                                                        layerName };
264
                getCheckBoxTable().addRow(row);
265
        }
266
        
267
        @Override
268
    public void assignParameters() {
269
                try {
270
                        ParametersSet params = m_Algorithm.getParameters();
271
                        params.getParameter(MergeAlgorithm.FIELDLAYER).setParameterValue(getSelectedVectorLayer());
272
                        params.getParameter(MergeAlgorithm.LAYERS).setParameterValue(getSelectedLayerList());
273
                        
274
                        OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
275
                        Output out = ooSet.getOutput(MergeAlgorithm.RESULT);
276
                        
277
                        //Reponer estas l?neas para cambiar el panel de salida y comentar la siguiente
278
                        //AlgorithmOutputPanel fsp = getAlgorithmOutputPanel();
279
                        //out.setOutputChannel(new CompositeSourceOutputChannel(fsp.getOutputParameters()));
280
                 out.setOutputChannel(outputChannelSelectionPanel.getOutputChannel());
281
                } catch (Exception e) {
282
                        Sextante.addErrorToLog(e);
283
                }
284
        }
285

    
286
        @Override
287
        public void setOutputValue(String arg0, String arg1) {
288
                
289
        }
290

    
291
        @Override
292
        public void setParameterValue(String arg0, String arg1) {
293
                
294
        }
295
        
296
        /**
297
         * Gets the input layer list
298
         * @return
299
         */
300
        private ObjectAndDescription[] getLayerList() {
301
                if(layerList == null) {
302
                        IVectorLayer[] layers = SextanteGUI.getInputFactory()
303
                        .getVectorLayers(IVectorLayer.SHAPE_TYPE_WRONG);
304
                        layerList = new ObjectAndDescription[layers.length];
305
                        for (int i = 0; i < layers.length; i++)
306
                                layerList[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
307
                }
308
                return layerList;
309
        }
310
        
311
        /**
312
         * Gets the list of selected layers
313
         * @return
314
         */
315
        private ArrayList<IVectorLayer> getSelectedLayerList() {
316
                ObjectAndDescription[] layerList = getLayerList();
317
                ArrayList<IVectorLayer> vLayer = new ArrayList<IVectorLayer>();
318
                for (int i = 0; i < layerList.length; i++) {
319
                        Boolean check = (Boolean)getCheckBoxTable().getModel().getValueAt(i, 0);
320
                        if(check.booleanValue())
321
                                vLayer.add((IVectorLayer)layerList[i].getObject());
322
                }
323
                return vLayer;
324
        }
325
        
326
        /**
327
         * Gets the selected vector layer in the JComboBox
328
         * @return
329
         */
330
        private IVectorLayer getSelectedVectorLayer() {
331
                if(getFieldsCombo().getSelectedItem() != null)
332
                        return (IVectorLayer)((ObjectAndDescription)getFieldsCombo().getSelectedItem()).getObject();
333
                return null;
334
        }
335
        
336
}