Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / colortable / panels / TabTable.java @ 13777

History | View | Annotate | Download (5.91 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. and 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.rastertools.colortable.panels;
20

    
21
import java.awt.BorderLayout;
22
import java.awt.Color;
23
import java.util.ArrayList;
24

    
25
import javax.swing.JPanel;
26
import javax.swing.JTable;
27
import javax.swing.event.TableModelEvent;
28
import javax.swing.event.TableModelListener;
29

    
30
import org.gvsig.gui.beans.table.TableContainer;
31
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
32
import org.gvsig.gui.beans.table.models.TableColorModel;
33
import org.gvsig.raster.datastruct.ColorItem;
34
import org.gvsig.rastertools.colortable.ui.ColorTableIconPainter;
35
import org.gvsig.rastertools.colortable.ui.ColorTablePanel;
36

    
37
import com.iver.andami.PluginServices;
38
/**
39
 * Pesta?a de la tabla de color
40
 *
41
 * @version 27/06/2007
42
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
43
 */
44
public class TabTable extends JPanel implements TableModelListener {
45
        private static final long serialVersionUID = -6971866166164473789L;
46
        //[start]Variables UI        
47
        private TableContainer  tableContainer  = null;
48
        private ColorTablePanel colorTablePanel = null;
49
        //[end]
50

    
51
        /**
52
         * Construye un TabTable
53
         * @param colorTablePanel
54
         */
55
        public TabTable(ColorTablePanel colorTablePanel) {
56
                this.colorTablePanel = colorTablePanel;
57
                initialize();
58
        }
59
        //[start] Code UI
60
        private void initialize() {
61
                setLayout(new BorderLayout());
62
                add(getTableContainer(), BorderLayout.CENTER);
63
        }
64

    
65
        public TableContainer getTableContainer() {
66
                if (tableContainer == null) {
67
                        String[] columnNames = {PluginServices.getText(this, "selec"), PluginServices.getText(this, "clase"), "RGB", PluginServices.getText(this, "valor"), PluginServices.getText(this, "hasta"), PluginServices.getText(this, "alpha")};
68
                        int[] columnWidths = {55, 68, 110, 64, 71, 43};
69
                        tableContainer = new TableContainer(columnNames, columnWidths);
70
                        tableContainer.setModel("TableColorModel");
71
                        tableContainer.initialize();
72
                }
73
                return tableContainer;
74
        }
75
        //[end]
76

    
77
        /**
78
         * Borra todas las filas de la tabla.
79
         */
80
        public void clearTable() {
81
                try {
82
                        getTableContainer().removeAllRows();
83
                } catch (NotInitializeException e) {
84
                        e.printStackTrace();
85
                }
86
        }
87

    
88
        // Devuelve el String de un color en formato ##0, ##0, ##0
89
        private String getColorString(Color c) {
90
                return c.getRed() + ", " + c.getGreen() + ", " + c.getBlue();
91
        }
92

    
93
        /**
94
         * A?ade una fila a la tabla asignando el color por par?metro. Este
95
         * color asignado ser? el que aparezca en el bot?n y en el texto RGB
96
         * @param color
97
         */
98
        public void addRowToTable(Color color, String name, Double fromRange, Double toRange, String alpha){
99
                try {
100
                        getTableContainer().addRow(new Object[] { color, name, getColorString(color), fromRange, toRange, alpha });
101
                } catch (NotInitializeException e1) {
102
                        return;
103
                }
104
        }
105
        
106
        /**
107
         * Convierte la tabla en un array de objetos para poder crear con el el objeto Palette
108
         * @return
109
         * @throws NotInitializeException
110
         */
111
        public ArrayList getPalette() {
112
                ArrayList arrayList = new ArrayList();
113
                JTable jTable = getTableContainer().getTable().getJTable();
114
                TableColorModel model = (TableColorModel) jTable.getModel();
115
                for (int iRow = 0; iRow < jTable.getRowCount(); iRow++) {
116
                        Color rgb = (Color) model.getValueAt(iRow, 0);
117
                        ColorItem colorItem = new ColorItem();
118
                        colorItem.setColor(new Color(
119
                                        rgb.getRed(),
120
                                        rgb.getGreen(),
121
                                        rgb.getBlue(),
122
                                        Integer.valueOf((String) model.getValueAt(iRow, 5)).intValue()));
123

    
124
                        if (model.getValueAt(iRow, 3) != null)
125
                                colorItem.setValue(((Double) model.getValueAt(iRow, 3)).doubleValue());
126
                        // TODO: Hacer algo para conservar el valor de interpolacion
127
                        colorItem.setNameClass((String) model.getValueAt(iRow, 1));
128
                        arrayList.add(colorItem);
129
                }
130

    
131
                return arrayList;
132
        }
133

    
134
        /**
135
         * Refresco de los items del panel de color
136
         * @param colorItems
137
         */
138
        public void refreshItems() {
139
                ((ColorTableIconPainter) colorTablePanel.getListViewComponent().getSelectedValue().getIcon()).setColorItems(getPalette(), colorTablePanel.getCheckBoxInterpolated().isSelected(), false);
140
                colorTablePanel.tabChanged();
141
        }
142
        
143
        /**
144
         * Carga inicial de los colores del panel
145
         * @param colorItems
146
         */
147
        public void reloadItems(ArrayList colorItems) {
148
                clearTable();
149
                for (int i = 0; i < colorItems.size(); i++) {
150
                        ColorItem c1 = (ColorItem) colorItems.get(i);
151
                        Double toRange = null;
152
                        if (i + 1 < colorItems.size())
153
                                toRange = new Double(((ColorItem) colorItems.get(i + 1)).getValue());
154
                        addRowToTable(c1.getColor(), c1.getNameClass(), new Double(c1.getValue()), toRange, c1.getColor().getAlpha() + "");
155
                }
156
                colorTablePanel.tabChanged();
157
        }
158

    
159
        /*
160
         * (non-Javadoc)
161
         * @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent)
162
         */
163
        public void tableChanged(TableModelEvent e) {
164
                if (!colorTablePanel.isActive())
165
                        return;
166

    
167
                ((ColorTableIconPainter) colorTablePanel.getListViewComponent().getSelectedValue().getIcon()).setColorItems(getPalette(), colorTablePanel.getCheckBoxInterpolated().isSelected(), false);
168
                //((ColorTableIconPainter) colorTablePanel.getListViewComponent().getSelectedValue().getIcon()).getColorTable().compressPalette();
169
                colorTablePanel.getListViewComponent().repaint();
170

    
171
                colorTablePanel.refreshItems(false);
172
  }        
173
}