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

History | View | Annotate | Download (18.3 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.HashMap;
27
import java.util.Iterator;
28
import java.util.List;
29
import java.util.Map;
30
import java.util.function.Predicate;
31

    
32
import javax.swing.ImageIcon;
33
import org.apache.commons.lang3.StringUtils;
34

    
35
import org.gvsig.andami.IconThemeHelper;
36
import org.gvsig.andami.PluginServices;
37
import org.gvsig.andami.PluginsLocator;
38
import org.gvsig.andami.actioninfo.ActionInfo;
39
import org.gvsig.andami.actioninfo.ActionInfoManager;
40
import org.gvsig.andami.ui.mdiManager.IWindow;
41
import org.gvsig.app.project.Project;
42
import org.gvsig.app.project.ProjectManager;
43
import org.gvsig.app.project.documents.AbstractDocument;
44
import org.gvsig.app.project.documents.AbstractDocumentManager;
45
import org.gvsig.app.project.documents.Document;
46
import org.gvsig.app.project.documents.actions.CopyDocumentAction;
47
import org.gvsig.app.project.documents.actions.CutDocumentAction;
48
import org.gvsig.app.project.documents.actions.PasteDocumentAction;
49
import org.gvsig.app.project.documents.gui.IDocumentWindow;
50
import org.gvsig.app.project.documents.gui.WindowLayout;
51
import org.gvsig.app.project.documents.view.dalactions.ViewZoomAction.ViewZoomActionFactory;
52
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
53
import org.gvsig.app.project.documents.view.gui.ViewProperties;
54
import org.gvsig.app.project.documents.view.toc.AbstractActionInfoAdapterToTocContextMenuAction;
55
import org.gvsig.fmap.dal.AbstractStoresRepository;
56
import org.gvsig.fmap.dal.DALLocator;
57
import org.gvsig.fmap.dal.DataManager;
58
import org.gvsig.fmap.dal.DataStore;
59
import org.gvsig.fmap.dal.DataStoreParameters;
60
import org.gvsig.fmap.dal.feature.FeatureStore;
61
import org.gvsig.fmap.mapcontext.MapContext;
62
import org.gvsig.fmap.mapcontext.MapContextLocator;
63
import org.gvsig.fmap.mapcontext.MapContextManager;
64
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
65
import org.gvsig.fmap.mapcontext.layers.FLayer;
66
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
67
import org.gvsig.tools.ToolsLocator;
68
import org.gvsig.tools.dynobject.DynStruct;
69
import org.gvsig.tools.extensionpoint.ExtensionBuilder;
70
import org.gvsig.tools.extensionpoint.ExtensionPoint;
71
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
72
import org.gvsig.tools.persistence.PersistenceManager;
73
import org.gvsig.tools.util.UnmodifiableBasicMap;
74
import org.gvsig.tools.util.UnmodifiableBasicSet;
75
import org.gvsig.tools.util.UnmodifiableBasicSetAdapter;
76
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
78

    
79
/**
80
 * Factory of View.
81
 *
82
 * @author 2005- Vicente Caballero
83
 * @author 2009- Joaquin del Cerro
84
 *
85
 */
86
public class ViewManager extends AbstractDocumentManager {
87

    
88
    private static final Logger LOGGER = LoggerFactory.getLogger(ViewManager.class);
89

    
90
    private static class ViewDocumentStoresRepository extends AbstractStoresRepository {
91

    
92
        public ViewDocumentStoresRepository(String name) {
93
            super(name);
94
        }
95

    
96
        private Map<String, DataStoreParameters> getAll(Predicate<DataStore> filter, int limit) {
97
            Map<String, DataStoreParameters> all = new HashMap<>();
98
            Project project = ProjectManager.getInstance().getCurrentProject();
99
            List<Document> views = project.getDocuments(TYPENAME);
100
            for (Document view : views) {
101
                for (FLayer layer : ((ViewDocument) view).layers()) {
102
                    if (layer instanceof FLyrVect) {
103
                        FeatureStore store = ((FLyrVect) layer).getFeatureStore();
104
                        if (filter == null || filter.test(store)) {
105
                            all.put(store.getName(), store.getParameters());
106
                        }
107
                        UnmodifiableBasicMap<String, DataStore> children = store.getChildren();
108
                        if (children != null) {
109
                            for (DataStore dataStore : children) {
110
                                if (filter == null || filter.test(dataStore)) {
111
                                    all.put(dataStore.getName(), dataStore.getParameters());
112
                                }
113
                            }
114
                        }
115
                    }
116
                    if (limit > 0 && all.size() >= limit) {
117
                        break;
118
                    }
119
                }
120
            }
121
            if (all.isEmpty()) {
122
                return null;
123
            }
124
            return all;
125
        }
126

    
127
        @Override
128
        protected DataStoreParameters getMyParameters(final String name) {
129
            Map<String, DataStoreParameters> all = this.getAll(new Predicate<DataStore>() {
130
                @Override
131
                public boolean test(DataStore t) {
132
                    return StringUtils.equalsIgnoreCase(name, t.getName());
133
                }
134
            }, 1);
135
            if (all == null) {
136
                return null;
137
            }
138
            return all.get(name);
139
        }
140

    
141
        @Override
142
        public boolean containsKey(final String key) {
143
            Map<String, DataStoreParameters> all = this.getAll(new Predicate<DataStore>() {
144
                @Override
145
                public boolean test(DataStore t) {
146
                    return StringUtils.equalsIgnoreCase(key, t.getName());
147
                }
148
            }, 1);
149
            if (all == null) {
150
                return false;
151
            }
152
            return all.containsKey(key);
153
        }
154

    
155
        @Override
156
        protected UnmodifiableBasicSet<String> getMyKeySet() {
157
            Map<String, DataStoreParameters> all = this.getAll(null, 0);
158
            if (all == null) {
159
                return null;
160
            }
161
            return new UnmodifiableBasicSetAdapter<>(all.keySet());
162
        }
163

    
164
        @Override
165
        protected boolean isEmptyMyRepository() {
166
            Map<String, DataStoreParameters> all = this.getAll(null, 1);
167
            return all.isEmpty();
168
        }
169

    
170
        @Override
171
        protected int getMySize() {
172
            Map<String, DataStoreParameters> all = this.getAll(null, 0);
173
            return all.size();
174
        }
175

    
176
        @Override
177
        public Iterator<DataStoreParameters> iterator() {
178
            Map<String, DataStoreParameters> all = this.getAll(null, 0);
179
            return all.values().iterator();
180
        }
181

    
182
        public static void selfRegister() {
183
            DataManager dataManager = DALLocator.getDataManager();
184
            dataManager.getStoresRepository().addRepository(new ViewDocumentStoresRepository("Layers"));            
185
        }
186
    }
187
    private static final String PERSISTENCE_VIEW_DOCUMENT_DEFINITION_NAME
188
            = "DefaultViewDocument";
189
    public static String TYPENAME = "project.document.view2d";
190
    private DynStruct persistenceDefinition;
191

    
192
    public ViewManager() {
193
        // Do nothing
194
    }
195

    
196
    public int getPriority() {
197
        return 0;
198
    }
199

    
200
    public ImageIcon getIcon() {
201
        return PluginServices.getIconTheme().get("document-view-icon");
202
    }
203

    
204
    public ImageIcon getIconSelected() {
205
        return PluginServices.getIconTheme().get("document-view-icon-sel");
206
    }
207

    
208
    public String getTitle() {
209
        return PluginServices.getText(this, "Vista");
210
    }
211

    
212
    public String getTypeName() {
213
        return TYPENAME;
214
    }
215

    
216
    public Class getMainWindowClass() {
217
        return DefaultViewPanel.class;
218
    }
219

    
220
    public AbstractDocument createDocument() {
221
        AbstractDocument doc = new DefaultViewDocument(this);
222
        if (this.notifyObservers(NOTIFY_AFTER_CREATEDOCUMENT, doc).isCanceled()) {
223
            return null;
224
        }
225
        return doc;
226

    
227
    }
228

    
229
    @Override
230
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
231
        IDocumentWindow win = (IDocumentWindow) super.getMainWindow(doc, layout);
232
        if (win == null) {
233
            win = this.createDocumentWindow(doc);
234
            if (layout != null && win != null) {
235
                win.setWindowLayout(layout);
236
            }
237
            if (this.notifyObservers(NOTIFY_AFTER_CREATEMAINWINDOW, win).isCanceled()) {
238
                return null;
239
            }
240
        }
241
        if (this.notifyObservers(NOTIFY_AFTER_GETMAINWINDOW, win).isCanceled()) {
242
            return null;
243
        }
244
        ((AbstractDocument) doc).raiseEventCreateWindow(win);
245
        return win;
246
    }
247

    
248
    @Override
249
    public IWindow getPropertiesWindow(Document doc) {
250
        IWindow win = super.getPropertiesWindow(doc);
251
        if (win == null) {
252
            win = new ViewProperties((DefaultViewDocument) doc);
253
            if (this.notifyObservers(NOTIFY_AFTER_CREATEPROPERTIESWINDOW, win).isCanceled()) {
254
                return null;
255
            }
256
        }
257
        if (this.notifyObservers(NOTIFY_AFTER_GETPROPERTIESWINDOW, win).isCanceled()) {
258
            return null;
259
        }
260
        return win;
261
    }
262

    
263
    public void addTOCContextAction(String theAction) {
264
        ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
265
        ActionInfo action = actionManager.getAction(theAction);
266
        if (action == null) {
267
            String errmsg = "Action '" + theAction + "' not found";
268
            LOGGER.info(errmsg);
269
            throw new RuntimeException(errmsg);
270
        }
271
        this.addTOCContextAction(action);
272
    }
273

    
274
    public void addTOCContextAction(String theAction, String group, int groupOrder, int order) {
275
        ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
276
        ActionInfo action = actionManager.getAction(theAction);
277
        if (action == null) {
278
            String errmsg = "Action '" + theAction + "' not found";
279
            LOGGER.info(errmsg);
280
            throw new RuntimeException(errmsg);
281
        }
282
        this.addTOCContextAction(
283
                action.getName(),
284
                new ActionInfoAdapterToContextMenuAction(action, group, groupOrder, 0)
285
        );
286
    }
287

    
288
    public void addTOCContextAction(ActionInfo action) {
289
        this.addTOCContextAction(action.getName(), new ActionInfoAdapterToContextMenuAction(action, "default", 9000));
290
    }
291

    
292
    public void addTOCContextAction(ActionInfo action, String group, int groupOrder) {
293
        this.addTOCContextAction(action.getName(), new ActionInfoAdapterToContextMenuAction(action, group, groupOrder));
294
    }
295

    
296
    class ActionInfoAdapterToContextMenuAction extends AbstractActionInfoAdapterToTocContextMenuAction {
297

    
298
        ActionInfoAdapterToContextMenuAction(ActionInfo action, String group, int groupOrder) {
299
            super(action, group, groupOrder);
300
        }
301

    
302
        ActionInfoAdapterToContextMenuAction(ActionInfo action, String group, int groupOrder, int order) {
303
            super(action, group, groupOrder, order);
304
        }
305
    }
306

    
307
    /**
308
     * @deprecated use addTOCContextAction(ActionInfo action, String group, int
309
     * groupOrder)
310
     * @param id
311
     * @param action
312
     */
313
    public void addTOCContextAction(String id, IContextMenuAction action) {
314
        initializeRegisterTOCActions();
315
        ExtensionPoint exPoint = ToolsLocator.getExtensionPointManager().add(
316
                "View_TocActions", "");
317
        if (action instanceof ExtensionBuilder) {
318
            exPoint.append(id, "", (ExtensionBuilder) action);
319
            return;
320
        }
321
        exPoint.append(id, "", new ContextMenuActionAdapterToExtensionBuilder(action));
322
    }
323

    
324
    class ContextMenuActionAdapterToExtensionBuilder implements ExtensionBuilder {
325

    
326
        IContextMenuAction menuAction = null;
327

    
328
        ContextMenuActionAdapterToExtensionBuilder(IContextMenuAction menuAction) {
329
            this.menuAction = menuAction;
330
        }
331

    
332
        public Object create() {
333
            return this.menuAction;
334
        }
335

    
336
        public Object create(Object[] args) {
337
            return this.menuAction;
338
        }
339

    
340
        public Object create(Map args) {
341
            return this.menuAction;
342
        }
343
    }
344

    
345
    private static void initializeRegisterTOCActions() {
346
        ExtensionPointManager epManager = ToolsLocator.getExtensionPointManager();
347

    
348
        if (!epManager.has("View_TocActions")) {
349
            epManager.add(
350
                    "View_TocActions",
351
                    "Context menu options of the TOC "
352
                    + " in the view window "
353
                    + "(register instances of "
354
                    + "org.gvsig.app.gui.toc.AbstractTocContextMenuAction)"
355
            );
356
        }
357
    }
358

    
359
    /**
360
     * Registers in the points of extension the Factory with alias.
361
     *
362
     */
363
    public static void register() {
364
        ViewManager factory = new ViewManager();
365
        ProjectManager.getInstance().registerDocumentFactory(factory);
366

    
367
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
368
        manager.registerFactory(factory);
369

    
370
        initializeRegisterTOCActions();
371

    
372
        ProjectManager.getInstance().registerDocumentAction(TYPENAME, new CopyDocumentAction());
373
        ProjectManager.getInstance().registerDocumentAction(TYPENAME, new CutDocumentAction());
374
        ProjectManager.getInstance().registerDocumentAction(TYPENAME, new PasteDocumentAction());
375

    
376
        IconThemeHelper.registerIcon("document", "document-view-icon", ViewManager.class);
377
        IconThemeHelper.registerIcon("document", "document-view-icon-sel", ViewManager.class);
378

    
379
        IconThemeHelper.registerIcon("cursor", "cursor-crux", ViewManager.class);
380
        IconThemeHelper.registerIcon("cursor", "cursor-info-by-point", ViewManager.class);
381
        IconThemeHelper.registerIcon("cursor", "cursor-pan", ViewManager.class);
382
        IconThemeHelper.registerIcon("cursor", "cursor-query-area", ViewManager.class);
383
        IconThemeHelper.registerIcon("cursor", "cursor-select-by-point", ViewManager.class);
384
        IconThemeHelper.registerIcon("cursor", "cursor-select-by-polygon", ViewManager.class);
385
        IconThemeHelper.registerIcon("cursor", "cursor-select-by-rectangle", ViewManager.class);
386
        IconThemeHelper.registerIcon("cursor", "cursor-zoom-in", ViewManager.class);
387
        IconThemeHelper.registerIcon("cursor", "cursor-zoom-out", ViewManager.class);
388

    
389
        IconThemeHelper.registerIcon("layer", "layer-icon", ViewManager.class);
390
        IconThemeHelper.registerIcon("layer", "layer-icon-group", ViewManager.class);
391
        IconThemeHelper.registerIcon("layer", "layer-icon-vectorial", ViewManager.class);
392
        IconThemeHelper.registerIcon("layer", "layer-icon-csv", ViewManager.class);
393
        IconThemeHelper.registerIcon("layer", "layer-icon-dgn", ViewManager.class);
394
        IconThemeHelper.registerIcon("layer", "layer-icon-dxf", ViewManager.class);
395
        IconThemeHelper.registerIcon("layer", "layer-icon-postgresql", ViewManager.class);
396
        IconThemeHelper.registerIcon("layer", "layer-icon-mysql", ViewManager.class);
397
        IconThemeHelper.registerIcon("layer", "layer-icon-jdbc", ViewManager.class);
398
        //IconThemeHelper.registerIcon("layer", "layer-icon-unavailable", ViewManager.class);
399
        IconThemeHelper.registerIcon("layer", "layer-chk-unavailable", ViewManager.class);
400
        IconThemeHelper.registerIcon("layer", "layer-chk-temporary", ViewManager.class);
401

    
402
        IconThemeHelper.registerIcon("legend", "legend-overview-single-symbol", ViewManager.class);
403
        IconThemeHelper.registerIcon("legend", "legend-overview-vectorial-interval", ViewManager.class);
404
        IconThemeHelper.registerIcon("legend", "legend-overview-vectorial-unique-value", ViewManager.class);
405

    
406
        MapContextManager mapContextMgr = MapContextLocator.getMapContextManager();
407
        mapContextMgr.registerIconLayer("CSV", "layer-icon-csv");
408
        mapContextMgr.registerIconLayer("DGN", "layer-icon-dgn");
409
        mapContextMgr.registerIconLayer("DXF", "layer-icon-dxf");
410
        mapContextMgr.registerIconLayer("jdbc", "layer-icon-jdbc");
411
        mapContextMgr.registerIconLayer("PostgreSQL", "layer-icon-postgresql");
412
        mapContextMgr.registerIconLayer("MySQL", "layer-icon-mysql");
413

    
414
        if (factory.persistenceDefinition == null) {
415
            factory.persistenceDefinition = manager.addDefinition(
416
                    ViewDocument.class,
417
                    PERSISTENCE_VIEW_DOCUMENT_DEFINITION_NAME,
418
                    "Default view document persistence definition",
419
                    null,
420
                    null
421
            );
422
            factory.persistenceDefinition.extend(manager.getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
423

    
424
            factory.persistenceDefinition.addDynFieldBoolean("useMapOverview").setMandatory(true);
425
            factory.persistenceDefinition.addDynFieldObject("mainMapContext").setClassOfValue(MapContext.class).setMandatory(true);
426
            factory.persistenceDefinition.addDynFieldObject("overviewMapContext").setClassOfValue(MapContext.class).setMandatory(false);
427
            factory.persistenceDefinition.addDynFieldObject("propertiesHelper").setClassOfValue(ExtendedPropertiesHelper.class)
428
                    .setMandatory(false);
429

    
430
        }
431
        ViewDocumentStoresRepository.selfRegister();
432
        ViewZoomActionFactory.selfRegister();
433
    }
434

    
435
    @SuppressWarnings("rawtypes")
436
    public DynStruct getDefinition(String className) {
437

    
438
        if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
439
            return this.persistenceDefinition;
440
        }
441
        if (this.persistenceDefinition.getFullName().equalsIgnoreCase(className)) {
442
            return this.persistenceDefinition;
443
        }
444
        if (this.getDocumentClass().getName().equals(className)) {
445
            return this.persistenceDefinition;
446
        }
447

    
448
        return null;
449
    }
450

    
451
    @SuppressWarnings("rawtypes")
452
    protected Class getDocumentClass() {
453
        return DefaultViewDocument.class;
454
    }
455

    
456
    public boolean manages(Object object) {
457
        return object instanceof ViewDocument;
458
    }
459
}