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