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 / documents / view / DefaultViewDocument.java @ 44531

History | View | Annotate | Download (8.36 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
package org.gvsig.app.project.documents.view;
25
26 43640 jjdelcerro
import javax.swing.JOptionPane;
27 40713 jldominguez
import org.gvsig.andami.PluginsLocator;
28
import org.gvsig.andami.PluginsManager;
29 40435 jjdelcerro
import org.gvsig.app.ApplicationLocator;
30 43640 jjdelcerro
import org.gvsig.app.ApplicationManager;
31 40713 jldominguez
import org.gvsig.app.extension.AddLayer;
32
import org.gvsig.app.gui.preferencespage.LayerOrderPage;
33 40435 jjdelcerro
import org.gvsig.app.project.ProjectPreferences;
34
import org.gvsig.app.project.documents.DocumentManager;
35 43640 jjdelcerro
import org.gvsig.fmap.dal.exception.ReadException;
36
import org.gvsig.fmap.geom.primitive.Envelope;
37 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContext;
38 40713 jldominguez
import org.gvsig.fmap.mapcontext.MapContextLocator;
39 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.ViewPort;
40 43640 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.BaseLayerCollectionListener;
41
import org.gvsig.fmap.mapcontext.layers.FLayer;
42
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent;
43 40713 jldominguez
import org.gvsig.fmap.mapcontext.layers.order.LayerOrderManager;
44 40435 jjdelcerro
import org.gvsig.tools.ToolsLocator;
45 40713 jldominguez
import org.gvsig.tools.dynobject.DynObject;
46 43640 jjdelcerro
import org.gvsig.tools.i18n.I18nManager;
47 40435 jjdelcerro
import org.gvsig.tools.persistence.PersistentState;
48
import org.gvsig.tools.persistence.exception.PersistenceException;
49 40713 jldominguez
import org.slf4j.Logger;
50
import org.slf4j.LoggerFactory;
51 40435 jjdelcerro
52
/**
53
 * Clase que representa una vista del proyecto
54 44455 omartinez
 *
55 40435 jjdelcerro
 */
56
public class DefaultViewDocument extends BaseViewDocument {
57
58 44455 omartinez
    private static Logger logger = LoggerFactory.getLogger(DefaultViewDocument.class);
59
    /**
60
     *
61
     */
62
    private static final long serialVersionUID = 676711512482334764L;
63 40435 jjdelcerro
64 44455 omartinez
    public static final String PERSISTENCE_DEFINITION_NAME = "DefaultViewDocument";
65
66 43686 jjdelcerro
    private class SetViewPortExtentWhenAddLayerListener extends BaseLayerCollectionListener {
67 40435 jjdelcerro
68 43640 jjdelcerro
        private Thread th = null;
69 43686 jjdelcerro
        private final ViewPort viewPort;
70 44455 omartinez
71 43686 jjdelcerro
        public SetViewPortExtentWhenAddLayerListener(ViewPort viewPort) {
72
            this.viewPort = viewPort;
73
        }
74 44455 omartinez
75 43640 jjdelcerro
        @Override
76
        public synchronized void layerAdded(LayerCollectionEvent e) {
77 44455 omartinez
            if (th != null) {
78 43640 jjdelcerro
                return;
79
            }
80
            if (viewPort.getEnvelope() == null) {
81
                final FLayer layer = e.getAffectedLayer();
82 44455 omartinez
                if (layer.isAvailable()) {
83 43640 jjdelcerro
                    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 44455 omartinez
                                        i18n.getTranslation("_Calculating_envelope"),
91
                                        JOptionPane.INFORMATION_MESSAGE
92 43640 jjdelcerro
                                );
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 44455 omartinez
                                        "",
101
                                        JOptionPane.INFORMATION_MESSAGE
102 43640 jjdelcerro
                                );
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 44455 omartinez
119 43640 jjdelcerro
    }
120
121 44455 omartinez
    public DefaultViewDocument() {
122
        this(null);
123
    }
124 40435 jjdelcerro
125 44455 omartinez
    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 40713 jldominguez
                 * Another user preference: the order manager
142 44455 omartinez
         */
143
        LayerOrderManager lom = getCurrentOrderManager();
144
        viewMapContext.setOrderManager(lom);
145
146
        /*
147 40435 jjdelcerro
                 * Not very elegant but we are forcing meters if projected
148 44455 omartinez
         */
149
        if (viewMapContext.getProjection().isProjected()) {
150
            vp.setMapUnits(1);
151
        } else {
152 40435 jjdelcerro
            vp.setMapUnits(8);
153 44455 omartinez
        }
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 43686 jjdelcerro
        this.getMapContext().getLayers().addLayerCollectionListener(
165 44455 omartinez
                new SetViewPortExtentWhenAddLayerListener(this.getMapContext().getViewPort())
166 43686 jjdelcerro
        );
167
        this.getMapOverViewContext().getLayers().addLayerCollectionListener(
168 44455 omartinez
                new SetViewPortExtentWhenAddLayerListener(this.getMapOverViewContext().getViewPort())
169 43686 jjdelcerro
        );
170 44455 omartinez
    }
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 40713 jldominguez
        PluginsManager pluginsManager = PluginsLocator.getManager();
197
        return pluginsManager.getPlugin(AddLayer.class).getPluginProperties();
198 44455 omartinez
    }
199 40435 jjdelcerro
200 44455 omartinez
    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 40435 jjdelcerro
//                 PersistenceManager manager = ToolsLocator.getPersistenceManager();
211
//                 PersistentState state = manager.getState(this);
212
//                 return manager.getStateAsText(state);
213 44455 omartinez
        return null;
214
    }
215 40435 jjdelcerro
216 44455 omartinez
    public void setStateFromText(String text) {
217
        // FIXME: jjdc PersistentManager getStateFromText o similar
218 40435 jjdelcerro
//                 PersistenceManager manager = ToolsLocator.getPersistenceManager();
219
//                 PersistentState state = manager.getStateFromText(text);
220
//                 this.loadFromState(state);
221 44455 omartinez
    }
222 40435 jjdelcerro
223
}