Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.raster.legend / org.gvsig.raster.legend.swing / org.gvsig.raster.legend.swing.impl / src / main / java / org / gvsig / raster / swing / legend / impl / colortable / create / CreateColorTablePanelController.java @ 43803

History | View | Annotate | Download (8.48 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.legend.impl.colortable.create;
24

    
25
import java.awt.Color;
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.security.InvalidParameterException;
29
import java.text.NumberFormat;
30
import java.util.Locale;
31

    
32
import javax.swing.JColorChooser;
33
import javax.swing.JComponent;
34
import javax.swing.text.DefaultFormatterFactory;
35
import javax.swing.text.NumberFormatter;
36

    
37
import org.gvsig.i18n.Messages;
38
import org.gvsig.raster.lib.legend.api.colortable.MakeColorTable;
39
import org.gvsig.raster.swing.legend.colortable.create.CreateColorTablePanel;
40
import org.gvsig.tools.ToolsLocator;
41
import org.gvsig.tools.i18n.I18nManager;
42

    
43

    
44
/**
45
 * @author fdiaz
46
 *
47
 */
48
public class CreateColorTablePanelController extends CreateColorTablePanelView implements CreateColorTablePanel {
49

    
50
    /**
51
     *
52
     */
53
    private static final long serialVersionUID = 9004095796574698464L;
54
    private Color fromColor;
55
    private Color toColor;
56

    
57

    
58
    public CreateColorTablePanelController() {
59
        translate();
60
        initializeComponents();
61

    
62
    }
63

    
64
    private void initializeComponents() {
65
        btnFromColor.addActionListener(new ActionListener() {
66

    
67
            @Override
68
            public void actionPerformed(ActionEvent e) {
69
                doSelectFromColor();
70

    
71
            }
72
        });
73

    
74
        btnToColor.addActionListener(new ActionListener() {
75

    
76
            @Override
77
            public void actionPerformed(ActionEvent e) {
78
                doSelectToColor();
79

    
80
            }
81
        });
82

    
83
        rdbIntervalSize.addActionListener(new ActionListener() {
84

    
85
            @Override
86
            public void actionPerformed(ActionEvent e) {
87
                updateModeComponents();
88
            }
89
        });
90

    
91
        rdbIntervalNumber.addActionListener(new ActionListener() {
92

    
93
            @Override
94
            public void actionPerformed(ActionEvent e) {
95
                updateModeComponents();
96
            }
97
        });
98

    
99
    }
100

    
101
    protected void updateModeComponents() {
102
        txtIntervalSize.setEnabled(rdbIntervalSize.isSelected());
103
        txtIntervals.setEnabled(rdbIntervalNumber.isSelected());
104
    }
105

    
106
    protected void doSelectToColor() {
107
        Color ret =
108
            JColorChooser.showDialog(CreateColorTablePanelController.this,
109
                Messages.getText("_to_color"),
110
                toColor);
111

    
112
        if (ret != null) {
113
            toColor = ret;
114
            lblToColorPreviewer.setBackground(toColor);
115
        } else {
116
            lblToColorPreviewer.setBackground(Color.WHITE);
117
        }
118
    }
119

    
120
    protected void doSelectFromColor() {
121
        Color ret =
122
            JColorChooser.showDialog(CreateColorTablePanelController.this,
123
                Messages.getText("_from_color"),
124
                fromColor);
125

    
126
        if (ret != null) {
127
            fromColor = ret;
128
            lblFromColorPreviewer.setBackground(fromColor);
129
        } else {
130
            lblFromColorPreviewer.setBackground(Color.BLACK);
131
        }
132
    }
133

    
134
    /* (non-Javadoc)
135
     * @see org.gvsig.tools.swing.api.Component#asJComponent()
136
     */
137
    @Override
138
    public JComponent asJComponent() {
139
        return this;
140
    }
141

    
142
    /* (non-Javadoc)
143
     * @see org.gvsig.raster.tools.swing.api.colortable.CreateColorTablePanel#fetch(org.gvsig.raster.tools.lib.api.colortable.CreateColorTable)
144
     */
145
    @Override
146
    public void fetch(MakeColorTable makeColorTable) {
147
        makeColorTable.setName(txtName.getText());
148
        makeColorTable.setMinimum(((Number)txtMinimum.getValue()).doubleValue());
149
        makeColorTable.setMaximum(((Number)txtMaximum.getValue()).doubleValue());
150

    
151
        if(rdbIntervalNumber.isSelected()){
152
            makeColorTable.setMode(MakeColorTable.MODE_NUMBER_OF_INTERVALS);
153
            makeColorTable.setIntervals(((Number)txtIntervals.getValue()).intValue());
154
        } else {
155
            makeColorTable.setMode(MakeColorTable.MODE_INTERVAL_SIZE);
156
            makeColorTable.setIntervalSize(((Number)txtIntervalSize.getValue()).doubleValue());
157
        }
158
        makeColorTable.setFromColor(this.fromColor);
159
        makeColorTable.setToColor(this.toColor);
160
        makeColorTable.setInterpolated(chkInterpolated.isSelected());
161
    }
162

    
163
    /* (non-Javadoc)
164
     * @see org.gvsig.raster.tools.swing.api.colortable.CreateColorTablePanel#setMinimum(double)
165
     */
166
    @Override
167
    public void setMinimum(double minimum) {
168
        txtMinimum.setValue(minimum);
169
    }
170

    
171
    /* (non-Javadoc)
172
     * @see org.gvsig.raster.tools.swing.api.colortable.CreateColorTablePanel#setMaximum(double)
173
     */
174
    @Override
175
    public void setMaximum(double maximum) {
176
        txtMaximum.setValue(maximum);
177
    }
178

    
179
    private void translate() {
180
        I18nManager i18nManager = ToolsLocator.getI18nManager();
181

    
182
        lblName.setText(i18nManager.getTranslation(lblName.getText()));
183
        lblMinimum.setText(i18nManager.getTranslation(lblMinimum.getText()));
184
        lblMaximum.setText(i18nManager.getTranslation(lblMaximum.getText()));
185
        rdbIntervalSize.setText(i18nManager.getTranslation(rdbIntervalSize.getText()));
186
        rdbIntervalNumber.setText(i18nManager.getTranslation(rdbIntervalNumber.getText()));
187
        lblFromColor.setText(i18nManager.getTranslation(lblFromColor.getText()));
188
        lblToColor.setText(i18nManager.getTranslation(lblToColor.getText()));
189

    
190
        NumberFormat numberInstance = NumberFormat.getNumberInstance();
191
        numberInstance.setGroupingUsed(false);
192
        numberInstance.setMaximumFractionDigits(Integer.MAX_VALUE);
193
        NumberFormatter numberFormatter = new NumberFormatter(numberInstance);
194
        DefaultFormatterFactory tf = new DefaultFormatterFactory(
195
            numberFormatter,
196
            numberFormatter,
197
            numberFormatter,
198
            numberFormatter);
199
        txtMinimum.setFormatterFactory(tf);
200
        txtMaximum.setFormatterFactory(tf);
201
        txtIntervalSize.setFormatterFactory(tf);
202

    
203
        NumberFormat integerInstance = NumberFormat.getIntegerInstance();
204
        integerInstance.setGroupingUsed(false);
205
        numberFormatter = new NumberFormatter(integerInstance);
206
        numberFormatter.setMinimum(1);
207
        numberFormatter.setMaximum(Integer.MAX_VALUE);
208
        tf = new DefaultFormatterFactory(
209
            numberFormatter,
210
            numberFormatter,
211
            numberFormatter,
212
            numberFormatter);
213
        txtIntervals.setFormatterFactory(tf);
214
    }
215

    
216
    @Override
217
    public void setMode(int mode) {
218
        switch (mode) {
219
        case MakeColorTable.MODE_INTERVAL_SIZE:
220
            rdbIntervalSize.setSelected(true);
221
            rdbIntervalNumber.setSelected(false);
222
            break;
223

    
224
        case MakeColorTable.MODE_NUMBER_OF_INTERVALS:
225
            rdbIntervalSize.setSelected(false);
226
            rdbIntervalNumber.setSelected(true);
227
            break;
228
        default:
229
            throw new InvalidParameterException("Creation mode of color table must be 0 or 1.");
230
        }
231
    }
232

    
233
    @Override
234
    public void setIntervals(int intervals) {
235
        txtIntervals.setValue(intervals);
236

    
237
    }
238

    
239
    @Override
240
    public void setIntervalSize(double intervalSize) {
241
       txtIntervalSize.setValue(intervalSize);
242

    
243
    }
244

    
245
    @Override
246
    public void setFromColor(Color fromColor) {
247
        this.fromColor = fromColor;
248
        lblFromColorPreviewer.setBackground(this.fromColor);
249
    }
250

    
251
    @Override
252
    public void setToColor(Color toColor) {
253
        this.toColor = toColor;
254
        lblToColorPreviewer.setBackground(this.toColor);
255
    }
256

    
257
    /**
258
     * @param locale
259
     *
260
     */
261
    public void setLocate(Locale locale) {
262
        Locale l = super.getLocale();
263
        if (!l.equals(locale)) {
264
            translate();
265
        }
266
        super.setLocale(locale);
267
    }
268
}