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

History | View | Annotate | Download (9.4 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.Arrays;
25
import java.util.Iterator;
26
import java.util.List;
27

    
28
import javax.swing.ImageIcon;
29

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

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

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

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

    
65
    private DynStruct persistenceDefinition = null;
66

    
67
    private final ThematicMapSwingManager tmSwingManager;
68

    
69
    private final ThematicMapManager tmManager;
70

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

    
77
        ThematicMapDocumentManager factory = new ThematicMapDocumentManager();
78

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

    
81
        registerIcons();
82

    
83
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
84
        manager.registerFactory(factory);
85

    
86
        if (factory.persistenceDefinition == null) {
87
            DynObjectManager dynman = ToolsLocator.getDynObjectManager();
88
            factory.persistenceDefinition =
89
                dynman.createDynClass(PersistenceManager.PERSISTENCE_NAMESPACE,
90
                    PERSISTENCE_THEMATICMAP_DOCUMENT_DEFINITION_NAME,
91
                    "Thematic map document Persistence definition");
92

    
93
            factory.persistenceDefinition.addDynFieldString("id").setMandatory(
94
                true);
95
            factory.persistenceDefinition.addDynFieldInt("version")
96
                .setMandatory(true);
97
            factory.persistenceDefinition.addDynFieldInt("buildNumber")
98
                .setMandatory(true);
99
            factory.persistenceDefinition.addDynFieldString("name")
100
                .setMandatory(true);
101
            factory.persistenceDefinition.addDynFieldString("comment")
102
                .setMandatory(false);
103
            factory.persistenceDefinition.addDynFieldObject("sourceView")
104
                .setMandatory(false).setClassOfValue(ViewDocument.class);
105

    
106
        }
107

    
108
    }
109

    
110
    /**
111
     *
112
     */
113
    private static void registerIcons() {
114
        PluginServices.getIconTheme().registerDefault(
115
            "document-thematicmap-icon",
116
            ThematicMapDocumentManager.class.getClassLoader().getResource(
117
                "images/document-thematicmap-icon.png"));
118
        PluginServices.getIconTheme().registerDefault(
119
            "document-thematicmap-icon-sel",
120
            ThematicMapDocumentManager.class.getClassLoader().getResource(
121
                "images/document-thematicmap-icon-sel.png"));
122
    }
123

    
124
    /**
125
     *
126
     */
127
    public ThematicMapDocumentManager() {
128
        tmSwingManager = ThematicMapSwingLocator.getSwingManager();
129
        tmManager = tmSwingManager.getManager();
130
    }
131

    
132
    @Override
133
    public ImageIcon getIcon() {
134
        if (!PluginServices.getIconTheme().exists("document-thematicmap-icon")) {
135
            registerIcons();
136
        }
137
        return PluginServices.getIconTheme().get("document-thematicmap-icon");
138
    }
139

    
140
    @Override
141
    public ImageIcon getIconSelected() {
142
        if (!PluginServices.getIconTheme().exists(
143
            "document-thematicmap-icon-sel")) {
144
            registerIcons();
145
        }
146
        return PluginServices.getIconTheme().get(
147
            "document-thematicmap-icon-sel");
148
    }
149

    
150
    @Override
151
    public String getTitle() {
152
        return tmSwingManager.getTranslation("Thematic_Map");
153
    }
154

    
155
    public String getTypeName() {
156
        return TYPENAME;
157
    }
158

    
159
    public AbstractDocument createDocument() {
160
        return new ThematicMapDocument(this);
161
    }
162

    
163
    public ThematicMapDocument createDocument(ThematicMap thematicMap) {
164
        return new ThematicMapDocument(this, thematicMap);
165
    }
166

    
167
    @Override
168
    public AbstractDocument createDocumentByUser() {
169
        Iterator<? extends Document> iterator = createDocumentsByUser();
170
        if (iterator == null || !iterator.hasNext()) {
171
            return null;
172
        }
173
        return (AbstractDocument) iterator.next();
174
    }
175

    
176
    @Override
177
    public Iterator<? extends Document> createDocumentsByUser() {
178
        ThematicMapSelector selector =
179
            new ThematicMapSelector(this, tmManager.getInstalledMaps());
180
        tmSwingManager.getWindowManager().showWindow(selector,
181
            tmSwingManager.getTranslation("open_thematic_map"), MODE.DIALOG);
182

    
183
        if (selector.getDocuments() == null
184
            || selector.getDocuments().isEmpty()) {
185
            return Arrays.asList(new Document[] {}).iterator();
186
        }
187
        return selector.getDocuments().iterator();
188
    }
189

    
190
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
191
        IDocumentWindow thematicMapPanel;
192
        thematicMapPanel =
193
            (IDocumentWindow) PluginServices.getMDIManager()
194
                .getSingletonWindow(getMainWindowClass(), doc);
195
        if (thematicMapPanel != null) {
196
            // The window document is already created, return it.
197
            return thematicMapPanel;
198
        }
199

    
200
        thematicMapPanel = this.createDocumentWindow(doc);
201
        if (layout != null && thematicMapPanel != null) {
202
            thematicMapPanel.setWindowLayout(layout);
203
        }
204
        return thematicMapPanel;
205
    }
206

    
207
    public IWindow getPropertiesWindow(Document doc) {
208
        ThematicMapDocumentProperties propertiesPanel =
209
            (ThematicMapDocumentProperties) PluginServices.getMDIManager()
210
                .getSingletonWindow(ThematicMapDocumentProperties.class, doc);
211
        if (propertiesPanel != null) {
212
            // The table window document is already created, return it.
213
            return propertiesPanel;
214
        }
215

    
216
        propertiesPanel =
217
            new ThematicMapDocumentProperties((ThematicMapDocument) doc);
218
        return propertiesPanel;
219
    }
220

    
221
    public Class<? extends IDocumentWindow> getMainWindowClass() {
222
        return ThematicMapDocumentViewer.class;
223
    }
224

    
225
    public boolean manages(Object object) {
226
        return object instanceof ThematicMapDocument;
227
    }
228

    
229
    public DynStruct getDefinition(String className) {
230
        // TODO review it
231
        if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
232
            return this.persistenceDefinition;
233
        }
234
        if (this.persistenceDefinition.getFullName()
235
            .equalsIgnoreCase(className)) {
236
            return this.persistenceDefinition;
237
        }
238
        if (this.getDocumentClass().getName().equals(className)) {
239
            return this.persistenceDefinition;
240
        }
241
        return null;
242
    }
243

    
244
    @SuppressWarnings("rawtypes")
245
    @Override
246
    protected Class getDocumentClass() {
247
        return ThematicMapDocument.class;
248
    }
249

    
250
    /**
251
     * Gets the first document which map is the specified
252
     * 
253
     * @param curMap
254
     * @return
255
     */
256
    public ThematicMapDocument getDocumentForMap(ThematicMap map) {
257
        if (map == null) {
258
            return null;
259
        }
260
        List<Document> docs = getProject().getDocuments(TYPENAME);
261
        for (Document document : docs) {
262
            if (((ThematicMapDocument) document).isMap(map)) {
263
                return (ThematicMapDocument) document;
264
            }
265
        }
266
        return null;
267
    }
268

    
269
    /**
270
     * Gets current project
271
     * 
272
     * @return
273
     */
274
    private Project getProject() {
275
        return ProjectManager.getInstance().getCurrentProject();
276
    }
277
}