Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.buffer / org.gvsig.raster.swing.buffer.impl / src / main / java / org / gvsig / raster / swing / buffer / impl / histogram / HistogramPanelController.java @ 43803

History | View | Annotate | Download (15.8 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2017 gvSIG Association
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., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.raster.swing.buffer.impl.histogram;
24

    
25
import java.awt.Color;
26
import java.awt.Container;
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31
import java.awt.event.ItemListener;
32
import java.util.List;
33
import java.util.Locale;
34

    
35
import javax.swing.JComponent;
36
import javax.swing.JScrollPane;
37
import javax.swing.JTable;
38
import javax.swing.JViewport;
39
import javax.swing.ListSelectionModel;
40
import javax.swing.SwingUtilities;
41
import javax.swing.event.TableModelEvent;
42
import javax.swing.event.TableModelListener;
43
import javax.swing.table.DefaultTableColumnModel;
44
import javax.swing.table.TableColumn;
45
import javax.swing.table.TableColumnModel;
46

    
47
import org.jfree.data.xy.XYSeries;
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

    
51
import org.gvsig.raster.lib.buffer.api.statistics.HistogramBand;
52
import org.gvsig.raster.lib.buffer.api.statistics.Statistics;
53
import org.gvsig.raster.swing.buffer.RasterSwingBufferLocator;
54
import org.gvsig.raster.swing.buffer.RasterSwingBufferManager;
55
import org.gvsig.raster.swing.buffer.SelectableBandsTableModel;
56
import org.gvsig.raster.swing.buffer.exceptions.RasterCreatingPanelException;
57
import org.gvsig.raster.swing.buffer.histogram.HistogramPanel;
58
import org.gvsig.raster.swing.buffer.impl.histogram.graphic.GraphicContainer;
59
import org.gvsig.raster.swing.buffer.impl.histogram.graphic.GraphicEvent;
60
import org.gvsig.raster.swing.buffer.impl.histogram.graphic.GraphicListener;
61
import org.gvsig.tools.ToolsLocator;
62
import org.gvsig.tools.i18n.I18nManager;
63

    
64
/**
65
 * @author fdiaz
66
 *
67
 */
68
public class HistogramPanelController extends HistogramPanelView implements HistogramPanel {
69

    
70
    /**
71
     *
72
     */
73
    private static final long serialVersionUID = 3098738174477175358L;
74

    
75
    /**
76
     *
77
     */
78
    private static final Logger LOG = LoggerFactory.getLogger(HistogramPanelController.class);
79

    
80
    private static String[] sources = { "_full", "_displayed_data" };
81
    private static String[] types = { "_normal", "_accumulated", "_logarithmic" };
82

    
83
    private static Color[] defaultColors = new Color[] {
84
        Color.red,
85
        Color.green,
86
        Color.blue,
87
        Color.darkGray,
88
        Color.cyan,
89
        Color.magenta,
90
        Color.yellow,
91
        Color.black,
92
        Color.orange };
93

    
94
    private Statistics statistics;
95
    private SelectableBandsTableModel tableModel;
96

    
97
    private Statistics fullStatistics;
98
    private Statistics visibleStatistics;
99
    private boolean enabledVisibleStatistics;
100

    
101
    private GraphicContainer graphicContainer;
102

    
103
    private Color[] colors;
104
    boolean initialized;
105

    
106
    /**
107
     * @throws RasterCreatingPanelException
108
     */
109
    public HistogramPanelController() throws RasterCreatingPanelException {
110
        this.initialized = false;
111
        translate();
112
        initializeComponents();
113
    }
114

    
115
    public void set(Statistics statistics) throws RasterCreatingPanelException{
116
        this.fullStatistics = statistics;
117
        this.statistics = statistics;
118
        if(this.tableModel != null){
119
            init();
120
        }
121
    }
122

    
123
    public void set(SelectableBandsTableModel tableModel) throws RasterCreatingPanelException{
124
        this.tableModel = tableModel;
125
        if(this.statistics != null){
126
            init();
127
        }
128
    }
129

    
130
    public void setVisibleStatistics(Statistics visibleStatistics) {
131
        this.visibleStatistics = visibleStatistics;
132
        if(this.initialized){
133
            updateComponents();
134
        }
135
    }
136

    
137
    @Override
138
    public void setBandColors(Color[] colors) {
139
        this.colors = colors;
140
        if(this.initialized){
141
            updateComponents();
142
        }
143
    }
144

    
145
    private void init() throws RasterCreatingPanelException{
146

    
147
        RasterSwingBufferManager swingManager = RasterSwingBufferLocator.getSwingManager();
148
        swingManager.makeASelectableBandsTable(tblBands, this.tableModel);
149

    
150
        ((SelectableBandsTableModel)tblBands.getModel()).addTableModelListener(new TableModelListener() {
151

    
152
            @Override
153
            public void tableChanged(TableModelEvent e) {
154
                updateComponents();
155
            }
156

    
157
        });
158

    
159
        updateComponents();
160
        this.initialized = true;
161
    }
162

    
163
    @Override
164
    public JComponent asJComponent() {
165
        return this;
166
    }
167

    
168
    private void initializeComponents() {
169

    
170
        cmbSource.addActionListener(new ActionListener() {
171
            @Override
172
            public void actionPerformed(ActionEvent e) {
173
                updateComponents();
174
            }
175
        });
176

    
177
        cmbType.addActionListener(new ActionListener() {
178
            @Override
179
            public void actionPerformed(ActionEvent e) {
180
                updateComponents();
181
            }
182
        });
183

    
184
        btnAllBands.addActionListener(new ActionListener() {
185
            @Override
186
            public void actionPerformed(ActionEvent e) {
187
                doSelectAllBands();
188
            }
189
        });
190

    
191
        btnClearBands.addActionListener(new ActionListener() {
192
            @Override
193
            public void actionPerformed(ActionEvent e) {
194
                doUnselectAllBands();
195
            }
196
        });
197

    
198
        btnExportTable.addActionListener(new ActionListener() {
199
            @Override
200
            public void actionPerformed(ActionEvent e) {
201
                doExportTable();
202
            }
203
        });
204

    
205
        cmbSource.setSelectedIndex(0);
206
        cmbSource.setEditable(false);
207

    
208
        tblBands.setRowSelectionAllowed(true);
209
        tblBands.setColumnSelectionAllowed(false);
210
        tblBands.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
211

    
212
        tblStatistics.setRowSelectionAllowed(false);
213
        tblBands.setColumnSelectionAllowed(false);
214

    
215
        chkRemoveTails.addActionListener(new ActionListener() {
216
            @Override
217
            public void actionPerformed(ActionEvent e) {
218
                doRemoveTails();
219
            }
220
        });
221

    
222
        chkShowStatistics.addActionListener(new ActionListener() {
223
            @Override
224
            public void actionPerformed(ActionEvent e) {
225
                doShowStatistics();
226
            }
227
        });
228

    
229
        pnlHistogram.setLayout(new GridBagLayout());
230
        GridBagConstraints c = new GridBagConstraints();
231
        c.weightx = 1.0;
232
        c.weighty = 1.0;
233
        c.fill = GridBagConstraints.BOTH;
234
        c.gridx = 0;
235
        c.gridy = 0;
236

    
237
        this.graphicContainer = getGraphicContainer();
238
        this.graphicContainer.addValueChangedListener(new GraphicListener() {
239

    
240
            @Override
241
            public void actionValueChanged(GraphicEvent e) {
242
                updateComponents();
243
            }
244
        });
245
        pnlHistogram.add(getGraphicContainer(), c);
246

    
247
    }
248

    
249
    /**
250
     * This method initializes jPanel
251
     *
252
     * @return javax.swing.JPanel
253
     */
254
    public GraphicContainer getGraphicContainer() {
255
        if (this.graphicContainer == null) {
256
            this.graphicContainer = new GraphicContainer(true);
257
        }
258
        return this.graphicContainer;
259
    }
260

    
261
    protected void doShowStatistics() {
262
            boolean showStatistics = chkShowStatistics.isSelected();
263
            JScrollPane scrollPane = getScrollPane(tblStatistics);
264
            if(scrollPane!=null){
265
                scrollPane.setVisible(showStatistics);
266
            }
267
            tblStatistics.setVisible(showStatistics);
268
            tblStatistics.getTableHeader().setVisible(showStatistics);
269
            btnExportTable.setVisible(showStatistics);
270
            chkRemoveTails.setVisible(showStatistics);
271
    }
272

    
273
    private JScrollPane getScrollPane(JTable table) {
274
        JScrollPane scrollPane = null;
275
        final Container parent = SwingUtilities.getUnwrappedParent(table);
276
        if (parent instanceof JViewport) {
277
            final Container container = parent.getParent();
278
            if (container instanceof JScrollPane) {
279
                scrollPane = (JScrollPane) container;
280
            }
281
        }
282
        return scrollPane;
283
    }
284

    
285
    protected void doRemoveTails() {
286
        updateComponents();
287
    }
288

    
289
    protected void doExportTable() {
290
        // TODO Auto-generated method stub
291

    
292
    }
293

    
294
    protected void doUnselectAllBands() {
295
        ((SelectableBandsTableModel)tblBands.getModel()).unselectAllBands();
296
    }
297

    
298
    protected void doSelectAllBands() {
299
        ((SelectableBandsTableModel)tblBands.getModel()).selectAllBands();
300
    }
301

    
302

    
303
    private void translate() {
304
        I18nManager i18nManager = ToolsLocator.getI18nManager();
305

    
306
        lblSource.setText(i18nManager.getTranslation(lblSource.getText()));
307
        lblSource.setToolTipText(i18nManager.getTranslation(lblSource.getToolTipText()));
308

    
309
        lblType.setText(i18nManager.getTranslation(lblType.getText()));
310
        lblType.setToolTipText(i18nManager.getTranslation(lblType.getToolTipText()));
311

    
312
        lblBands.setText(i18nManager.getTranslation(lblBands.getText()));
313
        lblBands.setToolTipText(i18nManager.getTranslation(lblBands.getToolTipText()));
314

    
315
        cmbSource.removeAllItems();
316
        for (int i = 0; i < sources.length; i++) {
317
            cmbSource.addItem(i18nManager.getTranslation(sources[i]));
318
        }
319

    
320
        cmbType.removeAllItems();
321
        for (int i = 0; i < types.length; i++) {
322
            cmbType.addItem(i18nManager.getTranslation(types[i]));
323
        }
324

    
325
        btnAllBands.setText(i18nManager.getTranslation(btnAllBands.getText()));
326
        btnAllBands.setToolTipText(i18nManager.getTranslation(btnAllBands.getToolTipText()));
327

    
328
        btnClearBands.setText(i18nManager.getTranslation(btnClearBands.getText()));
329
        btnClearBands.setToolTipText(i18nManager.getTranslation(btnClearBands.getToolTipText()));
330

    
331
        btnExportTable.setText(i18nManager.getTranslation(btnExportTable.getText()));
332
        btnExportTable.setToolTipText(i18nManager.getTranslation(btnExportTable.getToolTipText()));
333

    
334
        chkRemoveTails.setText(i18nManager.getTranslation(chkRemoveTails.getText()));
335
        chkRemoveTails.setToolTipText(i18nManager.getTranslation(chkRemoveTails.getToolTipText()));
336

    
337
        chkShowStatistics.setText(i18nManager.getTranslation(chkShowStatistics.getText()));
338
        chkShowStatistics.setToolTipText(i18nManager.getTranslation(chkShowStatistics.getToolTipText()));
339

    
340
    }
341

    
342
    /**
343
     * @param locale
344
     *
345
     */
346
    public void setLocate(Locale locale) {
347
        Locale l = super.getLocale();
348
        if (!l.equals(locale)) {
349
            translate();
350
        }
351
        super.setLocale(locale);
352
    }
353

    
354
    private void updateComponents() {
355
        if (this.fullStatistics == null || this.tableModel == null) {
356
            return;
357
        }
358

    
359
        if(this.enabledVisibleStatistics){
360
            this.cmbSource.setEnabled(true);
361
        } else {
362
            this.cmbSource.setSelectedIndex(0);
363
            this.cmbSource.setEnabled(false);
364
        }
365

    
366
        if (cmbSource.getSelectedIndex() == 0) {
367
            this.statistics = this.fullStatistics;
368
        } else {
369
            this.statistics = this.visibleStatistics;
370
        }
371

    
372
        updateStatisticsTable();
373
        updateHistogramGraphic();
374
    }
375

    
376
    /**
377
     *
378
     */
379
    private void updateStatisticsTable() {
380
        List<Integer> selectedBands = ((SelectableBandsTableModel) tblBands.getModel()).getSelectedBands();
381

    
382
        StatisticsTableModel statisticsModel = new StatisticsTableModel(this.statistics, selectedBands);
383
        tblStatistics.setModel(statisticsModel);
384

    
385
        tblStatistics.setColumnModel(new DefaultTableColumnModel());
386
        TableColumnModel statisticsColumnModel = tblStatistics.getColumnModel();
387
        statisticsColumnModel.setColumnSelectionAllowed(false);
388
        for (int i = 0; i < statisticsModel.getColumnCount(); i++) {
389
            TableColumn tableColumn = new TableColumn(i);
390
            tableColumn.setIdentifier(statisticsModel.getColumnName(i));
391
            tableColumn.setHeaderValue(statisticsModel.getColumnName(i));
392
            if (i > StatisticsTableModel.COLUMN_LENGTH) {
393
                tableColumn.setCellRenderer(new PerhapsNotValidValueCellRenderer());
394
            }
395
            statisticsColumnModel.addColumn(tableColumn);
396
        }
397
    }
398

    
399
    private void updateHistogramGraphic() {
400
        if(this.statistics==null){
401
            getGraphicContainer().getPGraphic().cleanChart();
402
            return;
403
        }
404

    
405
        List<Integer> selectedBands = ((SelectableBandsTableModel)tblBands.getModel()).getSelectedBands();
406

    
407
        Color[] bandsColor = new Color[selectedBands.size()];
408
        String[] bandNames = new String[selectedBands.size()];
409

    
410
        int index = 0;
411
        for (Integer selectedBand : selectedBands) {
412

    
413
            if( colors == null || colors.length!=tblBands.getModel().getRowCount()){
414
                bandsColor[index] = defaultColors[index % defaultColors.length];
415
            } else {
416
                bandsColor[index] = colors[selectedBand];
417
            }
418

    
419
            bandNames[index] = selectedBand.toString();
420

    
421
            getGraphicContainer().setBandColor(index, bandsColor[index]);
422
            index++;
423
        }
424

    
425
        getGraphicContainer().getPGraphic().setViewType(cmbType.getSelectedIndex());
426
        HistogramBand[] histogramBands = this.statistics.getHistogram();
427

    
428
        XYSeries[] datos = new XYSeries[selectedBands.size()];
429
        for (int iBand = 0; iBand < selectedBands.size(); iBand++) {
430
            // Definimos el principio y final de la grafica, sirve para descartar valores.
431
            int first = (int) getGraphicContainer().getX1(); //.getX1(); //(int) getHistogramPanel().getBoxValueX1();
432
            int end = (int) getGraphicContainer().getX2(); //(int) getHistogramPanel().getBoxValueX2();
433
            HistogramBand histogramBand = histogramBands[selectedBands.get(iBand)];
434
            int min = 0;
435
            int max = histogramBand.getNumClasses() - 1;
436

    
437
            first = min + ((first * (max - min))/ 100);
438
            end = min + ((end * (max - min))/ 100);
439

    
440
            // Si hay que eliminar los limites, quitamos el ultimo y primer valor de la grafica
441
            if (chkRemoveTails.isSelected()) {
442
                if ((first + 1) <= end)
443
                    first++;
444
                if ((end - 1) >= first)
445
                    end--;
446
            }
447
            datos[iBand] = new XYSeries(bandNames[iBand]);
448
            for (int i = first; i < end; i++) {
449
                datos[iBand].add(histogramBand.getIntervalMin(i), histogramBand.getValueCount(i));
450
            }
451
        }
452
        getGraphicContainer().getPGraphic().setNewChart(datos);
453
    }
454

    
455
    @Override
456
    public void setEnabledVisibleStatistics(boolean enabledVisibleStatistics) {
457
        this.enabledVisibleStatistics = enabledVisibleStatistics;
458
        if(this.initialized){
459
            updateComponents();
460
        }
461
    }
462

    
463
    @Override
464
    public boolean isEnabledVisibleStatistics() {
465
        return this.enabledVisibleStatistics;
466
    }
467

    
468

    
469
    public void addChangeItemSourceListener(ItemListener listener){
470
        if(this.initialized){
471
            cmbSource.addItemListener(listener);
472
        }
473
    }
474

    
475
    public void removeChangeItemSourceListener(ItemListener listener){
476
        if(this.initialized){
477
            cmbSource.addItemListener(listener);
478
        }
479
    }
480
}