Statistics
| Revision:

root / org.gvsig.educa.thematicmap.app / trunk / org.gvsig.educa.thematicmap.app / org.gvsig.educa.thematicmap.app.viewer / src / main / java / org / gvsig / educa / thematicmap / app / viewer / ThematicMapDocumentManager.java @ 60

History | View | Annotate | Download (8.86 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.educa.thematicmap.app.viewer;
23

    
24
import java.util.Iterator;
25
import java.util.List;
26

    
27
import javax.swing.ImageIcon;
28

    
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.andami.ui.mdiManager.IWindow;
31
import org.gvsig.app.project.Project;
32
import org.gvsig.app.project.ProjectManager;
33
import org.gvsig.app.project.documents.AbstractDocument;
34
import org.gvsig.app.project.documents.AbstractDocumentManager;
35
import org.gvsig.app.project.documents.Document;
36
import org.gvsig.app.project.documents.gui.IDocumentWindow;
37
import org.gvsig.app.project.documents.gui.WindowLayout;
38
import org.gvsig.app.project.documents.view.ViewDocument;
39
import org.gvsig.educa.thematicmap.ThematicMapManager;
40
import org.gvsig.educa.thematicmap.app.viewer.ui.ThematicMapDocumentProperties;
41
import org.gvsig.educa.thematicmap.app.viewer.ui.ThematicMapDocumentViewer;
42
import org.gvsig.educa.thematicmap.app.viewer.ui.ThematicMapSelector;
43
import org.gvsig.educa.thematicmap.map.ThematicMap;
44
import org.gvsig.educa.thematicmap.swing.ThematicMapSwingLocator;
45
import org.gvsig.educa.thematicmap.swing.ThematicMapSwingManager;
46
import org.gvsig.tools.ToolsLocator;
47
import org.gvsig.tools.dynobject.DynObjectManager;
48
import org.gvsig.tools.dynobject.DynStruct;
49
import org.gvsig.tools.persistence.PersistenceManager;
50
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
51

    
52
/**
53
 * @author gvSIG Team
54
 * @version $Id$
55
 * 
56
 */
57
public class ThematicMapDocumentManager extends AbstractDocumentManager {
58

    
59
    public static final String PERSISTENCE_THEMATICMAP_DOCUMENT_DEFINITION_NAME =
60
        "ThematicMapDocument";
61

    
62
    public static String TYPENAME = "project.document.thematicMap";
63

    
64
    private DynStruct persistenceDefinition = null;
65

    
66
    private final ThematicMapSwingManager tmSwingManager;
67

    
68
    private final ThematicMapManager tmManager;
69

    
70
    /**
71
     * Registers in the points of extension the Factory with alias.
72
     * 
73
     */
74
    public static void register() {
75

    
76
        ThematicMapDocumentManager factory = new ThematicMapDocumentManager();
77

    
78
        ProjectManager.getInstance().registerDocumentFactory(factory);
79

    
80
        PluginServices.getIconTheme().registerDefault(
81
            "document-thematicmap-icon",
82
            ThematicMapDocumentManager.class.getClassLoader().getResource(
83
                "images/document-thematicmap-icon.png"));
84
        PluginServices.getIconTheme().registerDefault(
85
            "document-thematicmap-icon-sel",
86
            ThematicMapDocumentManager.class.getClassLoader().getResource(
87
                "images/document-thematicmap-icon-sel.png"));
88

    
89
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
90
        manager.registerFactory(factory);
91

    
92
        if (factory.persistenceDefinition == null) {
93
            DynObjectManager dynman = ToolsLocator.getDynObjectManager();
94
            factory.persistenceDefinition =
95
                dynman.createDynClass(PersistenceManager.PERSISTENCE_NAMESPACE,
96
                    PERSISTENCE_THEMATICMAP_DOCUMENT_DEFINITION_NAME,
97
                    "Thematic map document Persistence definition");
98

    
99
            factory.persistenceDefinition.addDynFieldString("id").setMandatory(
100
                true);
101
            factory.persistenceDefinition.addDynFieldString("version")
102
                .setMandatory(true);
103
            factory.persistenceDefinition.addDynFieldInt("buildNumber")
104
                .setMandatory(true);
105
            factory.persistenceDefinition.addDynFieldString("name")
106
                .setMandatory(true);
107
            factory.persistenceDefinition.addDynFieldString("comment")
108
                .setMandatory(false);
109
            factory.persistenceDefinition.addDynFieldObject("sourceView")
110
                .setMandatory(false).setClassOfValue(ViewDocument.class);
111

    
112
        }
113

    
114
    }
115

    
116
    /**
117
     *
118
     */
119
    public ThematicMapDocumentManager() {
120
        tmSwingManager = ThematicMapSwingLocator.getSwingManager();
121
        tmManager = tmSwingManager.getManager();
122
    }
123

    
124
    @Override
125
    public ImageIcon getIcon() {
126
        return PluginServices.getIconTheme().get("document-thematicmap-icon");
127
    }
128

    
129
    @Override
130
    public ImageIcon getIconSelected() {
131
        return PluginServices.getIconTheme().get(
132
            "document-thematicmap-icon-sel");
133
    }
134

    
135
    @Override
136
    public String getTitle() {
137
        return tmSwingManager.getTranslation("Thematic_Map");
138
    }
139

    
140
    public String getTypeName() {
141
        return TYPENAME;
142
    }
143

    
144
    public AbstractDocument createDocument() {
145
        return new ThematicMapDocument(this);
146
    }
147

    
148
    public ThematicMapDocument createDocument(ThematicMap thematicMap) {
149
        return new ThematicMapDocument(this, thematicMap);
150
    }
151

    
152
    @Override
153
    public AbstractDocument createDocumentByUser() {
154
        Iterator<? extends Document> iterator = createDocumentsByUser();
155
        if (iterator == null || !iterator.hasNext()) {
156
            return null;
157
        }
158
        return (AbstractDocument) iterator.next();
159
    }
160

    
161
    @Override
162
    public Iterator<? extends Document> createDocumentsByUser() {
163
        ThematicMapSelector selector =
164
            new ThematicMapSelector(this, tmManager.getInstalledMaps());
165
        tmSwingManager.getWindowManager().showWindow(selector,
166
            tmSwingManager.getTranslation("open_thematic_map"), MODE.DIALOG);
167

    
168
        if (selector.getDocuments() == null
169
            || selector.getDocuments().isEmpty()) {
170
            return null;
171
        }
172
        return selector.getDocuments().iterator();
173
    }
174

    
175
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
176
        IDocumentWindow thematicMapPanel;
177
        thematicMapPanel =
178
            (IDocumentWindow) PluginServices.getMDIManager()
179
                .getSingletonWindow(getMainWindowClass(), doc);
180
        if (thematicMapPanel != null) {
181
            // The window document is already created, return it.
182
            return thematicMapPanel;
183
        }
184

    
185
        thematicMapPanel = this.createDocumentWindow(doc);
186
        if (layout != null && thematicMapPanel != null) {
187
            thematicMapPanel.setWindowLayout(layout);
188
        }
189
        return thematicMapPanel;
190
    }
191

    
192
    public IWindow getPropertiesWindow(Document doc) {
193
        ThematicMapDocumentProperties propertiesPanel =
194
            (ThematicMapDocumentProperties) PluginServices.getMDIManager()
195
                .getSingletonWindow(ThematicMapDocumentProperties.class, doc);
196
        if (propertiesPanel != null) {
197
            // The table window document is already created, return it.
198
            return propertiesPanel;
199
        }
200

    
201
        propertiesPanel =
202
            new ThematicMapDocumentProperties((ThematicMapDocument) doc);
203
        return propertiesPanel;
204
    }
205

    
206
    public Class<? extends IDocumentWindow> getMainWindowClass() {
207
        return ThematicMapDocumentViewer.class;
208
    }
209

    
210
    public boolean manages(Object object) {
211
        return object instanceof ThematicMapDocument;
212
    }
213

    
214
    public DynStruct getDefinition(String className) {
215
        // TODO review it
216
        if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
217
            return this.persistenceDefinition;
218
        }
219
        if (this.persistenceDefinition.getFullName()
220
            .equalsIgnoreCase(className)) {
221
            return this.persistenceDefinition;
222
        }
223
        if (this.getDocumentClass().getName().equals(className)) {
224
            return this.persistenceDefinition;
225
        }
226
        return null;
227
    }
228

    
229
    @SuppressWarnings("rawtypes")
230
    @Override
231
    protected Class getDocumentClass() {
232
        return ThematicMapDocument.class;
233
    }
234

    
235
    /**
236
     * @param curMap
237
     * @return
238
     */
239
    public ThematicMapDocument getDocumentForMap(ThematicMap map) {
240
        if (map == null) {
241
            return null;
242
        }
243
        List<Document> docs = getProject().getDocuments(TYPENAME);
244
        for (Document document : docs) {
245
            if (((ThematicMapDocument) document).isMap(map)) {
246
                return (ThematicMapDocument) document;
247
            }
248
        }
249
        return null;
250
    }
251

    
252
    private Project getProject() {
253
        return ProjectManager.getInstance().getCurrentProject();
254
    }
255
}