Statistics
| Revision:

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

History | View | Annotate | Download (8.69 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.gui.preferences.panels;
23

    
24
import java.awt.Component;
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.Insets;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.util.ArrayList;
31

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

    
40
import org.gvsig.raster.Configuration;
41
import org.gvsig.raster.datastruct.ColorTable;
42
import org.gvsig.raster.datastruct.persistence.ColorTableLibraryPersistence;
43
import org.gvsig.raster.gui.preferences.combocolortable.PaintItem;
44
import org.gvsig.raster.gui.preferences.combocolortable.PreferenceColorTableIconPainter;
45
import org.gvsig.raster.util.BasePanel;
46

    
47
import com.iver.utiles.FileUtils;
48
/**
49
 * PreferenceLoadLayer es una clase para la configuracion de las preferencias
50
 * de una capa raster.
51
 * 
52
 * @version 14/12/2007
53
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
54
 */
55
public class PreferenceLoadLayer extends BasePanel implements ActionListener {
56
        private static final long    serialVersionUID      = 1L;
57
        private JComboBox            comboBoxColorTable    = null;
58
        private JRadioButton         radioButtonRealce     = null;
59
        private JRadioButton         radioButtonColorTable = null;
60
        private JLabel               labelLoadLayer        = null;
61
        private ButtonGroup          buttonGroup           = null;
62

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

    
81
        protected void init() {
82
                GridBagConstraints gridBagConstraints;
83

    
84
                setLayout(new GridBagLayout());
85

    
86
                gridBagConstraints = new GridBagConstraints();
87
                gridBagConstraints.gridx = 0;
88
                gridBagConstraints.gridy = 0;
89
                gridBagConstraints.gridwidth = 2;
90
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
91
                gridBagConstraints.anchor = GridBagConstraints.WEST;
92
                gridBagConstraints.insets = new Insets(5, 5, 2, 5);
93
                add(getLabelLoadLayer(), gridBagConstraints);
94

    
95
                gridBagConstraints = new GridBagConstraints();
96
                gridBagConstraints.gridx = 0;
97
                gridBagConstraints.gridy = 1;
98
                gridBagConstraints.gridwidth = 2;
99
                gridBagConstraints.anchor = GridBagConstraints.WEST;
100
                gridBagConstraints.insets = new Insets(2, 5, 2, 5);
101
                add(getRadioButtonRealce(), gridBagConstraints);
102

    
103
                gridBagConstraints = new GridBagConstraints();
104
                gridBagConstraints.gridx = 0;
105
                gridBagConstraints.gridy = 2;
106
                gridBagConstraints.anchor = GridBagConstraints.WEST;
107
                gridBagConstraints.insets = new Insets(2, 5, 5, 2);
108
                add(getRadioButtonColorTable(), gridBagConstraints);
109

    
110
                gridBagConstraints = new GridBagConstraints();
111
                gridBagConstraints.gridx = 1;
112
                gridBagConstraints.gridy = 2;
113
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
114
                gridBagConstraints.weightx = 1.0;
115
                gridBagConstraints.insets = new Insets(2, 2, 5, 5);
116
                add(getComboBoxColorTable(), gridBagConstraints);
117
        }
118
        
119
        /**
120
         * Obtiene el path del fichero de paletas
121
         * @return
122
         */
123
        public String getPalettesPath() {
124
                return ( FileUtils.getAppHomeDir() + "colortable" );
125
        }
126
        
127
        /**
128
         * Obtiene el grupo de botones 
129
         * @return ButtonGroup
130
         */
131
        public ButtonGroup getButtonGroup() {
132
                if(buttonGroup == null)
133
                        buttonGroup = new ButtonGroup();
134
                return buttonGroup;
135
        }
136

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

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

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

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

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

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

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

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

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

    
189
                                comboBoxColorTable.addItem(array);
190
                        }
191

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

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

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

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

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

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

    
235
                boolean finded = selectComboName(colorTable);
236

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

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

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