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 @ 44198

History | View | Annotate | Download (13.4 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
    @Override
114
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
115
        IDocumentWindow win = (IDocumentWindow) super.getMainWindow(doc, layout);
116
        if ( win == null ) {
117
            win = this.createDocumentWindow(doc);
118
            if ( layout != null && win != null ) {
119
                win.setWindowLayout(layout);
120
            }
121
            if( this.notifyObservers(NOTIFY_AFTER_CREATEMAINWINDOW,win).isCanceled() ) {
122
                return null;
123
            }
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
    @Override
133
    public IWindow getPropertiesWindow(Document doc) {
134
        IWindow win = super.getPropertiesWindow(doc);
135
        if( win == null ) {
136
            win = new ViewProperties((DefaultViewDocument) doc);
137
            if( this.notifyObservers(NOTIFY_AFTER_CREATEPROPERTIESWINDOW,win).isCanceled() ) {
138
                return null;
139
            }
140
        }
141
        if( this.notifyObservers(NOTIFY_AFTER_GETPROPERTIESWINDOW,win).isCanceled() ) {
142
            return null;
143
        }
144
        return win;
145
    }
146

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

    
206
    class ContextMenuActionAdapterToExtensionBuilder implements ExtensionBuilder {
207
            IContextMenuAction menuAction = null;
208
            ContextMenuActionAdapterToExtensionBuilder(IContextMenuAction menuAction) {
209
                    this.menuAction = menuAction;
210
            }
211
                public Object create() {
212
                        return this.menuAction;
213
                }
214
                public Object create(Object[] args) {
215
                        return this.menuAction;
216
                }
217
                public Object create(Map args) {
218
                        return this.menuAction;
219
                }
220
    }
221
    
222
    private static void initializeRegisterTOCActions() {
223
                ExtensionPointManager epManager = ToolsLocator.getExtensionPointManager();
224

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

    
247
        initializeRegisterTOCActions();
248
        
249
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CopyDocumentAction());
250
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CutDocumentAction());
251
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new PasteDocumentAction());
252

    
253
                   IconThemeHelper.registerIcon("document", "document-view-icon", ViewManager.class);
254
                   IconThemeHelper.registerIcon("document", "document-view-icon-sel", ViewManager.class);
255
                   
256
                   IconThemeHelper.registerIcon("cursor", "cursor-crux", ViewManager.class);
257
                   IconThemeHelper.registerIcon("cursor", "cursor-info-by-point", ViewManager.class);
258
                   IconThemeHelper.registerIcon("cursor", "cursor-pan", ViewManager.class);
259
                   IconThemeHelper.registerIcon("cursor", "cursor-query-area", ViewManager.class);
260
                   IconThemeHelper.registerIcon("cursor", "cursor-select-by-point", ViewManager.class);
261
        IconThemeHelper.registerIcon("cursor", "cursor-select-by-polygon", ViewManager.class);
262
        IconThemeHelper.registerIcon("cursor", "cursor-select-by-rectangle", ViewManager.class);
263
                   IconThemeHelper.registerIcon("cursor", "cursor-zoom-in", ViewManager.class);
264
                   IconThemeHelper.registerIcon("cursor", "cursor-zoom-out", ViewManager.class);
265

    
266
                   IconThemeHelper.registerIcon("layer", "layer-icon", ViewManager.class);
267
                   IconThemeHelper.registerIcon("layer", "layer-icon-group", ViewManager.class);
268
                   IconThemeHelper.registerIcon("layer", "layer-icon-vectorial", ViewManager.class);
269
                   IconThemeHelper.registerIcon("layer", "layer-icon-csv", ViewManager.class);
270
                   IconThemeHelper.registerIcon("layer", "layer-icon-dgn", ViewManager.class);
271
                   IconThemeHelper.registerIcon("layer", "layer-icon-dxf", ViewManager.class);
272
                   IconThemeHelper.registerIcon("layer", "layer-icon-postgresql", ViewManager.class);
273
                   IconThemeHelper.registerIcon("layer", "layer-icon-mysql", ViewManager.class);
274
                   IconThemeHelper.registerIcon("layer", "layer-icon-jdbc", ViewManager.class);
275
                   //IconThemeHelper.registerIcon("layer", "layer-icon-unavailable", ViewManager.class);
276
                   IconThemeHelper.registerIcon("layer", "layer-chk-unavailable", ViewManager.class);
277
                   IconThemeHelper.registerIcon("layer", "layer-chk-temporary", ViewManager.class);
278

    
279
                   IconThemeHelper.registerIcon("legend", "legend-overview-single-symbol", ViewManager.class);
280
                   IconThemeHelper.registerIcon("legend", "legend-overview-vectorial-interval", ViewManager.class);
281
                   IconThemeHelper.registerIcon("legend", "legend-overview-vectorial-unique-value", ViewManager.class);
282

    
283
                   MapContextManager mapContextMgr = MapContextLocator.getMapContextManager();
284
                   mapContextMgr.registerIconLayer("CSV", "layer-icon-csv");
285
                   mapContextMgr.registerIconLayer("DGN", "layer-icon-dgn");
286
                   mapContextMgr.registerIconLayer("DXF", "layer-icon-dxf");
287
                   mapContextMgr.registerIconLayer("jdbc", "layer-icon-jdbc");
288
                   mapContextMgr.registerIconLayer("PostgreSQL", "layer-icon-postgresql");
289
                   mapContextMgr.registerIconLayer("MySQL", "layer-icon-mysql");
290
                   
291
        if (factory.persistenceDefinition == null){
292
            factory.persistenceDefinition = manager.addDefinition(
293
                ViewDocument.class,
294
                PERSISTENCE_VIEW_DOCUMENT_DEFINITION_NAME,
295
                "Default view document persistence definition",
296
                null, 
297
                null
298
            );
299
            factory.persistenceDefinition.extend(manager.getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
300

    
301
            factory.persistenceDefinition.addDynFieldBoolean("useMapOverview").setMandatory(true);
302
            factory.persistenceDefinition.addDynFieldObject("mainMapContext").setClassOfValue(MapContext.class).setMandatory(true);
303
            factory.persistenceDefinition.addDynFieldObject("overviewMapContext").setClassOfValue(MapContext.class).setMandatory(false);
304
            factory.persistenceDefinition.addDynFieldObject("propertiesHelper").setClassOfValue(ExtendedPropertiesHelper.class)
305
                .setMandatory(false);
306
        
307
        }
308

    
309

    
310
    }
311

    
312
    @SuppressWarnings("rawtypes")
313
    public DynStruct getDefinition(String className) {
314

    
315
        if( this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
316
            return this.persistenceDefinition;
317
        }
318
        if( this.persistenceDefinition.getFullName().equalsIgnoreCase(className)) {
319
            return this.persistenceDefinition;
320
        }
321
        if( this.getDocumentClass().getName().equals(className) ) {
322
            return this.persistenceDefinition;
323
        }
324

    
325
        return null;
326
    }
327

    
328
    @SuppressWarnings("rawtypes")
329
    protected Class getDocumentClass() {
330
        return DefaultViewDocument.class;
331
    }
332

    
333
    public boolean manages(Object object) {
334
        return object instanceof ViewDocument;
335
    }
336
}