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

History | View | Annotate | Download (8.78 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2010 Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.geoprocess.algorithm.merge;
20

    
21
import java.awt.Dimension;
22
import java.awt.GridBagConstraints;
23
import java.awt.GridBagLayout;
24
import java.awt.Insets;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.util.ArrayList;
28

    
29
import javax.swing.JCheckBox;
30
import javax.swing.JComboBox;
31
import javax.swing.JLabel;
32
import javax.swing.JPanel;
33

    
34
import es.unex.sextante.core.GeoAlgorithm;
35
import es.unex.sextante.core.ObjectAndDescription;
36
import es.unex.sextante.core.OutputObjectsSet;
37
import es.unex.sextante.core.ParametersSet;
38
import es.unex.sextante.core.Sextante;
39
import es.unex.sextante.dataObjects.IVectorLayer;
40
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
41
import es.unex.sextante.gui.core.SextanteGUI;
42
import es.unex.sextante.outputs.Output;
43

    
44
import org.gvsig.geoprocess.core.CompositeSourceOutputChannel;
45
import org.gvsig.geoprocess.sextante.gui.algorithm.AlgorithmOutputPanel;
46
import org.gvsig.gui.beans.table.TableContainer;
47
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
48

    
49
/**
50
 * Panel for merge algorithm
51
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
52
 */
53
public class MergeParametersPanel extends GeoAlgorithmParametersPanel implements ActionListener {
54
        private static final long                serialVersionUID   = 1L;
55
        private GeoAlgorithm                     m_Algorithm        = null;
56
        private JComboBox                        fieldsCombo        = null;
57
        private JCheckBox                        allLayers          = null;
58
        //private AlgorithmOutputPanel             output             = null;
59
        private final String[]                   columnNames        = { "Selected", "Layer" };
60
        private final int[]                      columnWidths       = { 35, 334 };
61
        private TableContainer                   table              = null;
62
        private ObjectAndDescription[]           layerList          = null;
63
        private AlgorithmOutputPanel             algorithmOutputPanel  = null;
64
        
65
        public MergeParametersPanel() {
66
                super();
67
        }
68

    
69
    public void init(GeoAlgorithm algorithm) {
70
            m_Algorithm = algorithm;
71
            initGUI();
72
    }
73

    
74
        private void initGUI() {
75
                GridBagLayout gbl = new GridBagLayout();
76
                this.setLayout(gbl);
77
                
78
                GridBagConstraints gbc = new GridBagConstraints();
79
                gbc.fill = GridBagConstraints.HORIZONTAL;
80
                gbc.weightx = 1.0;
81
                gbc.gridx = 0;
82
                gbc.gridy = 0;
83
                gbc.insets = new Insets(5, 5, 8, 5);
84
                this.add(getAllLayersCheck(), gbc);
85
                
86
                gbc.gridy = 1;
87
                gbc.fill = GridBagConstraints.BOTH;
88
                gbc.insets = new Insets(0, 0, 12, 0);
89
                gbc.weighty = 1.0;
90
                this.add(getCheckBoxTable(), gbc);
91
                
92
                gbc.gridy = 2;
93
                gbc.fill = GridBagConstraints.HORIZONTAL;
94
                gbc.insets = new Insets(5, 5, 8, 5);
95
                gbc.weightx = 1.0;
96
                gbc.weighty = 0;
97
                this.add(getFieldsComboPanel(Sextante.getText("Field"), getFieldsCombo()), gbc);
98
                
99
                gbc.gridy = 3;
100
                this.add(getAlgorithmOutputPanel(), gbc);
101
                
102
                initTable();
103
        }
104
        
105
        /**
106
         * Gets the output panel
107
         * @return
108
         */
109
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
110
                if(algorithmOutputPanel == null)
111
                    algorithmOutputPanel = new AlgorithmOutputPanel();
112
                return algorithmOutputPanel;
113
        }
114
        
115
        /**
116
         * Gets a new JPanel with the text and JComboBox 
117
         * @param text
118
         * @param combo
119
         * @return
120
         */
121
        public JPanel getFieldsComboPanel(String text, JComboBox combo) {
122
                JPanel panel = new JPanel();
123
                GridBagLayout gbl = new GridBagLayout();
124
                panel.setLayout(gbl);
125

    
126
                GridBagConstraints gbc = new GridBagConstraints();
127
                gbc.fill = GridBagConstraints.NONE;
128
                gbc.weightx = 0;
129
                gbc.gridx = 0;
130
                gbc.insets = new Insets(0, 2, 0, 50);
131
                JLabel label = new JLabel(text);
132
                label.setPreferredSize(new Dimension(80, 18));
133
                panel.add(label, gbc);
134

    
135
                gbc.fill = GridBagConstraints.HORIZONTAL;
136
                gbc.weightx = 1.0;
137
                gbc.gridx = 1;
138
                gbc.anchor = GridBagConstraints.EAST;
139
                gbc.insets = new Insets(0, 2, 0, 0);
140
                panel.add(combo, gbc);
141
                return panel;
142
        }
143
        
144
        /**
145
         * Gets a ComboBox
146
         * @return
147
         */
148
        public JComboBox getFieldsCombo() {
149
                if(fieldsCombo == null) {
150
                        fieldsCombo = new JComboBox();
151
                        fieldsCombo.setPreferredSize(new Dimension(0, 18));
152
                        ObjectAndDescription[] fieldList = getLayerList();
153
                        fieldsCombo.removeAllItems();
154
                        for (int i = 0; i < fieldList.length; i++) 
155
                                fieldsCombo.addItem(fieldList[i]);
156
                }
157
                return fieldsCombo;
158
        }
159
        
160
        /**
161
         * Gets a CheckBox
162
         * @return
163
         */
164
        public JCheckBox getAllLayersCheck() {
165
                if(allLayers == null) {
166
                        allLayers = new JCheckBox(Sextante.getText("select_all_layers"));
167
                        allLayers.addActionListener(this);
168
                }
169
                return allLayers;
170
        }
171

    
172
        /**
173
         * Gets the summary table
174
         * @return TableContainer
175
         */
176
        public TableContainer getCheckBoxTable() {
177
                if (table == null) {
178
                        table = new TableContainer(columnNames, columnWidths, null);
179
                        table.setModel("CheckBoxModel");
180
                        table.setControlVisible(false);
181
                        table.initialize();
182
                }
183
                return table;
184
        }
185
        
186
        //------------------------------------------------------------
187
        
188
        /*
189
         * (non-Javadoc)
190
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
191
         */
192
        public void actionPerformed(ActionEvent e) {
193
                if(e.getSource() == getAllLayersCheck()) {
194
                        //Selecci?n de todas las capas de la tabla
195
                        try {
196
                                for (int i = 0; i < getCheckBoxTable().getRowCount(); i++) {
197
                                        if(getAllLayersCheck().isSelected())
198
                                                getCheckBoxTable().getModel().setValueAt(new Boolean(true), i, 0);
199
                                        else
200
                                                getCheckBoxTable().getModel().setValueAt(new Boolean(false), i, 0);
201
                                }
202
                        } catch (NotInitializeException e1) {
203
                                Sextante.addErrorToLog(e1);
204
                        }
205
                }
206
        }
207
        
208
        /**
209
         * Adds to the table one entry for each field
210
         */
211
        private void initTable() {
212
                try {
213
                        getCheckBoxTable().removeAllRows();
214
                        ObjectAndDescription[] layerList = getLayerList();
215
                        for (int i = 0; i < layerList.length; i++)
216
                                addTableRow(layerList[i].getDescription());
217
                } catch (NotInitializeException e) {
218
                        Sextante.addErrorToLog(e);
219
                }
220
        }
221

    
222
        /**
223
         * Adds one entry to the table
224
         * @param layerName 
225
         *        Layer name
226
         * @throws NotInitializeException 
227
         */
228
        private void addTableRow(String layerName) throws NotInitializeException {
229
                Object[] row = {        new Boolean(false), 
230
                                                        layerName };
231
                getCheckBoxTable().addRow(row);
232
        }
233
        
234
        @Override
235
    public void assignParameters() {
236
                try {
237
                        ParametersSet params = m_Algorithm.getParameters();
238
                        params.getParameter(MergeAlgorithm.FIELDLAYER).setParameterValue(getSelectedVectorLayer());
239
                        params.getParameter(MergeAlgorithm.LAYERS).setParameterValue(getSelectedLayerList());
240
                        
241
                        OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
242
                        Output out = ooSet.getOutput(MergeAlgorithm.RESULT);
243
                        out.setOutputChannel(new CompositeSourceOutputChannel(getAlgorithmOutputPanel().getOutputParameters()));
244
                } catch (Exception e) {
245
                        Sextante.addErrorToLog(e);
246
                }
247
        }
248

    
249
        @Override
250
        public void setOutputValue(String arg0, String arg1) {
251
                
252
        }
253

    
254
        @Override
255
        public void setParameterValue(String arg0, String arg1) {
256
                
257
        }
258
        
259
        /**
260
         * Gets the input layer list
261
         * @return
262
         */
263
        private ObjectAndDescription[] getLayerList() {
264
                if(layerList == null) {
265
                        IVectorLayer[] layers = SextanteGUI.getInputFactory()
266
                        .getVectorLayers(IVectorLayer.SHAPE_TYPE_WRONG);
267
                        layerList = new ObjectAndDescription[layers.length];
268
                        for (int i = 0; i < layers.length; i++)
269
                                layerList[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
270
                }
271
                return layerList;
272
        }
273
        
274
        /**
275
         * Gets the list of selected layers
276
         * @return
277
         */
278
        private ArrayList<IVectorLayer> getSelectedLayerList() {
279
                ObjectAndDescription[] layerList = getLayerList();
280
                ArrayList<IVectorLayer> vLayer = new ArrayList<IVectorLayer>();
281
                for (int i = 0; i < layerList.length; i++) {
282
                        Boolean check = (Boolean)getCheckBoxTable().getModel().getValueAt(i, 0);
283
                        if(check.booleanValue())
284
                                vLayer.add((IVectorLayer)layerList[i].getObject());
285
                }
286
                return vLayer;
287
        }
288
        
289
        /**
290
         * Gets the selected vector layer in the JComboBox
291
         * @return
292
         */
293
        private IVectorLayer getSelectedVectorLayer() {
294
                if(getFieldsCombo().getSelectedItem() != null)
295
                        return (IVectorLayer)((ObjectAndDescription)getFieldsCombo().getSelectedItem()).getObject();
296
                return null;
297
        }
298
        
299
}