Statistics
| Revision:

gvsig-3d / 2.1 / trunk / org.gvsig.gvsig3d / org.gvsig.gvsig3d.app / org.gvsig.gvsig3d.app.commons / src / main / java / org / gvsig / gvsig3d / app / extension / View3DManager.java @ 385

History | View | Annotate | Download (9.98 KB)

1
/* gvSIG 3D extension for gvSIG
2
 *
3
 * Copyright (C) 2012 Prodevelop.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Prodevelop, S.L.
22
 *   Pza. Don Juan de Villarrasa, 14 - 5
23
 *   46001 Valencia
24
 *   Spain
25
 *
26
 *   +34 963 510 612
27
 *   +34 963 510 968
28
 *   prode@prodevelop.es
29
 *   http://www.prodevelop.es
30
 */
31
/*
32
 * AUTHORS:
33
 * 2012 AI2 - Instituto Universitario de Automatica e Informatica Industrial.
34
 * Universitat Politecnica de Valencia (UPV)
35
 * http://www.ai2.upv.es
36
 */
37

    
38

    
39
package org.gvsig.gvsig3d.app.extension;
40

    
41
import java.text.DateFormat;
42
import java.util.Date;
43

    
44
import javax.swing.ImageIcon;
45

    
46
import org.gvsig.andami.PluginServices;
47
import org.gvsig.andami.ui.mdiManager.IWindow;
48
import org.gvsig.app.project.DefaultProject;
49
import org.gvsig.app.project.ProjectManager;
50
import org.gvsig.app.project.documents.AbstractDocument;
51
import org.gvsig.app.project.documents.AbstractDocumentManager;
52
import org.gvsig.app.project.documents.Document;
53
import org.gvsig.app.project.documents.actions.CopyDocumentAction;
54
import org.gvsig.app.project.documents.actions.CutDocumentAction;
55
import org.gvsig.app.project.documents.actions.PasteDocumentAction;
56
import org.gvsig.app.project.documents.gui.IDocumentWindow;
57
import org.gvsig.app.project.documents.gui.WindowLayout;
58
import org.gvsig.app.project.documents.view.ViewDocument;
59
import org.gvsig.fmap.mapcontext.MapContext;
60
import org.gvsig.fmap.mapcontext.ViewPort;
61
import org.gvsig.gvsig3d.Gvsig3DLocator;
62
import org.gvsig.gvsig3d.Gvsig3DManager;
63
import org.gvsig.gvsig3d.app.view.ViewProperties3D;
64
import org.gvsig.gvsig3d.impl.map3d.MapContext3DImpl;
65
import org.gvsig.gvsig3d.map3d.layers.FLayers3D;
66
import org.gvsig.osgvp.viewer.IViewerContainer;
67
import org.gvsig.osgvp.viewer.ViewerFactory;
68
import org.gvsig.tools.ToolsLocator;
69
import org.gvsig.tools.dynobject.DynStruct;
70
import org.gvsig.tools.persistence.PersistenceManager;
71
import org.gvsig.tools.persistence.PersistentState;
72
import org.gvsig.tools.persistence.exception.PersistenceException;
73

    
74
/**
75
 * @author Jesus Zarzoso- jzarzoso@ai2.upv.es
76
 * @version $Id$
77
 * 
78
 */
79
public class View3DManager extends AbstractDocumentManager { 
80

    
81
        private static final String PERSISTENCE_VIEW3D_DOCUMENT_DEFINITION_NAME = "DefaultView3DDocument";
82
        public static String TYPENAME = "project.document.view3d";
83
        private DynStruct persistenceDefinition;
84
        private Gvsig3DManager _manager;
85

    
86
        public View3DManager() {
87
                // Do nothing
88
        }
89

    
90
        public int getPriority() {
91
                return 10;
92
        }
93

    
94
        public ImageIcon getIcon() {
95
                return PluginServices.getIconTheme().get("document-view3d-icon");
96
        }
97

    
98
        public ImageIcon getIconSelected() {
99
                return PluginServices.getIconTheme().get("document-view3d-icon-sel");
100
        }
101

    
102
        public String getTitle() {
103
                return PluginServices.getText(this, "show_3Dview");
104
        }
105

    
106
        public String getTypeName() {
107
                return TYPENAME;
108
        }
109

    
110
        public AbstractDocument createDocument() {
111

    
112
                String viewName = "View3D";
113
                String aux = PluginServices.getText(this, "untitled");
114
                int numViews = ProjectManager.getInstance().getCurrentProject()
115
                                .getDocuments(TYPENAME).size();
116
                viewName = aux + " - " + numViews++;
117

    
118
                // Buscamos si alguna vista ya ten?a este nombre:
119
                while (ProjectManager.getInstance().getCurrentProject()
120
                                .getDocument(viewName, TYPENAME) != null) {
121
                        viewName = aux + " - " + numViews++;
122
                }
123

    
124
                _manager = Gvsig3DLocator.getManager();
125

    
126
                DefaultView3DDocument viewDocument = new DefaultView3DDocument(this);
127

    
128
                ViewPort vp = _manager.createDefaultViewPort3DInstance(DefaultProject
129
                                .getPreferences().getDefaultProjection());
130
                FLayers3D layers = _manager.createDefaultFLayers3DInstance(vp);
131
                viewDocument.setMapContext(layers.getMapContext());
132
                viewDocument.setMapOverViewContext(new MapContext(new ViewPort(layers
133
                                .getMapContext().getProjection())));
134
                viewDocument.setName(viewName);
135
                viewDocument.setCreationDate(DateFormat.getInstance()
136
                                .format(new Date()));
137

    
138
                setView3DProperties(viewDocument);
139

    
140
                return viewDocument;
141
        }
142

    
143
        public IWindow getMainWindow(Document doc, WindowLayout layout) {
144

    
145
                DefaultView3DDocument viewDoc = (DefaultView3DDocument) doc;
146

    
147
                IDocumentWindow docview;
148

    
149
                docview = (IDocumentWindow) PluginServices.getMDIManager()
150
                                .getSingletonWindow(getMainWindowClass(), doc);
151
                if (docview != null) {
152
                        // The view window document is already created, return it.
153
                        return docview;
154
                }
155

    
156
                DefaultView3DPanel view = new DefaultView3DPanel();
157

    
158
                ViewPort vp = new ViewPort();
159

    
160
                IViewerContainer canvas3d = viewDoc.getOrCreateCanvas3D();
161
                view.setCanvas3d(canvas3d);
162
                ViewerFactory.getInstance().addViewerToAnimator(canvas3d);
163
                ViewerFactory.getInstance().startAnimator();
164
                view.setTerrain(viewDoc.getTerrain());
165
                view.setDataManager(viewDoc.getTerrainDataManager());
166
                view.setLayerManager(viewDoc.getTerrainLayerManager());
167

    
168
                view.setNavMode(_manager.createDefaultNavigationModeInstance(viewDoc
169
                                .getTerrainViewer().getTerrainCameraManipulator()));
170

    
171
                if (layout != null) {
172
                        view.setWindowLayout(layout);
173
                }
174
                view.initialize();
175
                view.setDocument(doc);
176
                if (viewDoc.getProjectCamera() != null) {
177

    
178
                        viewDoc.getOrCreateCanvas3D().getOSGViewer()
179
                                        .setCamera(viewDoc.getProjectCamera().getCamera());
180

    
181
                }
182
                ((AbstractDocument) doc).raiseEventCreateWindow(view);
183

    
184
                return view;
185

    
186
        }
187

    
188
        private void setView3DProperties(DefaultView3DDocument prView) {
189

    
190
                ViewProperties3D viewProperties = new ViewProperties3D(prView, true);
191
                PluginServices.getMDIManager().addWindow(viewProperties);
192
        }
193

    
194
        public IWindow getPropertiesWindow(Document doc) {
195
                return new ViewProperties3D((DefaultView3DDocument) doc, true);
196
        }
197

    
198
        /**
199
         * Registers in the points of extension the Factory with alias.
200
         * 
201
         */
202
        public static void register() {
203
                View3DManager factory = new View3DManager();
204
                ProjectManager.getInstance().registerDocumentFactory(factory);
205

    
206
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
207
                manager.registerFactory(factory);
208

    
209
                ProjectManager.getInstance().registerDocumentAction(TYPENAME,
210
                                new CopyDocumentAction());
211
                ProjectManager.getInstance().registerDocumentAction(TYPENAME,
212
                                new CutDocumentAction());
213
                ProjectManager.getInstance().registerDocumentAction(TYPENAME,
214
                                new PasteDocumentAction());
215

    
216
                PluginServices.getIconTheme().registerDefault(
217
                                "document-view3d-icon",
218
                                View3DManager.class.getClassLoader().getResource(
219
                                                "images/ProjectView3D.png"));
220
                PluginServices.getIconTheme().registerDefault(
221
                                "document-view3d-icon-sel",
222
                                View3DManager.class.getClassLoader().getResource(
223
                                                "images/ProjectView3DSel.png"));
224

    
225
                if (factory.persistenceDefinition == null) {
226
                        factory.persistenceDefinition = manager.addDefinition(
227
                                        ViewDocument.class,
228
                                        PERSISTENCE_VIEW3D_DOCUMENT_DEFINITION_NAME,
229
                                        "Default view 3D document persistence definition", null,
230
                                        null);
231
                        factory.persistenceDefinition
232
                                        .extend(manager
233
                                                        .getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
234

    
235
                        factory.persistenceDefinition.addDynFieldBoolean("useMapOverview")
236
                                        .setMandatory(true);
237
                        factory.persistenceDefinition.addDynFieldObject("mainMapContext")
238
                                        .setClassOfValue(MapContext3DImpl.class).setMandatory(true);
239
                        factory.persistenceDefinition
240
                                        .addDynFieldObject("overviewMapContext")
241
                                        .setClassOfValue(MapContext.class).setMandatory(false);
242

    
243
                }
244

    
245
        }
246

    
247
        @SuppressWarnings("rawtypes")
248
        public DynStruct getDefinition(String className) {
249

    
250
                if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
251
                        return this.persistenceDefinition;
252
                }
253
                if (this.persistenceDefinition.getFullName()
254
                                .equalsIgnoreCase(className)) {
255
                        return this.persistenceDefinition;
256
                }
257
                if (this.getDocumentClass().getName().equals(className)) {
258
                        return this.persistenceDefinition;
259
                }
260

    
261
                Class theClass;
262
                try {
263
                        theClass = (Class) Class.forName(className);
264
                } catch (ClassNotFoundException e) {
265
                        return null;
266
                }
267
                if (this.manages(theClass)) {
268
                        return this.persistenceDefinition;
269
                }
270

    
271
                return null;
272

    
273
        }
274

    
275
        @SuppressWarnings("rawtypes")
276
        protected Class getDocumentClass() {
277
                return DefaultView3DDocument.class;
278
        }
279

    
280
        public Class getMainWindowClass() {
281

    
282
                return DefaultView3DPanel.class;
283
        }
284

    
285
        public boolean manages(Object object) {
286
                return object instanceof ViewDocument;
287
        }
288

    
289
        public AbstractDocument createDocumentFromState(PersistentState state) {
290

    
291
                String viewName = "View3D";
292
                String aux = PluginServices.getText(this, "untitled");
293
                int numViews = ProjectManager.getInstance().getCurrentProject()
294
                                .getDocuments(TYPENAME).size();
295
                viewName = aux + " - " + numViews++;
296

    
297
                // Buscamos si alguna vista ya ten?a este nombre:
298
                while (ProjectManager.getInstance().getCurrentProject()
299
                                .getDocument(viewName, TYPENAME) != null) {
300
                        viewName = aux + " - " + numViews++;
301
                }
302

    
303
                _manager = Gvsig3DLocator.getManager();
304

    
305
                DefaultView3DDocument viewDocument = new DefaultView3DDocument(this);
306

    
307
                ViewPort vp = _manager.createDefaultViewPort3DInstance(DefaultProject
308
                                .getPreferences().getDefaultProjection());
309
                FLayers3D layers = _manager.createDefaultFLayers3DInstance(vp);
310
                viewDocument.setMapContext(layers.getMapContext());
311
                viewDocument.setMapOverViewContext(new MapContext(new ViewPort(layers
312
                                .getMapContext().getProjection())));
313
                viewDocument.setName(viewName);
314
                viewDocument.setCreationDate(DateFormat.getInstance()
315
                                .format(new Date()));
316

    
317
                return viewDocument;
318
        }
319

    
320
        public Object createFromState(PersistentState state)
321
                        throws PersistenceException {
322

    
323
                Document doc = this.createDocumentFromState(state);
324
                return doc;
325
        }
326

    
327
}