Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / gui / preferences / panels / PreferenceLoadLayer.java @ 22364

History | View | Annotate | Download (8.71 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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.raster.gui.preferences.panels;
20

    
21
import java.awt.Component;
22
import java.awt.GridBagConstraints;
23
import java.awt.GridBagLayout;
24
import java.awt.Insets;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.io.File;
28
import java.util.ArrayList;
29

    
30
import javax.swing.BorderFactory;
31
import javax.swing.ButtonGroup;
32
import javax.swing.JComboBox;
33
import javax.swing.JLabel;
34
import javax.swing.JList;
35
import javax.swing.JRadioButton;
36
import javax.swing.ListCellRenderer;
37

    
38
import org.gvsig.raster.Configuration;
39
import org.gvsig.raster.datastruct.ColorTable;
40
import org.gvsig.raster.datastruct.persistence.ColorTableLibraryPersistence;
41
import org.gvsig.raster.gui.preferences.combocolortable.PaintItem;
42
import org.gvsig.raster.gui.preferences.combocolortable.PreferenceColorTableIconPainter;
43
import org.gvsig.raster.util.BasePanel;
44
/**
45
 * PreferenceLoadLayer es una clase para la configuracion de las preferencias
46
 * de una capa raster.
47
 * 
48
 * @version 14/12/2007
49
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
50
 */
51
public class PreferenceLoadLayer extends BasePanel implements ActionListener {
52
        private static final long    serialVersionUID      = 1L;
53
        private JComboBox            comboBoxColorTable    = null;
54
        private JRadioButton         radioButtonRealce     = null;
55
        private JRadioButton         radioButtonColorTable = null;
56
        private JLabel               labelLoadLayer        = null;
57
        private ButtonGroup          buttonGroup           = null;
58

    
59
        /**
60
         *Inicializa componentes gr?ficos y traduce
61
         */
62
        public PreferenceLoadLayer() {
63
                init();
64
                translate();
65
        }
66
        
67
        /**
68
         * Traduce los textos de esta clase
69
         */
70
        protected void translate() {
71
                setBorder(BorderFactory.createTitledBorder(getText(this, "carga_capas")));
72
                getLabelLoadLayer().setText(getText(this, "loadlayer_aplicar") + ":");
73
                getRadioButtonRealce().setText(getText(this, "realce"));
74
                getRadioButtonColorTable().setText(getText(this, "tabla_color"));
75
        }
76

    
77
        protected void init() {
78
                GridBagConstraints gridBagConstraints;
79

    
80
                setLayout(new GridBagLayout());
81

    
82
                gridBagConstraints = new GridBagConstraints();
83
                gridBagConstraints.gridx = 0;
84
                gridBagConstraints.gridy = 0;
85
                gridBagConstraints.gridwidth = 2;
86
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
87
                gridBagConstraints.anchor = GridBagConstraints.WEST;
88
                gridBagConstraints.insets = new Insets(5, 5, 2, 5);
89
                add(getLabelLoadLayer(), gridBagConstraints);
90

    
91
                gridBagConstraints = new GridBagConstraints();
92
                gridBagConstraints.gridx = 0;
93
                gridBagConstraints.gridy = 1;
94
                gridBagConstraints.gridwidth = 2;
95
                gridBagConstraints.anchor = GridBagConstraints.WEST;
96
                gridBagConstraints.insets = new Insets(2, 5, 2, 5);
97
                add(getRadioButtonRealce(), gridBagConstraints);
98

    
99
                gridBagConstraints = new GridBagConstraints();
100
                gridBagConstraints.gridx = 0;
101
                gridBagConstraints.gridy = 2;
102
                gridBagConstraints.anchor = GridBagConstraints.WEST;
103
                gridBagConstraints.insets = new Insets(2, 5, 5, 2);
104
                add(getRadioButtonColorTable(), gridBagConstraints);
105

    
106
                gridBagConstraints = new GridBagConstraints();
107
                gridBagConstraints.gridx = 1;
108
                gridBagConstraints.gridy = 2;
109
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
110
                gridBagConstraints.weightx = 1.0;
111
                gridBagConstraints.insets = new Insets(2, 2, 5, 5);
112
                add(getComboBoxColorTable(), gridBagConstraints);
113
        }
114
        
115
        /**
116
         * Obtiene el path del fichero de paletas
117
         * @return
118
         */
119
        public String getPalettesPath() {
120
                return System.getProperty("user.home") +
121
                                File.separator +
122
                                "gvSIG" + // PluginServices.getArguments()[0] +
123
                                File.separator + "colortable";
124
        }
125
        
126
        /**
127
         * Obtiene el grupo de botones 
128
         * @return ButtonGroup
129
         */
130
        public ButtonGroup getButtonGroup() {
131
                if(buttonGroup == null)
132
                        buttonGroup = new ButtonGroup();
133
                return buttonGroup;
134
        }
135

    
136
        public JRadioButton getRadioButtonRealce() {
137
                if (radioButtonRealce == null) {
138
                        radioButtonRealce = new JRadioButton();
139
                        getButtonGroup().add(radioButtonRealce);
140
                        radioButtonRealce.addActionListener(this);
141
                        radioButtonRealce.setSelected(true);
142
                        radioButtonRealce.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
143
                        radioButtonRealce.setMargin(new Insets(0, 0, 0, 0));
144
                }
145
                return radioButtonRealce;
146
        }
147

    
148
        public JRadioButton getRadioButtonColorTable() {
149
                if (radioButtonColorTable == null) {
150
                        radioButtonColorTable = new JRadioButton();
151
                        getButtonGroup().add(radioButtonColorTable);
152
                        radioButtonColorTable.addActionListener(this);
153
                        radioButtonColorTable.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
154
                        radioButtonColorTable.setMargin(new Insets(0, 0, 0, 0));
155
                }
156
                return radioButtonColorTable;
157
        }
158

    
159
        public JLabel getLabelLoadLayer() {
160
                if (labelLoadLayer == null) {
161
                        labelLoadLayer = new JLabel();
162
                }
163
                return labelLoadLayer;
164
        }
165

    
166
        public JComboBox getComboBoxColorTable() {
167
                if (comboBoxColorTable == null) {
168
                        comboBoxColorTable = new JComboBox();
169

    
170
                        ArrayList fileList = ColorTableLibraryPersistence.getPaletteFileList(getPalettesPath());
171

    
172
                        for (int i = 0; i < fileList.size(); i++) {
173
                                ArrayList paletteItems = new ArrayList();
174
                                String paletteName = ColorTableLibraryPersistence.loadPalette(getPalettesPath(), (String) fileList.get(i), paletteItems);
175

    
176
                                if (paletteItems.size() <= 0)
177
                                        continue;
178

    
179
                                ColorTable colorTable = new ColorTable();
180
                                colorTable.setName(paletteName);
181
                                colorTable.createPaletteFromColorItems(paletteItems, true);
182
                                colorTable.setInterpolated(true);
183

    
184
                                ArrayList array = new ArrayList();
185
                                array.add(paletteName);
186
                                array.add(new PreferenceColorTableIconPainter(colorTable));
187

    
188
                                comboBoxColorTable.addItem(array);
189
                        }
190

    
191
                        comboBoxColorTable.setRenderer(new ListCellRenderer() {
192
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
193
                                        ArrayList array = (ArrayList) value;
194

    
195
                                        PaintItem paintItem = new PaintItem((String) array.get(0), (PreferenceColorTableIconPainter) array.get(1), isSelected);
196
                                        return paintItem;
197
                                }
198
                        });
199
                }
200
                return comboBoxColorTable;
201
        }
202

    
203
        private void setColorTableEnabled(boolean enabled) {
204
                getComboBoxColorTable().setEnabled(enabled);
205
        }
206

    
207
        /**
208
         * Establece los valores por defecto de la clase
209
         */
210
        public void initializeDefaults() {
211
                getRadioButtonRealce().setSelected(true);
212
                setColorTableEnabled(false);
213
                selectComboName("Default");
214
        }
215

    
216
        private boolean selectComboName(String name) {
217
                if (name != null) {
218
                        for (int i=0; i<getComboBoxColorTable().getItemCount(); i++) {
219
                                if (((String) ((ArrayList) getComboBoxColorTable().getItemAt(i)).get(0)).equals(name)) {
220
                                        getComboBoxColorTable().setSelectedIndex(i);
221
                                        return true;
222
                                }
223
                        }
224
                }
225
                return false;
226
        }
227

    
228
        /**
229
         * Establece los valores que ha definido el usuario en los componentes
230
         */
231
        public void initializeValues() {
232
                String colorTable = Configuration.getValue("loadlayer_usecolortable", (String) null);
233

    
234
                boolean finded = selectComboName(colorTable);
235

    
236
                if (finded == false) {
237
                        getRadioButtonRealce().setSelected(true);
238
                        setColorTableEnabled(false);
239
                        Configuration.setValue("loadlayer_usecolortable", (String) null);
240
                        selectComboName("Default");
241
                } else {
242
                        getRadioButtonColorTable().setSelected(true);
243
                        setColorTableEnabled(true);
244
                }
245
        }
246

    
247
        /**
248
         * Guarda los valores que hay en los componentes como valores de configuracion
249
         */
250
        public void storeValues() {
251
                if (getRadioButtonColorTable().isSelected())
252
                        Configuration.setValue("loadlayer_usecolortable", (String) ((ArrayList) getComboBoxColorTable().getSelectedItem()).get(0));
253
                else
254
                        Configuration.setValue("loadlayer_usecolortable", null);
255
        }
256

    
257
        public void actionPerformed(ActionEvent e) {
258
                if (getRadioButtonColorTable().isSelected()) {
259
                        setColorTableEnabled(true);
260
                } else {
261
                        setColorTableEnabled(false);
262
                }
263
        }
264
}