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 @ 47523

History | View | Annotate | Download (13.6 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.PluginsLocator;
39
import org.gvsig.andami.preferences.IPreference;
40
import org.gvsig.andami.preferences.StoreException;
41
import org.gvsig.app.ApplicationLocator;
42
import org.gvsig.app.project.ProjectPreferences;
43
import org.gvsig.fmap.dal.swing.DALSwingLocator;
44
import org.gvsig.fmap.dal.swing.ProjectionPickerController;
45
import org.gvsig.fmap.mapcontext.MapContext;
46
import org.gvsig.fmap.mapcontext.MapContextLocator;
47
import org.gvsig.tools.ToolsLocator;
48
import org.gvsig.tools.i18n.I18nManager;
49
import org.gvsig.tools.swing.api.ToolsSwingLocator;
50
import org.gvsig.tools.swing.api.ToolsSwingManager;
51
import org.gvsig.tools.swing.api.pickercontroller.ColorPickerController;
52
import org.gvsig.utils.XMLEntity;
53

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

    
67
    private static final long serialVersionUID = 1204688910771374514L;
68

    
69
    public static String id = ViewPage.class.getName();
70

    
71
    private static final IProjection FACTORY_DEFAULT_PROJECTION
72
            = MapContextLocator.getMapContextManager().getDefaultCRS();
73

    
74
    private static String[] unitsNames = null;
75
    private static String[] areaUnitsNames = null;
76

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

    
85
    private ImageIcon icon;
86

    
87
    private ProjectionPickerController defaultProjection;
88

    
89
    private ColorPickerController defaultViewBackColor = null;
90
    private ColorPickerController defaultSelectionColor = null;
91

    
92
    private boolean hasChanges = false;
93

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

    
101
        initUnitsNames();
102

    
103
        
104

    
105

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

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

    
128
        this.defaultViewBackColor = toolsSwingManager.createColorPickerController(
129
                this.txtDefaultViewBackColor,
130
                this.btnDefaultViewBackColor
131
        );
132

    
133
        this.defaultViewBackColor.addChangeListener(this);
134

    
135
        this.chkInvisibleNewLayers.addChangeListener(this);
136
        this.chkKeepScaleOnResizing.addChangeListener(this);
137

    
138
        this.jCmbDistanceArea.addItemListener(this);
139
        this.jCmbDistanceUnits.addItemListener(this);
140
        this.jCmbMapUnits.addItemListener(this);
141

    
142
    }
143

    
144
    private void translateAll() {
145
        I18nManager i18n = ToolsLocator.getI18nManager();
146

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

    
163
    @Override
164
    public boolean isResizeable() {
165
        return true;
166
    }
167

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

    
172
        this.defaultProjection.set(projectPreferences.getDefaultProjection());
173

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

    
197
        this.defaultViewBackColor.set(projectPreferences.getDefaultViewBackColor());
198
        this.defaultSelectionColor.set(projectPreferences.getDefaultSelectionColor());
199

    
200
        this.jCmbMapUnits.setModel(new DefaultComboBoxModel(unitsNames));
201
        this.jCmbMapUnits.setSelectedIndex(projectPreferences.getDefaultMapUnits());
202

    
203
        this.jCmbDistanceUnits.setModel(new DefaultComboBoxModel(unitsNames));
204
        this.jCmbDistanceUnits.setSelectedIndex(projectPreferences.getDefaultDistanceUnits());
205

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

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

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

    
220
    @Override
221
    public String getID() {
222
        return id;
223
    }
224

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

    
231
    @Override
232
    public JPanel getPanel() {
233
        return this;
234
    }
235

    
236
    @Override
237
    public void storeValues() throws StoreException {
238

    
239
        ProjectPreferences projectPreferences = ApplicationLocator.getProjectManager().getProjectPreferences();
240

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

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

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

    
258
            projectPreferences.setDefaultMapUnits(jCmbMapUnits.getSelectedIndex());
259
            projectPreferences.setDefaultDistanceUnits(jCmbDistanceUnits.getSelectedIndex());
260
            projectPreferences.setDefaultDistanceArea(jCmbDistanceArea.getSelectedIndex());
261

    
262
            projectPreferences.setAddNewLayersInInvisibleMode(chkInvisibleNewLayers.isSelected());
263
            projectPreferences.setKeepScaleOnResizing(chkKeepScaleOnResizing.isSelected());
264
            projectPreferences.setOpenWithANewMaximizedView(chkOpenWithANewMaximizedView.isSelected());
265
            projectPreferences.setHideLegendInToCOfNonVisibleLayers(this.chkHideLegendOfNonVisibleLayers.isSelected());
266
            
267
            MapContext.setSelectionColor(defaultSelectionColor.get());
268
            
269
        } catch (Exception e) {
270
            throw new StoreException(PluginServices.getText(this, "factor_zoom_incorrecto"));
271
        }
272
    }
273

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

    
308
    }
309

    
310
    @Override
311
    public ImageIcon getIcon() {
312
        if(icon == null){
313
            icon = IconThemeHelper.getImageIcon("document-view-icon");
314
        }
315
        return icon;
316
    }
317

    
318
    @Override
319
    public boolean isValueChanged() {
320
        return this.hasChanges;
321
    }
322

    
323
    @Override
324
    public void setChangesApplied() {
325
        this.hasChanges = false;
326
    }
327

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