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 / documents / view / DefaultViewDocument.java @ 44455
History | View | Annotate | Download (8.36 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 |
package org.gvsig.app.project.documents.view; |
25 |
|
26 |
import javax.swing.JOptionPane; |
27 |
import org.gvsig.andami.PluginsLocator; |
28 |
import org.gvsig.andami.PluginsManager; |
29 |
import org.gvsig.app.ApplicationLocator; |
30 |
import org.gvsig.app.ApplicationManager; |
31 |
import org.gvsig.app.extension.AddLayer; |
32 |
import org.gvsig.app.gui.preferencespage.LayerOrderPage; |
33 |
import org.gvsig.app.project.ProjectPreferences; |
34 |
import org.gvsig.app.project.documents.DocumentManager; |
35 |
import org.gvsig.fmap.dal.exception.ReadException; |
36 |
import org.gvsig.fmap.geom.primitive.Envelope; |
37 |
import org.gvsig.fmap.mapcontext.MapContext; |
38 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
39 |
import org.gvsig.fmap.mapcontext.ViewPort; |
40 |
import org.gvsig.fmap.mapcontext.layers.BaseLayerCollectionListener; |
41 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
42 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent; |
43 |
import org.gvsig.fmap.mapcontext.layers.order.LayerOrderManager; |
44 |
import org.gvsig.tools.ToolsLocator; |
45 |
import org.gvsig.tools.dynobject.DynObject; |
46 |
import org.gvsig.tools.i18n.I18nManager; |
47 |
import org.gvsig.tools.persistence.PersistentState; |
48 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
49 |
import org.slf4j.Logger; |
50 |
import org.slf4j.LoggerFactory; |
51 |
|
52 |
/**
|
53 |
* Clase que representa una vista del proyecto
|
54 |
*
|
55 |
*/
|
56 |
public class DefaultViewDocument extends BaseViewDocument { |
57 |
|
58 |
private static Logger logger = LoggerFactory.getLogger(DefaultViewDocument.class); |
59 |
/**
|
60 |
*
|
61 |
*/
|
62 |
private static final long serialVersionUID = 676711512482334764L; |
63 |
|
64 |
public static final String PERSISTENCE_DEFINITION_NAME = "DefaultViewDocument"; |
65 |
|
66 |
private class SetViewPortExtentWhenAddLayerListener extends BaseLayerCollectionListener { |
67 |
|
68 |
private Thread th = null; |
69 |
private final ViewPort viewPort; |
70 |
|
71 |
public SetViewPortExtentWhenAddLayerListener(ViewPort viewPort) {
|
72 |
this.viewPort = viewPort;
|
73 |
} |
74 |
|
75 |
@Override
|
76 |
public synchronized void layerAdded(LayerCollectionEvent e) { |
77 |
if (th != null) { |
78 |
return;
|
79 |
} |
80 |
if (viewPort.getEnvelope() == null) { |
81 |
final FLayer layer = e.getAffectedLayer();
|
82 |
if (layer.isAvailable()) {
|
83 |
th = new Thread(new Runnable() { |
84 |
@Override
|
85 |
public void run() { |
86 |
try {
|
87 |
ApplicationManager application = ApplicationLocator.getManager(); |
88 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
89 |
application.message( |
90 |
i18n.getTranslation("_Calculating_envelope"),
|
91 |
JOptionPane.INFORMATION_MESSAGE
|
92 |
); |
93 |
Envelope envelope = layer.getFullEnvelope(); |
94 |
// Si nadie le ha asignado un envelope mientras se
|
95 |
// calculaba el inicial le asignamos el calculado.
|
96 |
if (viewPort.getEnvelope() == null) { |
97 |
viewPort.setEnvelope(envelope); |
98 |
} |
99 |
application.message( |
100 |
"",
|
101 |
JOptionPane.INFORMATION_MESSAGE
|
102 |
); |
103 |
} catch (ReadException ex) {
|
104 |
} |
105 |
} |
106 |
}, "AddLayerToView");
|
107 |
th.start(); |
108 |
// Forzamos a salir del scheduler para dar paso al nuevo
|
109 |
// thread que acabamos de crear. Prefiero el sleep al yield
|
110 |
// ya que este ultimo puede ser ignorado por el scheduler.
|
111 |
try {
|
112 |
Thread.sleep(1); |
113 |
} catch (InterruptedException ex) { |
114 |
} |
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
} |
120 |
|
121 |
public DefaultViewDocument() {
|
122 |
this(null); |
123 |
} |
124 |
|
125 |
public DefaultViewDocument(DocumentManager factory) {
|
126 |
super(factory);
|
127 |
|
128 |
ViewPort vp; |
129 |
|
130 |
ProjectPreferences preferences = (ProjectPreferences) ApplicationLocator.getManager().getPreferences("project");
|
131 |
|
132 |
MapContext viewMapContext = new MapContext(
|
133 |
new ViewPort(preferences.getDefaultProjection())
|
134 |
); |
135 |
vp = viewMapContext.getViewPort(); |
136 |
vp.setBackColor(preferences.getDefaultViewBackColor()); |
137 |
vp.setDistanceUnits(preferences.getDefaultDistanceUnits()); |
138 |
vp.setDistanceArea(preferences.getDefaultDistanceArea()); |
139 |
|
140 |
/*
|
141 |
* Another user preference: the order manager
|
142 |
*/
|
143 |
LayerOrderManager lom = getCurrentOrderManager(); |
144 |
viewMapContext.setOrderManager(lom); |
145 |
|
146 |
/*
|
147 |
* Not very elegant but we are forcing meters if projected
|
148 |
*/
|
149 |
if (viewMapContext.getProjection().isProjected()) {
|
150 |
vp.setMapUnits(1);
|
151 |
} else {
|
152 |
vp.setMapUnits(8);
|
153 |
} |
154 |
|
155 |
this.setMapContext(viewMapContext);
|
156 |
|
157 |
MapContext overviewMapContext = new MapContext(
|
158 |
new ViewPort(viewMapContext.getProjection())
|
159 |
); |
160 |
vp = viewMapContext.getViewPort(); |
161 |
vp.setBackColor(preferences.getDefaultViewBackColor()); |
162 |
this.setMapOverViewContext(overviewMapContext);
|
163 |
|
164 |
this.getMapContext().getLayers().addLayerCollectionListener(
|
165 |
new SetViewPortExtentWhenAddLayerListener(this.getMapContext().getViewPort()) |
166 |
); |
167 |
this.getMapOverViewContext().getLayers().addLayerCollectionListener(
|
168 |
new SetViewPortExtentWhenAddLayerListener(this.getMapOverViewContext().getViewPort()) |
169 |
); |
170 |
} |
171 |
|
172 |
/**
|
173 |
* Get current order manager from persistence
|
174 |
*
|
175 |
* @return
|
176 |
*/
|
177 |
private LayerOrderManager getCurrentOrderManager() {
|
178 |
|
179 |
DynObject props = this.getPluginProperties();
|
180 |
// current_layer_order_manager
|
181 |
Object val_obj = null; |
182 |
if (props.hasDynValue(LayerOrderPage.PREFERENCES_ID)) {
|
183 |
val_obj = props.getDynValue(LayerOrderPage.PREFERENCES_ID); |
184 |
} |
185 |
if (val_obj != null && val_obj instanceof LayerOrderManager) { |
186 |
LayerOrderManager lom = (LayerOrderManager) val_obj; |
187 |
return lom;
|
188 |
} else {
|
189 |
logger.info("No order manager found in persistence.");
|
190 |
return MapContextLocator.getDefaultOrderManager();
|
191 |
} |
192 |
|
193 |
} |
194 |
|
195 |
private DynObject getPluginProperties() {
|
196 |
PluginsManager pluginsManager = PluginsLocator.getManager(); |
197 |
return pluginsManager.getPlugin(AddLayer.class).getPluginProperties();
|
198 |
} |
199 |
|
200 |
public void saveToState(PersistentState state) throws PersistenceException { |
201 |
super.saveToState(state);
|
202 |
} |
203 |
|
204 |
public void loadFromState(PersistentState state) throws PersistenceException { |
205 |
super.loadFromState(state);
|
206 |
} |
207 |
|
208 |
public String exportDocumentAsText() { |
209 |
// FIXME: jjdc PersistentManager getStateAsText o similar
|
210 |
// PersistenceManager manager = ToolsLocator.getPersistenceManager();
|
211 |
// PersistentState state = manager.getState(this);
|
212 |
// return manager.getStateAsText(state);
|
213 |
return null; |
214 |
} |
215 |
|
216 |
public void setStateFromText(String text) { |
217 |
// FIXME: jjdc PersistentManager getStateFromText o similar
|
218 |
// PersistenceManager manager = ToolsLocator.getPersistenceManager();
|
219 |
// PersistentState state = manager.getStateFromText(text);
|
220 |
// this.loadFromState(state);
|
221 |
} |
222 |
|
223 |
} |