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 / project / ProjectPreferences.java @ 44644

History | View | Annotate | Download (15.3 KB)

1 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 43095 jjdelcerro
 * 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 40558 jjdelcerro
 *
11 43095 jjdelcerro
 * 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 40558 jjdelcerro
 *
16 43095 jjdelcerro
 * 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 40558 jjdelcerro
 *
20 43095 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40558 jjdelcerro
 */
23 40435 jjdelcerro
package org.gvsig.app.project;
24
25
import java.awt.Color;
26 41311 jjdelcerro
import org.apache.commons.lang.StringUtils;
27 43095 jjdelcerro
import org.apache.commons.lang3.BooleanUtils;
28 40435 jjdelcerro
29
import org.cresques.cts.IProjection;
30
import org.gvsig.andami.PluginServices;
31 43095 jjdelcerro
import org.gvsig.andami.PluginsLocator;
32 40435 jjdelcerro
import org.gvsig.app.imp.DefaultPreferencesNode;
33
import org.gvsig.fmap.crs.CRSFactory;
34
import org.gvsig.fmap.mapcontext.MapContext;
35
import org.gvsig.fmap.mapcontext.MapContextLocator;
36 43095 jjdelcerro
import org.gvsig.tools.dynobject.DynObject;
37 40435 jjdelcerro
import org.gvsig.utils.StringUtilities;
38
import org.gvsig.utils.XMLEntity;
39
40
/**
41
 * Clase que representa las preferencias para el proyecto
42
 *
43 43095 jjdelcerro
 * @author 2009- Joaquin del Cerro
44
 *
45 40435 jjdelcerro
 */
46
public class ProjectPreferences extends DefaultPreferencesNode {
47
48 41311 jjdelcerro
    public static final String DEFAULT_PROJECTION_KEY_NAME = "DefaultProjection";
49 43095 jjdelcerro
    private static final String ZOOM_IN_FACTOR = "ZoomInFactor";
50
    private static final String ZOOM_OUT_FACTOR = "ZoomOutFactor";
51
    private static final String KEEP_SCALE_ON_RESIZING = "KeepScaleOnResizing";
52
    private static final String OPEN_WITH_A_NEW_MAXIMIZED_VIEW = "OpenWithANewMaximizedView";
53
    private static final String ADD_NEW_LAYERS_IN_INVISIBLE_MODE = "NewLayersInInvisibleMode";
54 40435 jjdelcerro
55 43095 jjdelcerro
    private IProjection defaultProjection = null;
56 40435 jjdelcerro
57 43095 jjdelcerro
    private final IProjection defaultFactoryProjection
58
            = MapContextLocator.getMapContextManager().getDefaultCRS();
59 40435 jjdelcerro
60 43095 jjdelcerro
    private Color defaultSelectionColor = Color.YELLOW;
61 40435 jjdelcerro
62 43095 jjdelcerro
    private Color defaultViewBackColor = Color.WHITE;
63 40435 jjdelcerro
64 43095 jjdelcerro
    private Color defaultOverviewBackColor = Color.WHITE;
65 40435 jjdelcerro
66 43095 jjdelcerro
    private int defaultMapUnits = -1;
67 40435 jjdelcerro
68 43095 jjdelcerro
    private int defaultDistanceUnits = -1;
69 40435 jjdelcerro
70 43095 jjdelcerro
    private int defaultDistanceArea = -1;
71
72 43100 jjdelcerro
    private final PluginServices plugin;
73
    private final DynObject pluginPreferences;
74
75 43095 jjdelcerro
    public ProjectPreferences() {
76 43100 jjdelcerro
        this.plugin = PluginsLocator.getManager().getPlugin(this);
77
        this.pluginPreferences = plugin.getPluginProperties();
78 43095 jjdelcerro
    }
79
80
    /**
81
     * Sets the projection used when no projection is defined
82
     *
83
     * @param defaultProjection
84
     */
85
    public void setDefaultProjection(IProjection defaultProjection) {
86
        this.defaultProjection = defaultProjection;
87
        if (defaultProjection != null) {
88
            XMLEntity xml = plugin.getPersistentXML();
89
            xml.putProperty(DEFAULT_PROJECTION_KEY_NAME, defaultProjection.getAbrev());
90
        }
91
    }
92
93
    public void setDefaultProjection(String defaultProjection) {
94
        if (!StringUtils.isEmpty(defaultProjection)) {
95
            this.setDefaultProjection(CRSFactory.getCRS(defaultProjection));
96
        }
97
    }
98
99
    public IProjection getDefaultProjection() {
100
        if (this.defaultProjection == null) {
101
            XMLEntity xml = plugin.getPersistentXML();
102
            if (xml.contains(DEFAULT_PROJECTION_KEY_NAME)) {
103
                String projCode = xml.getStringProperty(DEFAULT_PROJECTION_KEY_NAME);
104
                this.defaultProjection = CRSFactory.getCRS(projCode);
105
            } else {
106
                this.defaultProjection = defaultFactoryProjection;
107 41311 jjdelcerro
            }
108 43095 jjdelcerro
        }
109
        return this.defaultProjection;
110
    }
111
112
    @Override
113
    public int getInt(String name, int defaultValue) {
114
        if (name.equalsIgnoreCase("DefaultDistanceUnits")) {
115
            return this.getDefaultDistanceUnits();
116
        } else if (name.equalsIgnoreCase("DefaultDistanceArea")) {
117
            return this.getDefaultDistanceArea();
118
        } else if (name.equalsIgnoreCase("DefaultMapUnits")) {
119
            return this.getDefaultMapUnits();
120
        }
121
122
        return super.getInt(name, defaultValue);
123
    }
124
125
    /**
126
     * Sets the default selection color that will be used in subsequent
127
     * projects.
128
     *
129
     * @param color
130
     */
131
    public void setDefaultSelectionColor(Color color) {
132
        defaultSelectionColor = color;
133 43100 jjdelcerro
        XMLEntity xml = plugin.getPersistentXML();
134 43095 jjdelcerro
        xml.putProperty("DefaultSelectionColor", StringUtilities.color2String(color));
135
    }
136
137
    /**
138
     * Returns the current default selection color defined which is the color
139
     * defined when the user does not define any other one
140
     *
141
     * @return java.awt.Color
142
     */
143
    public Color getDefaultSelectionColor() {
144 43100 jjdelcerro
        XMLEntity xml = plugin.getPersistentXML();
145 43095 jjdelcerro
        if (xml.contains("DefaultSelectionColor")) {
146
            defaultSelectionColor = StringUtilities.string2Color(xml
147
                    .getStringProperty("DefaultSelectionColor"));
148
        }
149
        return defaultSelectionColor;
150
    }
151
152
    public Color getDefaultViewBackColor() {
153
        XMLEntity xml = PluginServices.getPluginServices("org.gvsig.app").getPersistentXML();
154
        if (xml.contains("DefaultViewBackColor")) {
155
            defaultViewBackColor = StringUtilities.
156
                    string2Color(xml.getStringProperty("DefaultViewBackColor"));
157
        }
158
        return defaultViewBackColor;
159
    }
160
161
    public void setDefaultViewBackColor(Color color) {
162
        defaultViewBackColor = color;
163 43100 jjdelcerro
        XMLEntity xml = plugin.getPersistentXML();
164 43095 jjdelcerro
        xml.putProperty("DefaultViewBackColor", StringUtilities.color2String(color));
165
    }
166
167
    public Color getDefaultOverviewBackColor() {
168 43100 jjdelcerro
        XMLEntity xml = plugin.getPersistentXML();
169 43095 jjdelcerro
        if (xml.contains("defaultOverviewBackColor")) {
170
            defaultOverviewBackColor = StringUtilities.string2Color(xml
171
                    .getStringProperty("defaultOverviewBackColor"));
172
        }
173
        return defaultOverviewBackColor;
174
175
    }
176
177
    public void setDefaultOverviewBackColor(Color color) {
178
        defaultOverviewBackColor = color;
179
    }
180
181
    /**
182
     * Returns the user's default map units. This is the cartography data
183
     * distance units.
184
     *
185
     * @return int (index of the <b>Attributes.NAMES array</b>)
186
     */
187
    public int getDefaultMapUnits() {
188
        if (defaultMapUnits == -1) {
189 43100 jjdelcerro
            XMLEntity xml = plugin.getPersistentXML();
190 43095 jjdelcerro
            if (xml.contains("DefaultMapUnits")) {
191
                defaultMapUnits = xml.getIntProperty("DefaultMapUnits");
192
            } else {
193
                // first app run case
194
                String[] unitNames = MapContext.getDistanceNames();
195
                for (int i = 0; i < unitNames.length; i++) {
196
                    // meter is the factory default's map unit
197
                    if (unitNames[i].equals("Metros")) {
198
                        defaultMapUnits = i;
199
                        break;
200
                    }
201
                }
202 41311 jjdelcerro
            }
203 43095 jjdelcerro
            if (defaultMapUnits == -1 || defaultMapUnits >= MapContext.getDistanceNames().length) {
204
                defaultMapUnits = MapContext.getDistancePosition("Metros");
205
            }
206 41311 jjdelcerro
        }
207 43095 jjdelcerro
        return defaultMapUnits;
208
    }
209
210
    /**
211
     * Returns the user's default view units for measuring distances. This is
212
     * the units that the user will see in the status bar of the view.
213
     *
214
     * @return int (index of the <b>Attributes.NAMES array</b>)
215
     */
216
    public int getDefaultDistanceUnits() {
217
        if (defaultDistanceUnits == -1) {
218 43100 jjdelcerro
            XMLEntity xml = plugin.getPersistentXML();
219 43095 jjdelcerro
            if (xml.contains("DefaultDistanceUnits")) {
220
                defaultDistanceUnits = xml
221
                        .getIntProperty("DefaultDistanceUnits");
222
            } else {
223
                // first app run case
224
                String[] unitNames = MapContext.getDistanceNames();
225
                for (int i = 0; i < unitNames.length; i++) {
226
                    // meter is the factory default's distance unit
227
                    if (unitNames[i].equals("Metros")) {
228
                        defaultDistanceUnits = i;
229
                        break;
230
                    }
231 41311 jjdelcerro
                }
232
            }
233 43095 jjdelcerro
            if (defaultDistanceUnits == -1 || defaultDistanceUnits >= MapContext.getDistanceNames().length) {
234
                defaultDistanceUnits = MapContext.getDistancePosition("Metros");
235
            }
236
        }
237
        return defaultDistanceUnits;
238
    }
239 40435 jjdelcerro
240 43095 jjdelcerro
    /**
241
     * Returns the user's default view units for measuring areas. This is the
242
     * units that the user will see in the status bar of the view.
243
     *
244
     * @return int (index of the <b>Attributes.NAMES array</b>)
245
     */
246
    public int getDefaultDistanceArea() {
247
        if (defaultDistanceArea == -1) {
248 43100 jjdelcerro
            XMLEntity xml = plugin.getPersistentXML();
249 43095 jjdelcerro
            if (xml.contains("DefaultDistanceArea")) {
250
                defaultDistanceArea = xml
251
                        .getIntProperty("DefaultDistanceArea");
252
            } else {
253
                // first app run case
254
                String[] unitNames = MapContext.getAreaNames();
255
                for (int i = 0; i < unitNames.length; i++) {
256
                    // meter is the factory default's distance unit
257
                    if (unitNames[i].equals("Metros")) {
258
                        defaultDistanceArea = i;
259
                        break;
260
                    }
261
                }
262
            }
263
            if (defaultDistanceArea == -1 || defaultDistanceArea >= MapContext.getAreaNames().length) {
264
                defaultDistanceArea = getDefaultDistanceUnits();
265
            }
266
        }
267
        return defaultDistanceArea;
268
    }
269 40435 jjdelcerro
270 43095 jjdelcerro
    /**
271
     * Sets the default map unit (the units used by the data).
272
     *
273
     * @param mapUnits
274
     */
275
    public void setDefaultMapUnits(int mapUnits) {
276
        defaultMapUnits = mapUnits;
277 43100 jjdelcerro
        XMLEntity xml = plugin.getPersistentXML();
278 43095 jjdelcerro
        xml.putProperty("DefaultMapUnits", mapUnits);
279
    }
280 40435 jjdelcerro
281 43095 jjdelcerro
    /**
282
     * Sets the default distance units (the units shown in the status bar)
283
     *
284
     * @param distanceUnits
285
     */
286
    public void setDefaultDistanceUnits(int distanceUnits) {
287
        defaultDistanceUnits = distanceUnits;
288 43100 jjdelcerro
        XMLEntity xml = plugin.getPersistentXML();
289 43095 jjdelcerro
        xml.putProperty("DefaultDistanceUnits", distanceUnits);
290
    }
291 40435 jjdelcerro
292 43095 jjdelcerro
    /**
293
     * Sets the default distance area (the units shown in the status bar)
294
     *
295
     * @param distanceArea
296
     */
297
    public void setDefaultDistanceArea(int distanceArea) {
298
        defaultDistanceArea = distanceArea;
299 43100 jjdelcerro
        XMLEntity xml = plugin.getPersistentXML();
300 43095 jjdelcerro
        xml.putProperty("DefaultDistanceArea", distanceArea);
301
    }
302 40435 jjdelcerro
303 43095 jjdelcerro
    public boolean getAddNewLayersInInvisibleMode() {
304 43100 jjdelcerro
        Boolean addNewLayersInInvisibleMode = (Boolean) this.pluginPreferences.getDynValue(ADD_NEW_LAYERS_IN_INVISIBLE_MODE);
305 43095 jjdelcerro
        if (addNewLayersInInvisibleMode == null) {
306 43100 jjdelcerro
            XMLEntity xml = plugin.getPersistentXML();
307 43095 jjdelcerro
            if (xml.contains(ADD_NEW_LAYERS_IN_INVISIBLE_MODE)) {
308
                addNewLayersInInvisibleMode = xml.getBooleanProperty(ADD_NEW_LAYERS_IN_INVISIBLE_MODE);
309
            } else {
310
                addNewLayersInInvisibleMode = false;
311
            }
312 43100 jjdelcerro
            this.pluginPreferences.setDynValue(ADD_NEW_LAYERS_IN_INVISIBLE_MODE, addNewLayersInInvisibleMode);
313 43095 jjdelcerro
        }
314
        return BooleanUtils.isTrue(addNewLayersInInvisibleMode);
315
    }
316 40435 jjdelcerro
317 43095 jjdelcerro
    public void setAddNewLayersInInvisibleMode(boolean addNewLayersInInvisibleMode) {
318 43100 jjdelcerro
        this.pluginPreferences.setDynValue(ADD_NEW_LAYERS_IN_INVISIBLE_MODE, addNewLayersInInvisibleMode);
319 43095 jjdelcerro
    }
320 40435 jjdelcerro
321 43095 jjdelcerro
    public boolean getOpenWithANewMaximizedView() {
322 43100 jjdelcerro
        Boolean openWithANewMaximizedView = (Boolean) this.pluginPreferences.getDynValue(OPEN_WITH_A_NEW_MAXIMIZED_VIEW);
323 43095 jjdelcerro
        if (openWithANewMaximizedView == null) {
324 43100 jjdelcerro
            XMLEntity xml = plugin.getPersistentXML();
325 43095 jjdelcerro
            if (xml.contains(OPEN_WITH_A_NEW_MAXIMIZED_VIEW)) {
326
                openWithANewMaximizedView = xml.getBooleanProperty(OPEN_WITH_A_NEW_MAXIMIZED_VIEW);
327
            } else {
328
                openWithANewMaximizedView = true;
329
            }
330 43100 jjdelcerro
            this.pluginPreferences.setDynValue(OPEN_WITH_A_NEW_MAXIMIZED_VIEW, openWithANewMaximizedView);
331 43095 jjdelcerro
        }
332
        return BooleanUtils.isTrue(openWithANewMaximizedView);
333
    }
334 40435 jjdelcerro
335 43095 jjdelcerro
    public void setOpenWithANewMaximizedView(boolean openWithANewMaximizedView) {
336 43100 jjdelcerro
        this.pluginPreferences.setDynValue(OPEN_WITH_A_NEW_MAXIMIZED_VIEW, openWithANewMaximizedView);
337 43095 jjdelcerro
    }
338 40435 jjdelcerro
339 43095 jjdelcerro
    public boolean getKeepScaleOnResizing() {
340 43100 jjdelcerro
        Boolean keepScaleOnResizing = (Boolean) this.pluginPreferences.getDynValue(KEEP_SCALE_ON_RESIZING);
341 43095 jjdelcerro
        if (keepScaleOnResizing == null) {
342 43100 jjdelcerro
            XMLEntity xml = plugin.getPersistentXML();
343 43095 jjdelcerro
            if (xml.contains(KEEP_SCALE_ON_RESIZING)) {
344
                keepScaleOnResizing = xml.getBooleanProperty(KEEP_SCALE_ON_RESIZING);
345
            } else {
346
                keepScaleOnResizing = false;
347
            }
348 43100 jjdelcerro
            this.pluginPreferences.setDynValue(KEEP_SCALE_ON_RESIZING, keepScaleOnResizing);
349 43095 jjdelcerro
        }
350
        return BooleanUtils.isTrue(keepScaleOnResizing);
351
    }
352
353
    public void setKeepScaleOnResizing(boolean keepScaleOnResizing) {
354 43100 jjdelcerro
        this.pluginPreferences.setDynValue(KEEP_SCALE_ON_RESIZING, keepScaleOnResizing);
355 43095 jjdelcerro
    }
356
357
    public double getZoomInFactor() {
358 43100 jjdelcerro
        Double zoomInFactor = (Double) this.pluginPreferences.getDynValue(ZOOM_IN_FACTOR);
359 43095 jjdelcerro
        if (zoomInFactor == null) {
360 43100 jjdelcerro
            XMLEntity xml = plugin.getPersistentXML();
361 43095 jjdelcerro
            if (xml.contains(ZOOM_IN_FACTOR)) {
362
                zoomInFactor = xml.getDoubleProperty(ZOOM_IN_FACTOR);
363
            } else {
364
                zoomInFactor = MapContext.ZOOMINFACTOR;
365
            }
366 43100 jjdelcerro
            this.pluginPreferences.setDynValue(ZOOM_IN_FACTOR, zoomInFactor);
367 43095 jjdelcerro
        }
368
        return zoomInFactor;
369
    }
370 43100 jjdelcerro
371 43095 jjdelcerro
    public void setZoomInFactor(double zoomInFactor) {
372 43100 jjdelcerro
        this.pluginPreferences.setDynValue(ZOOM_IN_FACTOR, zoomInFactor);
373 43095 jjdelcerro
    }
374
375
    public double getZoomOutFactor() {
376 43100 jjdelcerro
        Double zoomOutFactor = (Double) this.pluginPreferences.getDynValue(ZOOM_OUT_FACTOR);
377 43095 jjdelcerro
        if (zoomOutFactor == null) {
378 43100 jjdelcerro
            XMLEntity xml = plugin.getPersistentXML();
379 43095 jjdelcerro
            if (xml.contains(ZOOM_OUT_FACTOR)) {
380
                zoomOutFactor = xml.getDoubleProperty(ZOOM_OUT_FACTOR);
381
            } else {
382
                zoomOutFactor = MapContext.ZOOMINFACTOR;
383
            }
384 43100 jjdelcerro
            this.pluginPreferences.setDynValue(ZOOM_OUT_FACTOR, zoomOutFactor);
385 43095 jjdelcerro
        }
386
        return zoomOutFactor;
387
    }
388 43100 jjdelcerro
389 43095 jjdelcerro
    public void setZoomOutFactor(double zoomOutFactor) {
390 43100 jjdelcerro
        this.pluginPreferences.setDynValue(ZOOM_OUT_FACTOR, zoomOutFactor);
391 43095 jjdelcerro
    }
392
393 43100 jjdelcerro
    public boolean getHideLegendInToCOfNonVisibleLayers() {
394
        Boolean hideLegendInToCOfNonVisibleLayers = (Boolean)this.pluginPreferences.getDynValue("hideLegendInToCOfNonVisibleLayers");
395
        return BooleanUtils.toBooleanDefaultIfNull(hideLegendInToCOfNonVisibleLayers,true);
396
    }
397 43095 jjdelcerro
398 43100 jjdelcerro
    public void setHideLegendInToCOfNonVisibleLayers(boolean hideLegendInToCOfNonVisibleLayers) {
399
        this.pluginPreferences.setDynValue("hideLegendInToCOfNonVisibleLayers", hideLegendInToCOfNonVisibleLayers);
400
    }
401
402 40435 jjdelcerro
}