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 / ProjectExtent.java @ 40558

History | View | Annotate | Download (6.19 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2009 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
* 
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
* 
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
* 
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
42
* MA  02110-1301, USA.
43
* 
44
*/
45

    
46
/*
47
* AUTHORS (In addition to CIT):
48
* 2004-2009 IVER TI
49
*   
50
*/
51
package org.gvsig.app.project;
52

    
53
import java.awt.geom.Rectangle2D;
54

    
55
import org.gvsig.fmap.geom.Geometry;
56
import org.gvsig.fmap.geom.GeometryLocator;
57
import org.gvsig.fmap.geom.GeometryManager;
58
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
59
import org.gvsig.fmap.geom.primitive.Envelope;
60
import org.gvsig.fmap.geom.primitive.Point;
61
import org.gvsig.tools.ToolsLocator;
62
import org.gvsig.tools.dynobject.DynStruct;
63
import org.gvsig.tools.persistence.PersistenceManager;
64
import org.gvsig.tools.persistence.Persistent;
65
import org.gvsig.tools.persistence.PersistentState;
66
import org.gvsig.tools.persistence.exception.PersistenceException;
67
import org.slf4j.Logger;
68
import org.slf4j.LoggerFactory;
69

    
70

    
71
/**
72
 *
73
 * @author 2006-2009 Jose Manuel Vivo
74
 * @author 2005-         Vicente Caballero
75
 * @author 2009-         Joaquin del Cerro
76
 * 
77
 */
78

    
79
public class ProjectExtent implements Persistent {
80
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
81
        private static final Logger logger = LoggerFactory.getLogger(ProjectExtent.class);
82
        
83
    public static final String PERSISTENCE_DEFINITION_NAME = "org.gvsig.app.project.ProjectExtent";
84

    
85
        private Envelope extent;
86
    private String description;
87

    
88
    public ProjectExtent() {
89
            this.extent = null;
90
            this.description = null;
91
    }
92
    
93
    public String getDescription() {
94
        return description;
95
    }
96

    
97
    public void setDescription(String string) {
98
        description = string;
99
    }
100

    
101
    public Envelope getExtent() {
102
            if( this.extent == null ) {
103
                    try {
104
                                this.extent = geomManager.createEnvelope(Geometry.SUBTYPES.GEOM2D);
105
                        } catch (CreateEnvelopeException e) {
106
                                logger.warn("Can't create envelop.", e);
107
                        }
108
            }
109
            return this.extent;
110
    }
111

    
112
    public void setExtent(Envelope envelope) {
113
        this.extent = envelope;
114
    }
115

    
116
    /**
117
     * @deprecated use {link {@link #setExtent(Envelope)}
118
     */
119
    public void setExtent(Rectangle2D rectangle2D) {
120
        try {
121
                        this.extent = geomManager.createEnvelope(
122
                                        rectangle2D.getMinX(),
123
                                        rectangle2D.getMinY(),
124
                                        rectangle2D.getMaxX(),
125
                                        rectangle2D.getMaxY(),
126
                                        Geometry.SUBTYPES.GEOM2D);
127
                } catch (CreateEnvelopeException e) {
128
                        logger.warn("Can't create envelope.", e);
129
                        throw new RuntimeException("Can't create envelope.",e);
130
                }
131
    }
132

    
133
    public String getEncuadre() {
134
            Point lower = extent.getLowerCorner();
135
            Point upper = extent.getUpperCorner();
136
            return lower.getX() + "," + lower.getY() + "," + upper.getX() + "," + upper.getY(); 
137
    }
138

    
139
    public void setEncuadre(String encuadre) {
140
        String[] coords = encuadre.split(",");
141
        try {
142
                        this.extent = geomManager.createEnvelope(
143
                                        Double.parseDouble(coords[0]),
144
                                        Double.parseDouble(coords[1]),
145
                                        Double.parseDouble(coords[2]),
146
                                        Double.parseDouble(coords[3]),
147
                                        Geometry.SUBTYPES.GEOM2D);
148
                } catch (NumberFormatException e) {
149
                        logger.warn("Incorrect format string.", e);
150
                        throw new RuntimeException("Incorrect format string.",e);
151
                } catch (CreateEnvelopeException e) {
152
                        logger.warn("Can't create envelope.", e);
153
                        throw new RuntimeException("Can't create envelope.",e);
154
                }
155
    }
156

    
157
    /**
158
     * @see java.lang.Object#toString()
159
     */
160
    public String toString() {
161
        return description;
162
    }
163

    
164
    public void loadFromState(PersistentState state)
165
                        throws PersistenceException {
166
                this.extent = (Envelope) state.get("envelope");
167
                this.description = state.getString("description");
168
        }
169

    
170
        public void saveToState(PersistentState state) throws PersistenceException {
171
        state.set("description", description);
172
        state.set("envelope", extent);
173
        }
174

    
175
    /**
176
     * 
177
     */
178
    public static void registerPersistent() {
179
        
180
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
181
        DynStruct definition = manager.getDefinition(PERSISTENCE_DEFINITION_NAME);
182
        if ( definition == null ){
183
            definition = manager.addDefinition(
184
                ProjectExtent.class,
185
                PERSISTENCE_DEFINITION_NAME,
186
                PERSISTENCE_DEFINITION_NAME + "  persistence definition",
187
                    null, 
188
                    null
189
            );
190
            definition.addDynFieldObject("envelope").setMandatory(true).setClassOfValue(Envelope.class);
191
            definition.addDynFieldString("description").setMandatory(true);
192
        }        
193
    }
194

    
195
}