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

History | View | Annotate | Download (13.7 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
 *
58
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
59
 */
60
public class MergeParametersPanel extends GeoAlgorithmParametersPanel implements ActionListener {
61

    
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 outputChannelSelectionPanelPol;
74
    private OutputChannelSelectionPanel outputChannelSelectionPanelLine;
75
    private OutputChannelSelectionPanel outputChannelSelectionPanelPoint;
76
    private JPanel outputPanel;
77

    
78
    public MergeParametersPanel() {
79
        super();
80
    }
81

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

    
87
    private void initGUI() {
88
        GridBagLayout gbl = new GridBagLayout();
89
        this.setLayout(gbl);
90

    
91
        GridBagConstraints gbc = new GridBagConstraints();
92
        gbc.fill = GridBagConstraints.HORIZONTAL;
93
        gbc.weightx = 1.0;
94
        gbc.gridx = 0;
95
        gbc.gridy = 0;
96
        gbc.insets = new Insets(5, 5, 8, 5);
97
        this.add(getAllLayersCheck(), gbc);
98

    
99
        gbc.gridy = 1;
100
        gbc.fill = GridBagConstraints.BOTH;
101
        gbc.insets = new Insets(0, 0, 12, 0);
102
        gbc.weighty = 1.0;
103
        this.add(getCheckBoxTable(), gbc);
104

    
105
        gbc.gridy = 2;
106
        gbc.fill = GridBagConstraints.HORIZONTAL;
107
        gbc.insets = new Insets(5, 5, 8, 5);
108
        gbc.weightx = 1.0;
109
        gbc.weighty = 0;
110
        this.add(getFieldsComboPanel(GeoProcessLocator.getGeoProcessManager().getTranslation("use_fields_from_layer"), getFieldsCombo()), gbc);
111

    
112
        gbc.gridy = 3;
113
        this.add(getOutputChannelSelectionPanel(), gbc);
114

    
115
        initTable();
116
    }
117

    
118
    /**
119
     * Gets the output panel (SEXTANTE)
120
     *
121
     * @return
122
     */
123
    private JPanel getOutputChannelSelectionPanel() {
124
        if (outputPanel == null) {
125
            try {
126
                outputPanel = new JPanel();
127
                outputPanel.setLayout(new GridBagLayout());
128
                final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
129
                final Output out_pol = ooSet.getOutput(MergeAlgorithm.RESULT_POL);
130
                final Output out_line = ooSet.getOutput(MergeAlgorithm.RESULT_LINE);
131
                final Output out_point = ooSet.getOutput(MergeAlgorithm.RESULT_POINT);
132

    
133
                outputChannelSelectionPanelPol = new OutputChannelSelectionPanel(out_pol, m_Algorithm.getParameters());
134
                outputChannelSelectionPanelLine = new OutputChannelSelectionPanel(out_line, m_Algorithm.getParameters());
135
                outputChannelSelectionPanelPoint = new OutputChannelSelectionPanel(out_point, m_Algorithm.getParameters());
136

    
137
                String label = GeoProcessLocator.getGeoProcessManager().getTranslation("Merge");
138

    
139
                GridBagConstraints gbc = new GridBagConstraints();
140
                gbc.fill = GridBagConstraints.HORIZONTAL;
141
                gbc.insets = new Insets(0, 0, 4, 0);
142
                gbc.weightx = 1;
143
                gbc.gridx = 0;
144
                gbc.gridy = 0;
145
                outputPanel.add(new JLabel(" " + label + " ["
146
                        + GeoProcessLocator.getGeoProcessManager().getTranslation("Polygon") + "]               "), gbc);
147

    
148
                gbc.gridx = 0;
149
                gbc.gridy = 1;
150
                outputPanel.add(new JLabel(" " + label + " ["
151
                        + GeoProcessLocator.getGeoProcessManager().getTranslation("Line") + "]               "), gbc);
152

    
153
                gbc.gridx = 0;
154
                gbc.gridy = 2;
155
                outputPanel.add(new JLabel(" " + label + " ["
156
                        + GeoProcessLocator.getGeoProcessManager().getTranslation("Point") + "]               "), gbc);
157

    
158
                gbc.fill = GridBagConstraints.HORIZONTAL;
159
                gbc.weightx = 1;
160
                gbc.gridx = 1;
161
                gbc.gridy = 0;
162
                outputPanel.add(outputChannelSelectionPanelPol, gbc);
163

    
164
                gbc.gridx = 1;
165
                gbc.gridy = 1;
166
                outputPanel.add(outputChannelSelectionPanelLine, gbc);
167

    
168
                gbc.gridx = 1;
169
                gbc.gridy = 2;
170
                outputPanel.add(outputChannelSelectionPanelPoint, gbc);
171
            } catch (final Exception e) {
172
                Sextante.addErrorToLog(e);
173
            }
174
        }
175
        return outputPanel;
176
    }
177

    
178
    /**
179
     * Gets the output panel (DAL)
180
     *
181
     * @return
182
     */
183
    @SuppressWarnings("unused")
184
    private AlgorithmOutputPanel getAlgorithmOutputPanel() {
185
        if (algorithmOutputPanel == null) {
186
            algorithmOutputPanel = new AlgorithmOutputPanel();
187
        }
188
        return algorithmOutputPanel;
189
    }
190

    
191
    /**
192
     * Gets a new JPanel with the text and JComboBox
193
     *
194
     * @param text
195
     * @param combo
196
     * @return
197
     */
198
    public JPanel getFieldsComboPanel(String text, JComboBox combo) {
199
        JPanel panel = new JPanel();
200
        GridBagLayout gbl = new GridBagLayout();
201
        panel.setLayout(gbl);
202

    
203
        GridBagConstraints gbc = new GridBagConstraints();
204
        gbc.fill = GridBagConstraints.HORIZONTAL;
205
        gbc.weightx = 1.0;
206
        gbc.gridx = 0;
207
        gbc.insets = new Insets(0, 0, 4, 0);
208
        JLabel label = new JLabel(text);
209
//                label.setPreferredSize(new Dimension(80, 18));
210
        panel.add(label, gbc);
211

    
212
        gbc.fill = GridBagConstraints.HORIZONTAL;
213
        gbc.weightx = 1.0;
214
        gbc.gridx = 1;
215
//                gbc.anchor = GridBagConstraints.EAST;
216
        gbc.insets = new Insets(0, 0, 4, 0);
217
        panel.add(combo, gbc);
218
        return panel;
219
    }
220

    
221
    /**
222
     * Gets a ComboBox
223
     *
224
     * @return
225
     */
226
    public JComboBox getFieldsCombo() {
227
        if (fieldsCombo == null) {
228
            fieldsCombo = new JComboBox();
229
//                        fieldsCombo.setPreferredSize(new Dimension(0, 18));
230
            ObjectAndDescription[] fieldList = getLayerList();
231
            fieldsCombo.removeAllItems();
232
            for (int i = 0; i < fieldList.length; i++) {
233
                fieldsCombo.addItem(fieldList[i]);
234
            }
235
        }
236
        return fieldsCombo;
237
    }
238

    
239
    /**
240
     * Gets a CheckBox
241
     *
242
     * @return
243
     */
244
    public JCheckBox getAllLayersCheck() {
245
        if (allLayers == null) {
246
            allLayers = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("select_all_layers"));
247
            allLayers.addActionListener(this);
248
        }
249
        return allLayers;
250
    }
251

    
252
    /**
253
     * Gets the summary table
254
     *
255
     * @return TableContainer
256
     */
257
    public TableContainer getCheckBoxTable() {
258
        if (table == null) {
259
            table = new TableContainer(columnNames, columnWidths, null);
260
            table.setModel("CheckBoxModel");
261
            table.setControlVisible(false);
262
            table.initialize();
263
        }
264
        return table;
265
    }
266

    
267
    //------------------------------------------------------------
268

    
269
    /*
270
         * (non-Javadoc)
271
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
272
     */
273
    public void actionPerformed(ActionEvent e) {
274
        if (e.getSource() == getAllLayersCheck()) {
275
            //Selecci?n de todas las capas de la tabla
276
            try {
277
                for (int i = 0; i < getCheckBoxTable().getRowCount(); i++) {
278
                    if (getAllLayersCheck().isSelected()) {
279
                        getCheckBoxTable().getModel().setValueAt(new Boolean(true), i, 0);
280
                    } else {
281
                        getCheckBoxTable().getModel().setValueAt(new Boolean(false), i, 0);
282
                    }
283
                }
284
            } catch (NotInitializeException e1) {
285
                Sextante.addErrorToLog(e1);
286
            }
287
        }
288
    }
289

    
290
    /**
291
     * Adds to the table one entry for each field
292
     */
293
    private void initTable() {
294
        try {
295
            getCheckBoxTable().removeAllRows();
296
            ObjectAndDescription[] layerList = getLayerList();
297
            for (int i = 0; i < layerList.length; i++) {
298
                addTableRow(layerList[i].getDescription());
299
            }
300
        } catch (NotInitializeException e) {
301
            Sextante.addErrorToLog(e);
302
        }
303
    }
304

    
305
    /**
306
     * Adds one entry to the table
307
     *
308
     * @param layerName Layer name
309
     * @throws NotInitializeException
310
     */
311
    private void addTableRow(String layerName) throws NotInitializeException {
312
        Object[] row = {new Boolean(false),
313
            layerName};
314
        getCheckBoxTable().addRow(row);
315
    }
316

    
317
    @Override
318
    public void assignParameters() {
319
        try {
320
            ParametersSet params = m_Algorithm.getParameters();
321
            params.getParameter(MergeAlgorithm.FIELDLAYER).setParameterValue(getSelectedVectorLayer());
322
            params.getParameter(MergeAlgorithm.LAYERS).setParameterValue(getSelectedLayerList());
323

    
324
            OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
325
            Output outPol = ooSet.getOutput(MergeAlgorithm.RESULT_POL);
326
            Output outLine = ooSet.getOutput(MergeAlgorithm.RESULT_LINE);
327
            Output outPoint = ooSet.getOutput(MergeAlgorithm.RESULT_POINT);
328

    
329
            //Reponer estas l?neas para cambiar el panel de salida y comentar la siguiente
330
            //AlgorithmOutputPanel fsp = getAlgorithmOutputPanel();
331
            //out.setOutputChannel(new CompositeSourceOutputChannel(fsp.getOutputParameters()));
332
            outPol.setOutputChannel(outputChannelSelectionPanelPol.getOutputChannel());
333
            outLine.setOutputChannel(outputChannelSelectionPanelLine.getOutputChannel());
334
            outPoint.setOutputChannel(outputChannelSelectionPanelPoint.getOutputChannel());
335
        } catch (Exception e) {
336
            Sextante.addErrorToLog(e);
337
        }
338
    }
339

    
340
    @Override
341
    public void setOutputValue(String arg0, String arg1) {
342

    
343
    }
344

    
345
    @Override
346
    public void setParameterValue(String arg0, String arg1) {
347

    
348
    }
349

    
350
    /**
351
     * Gets the input layer list
352
     *
353
     * @return
354
     */
355
    private ObjectAndDescription[] getLayerList() {
356
        if (layerList == null) {
357
            IVectorLayer[] layers = SextanteGUI.getInputFactory()
358
                    .getVectorLayers(IVectorLayer.SHAPE_TYPE_WRONG);
359
            layerList = new ObjectAndDescription[layers.length];
360
            for (int i = 0; i < layers.length; i++) {
361
                layerList[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
362
            }
363
        }
364
        return layerList;
365
    }
366

    
367
    /**
368
     * Gets the list of selected layers
369
     *
370
     * @return
371
     */
372
    private ArrayList<IVectorLayer> getSelectedLayerList() {
373
        ObjectAndDescription[] layerList = getLayerList();
374
        ArrayList<IVectorLayer> vLayer = new ArrayList<IVectorLayer>();
375
        for (int i = 0; i < layerList.length; i++) {
376
            Boolean check = (Boolean) getCheckBoxTable().getModel().getValueAt(i, 0);
377
            if (check.booleanValue()) {
378
                vLayer.add((IVectorLayer) layerList[i].getObject());
379
            }
380
        }
381
        return vLayer;
382
    }
383

    
384
    /**
385
     * Gets the selected vector layer in the JComboBox
386
     *
387
     * @return
388
     */
389
    private IVectorLayer getSelectedVectorLayer() {
390
        if (getFieldsCombo().getSelectedItem() != null) {
391
            return (IVectorLayer) ((ObjectAndDescription) getFieldsCombo().getSelectedItem()).getObject();
392
        }
393
        return null;
394
    }
395

    
396
}