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 / ViewManager.java @ 42175

History | View | Annotate | Download (13 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 java.util.Map;
27

    
28
import javax.swing.ImageIcon;
29

    
30
import org.gvsig.andami.IconThemeHelper;
31
import org.gvsig.andami.PluginServices;
32
import org.gvsig.andami.PluginsLocator;
33
import org.gvsig.andami.actioninfo.ActionInfo;
34
import org.gvsig.andami.actioninfo.ActionInfoManager;
35
import org.gvsig.andami.ui.mdiManager.IWindow;
36
import org.gvsig.app.project.ProjectManager;
37
import org.gvsig.app.project.documents.AbstractDocument;
38
import org.gvsig.app.project.documents.AbstractDocumentManager;
39
import org.gvsig.app.project.documents.Document;
40
import org.gvsig.app.project.documents.actions.CopyDocumentAction;
41
import org.gvsig.app.project.documents.actions.CutDocumentAction;
42
import org.gvsig.app.project.documents.actions.PasteDocumentAction;
43
import org.gvsig.app.project.documents.gui.IDocumentWindow;
44
import org.gvsig.app.project.documents.gui.WindowLayout;
45
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
46
import org.gvsig.app.project.documents.view.gui.ViewProperties;
47
import org.gvsig.app.project.documents.view.toc.AbstractActionInfoAdapterToTocContextMenuAction;
48
import org.gvsig.fmap.mapcontext.MapContext;
49
import org.gvsig.fmap.mapcontext.MapContextLocator;
50
import org.gvsig.fmap.mapcontext.MapContextManager;
51
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
52
import org.gvsig.tools.ToolsLocator;
53
import org.gvsig.tools.dynobject.DynStruct;
54
import org.gvsig.tools.extensionpoint.ExtensionBuilder;
55
import org.gvsig.tools.extensionpoint.ExtensionPoint;
56
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
57
import org.gvsig.tools.persistence.PersistenceManager;
58
import org.slf4j.Logger;
59
import org.slf4j.LoggerFactory;
60

    
61
/**
62
 * Factory of View.
63
 *
64
 * @author 2005-         Vicente Caballero
65
 * @author 2009-         Joaquin del Cerro
66
 * 
67
 */
68
public class ViewManager extends AbstractDocumentManager {
69
    private static final Logger logger = LoggerFactory.getLogger(ViewManager.class);
70
    
71
    private static final String PERSISTENCE_VIEW_DOCUMENT_DEFINITION_NAME =
72
        "DefaultViewDocument";
73
    public static String TYPENAME = "project.document.view2d";
74
    private DynStruct persistenceDefinition;
75
    
76
    public ViewManager() {
77
            // Do nothing
78
    }
79
    
80
    public int getPriority() {
81
        return 0;
82
    }
83

    
84
    public ImageIcon getIcon() {
85
                return PluginServices.getIconTheme().get("document-view-icon");
86
    }
87

    
88
    public ImageIcon getIconSelected() {
89
                return PluginServices.getIconTheme().get("document-view-icon-sel");
90
    }
91

    
92
    public String getTitle() {
93
        return PluginServices.getText(this, "Vista");
94
    }
95

    
96
    public String getTypeName() {
97
        return TYPENAME;
98
    }
99
    
100
    public Class getMainWindowClass() {
101
        return DefaultViewPanel.class;
102
    }
103
    
104
    public AbstractDocument createDocument() {
105
        AbstractDocument doc = new DefaultViewDocument(this);
106
        if( this.notifyObservers(NOTIFY_AFTER_CREATEDOCUMENT,doc).isCanceled() ) {
107
            return null;
108
        }
109
            return doc;        
110
        
111
    }
112

    
113
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
114
        IDocumentWindow win;
115

    
116
        win = (IDocumentWindow) PluginServices.getMDIManager().getSingletonWindow(getMainWindowClass(), doc);
117
        if ( win != null ) {
118
            // The view window document is already created, return it.
119
            return win;
120
        }
121
        win = this.createDocumentWindow(doc);
122
        if ( layout != null && win != null ) {
123
            win.setWindowLayout(layout);
124
        }
125
        if( this.notifyObservers(NOTIFY_AFTER_GETMAINWINDOW,win).isCanceled() ) {
126
            return null;
127
        }
128
        ((AbstractDocument) doc).raiseEventCreateWindow(win);
129
        return win;
130
    }
131

    
132
    public IWindow getPropertiesWindow(Document doc) {
133
        IWindow win = new ViewProperties((DefaultViewDocument) doc);
134
        if( this.notifyObservers(NOTIFY_AFTER_GETPROPERTIESWINDOW,win).isCanceled() ) {
135
            return null;
136
        }
137
        return win;
138
    }
139

    
140
    public void addTOCContextAction(String theAction) {
141
            ActionInfoManager actionManager = PluginsLocator.getActionInfoManager(); 
142
            ActionInfo action = actionManager.getAction(theAction);
143
            if( action==null ) {
144
                    String errmsg = "Action '"+theAction+"' not found";
145
                    logger.info(errmsg);
146
                    throw new RuntimeException(errmsg);
147
            }
148
            this.addTOCContextAction(action);
149
    }
150
    
151
    public void addTOCContextAction(String theAction, String group, int groupOrder, int order) {
152
            ActionInfoManager actionManager = PluginsLocator.getActionInfoManager(); 
153
            ActionInfo action = actionManager.getAction(theAction);
154
            if( action==null ) {
155
                    String errmsg = "Action '"+theAction+"' not found";
156
                    logger.info(errmsg);
157
                    throw new RuntimeException(errmsg);
158
            }
159
            this.addTOCContextAction(
160
                action.getName(), 
161
                new ActionInfoAdapterToContextMenuAction(action, group, groupOrder,0)
162
        );
163
    }
164
    
165
    public void addTOCContextAction(ActionInfo action) {
166
            this.addTOCContextAction(action.getName(), new ActionInfoAdapterToContextMenuAction(action, "default", 10000));
167
    }
168
    
169
    public void addTOCContextAction(ActionInfo action, String group, int groupOrder) {
170
            this.addTOCContextAction(action.getName(), new ActionInfoAdapterToContextMenuAction(action, group, groupOrder));
171
    }
172
    
173
    class ActionInfoAdapterToContextMenuAction extends AbstractActionInfoAdapterToTocContextMenuAction {
174
            
175
            ActionInfoAdapterToContextMenuAction(ActionInfo action, String group, int groupOrder) {
176
            super(action, group, groupOrder);
177
            }
178
            ActionInfoAdapterToContextMenuAction(ActionInfo action, String group, int groupOrder, int order) {
179
            super(action, group, groupOrder,order);
180
            }
181
    }
182
    
183
    /**
184
     * @deprecated use addTOCContextAction(ActionInfo action, String group, int groupOrder)
185
     * @param id
186
     * @param action 
187
     */
188
    public void addTOCContextAction(String id, IContextMenuAction action) {
189
            initializeRegisterTOCActions();
190
            ExtensionPoint exPoint = ToolsLocator.getExtensionPointManager().add(
191
                            "View_TocActions", "");
192
            if( action instanceof ExtensionBuilder ) {
193
                    exPoint.append(id, "", (ExtensionBuilder)action);
194
                    return;
195
            }
196
                exPoint.append(id, "", new ContextMenuActionAdapterToExtensionBuilder(action));
197
    }
198

    
199
    class ContextMenuActionAdapterToExtensionBuilder implements ExtensionBuilder {
200
            IContextMenuAction menuAction = null;
201
            ContextMenuActionAdapterToExtensionBuilder(IContextMenuAction menuAction) {
202
                    this.menuAction = menuAction;
203
            }
204
                public Object create() {
205
                        return this.menuAction;
206
                }
207
                public Object create(Object[] args) {
208
                        return this.menuAction;
209
                }
210
                public Object create(Map args) {
211
                        return this.menuAction;
212
                }
213
    }
214
    
215
    private static void initializeRegisterTOCActions() {
216
                ExtensionPointManager epManager = ToolsLocator.getExtensionPointManager();
217

    
218
                if (!epManager.has("View_TocActions")) {
219
                        epManager.add(
220
                                        "View_TocActions",
221
                                        "Context menu options of the TOC " +
222
                                                " in the view window "+
223
                                                "(register instances of " +
224
                                                "org.gvsig.app.gui.toc.AbstractTocContextMenuAction)"
225
                        );
226
                }
227
    }
228
    
229
    /**
230
     * Registers in the points of extension the Factory with alias.
231
     *
232
     */
233
    public static void register() {
234
        ViewManager factory = new ViewManager();
235
            ProjectManager.getInstance().registerDocumentFactory(factory);
236
            
237
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
238
        manager.registerFactory(factory);
239

    
240
        initializeRegisterTOCActions();
241
        
242
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CopyDocumentAction());
243
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CutDocumentAction());
244
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new PasteDocumentAction());
245

    
246
                   IconThemeHelper.registerIcon("document", "document-view-icon", ViewManager.class);
247
                   IconThemeHelper.registerIcon("document", "document-view-icon-sel", ViewManager.class);
248
                   
249
                   IconThemeHelper.registerIcon("cursor", "cursor-crux", ViewManager.class);
250
                   IconThemeHelper.registerIcon("cursor", "cursor-info-by-point", ViewManager.class);
251
                   IconThemeHelper.registerIcon("cursor", "cursor-pan", ViewManager.class);
252
                   IconThemeHelper.registerIcon("cursor", "cursor-query-area", ViewManager.class);
253
                   IconThemeHelper.registerIcon("cursor", "cursor-select-by-point", ViewManager.class);
254
        IconThemeHelper.registerIcon("cursor", "cursor-select-by-polygon", ViewManager.class);
255
        IconThemeHelper.registerIcon("cursor", "cursor-select-by-rectangle", ViewManager.class);
256
                   IconThemeHelper.registerIcon("cursor", "cursor-zoom-in", ViewManager.class);
257
                   IconThemeHelper.registerIcon("cursor", "cursor-zoom-out", ViewManager.class);
258

    
259
                   IconThemeHelper.registerIcon("layer", "layer-icon", ViewManager.class);
260
                   IconThemeHelper.registerIcon("layer", "layer-icon-group", ViewManager.class);
261
                   IconThemeHelper.registerIcon("layer", "layer-icon-vectorial", ViewManager.class);
262
                   IconThemeHelper.registerIcon("layer", "layer-icon-dgn", ViewManager.class);
263
                   IconThemeHelper.registerIcon("layer", "layer-icon-dxf", ViewManager.class);
264
                   IconThemeHelper.registerIcon("layer", "layer-icon-postgresql", ViewManager.class);
265
                   IconThemeHelper.registerIcon("layer", "layer-icon-mysql", ViewManager.class);
266
                   IconThemeHelper.registerIcon("layer", "layer-icon-jdbc", ViewManager.class);
267
                   //IconThemeHelper.registerIcon("layer", "layer-icon-unavailable", ViewManager.class);
268
                   IconThemeHelper.registerIcon("layer", "layer-chk-unavailable", ViewManager.class);
269
                   IconThemeHelper.registerIcon("layer", "layer-chk-temporary", ViewManager.class);
270

    
271
                   IconThemeHelper.registerIcon("legend", "legend-overview-single-symbol", ViewManager.class);
272
                   IconThemeHelper.registerIcon("legend", "legend-overview-vectorial-interval", ViewManager.class);
273
                   IconThemeHelper.registerIcon("legend", "legend-overview-vectorial-unique-value", ViewManager.class);
274

    
275
                   MapContextManager mapContextMgr = MapContextLocator.getMapContextManager();
276
                   mapContextMgr.registerIconLayer("DGN", "layer-icon-dgn");
277
                   mapContextMgr.registerIconLayer("DXF", "layer-icon-dxf");
278
                   mapContextMgr.registerIconLayer("jdbc", "layer-icon-jdbc");
279
                   mapContextMgr.registerIconLayer("PostgreSQL", "layer-icon-postgresql");
280
                   mapContextMgr.registerIconLayer("MySQL", "layer-icon-mysql");
281
                   
282
        if (factory.persistenceDefinition == null){
283
            factory.persistenceDefinition = manager.addDefinition(
284
                ViewDocument.class,
285
                PERSISTENCE_VIEW_DOCUMENT_DEFINITION_NAME,
286
                "Default view document persistence definition",
287
                null, 
288
                null
289
            );
290
            factory.persistenceDefinition.extend(manager.getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
291

    
292
            factory.persistenceDefinition.addDynFieldBoolean("useMapOverview").setMandatory(true);
293
            factory.persistenceDefinition.addDynFieldObject("mainMapContext").setClassOfValue(MapContext.class).setMandatory(true);
294
            factory.persistenceDefinition.addDynFieldObject("overviewMapContext").setClassOfValue(MapContext.class).setMandatory(false);
295
            factory.persistenceDefinition.addDynFieldObject("propertiesHelper").setClassOfValue(ExtendedPropertiesHelper.class)
296
                .setMandatory(false);
297
        
298
        }
299

    
300

    
301
    }
302

    
303
    @SuppressWarnings("rawtypes")
304
    public DynStruct getDefinition(String className) {
305

    
306
        if( this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
307
            return this.persistenceDefinition;
308
        }
309
        if( this.persistenceDefinition.getFullName().equalsIgnoreCase(className)) {
310
            return this.persistenceDefinition;
311
        }
312
        if( this.getDocumentClass().getName().equals(className) ) {
313
            return this.persistenceDefinition;
314
        }
315

    
316
        return null;
317
    }
318

    
319
    @SuppressWarnings("rawtypes")
320
    protected Class getDocumentClass() {
321
        return DefaultViewDocument.class;
322
    }
323

    
324
    public boolean manages(Object object) {
325
        return object instanceof ViewDocument;
326
    }
327
}