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 @ 44198
History | View | Annotate | Download (5.25 KB)
1 | 40558 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | |
25 | package org.gvsig.app.project; |
||
26 | |||
27 | import java.awt.geom.Rectangle2D; |
||
28 | |||
29 | import org.gvsig.fmap.geom.Geometry; |
||
30 | import org.gvsig.fmap.geom.GeometryLocator; |
||
31 | import org.gvsig.fmap.geom.GeometryManager; |
||
32 | import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
||
33 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
34 | import org.gvsig.fmap.geom.primitive.Point; |
||
35 | import org.gvsig.tools.ToolsLocator; |
||
36 | import org.gvsig.tools.dynobject.DynStruct; |
||
37 | import org.gvsig.tools.persistence.PersistenceManager; |
||
38 | import org.gvsig.tools.persistence.Persistent; |
||
39 | import org.gvsig.tools.persistence.PersistentState; |
||
40 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
41 | import org.slf4j.Logger; |
||
42 | import org.slf4j.LoggerFactory; |
||
43 | |||
44 | |||
45 | /**
|
||
46 | *
|
||
47 | * @author 2006-2009 Jose Manuel Vivo
|
||
48 | * @author 2005- Vicente Caballero
|
||
49 | * @author 2009- Joaquin del Cerro
|
||
50 | *
|
||
51 | */
|
||
52 | |||
53 | public class ProjectExtent implements Persistent { |
||
54 | private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
||
55 | private static final Logger logger = LoggerFactory.getLogger(ProjectExtent.class); |
||
56 | |||
57 | public static final String PERSISTENCE_DEFINITION_NAME = "org.gvsig.app.project.ProjectExtent"; |
||
58 | |||
59 | private Envelope extent;
|
||
60 | private String description; |
||
61 | |||
62 | public ProjectExtent() {
|
||
63 | this.extent = null; |
||
64 | this.description = null; |
||
65 | } |
||
66 | |||
67 | public String getDescription() { |
||
68 | return description;
|
||
69 | } |
||
70 | |||
71 | public void setDescription(String string) { |
||
72 | description = string; |
||
73 | } |
||
74 | |||
75 | public Envelope getExtent() {
|
||
76 | if( this.extent == null ) { |
||
77 | try {
|
||
78 | this.extent = geomManager.createEnvelope(Geometry.SUBTYPES.GEOM2D);
|
||
79 | } catch (CreateEnvelopeException e) {
|
||
80 | logger.warn("Can't create envelop.", e);
|
||
81 | } |
||
82 | } |
||
83 | return this.extent; |
||
84 | } |
||
85 | |||
86 | public void setExtent(Envelope envelope) { |
||
87 | this.extent = envelope;
|
||
88 | } |
||
89 | |||
90 | /**
|
||
91 | * @deprecated use {link {@link #setExtent(Envelope)}
|
||
92 | */
|
||
93 | public void setExtent(Rectangle2D rectangle2D) { |
||
94 | try {
|
||
95 | this.extent = geomManager.createEnvelope(
|
||
96 | rectangle2D.getMinX(), |
||
97 | rectangle2D.getMinY(), |
||
98 | rectangle2D.getMaxX(), |
||
99 | rectangle2D.getMaxY(), |
||
100 | Geometry.SUBTYPES.GEOM2D); |
||
101 | } catch (CreateEnvelopeException e) {
|
||
102 | logger.warn("Can't create envelope.", e);
|
||
103 | throw new RuntimeException("Can't create envelope.",e); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | public String getEncuadre() { |
||
108 | Point lower = extent.getLowerCorner();
|
||
109 | Point upper = extent.getUpperCorner();
|
||
110 | return lower.getX() + "," + lower.getY() + "," + upper.getX() + "," + upper.getY(); |
||
111 | } |
||
112 | |||
113 | public void setEncuadre(String encuadre) { |
||
114 | String[] coords = encuadre.split(","); |
||
115 | try {
|
||
116 | this.extent = geomManager.createEnvelope(
|
||
117 | Double.parseDouble(coords[0]), |
||
118 | Double.parseDouble(coords[1]), |
||
119 | Double.parseDouble(coords[2]), |
||
120 | Double.parseDouble(coords[3]), |
||
121 | Geometry.SUBTYPES.GEOM2D); |
||
122 | } catch (NumberFormatException e) { |
||
123 | logger.warn("Incorrect format string.", e);
|
||
124 | throw new RuntimeException("Incorrect format string.",e); |
||
125 | } catch (CreateEnvelopeException e) {
|
||
126 | logger.warn("Can't create envelope.", e);
|
||
127 | throw new RuntimeException("Can't create envelope.",e); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /**
|
||
132 | * @see java.lang.Object#toString()
|
||
133 | */
|
||
134 | public String toString() { |
||
135 | return description;
|
||
136 | } |
||
137 | |||
138 | public void loadFromState(PersistentState state) |
||
139 | throws PersistenceException {
|
||
140 | this.extent = (Envelope) state.get("envelope"); |
||
141 | this.description = state.getString("description"); |
||
142 | } |
||
143 | |||
144 | public void saveToState(PersistentState state) throws PersistenceException { |
||
145 | state.set("description", description);
|
||
146 | state.set("envelope", extent);
|
||
147 | } |
||
148 | |||
149 | /**
|
||
150 | *
|
||
151 | */
|
||
152 | public static void registerPersistent() { |
||
153 | |||
154 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
155 | DynStruct definition = manager.getDefinition(PERSISTENCE_DEFINITION_NAME); |
||
156 | if ( definition == null ){ |
||
157 | definition = manager.addDefinition( |
||
158 | ProjectExtent.class, |
||
159 | PERSISTENCE_DEFINITION_NAME, |
||
160 | PERSISTENCE_DEFINITION_NAME + " persistence definition",
|
||
161 | null,
|
||
162 | null
|
||
163 | ); |
||
164 | definition.addDynFieldObject("envelope").setMandatory(true).setClassOfValue(Envelope.class); |
||
165 | definition.addDynFieldString("description").setMandatory(true); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | } |