Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / colortable / panels / TabTable.java @ 14266

History | View | Annotate | Download (5.71 KB)

1 12914 bsanchez
/* 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 13292 bsanchez
import java.util.ArrayList;
24 12914 bsanchez
25
import javax.swing.JPanel;
26 13292 bsanchez
import javax.swing.JTable;
27
import javax.swing.event.TableModelEvent;
28
import javax.swing.event.TableModelListener;
29 12914 bsanchez
30
import org.gvsig.gui.beans.table.TableContainer;
31
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
32 13292 bsanchez
import org.gvsig.gui.beans.table.models.TableColorModel;
33
import org.gvsig.raster.datastruct.ColorItem;
34 14140 bsanchez
import org.gvsig.rastertools.colortable.data.ColorTableData;
35 13292 bsanchez
import org.gvsig.rastertools.colortable.ui.ColorTablePanel;
36 12914 bsanchez
37
import com.iver.andami.PluginServices;
38
/**
39 13292 bsanchez
 * Pesta?a de la tabla de color
40 12914 bsanchez
 *
41
 * @version 27/06/2007
42
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
43
 */
44 13292 bsanchez
public class TabTable extends JPanel implements TableModelListener {
45 12914 bsanchez
        private static final long serialVersionUID = -6971866166164473789L;
46 14140 bsanchez
        private ColorTableData colorTableData = null;
47
48 13292 bsanchez
        //[start]Variables UI
49
        private TableContainer  tableContainer  = null;
50
        private ColorTablePanel colorTablePanel = null;
51
        //[end]
52 12914 bsanchez
53 13292 bsanchez
        /**
54
         * Construye un TabTable
55
         * @param colorTablePanel
56
         */
57 14140 bsanchez
        public TabTable(ColorTablePanel colorTablePanel, ColorTableData colorTableData) {
58 13292 bsanchez
                this.colorTablePanel = colorTablePanel;
59 14140 bsanchez
                this.colorTableData = colorTableData;
60 12914 bsanchez
                initialize();
61
        }
62 14266 bsanchez
63 13292 bsanchez
        //[start] Code UI
64 12914 bsanchez
        private void initialize() {
65
                setLayout(new BorderLayout());
66
                add(getTableContainer(), BorderLayout.CENTER);
67
        }
68
69
        public TableContainer getTableContainer() {
70
                if (tableContainer == null) {
71
                        String[] columnNames = {PluginServices.getText(this, "selec"), PluginServices.getText(this, "clase"), "RGB", PluginServices.getText(this, "valor"), PluginServices.getText(this, "hasta"), PluginServices.getText(this, "alpha")};
72 13777 bsanchez
                        int[] columnWidths = {55, 68, 110, 64, 71, 43};
73 12914 bsanchez
                        tableContainer = new TableContainer(columnNames, columnWidths);
74
                        tableContainer.setModel("TableColorModel");
75
                        tableContainer.initialize();
76
                }
77
                return tableContainer;
78
        }
79 13292 bsanchez
        //[end]
80 12914 bsanchez
81
        /**
82
         * Borra todas las filas de la tabla.
83
         */
84
        public void clearTable() {
85
                try {
86
                        getTableContainer().removeAllRows();
87
                } catch (NotInitializeException e) {
88
                        e.printStackTrace();
89
                }
90
        }
91
92 13292 bsanchez
        // Devuelve el String de un color en formato ##0, ##0, ##0
93 12914 bsanchez
        private String getColorString(Color c) {
94
                return c.getRed() + ", " + c.getGreen() + ", " + c.getBlue();
95
        }
96
97
        /**
98
         * A?ade una fila a la tabla asignando el color por par?metro. Este
99
         * color asignado ser? el que aparezca en el bot?n y en el texto RGB
100
         * @param color
101
         */
102
        public void addRowToTable(Color color, String name, Double fromRange, Double toRange, String alpha){
103
                try {
104
                        getTableContainer().addRow(new Object[] { color, name, getColorString(color), fromRange, toRange, alpha });
105
                } catch (NotInitializeException e1) {
106
                }
107
        }
108 13292 bsanchez
109
        /**
110
         * Convierte la tabla en un array de objetos para poder crear con el el objeto Palette
111
         * @return
112
         * @throws NotInitializeException
113
         */
114
        public ArrayList getPalette() {
115
                ArrayList arrayList = new ArrayList();
116
                JTable jTable = getTableContainer().getTable().getJTable();
117
                TableColorModel model = (TableColorModel) jTable.getModel();
118
                for (int iRow = 0; iRow < jTable.getRowCount(); iRow++) {
119
                        Color rgb = (Color) model.getValueAt(iRow, 0);
120
                        ColorItem colorItem = new ColorItem();
121
                        colorItem.setColor(new Color(
122
                                        rgb.getRed(),
123
                                        rgb.getGreen(),
124
                                        rgb.getBlue(),
125
                                        Integer.valueOf((String) model.getValueAt(iRow, 5)).intValue()));
126
127
                        if (model.getValueAt(iRow, 3) != null)
128
                                colorItem.setValue(((Double) model.getValueAt(iRow, 3)).doubleValue());
129
                        // TODO: Hacer algo para conservar el valor de interpolacion
130
                        colorItem.setNameClass((String) model.getValueAt(iRow, 1));
131
                        arrayList.add(colorItem);
132
                }
133
134
                return arrayList;
135
        }
136
137
        /**
138
         * Carga inicial de los colores del panel
139
         * @param colorItems
140
         */
141
        public void reloadItems(ArrayList colorItems) {
142
                clearTable();
143
                for (int i = 0; i < colorItems.size(); i++) {
144
                        ColorItem c1 = (ColorItem) colorItems.get(i);
145
                        Double toRange = null;
146 14266 bsanchez
                        if ((i + 1) < colorItems.size())
147 13292 bsanchez
                                toRange = new Double(((ColorItem) colorItems.get(i + 1)).getValue());
148
                        addRowToTable(c1.getColor(), c1.getNameClass(), new Double(c1.getValue()), toRange, c1.getColor().getAlpha() + "");
149
                }
150
        }
151
152 14266 bsanchez
        /**
153
   * Devuelve el componente que trata los datos
154
   * @return
155
   */
156
  private ColorTableData getColorTableData() {
157
          return colorTableData;
158
  }
159
160 13292 bsanchez
        /*
161
         * (non-Javadoc)
162
         * @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent)
163
         */
164
        public void tableChanged(TableModelEvent e) {
165
                if (!colorTablePanel.isActive())
166
                        return;
167
168 14140 bsanchez
                colorTableData.getColorTable().createPaletteFromColorItems(getPalette(), false);
169
                colorTableData.getColorTable().setInterpolated(colorTablePanel.getCheckBoxInterpolated().isSelected());
170 13292 bsanchez
171
                colorTablePanel.refreshItems(false);
172 14266 bsanchez
                getColorTableData().setHasChanged(true);
173
                getColorTableData().refreshPreview();
174
  }
175 12914 bsanchez
}