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

History | View | Annotate | Download (15.9 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.project;
24

    
25
import java.awt.Color;
26
import javax.swing.UIManager;
27
import org.apache.commons.lang.StringUtils;
28
import org.apache.commons.lang3.BooleanUtils;
29

    
30
import org.cresques.cts.IProjection;
31
import org.gvsig.andami.PluginServices;
32
import org.gvsig.andami.PluginsLocator;
33
import org.gvsig.app.imp.DefaultPreferencesNode;
34
import org.gvsig.fmap.crs.CRSFactory;
35
import org.gvsig.fmap.mapcontext.MapContext;
36
import org.gvsig.fmap.mapcontext.MapContextLocator;
37
import org.gvsig.tools.dynobject.DynObject;
38
import org.gvsig.utils.StringUtilities;
39
import org.gvsig.utils.XMLEntity;
40

    
41
/**
42
 * Clase que representa las preferencias para el proyecto
43
 *
44
 * @author 2009- Joaquin del Cerro
45
 *
46
 */
47
public class ProjectPreferences extends DefaultPreferencesNode {
48

    
49
    public static final String DEFAULT_PROJECTION_KEY_NAME = "DefaultProjection";
50
    private static final String ZOOM_IN_FACTOR = "ZoomInFactor";
51
    private static final String ZOOM_OUT_FACTOR = "ZoomOutFactor";
52
    private static final String KEEP_SCALE_ON_RESIZING = "KeepScaleOnResizing";
53
    private static final String OPEN_WITH_A_NEW_MAXIMIZED_VIEW = "OpenWithANewMaximizedView";
54
    private static final String ADD_NEW_LAYERS_IN_INVISIBLE_MODE = "NewLayersInInvisibleMode";
55

    
56
    private IProjection defaultProjection = null;
57

    
58
    private final IProjection defaultFactoryProjection
59
            = MapContextLocator.getMapContextManager().getDefaultCRS();
60

    
61
    private Color defaultSelectionColor = Color.YELLOW;
62

    
63
    private Color defaultViewBackColor = null;  // Color.WHITE;
64

    
65
    private Color defaultOverviewBackColor = null; //Color.WHITE;
66

    
67
    private int defaultMapUnits = -1;
68

    
69
    private int defaultDistanceUnits = -1;
70

    
71
    private int defaultDistanceArea = -1;
72

    
73
    private final PluginServices plugin;
74
    private final DynObject pluginPreferences;
75

    
76
    public ProjectPreferences() {
77
        this.plugin = PluginsLocator.getManager().getPlugin(this);
78
        this.pluginPreferences = plugin.getPluginProperties();
79
    }
80

    
81
    /**
82
     * Sets the projection used when no projection is defined
83
     *
84
     * @param defaultProjection
85
     */
86
    public void setDefaultProjection(IProjection defaultProjection) {
87
        this.defaultProjection = defaultProjection;
88
        if (defaultProjection != null) {
89
            XMLEntity xml = plugin.getPersistentXML();
90
            xml.putProperty(DEFAULT_PROJECTION_KEY_NAME, defaultProjection.getAbrev());
91
        }
92
    }
93

    
94
    public void setDefaultProjection(String defaultProjection) {
95
        if (!StringUtils.isEmpty(defaultProjection)) {
96
            this.setDefaultProjection(CRSFactory.getCRS(defaultProjection));
97
        }
98
    }
99

    
100
    public IProjection getDefaultProjection() {
101
        if (this.defaultProjection == null) {
102
            XMLEntity xml = plugin.getPersistentXML();
103
            if (xml.contains(DEFAULT_PROJECTION_KEY_NAME)) {
104
                String projCode = xml.getStringProperty(DEFAULT_PROJECTION_KEY_NAME);
105
                this.defaultProjection = CRSFactory.getCRS(projCode);
106
            } else {
107
                this.defaultProjection = defaultFactoryProjection;
108
            }
109
        }
110
        return this.defaultProjection;
111
    }
112

    
113
    @Override
114
    public int getInt(String name, int defaultValue) {
115
        if (name.equalsIgnoreCase("DefaultDistanceUnits")) {
116
            return this.getDefaultDistanceUnits();
117
        } else if (name.equalsIgnoreCase("DefaultDistanceArea")) {
118
            return this.getDefaultDistanceArea();
119
        } else if (name.equalsIgnoreCase("DefaultMapUnits")) {
120
            return this.getDefaultMapUnits();
121
        }
122

    
123
        return super.getInt(name, defaultValue);
124
    }
125

    
126
    /**
127
     * Sets the default selection color that will be used in subsequent
128
     * projects.
129
     *
130
     * @param color
131
     */
132
    public void setDefaultSelectionColor(Color color) {
133
        defaultSelectionColor = color;
134
        XMLEntity xml = plugin.getPersistentXML();
135
        xml.putProperty("DefaultSelectionColor", StringUtilities.color2String(color));
136
    }
137

    
138
    /**
139
     * Returns the current default selection color defined which is the color
140
     * defined when the user does not define any other one
141
     *
142
     * @return java.awt.Color
143
     */
144
    public Color getDefaultSelectionColor() {
145
        XMLEntity xml = plugin.getPersistentXML();
146
        if (xml.contains("DefaultSelectionColor")) {
147
            defaultSelectionColor = StringUtilities.string2Color(xml
148
                    .getStringProperty("DefaultSelectionColor"));
149
        }
150
        return defaultSelectionColor;
151
    }
152

    
153
    public Color getDefaultViewBackColor() {
154
        XMLEntity xml = PluginServices.getPluginServices("org.gvsig.app").getPersistentXML();
155
        if (xml.contains("DefaultViewBackColor")) {
156
            defaultViewBackColor = StringUtilities.
157
                    string2Color(xml.getStringProperty("DefaultViewBackColor"));
158
        } 
159
        if( defaultViewBackColor == null ) {
160
            defaultViewBackColor  = UIManager.getColor("EditorPane.background");
161
            if( defaultViewBackColor == null ) {
162
                defaultViewBackColor  = Color.WHITE;
163
            }
164
        }
165
        return defaultViewBackColor;
166
    }
167

    
168
    public void setDefaultViewBackColor(Color color) {
169
        defaultViewBackColor = color;
170
        XMLEntity xml = plugin.getPersistentXML();
171
        xml.putProperty("DefaultViewBackColor", StringUtilities.color2String(color));
172
    }
173

    
174
    public Color getDefaultOverviewBackColor() {
175
        XMLEntity xml = plugin.getPersistentXML();
176
        if (xml.contains("defaultOverviewBackColor")) {
177
            defaultOverviewBackColor = StringUtilities.string2Color(xml
178
                    .getStringProperty("defaultOverviewBackColor"));
179
        } 
180
        if( defaultOverviewBackColor == null ) {
181
            defaultOverviewBackColor  = UIManager.getColor("EditorPane.background");
182
            if( defaultOverviewBackColor == null ) {
183
                defaultOverviewBackColor  = Color.WHITE;
184
            }
185
        }
186
        return defaultOverviewBackColor;
187

    
188
    }
189

    
190
    public void setDefaultOverviewBackColor(Color color) {
191
        defaultOverviewBackColor = color;
192
    }
193

    
194
    /**
195
     * Returns the user's default map units. This is the cartography data
196
     * distance units.
197
     *
198
     * @return int (index of the <b>Attributes.NAMES array</b>)
199
     */
200
    public int getDefaultMapUnits() {
201
        if (defaultMapUnits == -1) {
202
            XMLEntity xml = plugin.getPersistentXML();
203
            if (xml.contains("DefaultMapUnits")) {
204
                defaultMapUnits = xml.getIntProperty("DefaultMapUnits");
205
            } else {
206
                // first app run case
207
                String[] unitNames = MapContext.getDistanceNames();
208
                for (int i = 0; i < unitNames.length; i++) {
209
                    // meter is the factory default's map unit
210
                    if (unitNames[i].equals("Metros")) {
211
                        defaultMapUnits = i;
212
                        break;
213
                    }
214
                }
215
            }
216
            if (defaultMapUnits == -1 || defaultMapUnits >= MapContext.getDistanceNames().length) {
217
                defaultMapUnits = MapContext.getDistancePosition("Metros");
218
            }
219
        }
220
        return defaultMapUnits;
221
    }
222

    
223
    /**
224
     * Returns the user's default view units for measuring distances. This is
225
     * the units that the user will see in the status bar of the view.
226
     *
227
     * @return int (index of the <b>Attributes.NAMES array</b>)
228
     */
229
    public int getDefaultDistanceUnits() {
230
        if (defaultDistanceUnits == -1) {
231
            XMLEntity xml = plugin.getPersistentXML();
232
            if (xml.contains("DefaultDistanceUnits")) {
233
                defaultDistanceUnits = xml
234
                        .getIntProperty("DefaultDistanceUnits");
235
            } else {
236
                // first app run case
237
                String[] unitNames = MapContext.getDistanceNames();
238
                for (int i = 0; i < unitNames.length; i++) {
239
                    // meter is the factory default's distance unit
240
                    if (unitNames[i].equals("Metros")) {
241
                        defaultDistanceUnits = i;
242
                        break;
243
                    }
244
                }
245
            }
246
            if (defaultDistanceUnits == -1 || defaultDistanceUnits >= MapContext.getDistanceNames().length) {
247
                defaultDistanceUnits = MapContext.getDistancePosition("Metros");
248
            }
249
        }
250
        return defaultDistanceUnits;
251
    }
252

    
253
    /**
254
     * Returns the user's default view units for measuring areas. This is the
255
     * units that the user will see in the status bar of the view.
256
     *
257
     * @return int (index of the <b>Attributes.NAMES array</b>)
258
     */
259
    public int getDefaultDistanceArea() {
260
        if (defaultDistanceArea == -1) {
261
            XMLEntity xml = plugin.getPersistentXML();
262
            if (xml.contains("DefaultDistanceArea")) {
263
                defaultDistanceArea = xml
264
                        .getIntProperty("DefaultDistanceArea");
265
            } else {
266
                // first app run case
267
                String[] unitNames = MapContext.getAreaNames();
268
                for (int i = 0; i < unitNames.length; i++) {
269
                    // meter is the factory default's distance unit
270
                    if (unitNames[i].equals("Metros")) {
271
                        defaultDistanceArea = i;
272
                        break;
273
                    }
274
                }
275
            }
276
            if (defaultDistanceArea == -1 || defaultDistanceArea >= MapContext.getAreaNames().length) {
277
                defaultDistanceArea = getDefaultDistanceUnits();
278
            }
279
        }
280
        return defaultDistanceArea;
281
    }
282

    
283
    /**
284
     * Sets the default map unit (the units used by the data).
285
     *
286
     * @param mapUnits
287
     */
288
    public void setDefaultMapUnits(int mapUnits) {
289
        defaultMapUnits = mapUnits;
290
        XMLEntity xml = plugin.getPersistentXML();
291
        xml.putProperty("DefaultMapUnits", mapUnits);
292
    }
293

    
294
    /**
295
     * Sets the default distance units (the units shown in the status bar)
296
     *
297
     * @param distanceUnits
298
     */
299
    public void setDefaultDistanceUnits(int distanceUnits) {
300
        defaultDistanceUnits = distanceUnits;
301
        XMLEntity xml = plugin.getPersistentXML();
302
        xml.putProperty("DefaultDistanceUnits", distanceUnits);
303
    }
304

    
305
    /**
306
     * Sets the default distance area (the units shown in the status bar)
307
     *
308
     * @param distanceArea
309
     */
310
    public void setDefaultDistanceArea(int distanceArea) {
311
        defaultDistanceArea = distanceArea;
312
        XMLEntity xml = plugin.getPersistentXML();
313
        xml.putProperty("DefaultDistanceArea", distanceArea);
314
    }
315

    
316
    public boolean getAddNewLayersInInvisibleMode() {
317
        Boolean addNewLayersInInvisibleMode = (Boolean) this.pluginPreferences.getDynValue(ADD_NEW_LAYERS_IN_INVISIBLE_MODE);
318
        if (addNewLayersInInvisibleMode == null) {
319
            XMLEntity xml = plugin.getPersistentXML();
320
            if (xml.contains(ADD_NEW_LAYERS_IN_INVISIBLE_MODE)) {
321
                addNewLayersInInvisibleMode = xml.getBooleanProperty(ADD_NEW_LAYERS_IN_INVISIBLE_MODE);
322
            } else {
323
                addNewLayersInInvisibleMode = false;
324
            }
325
            this.pluginPreferences.setDynValue(ADD_NEW_LAYERS_IN_INVISIBLE_MODE, addNewLayersInInvisibleMode);
326
        }
327
        return BooleanUtils.isTrue(addNewLayersInInvisibleMode);
328
    }
329

    
330
    public void setAddNewLayersInInvisibleMode(boolean addNewLayersInInvisibleMode) {
331
        this.pluginPreferences.setDynValue(ADD_NEW_LAYERS_IN_INVISIBLE_MODE, addNewLayersInInvisibleMode);
332
    }
333

    
334
    public boolean getOpenWithANewMaximizedView() {
335
        Boolean openWithANewMaximizedView = (Boolean) this.pluginPreferences.getDynValue(OPEN_WITH_A_NEW_MAXIMIZED_VIEW);
336
        if (openWithANewMaximizedView == null) {
337
            XMLEntity xml = plugin.getPersistentXML();
338
            if (xml.contains(OPEN_WITH_A_NEW_MAXIMIZED_VIEW)) {
339
                openWithANewMaximizedView = xml.getBooleanProperty(OPEN_WITH_A_NEW_MAXIMIZED_VIEW);
340
            } else {
341
                openWithANewMaximizedView = true;
342
            }
343
            this.pluginPreferences.setDynValue(OPEN_WITH_A_NEW_MAXIMIZED_VIEW, openWithANewMaximizedView);
344
        }
345
        return BooleanUtils.isTrue(openWithANewMaximizedView);
346
    }
347

    
348
    public void setOpenWithANewMaximizedView(boolean openWithANewMaximizedView) {
349
        this.pluginPreferences.setDynValue(OPEN_WITH_A_NEW_MAXIMIZED_VIEW, openWithANewMaximizedView);
350
    }
351

    
352
    public boolean getKeepScaleOnResizing() {
353
        Boolean keepScaleOnResizing = (Boolean) this.pluginPreferences.getDynValue(KEEP_SCALE_ON_RESIZING);
354
        if (keepScaleOnResizing == null) {
355
            XMLEntity xml = plugin.getPersistentXML();
356
            if (xml.contains(KEEP_SCALE_ON_RESIZING)) {
357
                keepScaleOnResizing = xml.getBooleanProperty(KEEP_SCALE_ON_RESIZING);
358
            } else {
359
                keepScaleOnResizing = false;
360
            }
361
            this.pluginPreferences.setDynValue(KEEP_SCALE_ON_RESIZING, keepScaleOnResizing);
362
        }
363
        return BooleanUtils.isTrue(keepScaleOnResizing);
364
    }
365

    
366
    public void setKeepScaleOnResizing(boolean keepScaleOnResizing) {
367
        this.pluginPreferences.setDynValue(KEEP_SCALE_ON_RESIZING, keepScaleOnResizing);
368
    }
369

    
370
    public double getZoomInFactor() {
371
        Double zoomInFactor = (Double) this.pluginPreferences.getDynValue(ZOOM_IN_FACTOR);
372
        if (zoomInFactor == null) {
373
            XMLEntity xml = plugin.getPersistentXML();
374
            if (xml.contains(ZOOM_IN_FACTOR)) {
375
                zoomInFactor = xml.getDoubleProperty(ZOOM_IN_FACTOR);
376
            } else {
377
                zoomInFactor = MapContext.ZOOMINFACTOR;
378
            }
379
            this.pluginPreferences.setDynValue(ZOOM_IN_FACTOR, zoomInFactor);
380
        }
381
        return zoomInFactor;
382
    }
383

    
384
    public void setZoomInFactor(double zoomInFactor) {
385
        this.pluginPreferences.setDynValue(ZOOM_IN_FACTOR, zoomInFactor);
386
    }
387

    
388
    public double getZoomOutFactor() {
389
        Double zoomOutFactor = (Double) this.pluginPreferences.getDynValue(ZOOM_OUT_FACTOR);
390
        if (zoomOutFactor == null) {
391
            XMLEntity xml = plugin.getPersistentXML();
392
            if (xml.contains(ZOOM_OUT_FACTOR)) {
393
                zoomOutFactor = xml.getDoubleProperty(ZOOM_OUT_FACTOR);
394
            } else {
395
                zoomOutFactor = MapContext.ZOOMINFACTOR;
396
            }
397
            this.pluginPreferences.setDynValue(ZOOM_OUT_FACTOR, zoomOutFactor);
398
        }
399
        return zoomOutFactor;
400
    }
401

    
402
    public void setZoomOutFactor(double zoomOutFactor) {
403
        this.pluginPreferences.setDynValue(ZOOM_OUT_FACTOR, zoomOutFactor);
404
    }
405

    
406
    public boolean getHideLegendInToCOfNonVisibleLayers() {
407
        Boolean hideLegendInToCOfNonVisibleLayers = (Boolean)this.pluginPreferences.getDynValue("hideLegendInToCOfNonVisibleLayers");
408
        return BooleanUtils.toBooleanDefaultIfNull(hideLegendInToCOfNonVisibleLayers,true);
409
    }
410

    
411
    public void setHideLegendInToCOfNonVisibleLayers(boolean hideLegendInToCOfNonVisibleLayers) {
412
        this.pluginPreferences.setDynValue("hideLegendInToCOfNonVisibleLayers", hideLegendInToCOfNonVisibleLayers);
413
    }
414

    
415
}