Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / ProjectExtent.java @ 38772

History | View | Annotate | Download (5.25 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2009 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2004-2009 IVER TI
26
*   
27
*/
28
package org.gvsig.app.project;
29

    
30
import java.awt.geom.Rectangle2D;
31

    
32
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.GeometryLocator;
34
import org.gvsig.fmap.geom.GeometryManager;
35
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
36
import org.gvsig.fmap.geom.primitive.Envelope;
37
import org.gvsig.fmap.geom.primitive.Point;
38
import org.gvsig.tools.ToolsLocator;
39
import org.gvsig.tools.dynobject.DynStruct;
40
import org.gvsig.tools.persistence.PersistenceManager;
41
import org.gvsig.tools.persistence.Persistent;
42
import org.gvsig.tools.persistence.PersistentState;
43
import org.gvsig.tools.persistence.exception.PersistenceException;
44
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46

    
47

    
48
/**
49
 *
50
 * @author 2006-2009 Jose Manuel Vivo
51
 * @author 2005-         Vicente Caballero
52
 * @author 2009-         Joaquin del Cerro
53
 * 
54
 */
55

    
56
public class ProjectExtent implements Persistent {
57
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
58
        private static final Logger logger = LoggerFactory.getLogger(ProjectExtent.class);
59
        
60
    public static final String PERSISTENCE_DEFINITION_NAME = "org.gvsig.app.project.ProjectExtent";
61

    
62
        private Envelope extent;
63
    private String description;
64

    
65
    public ProjectExtent() {
66
            this.extent = null;
67
            this.description = null;
68
    }
69
    
70
    public String getDescription() {
71
        return description;
72
    }
73

    
74
    public void setDescription(String string) {
75
        description = string;
76
    }
77

    
78
    public Envelope getExtent() {
79
            if( this.extent == null ) {
80
                    try {
81
                                this.extent = geomManager.createEnvelope(Geometry.SUBTYPES.GEOM2D);
82
                        } catch (CreateEnvelopeException e) {
83
                                logger.warn("Can't create envelop.", e);
84
                        }
85
            }
86
            return this.extent;
87
    }
88

    
89
    public void setExtent(Envelope envelope) {
90
        this.extent = envelope;
91
    }
92

    
93
    /**
94
     * @deprecated use {link {@link #setExtent(Envelope)}
95
     */
96
    public void setExtent(Rectangle2D rectangle2D) {
97
        try {
98
                        this.extent = geomManager.createEnvelope(
99
                                        rectangle2D.getMinX(),
100
                                        rectangle2D.getMinY(),
101
                                        rectangle2D.getMaxX(),
102
                                        rectangle2D.getMaxY(),
103
                                        Geometry.SUBTYPES.GEOM2D);
104
                } catch (CreateEnvelopeException e) {
105
                        logger.warn("Can't create envelope.", e);
106
                        throw new RuntimeException("Can't create envelope.",e);
107
                }
108
    }
109

    
110
    public String getEncuadre() {
111
            Point lower = extent.getLowerCorner();
112
            Point upper = extent.getUpperCorner();
113
            return lower.getX() + "," + lower.getY() + "," + upper.getX() + "," + upper.getY(); 
114
    }
115

    
116
    public void setEncuadre(String encuadre) {
117
        String[] coords = encuadre.split(",");
118
        try {
119
                        this.extent = geomManager.createEnvelope(
120
                                        Double.parseDouble(coords[0]),
121
                                        Double.parseDouble(coords[1]),
122
                                        Double.parseDouble(coords[2]),
123
                                        Double.parseDouble(coords[3]),
124
                                        Geometry.SUBTYPES.GEOM2D);
125
                } catch (NumberFormatException e) {
126
                        logger.warn("Incorrect format string.", e);
127
                        throw new RuntimeException("Incorrect format string.",e);
128
                } catch (CreateEnvelopeException e) {
129
                        logger.warn("Can't create envelope.", e);
130
                        throw new RuntimeException("Can't create envelope.",e);
131
                }
132
    }
133

    
134
    /**
135
     * @see java.lang.Object#toString()
136
     */
137
    public String toString() {
138
        return description;
139
    }
140

    
141
    public void loadFromState(PersistentState state)
142
                        throws PersistenceException {
143
                this.extent = (Envelope) state.get("envelope");
144
                this.description = state.getString("description");
145
        }
146

    
147
        public void saveToState(PersistentState state) throws PersistenceException {
148
        state.set("description", description);
149
        state.set("envelope", extent);
150
        }
151

    
152
    /**
153
     * 
154
     */
155
    public static void registerPersistent() {
156
        
157
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
158
        DynStruct definition = manager.getDefinition(PERSISTENCE_DEFINITION_NAME);
159
        if ( definition == null ){
160
            definition = manager.addDefinition(
161
                ProjectExtent.class,
162
                PERSISTENCE_DEFINITION_NAME,
163
                PERSISTENCE_DEFINITION_NAME + "  persistence definition",
164
                    null, 
165
                    null
166
            );
167
            definition.addDynFieldObject("envelope").setMandatory(true).setClassOfValue(Envelope.class);
168
            definition.addDynFieldString("description").setMandatory(true);
169
        }        
170
    }
171

    
172
}