Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / gui / preferencespage / ViewPage.java @ 47790

History | View | Annotate | Download (13.4 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.app.gui.preferencespage;
24

    
25
import java.awt.Color;
26
import java.awt.event.ItemEvent;
27
import java.awt.event.ItemListener;
28
import javax.swing.DefaultComboBoxModel;
29
import javax.swing.ImageIcon;
30
import javax.swing.JPanel;
31
import javax.swing.SpinnerNumberModel;
32
import javax.swing.UIManager;
33
import javax.swing.event.ChangeEvent;
34
import javax.swing.event.ChangeListener;
35
import org.cresques.cts.IProjection;
36
import org.gvsig.andami.IconThemeHelper;
37
import org.gvsig.andami.PluginServices;
38
import org.gvsig.andami.preferences.IPreference;
39
import org.gvsig.andami.preferences.StoreException;
40
import org.gvsig.app.ApplicationLocator;
41
import org.gvsig.app.project.ProjectPreferences;
42
import org.gvsig.fmap.dal.swing.DALSwingLocator;
43
import org.gvsig.fmap.dal.swing.ProjectionPickerController;
44
import org.gvsig.fmap.mapcontext.MapContext;
45
import org.gvsig.fmap.mapcontext.MapContextLocator;
46
import org.gvsig.tools.ToolsLocator;
47
import org.gvsig.tools.i18n.I18nManager;
48
import org.gvsig.tools.swing.api.ToolsSwingLocator;
49
import org.gvsig.tools.swing.api.ToolsSwingManager;
50
import org.gvsig.tools.swing.api.pickercontroller.ColorPickerController;
51

    
52
/**
53
 * View document configuration page.
54
 *
55
 * Here the user can establish what settings wants to use by default regarding
56
 * to the document View.
57
 *
58
 */
59
public class ViewPage extends ViewPageView implements ChangeListener, ItemListener, IPreference {
60
    //
61
    // The class ViewPageView can extend from AbstractPreferencePage, not JPanel,
62
    // if rereate it from the GUI builder change te class declaration to:
63
    //    public abstract class ViewPageView extends AbstractPreferencePage
64

    
65
    private static final long serialVersionUID = 1204688910771374514L;
66

    
67
    public static String id = ViewPage.class.getName();
68

    
69
    private static final IProjection FACTORY_DEFAULT_PROJECTION
70
            = MapContextLocator.getMapContextManager().getDefaultCRS();
71

    
72
    private static String[] unitsNames = null;
73
    private static String[] areaUnitsNames = null;
74

    
75
    private static final double DEFAULT_ZOOM_IN_FACTOR = 2.0;
76
    private static final double DEFAULT_ZOOM_OUT_FACTOR = 1.5;
77
    private static final Color FACTORY_DEFAULT_SELECTION_COLOR = Color.YELLOW;
78
    private static final Color FACTORY_DEFAULT_VIEW_BACK_COLOR = Color.WHITE;
79
    private static int FACTORY_DEFAULT_MAP_UNITS;
80
    private static int FACTORY_DEFAULT_DISTANCE_UNITS;
81
    private static int FACTORY_DEFAULT_DISTANCE_AREA;
82

    
83
    private ImageIcon icon;
84

    
85
    private ProjectionPickerController defaultProjection;
86

    
87
    private ColorPickerController defaultViewBackColor = null;
88
    private ColorPickerController defaultSelectionColor = null;
89

    
90
    private boolean hasChanges = false;
91

    
92
    /**
93
     * Creates a new panel containing View preferences settings.
94
     *
95
     */
96
    public ViewPage() {
97
        super();
98

    
99
        initUnitsNames();
100

    
101
        
102

    
103

    
104
        this.initComponents();
105
        this.translateAll();
106
        this.initializeValues();
107
    }
108
    
109
    private void initComponents(){
110
        this.chkKeepScaleOnResizing.setEnabled(false);
111
        
112
        this.defaultProjection = DALSwingLocator.getDataSwingManager().createProjectionPickerController(
113
                this.txtDefaultProjection,
114
                this.btnChangeProjection
115
        );
116
        this.defaultProjection.addChangeListener(this);
117

    
118
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
119
        this.defaultSelectionColor = toolsSwingManager.createColorPickerController(
120
                this.txtDefaultSelectionColor,
121
                this.btnDefaultSelectionColor,
122
                this.sldDefaultSelectionColor
123
        );
124
        this.defaultSelectionColor.addChangeListener(this);
125

    
126
        this.defaultViewBackColor = toolsSwingManager.createColorPickerController(
127
                this.txtDefaultViewBackColor,
128
                this.btnDefaultViewBackColor
129
        );
130

    
131
        this.defaultViewBackColor.addChangeListener(this);
132

    
133
        this.chkInvisibleNewLayers.addChangeListener(this);
134
        this.chkKeepScaleOnResizing.addChangeListener(this);
135

    
136
        this.jCmbDistanceArea.addItemListener(this);
137
        this.jCmbDistanceUnits.addItemListener(this);
138
        this.jCmbMapUnits.addItemListener(this);
139

    
140
    }
141

    
142
    private void translateAll() {
143
        I18nManager i18n = ToolsLocator.getI18nManager();
144

    
145
        this.lblDefaultProjection.setText(i18n.getTranslation("default_projection"));
146
        this.chkInvisibleNewLayers.setText(i18n.getTranslation("options.view.invisible_new_layers"));
147
        this.chkKeepScaleOnResizing.setText(i18n.getTranslation("options.view.keep_scale_on_resizing"));
148
        this.lblZoomInFactor.setText(i18n.getTranslation("zoom_in_factor"));
149
        this.lblZoomOutFactor.setText(i18n.getTranslation("zoom_out_factor"));
150
        this.lblDefaultViewBackColor.setText(i18n.getTranslation("options.view.default_view_back_color"));
151
        this.lblDefaultSelectionColor.setText(i18n.getTranslation("options.view.default_selection_color"));
152
        this.lblMapUnits.setText(i18n.getTranslation("map_units"));
153
        this.lblDistanceUnits.setText(i18n.getTranslation("distance_units"));
154
        this.lblDistanceArea.setText(i18n.getTranslation("distance_area"));
155
        this.chkHideLegendOfNonVisibleLayers.setText(i18n.getTranslation("_Hide_legend_of_non_visible_layers"));
156
        this.chkOpenWithANewMaximizedView.setText(i18n.getTranslation("_Open_with_a_new_maximized_view"));
157
        this.btnChangeProjection.setText(i18n.getTranslation("_Change"));
158
        this.lblAlpha.setText(i18n.getTranslation("Alpha"));
159
    }
160

    
161
    @Override
162
    public boolean isResizeable() {
163
        return true;
164
    }
165

    
166
    @Override
167
    public void initializeValues() {
168
        ProjectPreferences projectPreferences = ApplicationLocator.getProjectManager().getProjectPreferences();
169

    
170
        this.defaultProjection.set(projectPreferences.getDefaultProjection());
171

    
172
        this.chkInvisibleNewLayers.setSelected(projectPreferences.getAddNewLayersInInvisibleMode());
173
        this.chkKeepScaleOnResizing.setSelected(projectPreferences.getKeepScaleOnResizing());
174
        this.chkOpenWithANewMaximizedView.setSelected(projectPreferences.getOpenWithANewMaximizedView());
175
        this.chkHideLegendOfNonVisibleLayers.setSelected(projectPreferences.getHideLegendInToCOfNonVisibleLayers());
176
        this.txtZoomInFactor.setModel(
177
                new SpinnerNumberModel(
178
                        projectPreferences.getZoomInFactor(), //initial value
179
                        0.01, //min
180
                        1000, //max
181
                        0.25 //step
182
                )
183
        );
184
        this.txtZoomOutFactor.setModel(
185
                new SpinnerNumberModel(
186
                        projectPreferences.getZoomOutFactor(), //initial value
187
                        0.01, //min
188
                        1000, //max
189
                        0.25 //step
190
                )
191
        );
192
        MapContext.ZOOMINFACTOR = projectPreferences.getZoomInFactor();
193
        MapContext.ZOOMOUTFACTOR = projectPreferences.getZoomOutFactor();
194

    
195
        this.defaultViewBackColor.set(projectPreferences.getDefaultViewBackColor());
196
        this.defaultSelectionColor.set(projectPreferences.getDefaultSelectionColor());
197

    
198
        this.jCmbMapUnits.setModel(new DefaultComboBoxModel(unitsNames));
199
        this.jCmbMapUnits.setSelectedIndex(projectPreferences.getDefaultMapUnits());
200

    
201
        this.jCmbDistanceUnits.setModel(new DefaultComboBoxModel(unitsNames));
202
        this.jCmbDistanceUnits.setSelectedIndex(projectPreferences.getDefaultDistanceUnits());
203

    
204
        this.jCmbDistanceArea.setModel(new DefaultComboBoxModel(areaUnitsNames));
205
        this.jCmbDistanceArea.setSelectedIndex(projectPreferences.getDefaultDistanceArea());
206
    }
207

    
208
    @Override
209
    public void stateChanged(ChangeEvent e) {
210
        this.hasChanges = true;
211
    }
212

    
213
    @Override
214
    public void itemStateChanged(ItemEvent e) {
215
        this.hasChanges = true;
216
    }
217

    
218
    @Override
219
    public String getID() {
220
        return id;
221
    }
222

    
223
    @Override
224
    public String getTitle() {
225
        I18nManager i18nManager = ToolsLocator.getI18nManager();
226
        return i18nManager.getTranslation("Vista");
227
    }
228

    
229
    @Override
230
    public JPanel getPanel() {
231
        return this;
232
    }
233

    
234
    @Override
235
    public void storeValues() throws StoreException {
236

    
237
        ProjectPreferences projectPreferences = ApplicationLocator.getProjectManager().getProjectPreferences();
238

    
239
        try {
240
            double zif = (double) txtZoomInFactor.getValue();
241
            double zof = (double) txtZoomOutFactor.getValue();
242
            if (zif == 0 || zof == 0) {
243
                throw new NumberFormatException();
244
            }
245

    
246
            projectPreferences.setZoomInFactor(zif);
247
            projectPreferences.setZoomOutFactor(zof);
248
            MapContext.ZOOMINFACTOR = zif;
249
            MapContext.ZOOMOUTFACTOR = zof;
250

    
251
            projectPreferences.setDefaultProjection(this.defaultProjection.get());
252
            projectPreferences.setDefaultSelectionColor(defaultSelectionColor.get());
253
            projectPreferences.setDefaultViewBackColor(defaultViewBackColor.get());
254
            projectPreferences.setDefaultOverviewBackColor(defaultViewBackColor.get());
255

    
256
            projectPreferences.setDefaultMapUnits(jCmbMapUnits.getSelectedIndex());
257
            projectPreferences.setDefaultDistanceUnits(jCmbDistanceUnits.getSelectedIndex());
258
            projectPreferences.setDefaultDistanceArea(jCmbDistanceArea.getSelectedIndex());
259

    
260
            projectPreferences.setAddNewLayersInInvisibleMode(chkInvisibleNewLayers.isSelected());
261
            projectPreferences.setKeepScaleOnResizing(chkKeepScaleOnResizing.isSelected());
262
            projectPreferences.setOpenWithANewMaximizedView(chkOpenWithANewMaximizedView.isSelected());
263
            projectPreferences.setHideLegendInToCOfNonVisibleLayers(this.chkHideLegendOfNonVisibleLayers.isSelected());
264
            
265
        } catch (Exception e) {
266
            throw new StoreException(PluginServices.getText(this, "factor_zoom_incorrecto"));
267
        }
268
    }
269

    
270
    @Override
271
    public void initializeDefaults() {
272
        defaultProjection.set(FACTORY_DEFAULT_PROJECTION);
273
        txtZoomInFactor.setModel(
274
                new SpinnerNumberModel(
275
                        DEFAULT_ZOOM_IN_FACTOR, //initial value
276
                        1, //min
277
                        1000, //max
278
                        0.25 //step
279
                )
280
        );
281
        txtZoomOutFactor.setModel(
282
                new SpinnerNumberModel(
283
                        DEFAULT_ZOOM_OUT_FACTOR, //initial value
284
                        1, //min
285
                        1000, //max
286
                        0.25 //step
287
                )
288
        );        
289
        this.chkInvisibleNewLayers.setSelected(false);
290
        this.chkKeepScaleOnResizing.setSelected(false);
291
        this.chkHideLegendOfNonVisibleLayers.setSelected(true);
292
        try {
293
            this.defaultViewBackColor.set(
294
                (Color) UIManager.getLookAndFeel().getDefaults().get("TextField.background")
295
            );
296
        } catch(Exception es ) {
297
            this.defaultViewBackColor.set(FACTORY_DEFAULT_VIEW_BACK_COLOR);
298
        }
299
        this.defaultSelectionColor.set(FACTORY_DEFAULT_SELECTION_COLOR);
300
        this.jCmbMapUnits.setSelectedIndex(FACTORY_DEFAULT_MAP_UNITS);
301
        this.jCmbDistanceUnits.setSelectedIndex(FACTORY_DEFAULT_DISTANCE_UNITS);
302
        this.jCmbDistanceArea.setSelectedIndex(FACTORY_DEFAULT_DISTANCE_AREA);
303

    
304
    }
305

    
306
    @Override
307
    public ImageIcon getIcon() {
308
        if(icon == null){
309
            icon = IconThemeHelper.getImageIcon("document-view-icon");
310
        }
311
        return icon;
312
    }
313

    
314
    @Override
315
    public boolean isValueChanged() {
316
        return this.hasChanges;
317
    }
318

    
319
    @Override
320
    public void setChangesApplied() {
321
        this.hasChanges = false;
322
    }
323

    
324
    private void initUnitsNames() {
325
        I18nManager i18n = ToolsLocator.getI18nManager();
326
        if (unitsNames == null) {
327
            String[] names = MapContext.getDistanceNames();
328
            unitsNames = new String[names.length];
329
            for (int i = 0; i < names.length; i++) {
330
                unitsNames[i] = i18n.getTranslation(names[i]);
331
                if (names[i].equals("Metros")) {
332
                    FACTORY_DEFAULT_DISTANCE_UNITS = i;
333
                    FACTORY_DEFAULT_DISTANCE_AREA = i;
334
                    FACTORY_DEFAULT_MAP_UNITS = i;
335
                }
336
            }
337
        }
338
        if( areaUnitsNames == null ) {
339
            String[] names = MapContext.getAreaNames();
340
            areaUnitsNames = new String[names.length];
341
            for (int i = 0; i < names.length; i++) {
342
                areaUnitsNames[i] = i18n.getTranslation(names[i])+MapContext.getOfLinear(i);
343
            }
344
        }
345
    }
346
}