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

History | View | Annotate | Download (16.1 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
import org.cresques.cts.IProjection;
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.PluginsLocator;
32
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
import org.gvsig.tools.dynobject.DynObject;
37
import org.gvsig.utils.StringUtilities;
38
import org.gvsig.utils.XMLEntity;
39

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

    
48
    public static final String DEFAULT_PROJECTION_KEY_NAME = "DefaultProjection";
49
    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

    
55
    private IProjection defaultProjection = null;
56

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

    
60
    private Color defaultSelectionColor = MapContext.DEFAULT_SELECTION_COLOR;
61

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

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

    
66
    private int defaultMapUnits = -1;
67

    
68
    private int defaultDistanceUnits = -1;
69

    
70
    private int defaultDistanceArea = -1;
71

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

    
75
    public ProjectPreferences() {
76
        this.plugin = PluginsLocator.getManager().getPlugin(this);
77
        this.pluginPreferences = plugin.getPluginProperties();
78
    }
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
            }
108
        }
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
        if(color == null){
133
            return;
134
        }
135
        defaultSelectionColor = color;
136
        XMLEntity xml = plugin.getPersistentXML();
137
        xml.putProperty("DefaultSelectionColor", StringUtilities.color2String(color));
138
    }
139

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

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

    
173
    public void setDefaultViewBackColor(Color color) {
174
        defaultViewBackColor = color;
175
        XMLEntity xml = plugin.getPersistentXML();
176
        xml.putProperty("DefaultViewBackColor", StringUtilities.color2String(color));
177
    }
178

    
179
    public Color getDefaultOverviewBackColor() {
180
        XMLEntity xml = plugin.getPersistentXML();
181
        if (xml.contains("defaultOverviewBackColor")) {
182
            defaultOverviewBackColor = StringUtilities.string2Color(xml
183
                    .getStringProperty("defaultOverviewBackColor"));
184
        } 
185
        if( defaultOverviewBackColor == null ) {
186
            defaultOverviewBackColor  = UIManager.getColor("EditorPane.background");
187
            if( defaultOverviewBackColor == null ) {
188
                defaultOverviewBackColor  = Color.WHITE;
189
            }
190
        }
191
        return defaultOverviewBackColor;
192

    
193
    }
194

    
195
    public void setDefaultOverviewBackColor(Color color) {
196
        defaultOverviewBackColor = color;
197
    }
198

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

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

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

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

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

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

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

    
335
    public void setAddNewLayersInInvisibleMode(boolean addNewLayersInInvisibleMode) {
336
        this.pluginPreferences.setDynValue(ADD_NEW_LAYERS_IN_INVISIBLE_MODE, addNewLayersInInvisibleMode);
337
    }
338

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

    
353
    public void setOpenWithANewMaximizedView(boolean openWithANewMaximizedView) {
354
        this.pluginPreferences.setDynValue(OPEN_WITH_A_NEW_MAXIMIZED_VIEW, openWithANewMaximizedView);
355
    }
356

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

    
371
    public void setKeepScaleOnResizing(boolean keepScaleOnResizing) {
372
        this.pluginPreferences.setDynValue(KEEP_SCALE_ON_RESIZING, keepScaleOnResizing);
373
    }
374

    
375
    public double getZoomInFactor() {
376
        Double zoomInFactor = (Double) this.pluginPreferences.getDynValue(ZOOM_IN_FACTOR);
377
        if (zoomInFactor == null) {
378
            XMLEntity xml = plugin.getPersistentXML();
379
            if (xml.contains(ZOOM_IN_FACTOR)) {
380
                zoomInFactor = xml.getDoubleProperty(ZOOM_IN_FACTOR);
381
            } else {
382
                zoomInFactor = MapContext.ZOOMINFACTOR;
383
            }
384
            this.pluginPreferences.setDynValue(ZOOM_IN_FACTOR, zoomInFactor);
385
        }
386
        return zoomInFactor;
387
    }
388

    
389
    public void setZoomInFactor(double zoomInFactor) {
390
        this.pluginPreferences.setDynValue(ZOOM_IN_FACTOR, zoomInFactor);
391
    }
392

    
393
    public double getZoomOutFactor() {
394
        Double zoomOutFactor = (Double) this.pluginPreferences.getDynValue(ZOOM_OUT_FACTOR);
395
        if (zoomOutFactor == null) {
396
            XMLEntity xml = plugin.getPersistentXML();
397
            if (xml.contains(ZOOM_OUT_FACTOR)) {
398
                zoomOutFactor = xml.getDoubleProperty(ZOOM_OUT_FACTOR);
399
            } else {
400
                zoomOutFactor = MapContext.ZOOMINFACTOR;
401
            }
402
            this.pluginPreferences.setDynValue(ZOOM_OUT_FACTOR, zoomOutFactor);
403
        }
404
        return zoomOutFactor;
405
    }
406

    
407
    public void setZoomOutFactor(double zoomOutFactor) {
408
        this.pluginPreferences.setDynValue(ZOOM_OUT_FACTOR, zoomOutFactor);
409
    }
410

    
411
    public boolean getHideLegendInToCOfNonVisibleLayers() {
412
        Boolean hideLegendInToCOfNonVisibleLayers = (Boolean)this.pluginPreferences.getDynValue("hideLegendInToCOfNonVisibleLayers");
413
        return BooleanUtils.toBooleanDefaultIfNull(hideLegendInToCOfNonVisibleLayers,true);
414
    }
415

    
416
    public void setHideLegendInToCOfNonVisibleLayers(boolean hideLegendInToCOfNonVisibleLayers) {
417
        this.pluginPreferences.setDynValue("hideLegendInToCOfNonVisibleLayers", hideLegendInToCOfNonVisibleLayers);
418
    }
419

    
420
}