Statistics
| Revision:

gvsig-raster / org.gvsig.raster.principalcomponents / trunk / org.gvsig.raster.principalcomponents / org.gvsig.raster.principalcomponents.swing / org.gvsig.raster.principalcomponents.swing.impl / src / main / java / org / gvsig / raster / principalcomponents / swing / impl / main / PCAComponentsListPanelImpl.java @ 2125

History | View | Annotate | Download (9.45 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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
*/
22
package org.gvsig.raster.principalcomponents.swing.impl.main;
23

    
24
import java.awt.BorderLayout;
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.event.ActionListener;
28
import java.util.EventListener;
29
import java.util.List;
30

    
31
import javax.swing.BorderFactory;
32
import javax.swing.JButton;
33
import javax.swing.JComponent;
34
import javax.swing.JPanel;
35
import javax.swing.JRadioButton;
36

    
37
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
38
import org.gvsig.i18n.Messages;
39
import org.gvsig.raster.algorithm.BasicAPISwingPanel;
40
import org.gvsig.raster.algorithm.RasterBaseAlgorithmLibrary;
41
import org.gvsig.raster.algorithm.process.DataProcess;
42
import org.gvsig.raster.algorithm.process.ProcessException;
43
import org.gvsig.raster.principalcomponents.algorithm.PCStatsDataStructure;
44
import org.gvsig.raster.principalcomponents.algorithm.PrincipalComponentsAlgorithmLibrary;
45
import org.gvsig.raster.principalcomponents.swing.PrincipalComponentsSwingLibrary;
46
import org.gvsig.raster.roi.ROI;
47
import org.gvsig.raster.swing.RasterSwingLocator;
48
import org.gvsig.raster.swing.RasterSwingManager;
49
import org.gvsig.raster.swing.pagedtable.ModelLoader;
50
import org.gvsig.raster.swing.pagedtable.PagedTable;
51

    
52
/**
53
 * @author Nacho Brodin (nachobrodin@gmail.com)
54
 */
55
public class PCAComponentsListPanelImpl extends JPanel implements BasicAPISwingPanel {
56
        private static final long               serialVersionUID         = 1L;
57
        private Object                          inputStore               = null;
58
        private PagedTable                      table                    = null;
59
        private JRadioButton                    byBandRadioButton        = null;
60
        private JRadioButton                    varCovarRadioButton      = null;
61
        private JRadioButton                    autovectorRadioButton    = null;
62
        private JButton                         generateButton           = null;
63
        private PCStatsDataStructure            stats                    = null;
64
        private String                          fileName                 = null;
65
        private boolean[]                       bands                    = null;
66
        private List<ROI>                       rois                     = null;
67
        
68
        public PCAComponentsListPanelImpl(
69
                        Object inputStore, 
70
                        PCStatsDataStructure stats, 
71
                        String fileName, 
72
                        boolean[] bands, 
73
                        List<ROI> rois) {
74
                this.inputStore = inputStore;
75
                this.stats = stats;
76
                this.fileName = fileName;
77
                this.bands = bands;
78
                this.rois = rois;
79
                init();
80
        }
81
        
82
        private void init() {
83
                setLayout(new GridBagLayout());
84
                
85
                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
86
                gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
87
                gridBagConstraints1.weightx = 1;
88
                gridBagConstraints1.weighty = 1;
89
                gridBagConstraints1.insets = new java.awt.Insets(0, 0, 0, 0);
90
                gridBagConstraints1.gridx = 0;
91
                gridBagConstraints1.gridy = 0;
92
                add(getInputsPanel(), gridBagConstraints1);
93
                
94
                gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
95
                gridBagConstraints1.weighty = 0;
96
                gridBagConstraints1.gridy = 1;
97
                add(getPanelStatistics(), gridBagConstraints1);
98
        }
99
        
100
        public JPanel getPanelStatistics() {
101
                JPanel p = new JPanel();
102
                p.setBorder(BorderFactory.createTitledBorder(Messages.getText("statistics")));
103
                p.setLayout(new GridBagLayout());
104
                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
105
                gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
106
                gridBagConstraints1.weightx = 1;
107
                gridBagConstraints1.insets = new java.awt.Insets(0, 0, 2, 0);
108
                gridBagConstraints1.gridwidth = 2;
109
                
110
                gridBagConstraints1.gridx = 0;
111
                gridBagConstraints1.gridy = 0;
112
                p.add(getByBandRadioButton(), gridBagConstraints1);
113
                
114
                gridBagConstraints1.gridwidth = 1;
115
                gridBagConstraints1.gridy = 1;
116
                p.add(getVarCovarRadioButton(), gridBagConstraints1);
117
                
118
                gridBagConstraints1.gridwidth = 2;
119
                gridBagConstraints1.gridy = 2;
120
                p.add(getAutovectorRadioButton(), gridBagConstraints1);
121
                
122
                gridBagConstraints1.gridwidth = 1;
123
                gridBagConstraints1.gridx = 1;
124
                gridBagConstraints1.gridy = 1;
125
                p.add(getGenerateButton(), gridBagConstraints1);
126
                
127
                return p;
128
        }
129
        
130
        public JRadioButton getByBandRadioButton() {
131
                if(byBandRadioButton == null) {
132
                        byBandRadioButton = new JRadioButton(Messages.getText("by_band"));
133
                        byBandRadioButton.setSelected(true);
134
                }
135
                return byBandRadioButton;
136
        }
137
        
138
        public JRadioButton getVarCovarRadioButton() {
139
                if(varCovarRadioButton == null)
140
                        varCovarRadioButton = new JRadioButton(Messages.getText("var_covar"));
141
                return varCovarRadioButton;
142
        }
143
        
144
        public JRadioButton getAutovectorRadioButton() {
145
                if(autovectorRadioButton == null)
146
                        autovectorRadioButton = new JRadioButton(Messages.getText("autovector"));
147
                return autovectorRadioButton;
148
        }
149
        
150
        public JButton getGenerateButton() {
151
                if(generateButton == null)
152
                        generateButton = new JButton(Messages.getText("generate"));
153
                return generateButton;
154
        }
155
        
156
        private JPanel getInputsPanel() {
157
                JPanel p = new JPanel();
158
                p.setBorder(BorderFactory.createTitledBorder(Messages.getText("components_selection")));
159
                p.setLayout(new BorderLayout());
160
                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
161
                gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
162
                gridBagConstraints1.weightx = 1;
163
                gridBagConstraints1.weighty = 1;
164
                gridBagConstraints1.insets = new java.awt.Insets(2, 2, 2, 2);
165
                
166
                gridBagConstraints1.gridx = 0;
167
                gridBagConstraints1.gridy = 0;
168
                p.add(getPagedTable().getComponent(), BorderLayout.CENTER);
169
                
170
                return p;
171
        }
172
        
173
        public JComponent getComponent() {
174
                return this;
175
        }
176
        
177
        public PagedTable getPagedTable() {
178
                if(table == null) {
179
                        RasterSwingManager manager = RasterSwingLocator.getSwingManager();
180
                        String[] columnNames = {
181
                                        "", 
182
                                        "C",
183
                                        Messages.getText("autovalor"),
184
                                        "%"};
185
                        int[] columnSizes = {30, 35, -1, -1};
186

    
187
                        BandTableModel model = new BandTableModel(columnNames);
188
                        ModelLoader loader = manager.createModelLoader(model);
189
                        CheckBoxColumnRenderer render = new CheckBoxColumnRenderer(null);
190
                        CheckBoxColumnEditor editor = new CheckBoxColumnEditor();
191
                        loader.setRenderForColumn(0, render);
192
                        loader.setCellEditorForColumn(0, editor);
193
                        loader.setColumnNames(columnNames);
194
                        loader.setColumnWidths(columnSizes);
195
                        
196
                        table = manager.createPagedTable(loader);
197
                        table.showControllerTable(false);
198
                        table.showMoveRowsControls(false);
199
                }
200
                return table;
201
        }
202

    
203
        public Object getResult() throws ProcessException {
204
                DataProcess task = RasterBaseAlgorithmLibrary.getManager().createRasterTask(PrincipalComponentsAlgorithmLibrary.PC_PROCESS_LABEL);
205
                List<String> params = task.getRasterTaskInputParameters(PrincipalComponentsAlgorithmLibrary.PC_PROCESS_LABEL);
206
                for (int i = 0; i < params.size(); i++) {
207
                        String paramName = params.get(i);
208
                        Class<?> paramType = task.getParameterTypeByProcess(PrincipalComponentsAlgorithmLibrary.PC_PROCESS_LABEL, paramName);
209
                        if(paramType == RasterDataStore.class) {
210
                                task.addParam(paramName, (RasterDataStore)inputStore);
211
                        }
212
                        if(paramName.equals("SELECTEDPCS") && paramType == Boolean[].class) {
213
                                boolean[] components = new boolean[getPagedTable().getRowCount()];
214
                                for (int j = 0; j < getPagedTable().getRowCount(); j++) {
215
                                        Object obj = getPagedTable().getValueAt(j, 0);
216
                                        if(obj instanceof Boolean)
217
                                                components[j] = ((Boolean)obj).booleanValue();
218
                                }
219
                                task.addParam(paramName, components);
220
                        }
221
                        
222
                        if(paramName.equals("BANDS") && paramType == Boolean[].class) {
223
                                task.addParam(paramName, bands);
224
                        }
225
                        
226
                        if(paramType == PCStatsDataStructure.class) {
227
                                task.addParam(paramName, stats);
228
                        }
229
                        
230
                        if(paramType == String.class) {
231
                                task.addParam(paramName, fileName);
232
                        }
233
                        
234
                        if(paramType == List.class) {
235
                                task.addParam(paramName, rois);
236
                        }
237
                }
238
                return new Object[]{task, getByBandRadioButton().isSelected(), getVarCovarRadioButton().isSelected(), getAutovectorRadioButton().isSelected()};
239
        }
240

    
241

    
242
        public void initialize() {
243
                double acumulado = 0;
244
                for (int i = 0; i < stats.getAutovalues().length; i++)
245
                        acumulado += stats.getAutovalues()[i];
246
                int autova[] = new int[stats.getAutovalues().length];
247
                int cont = stats.getAutovalues().length - 1;
248
                for (int i = 0; i < stats.getAutovalues().length; i++) {
249
                        autova[i] = cont;
250
                        cont--;
251
                }                
252
                
253
                for (int i = stats.getAutovalues().length-1; i >= 0; i--) {
254
                        getPagedTable().addRow(new Object[] {true, 
255
                                autova[i],
256
                                stats.getAutovalues()[i],
257
                                stats.getAutovalues()[i] / acumulado});
258
                }
259
        }
260

    
261
        public int getComponentIDByObject(Object obj) {
262
                return obj == getGenerateButton() ? PrincipalComponentsSwingLibrary.GENERATE_STATIST : -1;
263
        }
264

    
265
        public void addListener(EventListener listener) {
266
                if(listener instanceof ActionListener)
267
                        getGenerateButton().addActionListener((ActionListener)listener);
268
        }
269

    
270
}