Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / dissolveMultiple / DissolveMultipleParametersPanel.java @ 59

History | View | Annotate | Download (8.97 KB)

1

    
2

    
3
package es.unex.sextante.vectorTools.dissolveMultiple;
4

    
5
import info.clearthought.layout.TableLayout;
6
import info.clearthought.layout.TableLayoutConstants;
7

    
8
import java.util.List;
9

    
10
import javax.swing.ComboBoxModel;
11
import javax.swing.DefaultComboBoxModel;
12
import javax.swing.JComboBox;
13
import javax.swing.JLabel;
14
import javax.swing.JScrollPane;
15
import javax.swing.JTable;
16
import javax.swing.ScrollPaneConstants;
17

    
18
import org.japura.gui.Anchor;
19
import org.japura.gui.BatchSelection;
20
import org.japura.gui.CheckComboBox;
21
import org.japura.gui.EmbeddedComponent;
22
import org.japura.gui.model.ListCheckModel;
23

    
24
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer;
25
import es.unex.sextante.core.GeoAlgorithm;
26
import es.unex.sextante.core.ObjectAndDescription;
27
import es.unex.sextante.core.OutputObjectsSet;
28
import es.unex.sextante.core.ParametersSet;
29
import es.unex.sextante.core.Sextante;
30
import es.unex.sextante.dataObjects.IVectorLayer;
31
import es.unex.sextante.exceptions.WrongInputException;
32
import es.unex.sextante.exceptions.WrongParameterIDException;
33
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
34
import es.unex.sextante.gui.algorithm.OutputChannelSelectionPanel;
35
import es.unex.sextante.gui.core.SextanteGUI;
36
import es.unex.sextante.gui.exceptions.LayerCannotBeOverwrittenException;
37
import es.unex.sextante.outputs.IOutputChannel;
38
import es.unex.sextante.outputs.Output;
39
import es.unex.sextante.outputs.OverwriteOutputChannel;
40

    
41

    
42
public class DissolveMultipleParametersPanel
43
         extends
44
            GeoAlgorithmParametersPanel {
45

    
46
   private GeoAlgorithm                m_Algorithm;
47
   private JLabel                      jLabelGroupings;
48
   private CheckComboBox               fieldsBox;
49
   private JTable                      jTableGroupings;
50
   private JComboBox                   jComboBoxLayers;
51
   private JLabel                      jLabelField;
52
   private JLabel                      jLabelLayer;
53
   private JScrollPane                 jScrollPaneFields;
54
   private OutputChannelSelectionPanel outputChannelSelectionPanel;
55

    
56

    
57
   public DissolveMultipleParametersPanel() {
58

    
59
      super();
60

    
61
   }
62

    
63

    
64
   @Override
65
   public void init(final GeoAlgorithm algorithm) {
66

    
67
      m_Algorithm = algorithm;
68

    
69
      initGUI();
70

    
71
   }
72

    
73

    
74
   @Override
75
   public void assignParameters() throws WrongInputException, LayerCannotBeOverwrittenException {
76

    
77

    
78
      final ObjectAndDescription oad = (ObjectAndDescription) jComboBoxLayers.getSelectedItem();
79
      final IVectorLayer layer = (IVectorLayer) oad.getObject();
80
      final ParametersSet params = m_Algorithm.getParameters();
81
      try {
82
         params.getParameter(DissolveMultipleAlgorithm.LAYER).setParameterValue(layer);
83
         final StringBuffer sb = new StringBuffer();
84
         final List<Object> selected = fieldsBox.getModel().getCheckeds();
85
         for (int i = 0; i < selected.size(); i++) {
86
            if (i == 0) {
87
               sb.append(selected.get(i).toString());
88
            }
89
            else {
90
               sb.append("," + selected.get(i).toString());
91
            }
92
         }
93
         params.getParameter(DissolveMultipleAlgorithm.GROUPING_FIELDS).setParameterValue(sb.toString());
94
         final DissolveTableModel model = (DissolveTableModel) jTableGroupings.getModel();
95
         params.getParameter(DissolveMultipleAlgorithm.GROUPING_FUNCTIONS).setParameterValue(model.getAsString());
96
      }
97
      catch (final WrongParameterIDException e) {
98
         throw new WrongInputException();
99
      }
100

    
101

    
102
      IOutputChannel channel;
103
      Output out;
104
      try {
105
         final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
106
         out = ooSet.getOutput(DissolveMultipleAlgorithm.RESULT);
107
         channel = outputChannelSelectionPanel.getOutputChannel();
108
      }
109
      catch (final Exception e) {
110
         throw new WrongInputException();
111
      }
112

    
113
      if (channel instanceof OverwriteOutputChannel) {
114
         final OverwriteOutputChannel ooc = (OverwriteOutputChannel) channel;
115
         if (!ooc.getLayer().canBeEdited()) {
116
            throw new LayerCannotBeOverwrittenException();
117
         }
118
      }
119
      out.setOutputChannel(channel);
120

    
121
   }
122

    
123

    
124
   private void initGUI() {
125
      try {
126
         {
127
            final TableLayout thisLayout = new TableLayout(new double[][] {
128
                     { 5.0, TableLayoutConstants.FILL, TableLayoutConstants.FILL, 5.0 },
129
                     { 5.0, 25.0, 5.0, 25.0, 5.0, 25.0, TableLayoutConstants.FILL, 5.0, 20.0, 5.0 } });
130
            thisLayout.setHGap(5);
131
            thisLayout.setVGap(5);
132
            this.setLayout(thisLayout);
133
            this.setPreferredSize(new java.awt.Dimension(469, 257));
134
            {
135
               jLabelGroupings = new JLabel();
136
               this.add(jLabelGroupings, "1, 5");
137
               jLabelGroupings.setText(Sextante.getText("Summary_statistics"));
138
            }
139
            {
140
               jScrollPaneFields = new JScrollPane();
141
               jScrollPaneFields.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
142
               this.add(jScrollPaneFields, "1, 6, 2, 6");
143
               {
144
                  jTableGroupings = new JTable();
145
                  jScrollPaneFields.setViewportView(jTableGroupings);
146
               }
147
            }
148
            {
149
               jLabelLayer = new JLabel();
150
               this.add(jLabelLayer, "1, 1");
151
               jLabelLayer.setText(Sextante.getText("Layer"));
152
            }
153
            {
154
               jLabelField = new JLabel();
155
               this.add(jLabelField, "1, 3");
156
               jLabelField.setText(Sextante.getText("Fields"));
157
            }
158
            {
159
               fieldsBox = new CheckComboBox();
160
               fieldsBox.setTextFor(CheckComboBox.NONE, Sextante.getText("no_elements_selected"));
161
               fieldsBox.setTextFor(CheckComboBox.MULTIPLE, Sextante.getText("multiple_elements_selected"));
162
               final BatchSelection bs = new BatchSelection.CheckBox();
163
               final EmbeddedComponent comp = new EmbeddedComponent(bs, Anchor.NORTH);
164
               fieldsBox.setEmbeddedComponent(comp);
165
               this.add(fieldsBox, "2, 3");
166
            }
167
            {
168
               final ComboBoxModel jComboBoxLayersModel = new DefaultComboBoxModel(getLayers());
169
               jComboBoxLayers = new JComboBox();
170
               this.add(jComboBoxLayers, "2, 1");
171
               jComboBoxLayers.setModel(jComboBoxLayersModel);
172
               jComboBoxLayers.addItemListener(new java.awt.event.ItemListener() {
173
                  public void itemStateChanged(final java.awt.event.ItemEvent e) {
174
                     setFields();
175
                     setTableModel();
176
                  }
177
               });
178
               jComboBoxLayers.setSelectedIndex(0);
179
               setFields();
180
               //fieldsBox.initGUI();
181
               setTableModel();
182
            }
183
            {
184
               final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
185
               final Output out = ooSet.getOutput(DissolveMultipleAlgorithm.RESULT);
186
               outputChannelSelectionPanel = new OutputChannelSelectionPanel(out, m_Algorithm.getParameters());
187
               this.add(outputChannelSelectionPanel, "2,8");
188
               this.add(new JLabel(Sextante.getText("Result")), "1,8");
189
            }
190
         }
191
      }
192
      catch (final Exception e) {
193
         Sextante.addErrorToLog(e);
194
      }
195
   }
196

    
197

    
198
   protected void setFields() {
199

    
200
      final String[] fields = getFields();
201
      final ListCheckModel model = fieldsBox.getModel();
202
      model.clear();
203
      for (final Object obj : fields) {
204
         model.addElement(obj);
205
      }
206

    
207
      fieldsBox.setModel(model);
208
      //fieldsBox.setFields(getFields());
209

    
210
   }
211

    
212

    
213
   private void setTableModel() {
214

    
215
      final ObjectAndDescription oad = (ObjectAndDescription) jComboBoxLayers.getSelectedItem();
216
      final IVectorLayer layer = (IVectorLayer) oad.getObject();
217
      final DissolveTableModel tableModel = new DissolveTableModel(layer);
218
      jTableGroupings.setModel(tableModel);
219

    
220
   }
221

    
222

    
223
   private String[] getFields() {
224

    
225
      final ObjectAndDescription oad = (ObjectAndDescription) jComboBoxLayers.getSelectedItem();
226
      final IVectorLayer layer = (IVectorLayer) oad.getObject();
227
      return getFields(layer);
228

    
229
   }
230

    
231

    
232
   private String[] getFields(final IVectorLayer layer) {
233

    
234
      return layer.getFieldNames();
235

    
236
   }
237

    
238

    
239
   private ObjectAndDescription[] getLayers() {
240

    
241
      final IVectorLayer[] layers = SextanteGUI.getInputFactory().getVectorLayers(AdditionalInfoVectorLayer.SHAPE_TYPE_ANY);
242
      final ObjectAndDescription[] oad = new ObjectAndDescription[layers.length];
243
      for (int i = 0; i < layers.length; i++) {
244
         oad[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
245
      }
246
      return oad;
247

    
248
   }
249

    
250

    
251
   @Override
252
   public void setOutputValue(final String sOutputName,
253
                              final String sValue) {
254

    
255
      outputChannelSelectionPanel.setText(sValue);
256

    
257
   }
258

    
259

    
260
   @Override
261
   public void setParameterValue(final String parameterName,
262
                                 final String value) {
263
      // TODO Auto-generated method stub
264

    
265
   }
266

    
267
}