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 / ProjectManager.java @ 43328

History | View | Annotate | Download (10.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;
25

    
26
import java.text.MessageFormat;
27
import java.util.ArrayList;
28
import java.util.Collections;
29
import java.util.Comparator;
30
import java.util.HashMap;
31
import java.util.Iterator;
32
import java.util.List;
33
import java.util.Map;
34

    
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
import org.gvsig.andami.PluginServices;
39
import org.gvsig.app.extension.ProjectExtension;
40
import org.gvsig.app.project.documents.DefaultDocumentActionGroup;
41
import org.gvsig.app.project.documents.Document;
42
import org.gvsig.app.project.documents.DocumentAction;
43
import org.gvsig.app.project.documents.DocumentActionGroup;
44
import org.gvsig.app.project.documents.DocumentManager;
45
import org.gvsig.app.project.documents.gui.ProjectWindow;
46
import org.gvsig.fmap.mapcontrol.MapControlLocator;
47
import org.gvsig.propertypage.PropertiesPage;
48
import org.gvsig.propertypage.PropertiesPageFactory;
49
import org.gvsig.propertypage.PropertiesPageManager;
50
import org.gvsig.tools.ToolsLocator;
51
import org.gvsig.tools.extensionpoint.ExtensionPoint;
52
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
53
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
54
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable;
55

    
56
public class ProjectManager extends BaseWeakReferencingObservable {
57

    
58
    private static final Logger logger = LoggerFactory
59
        .getLogger(ProjectManager.class);
60
    private static ProjectManager factory = null;
61

    
62
    // private static final String KEY_PROJECT = "app.project";
63
    // private static final String KEY_DOCUMENTS = "app.project.documents";
64
    private static final String KEY_DOCUMENTS_FACTORIES =
65
        "app.project.documents.factories";
66
    private static final String KEY_DOCUMENTS_ACTIONS =
67
        "app.project.documents.actions";
68

    
69
    private Map<String, DocumentActionGroup> documentActionGroups;
70

    
71
    public static ProjectManager getInstance() {
72
        if (factory == null) {
73
            factory = new ProjectManager();
74
        }
75
        return factory;
76
    }
77

    
78
    private ProjectManager() {
79
        this.documentActionGroups = new HashMap<String, DocumentActionGroup>();
80
    }
81

    
82
    
83
    public Project getCurrentProject() {
84
        return ((ProjectExtension) PluginServices
85
            .getExtension(ProjectExtension.class)).getProject();
86
    }
87
    
88
    public void setCurrentProject(Project project) {
89
        ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).setProject(project);
90
    }
91

    
92
    public ProjectWindow getCurrentProjectWindow() {
93
        ProjectExtension projectExtension =
94
            (ProjectExtension) PluginServices
95
                .getExtension(ProjectExtension.class);
96
        ProjectWindow window =
97
            (ProjectWindow) projectExtension.getProjectWindow();
98
        return window;
99
    }
100

    
101
    /**
102
     * @deprecated use {@link #getDocumentManagers()} instead.
103
     */
104
    public List<DocumentManager> getDocumentManager() {
105
        return getDocumentManagers();
106
    }
107

    
108
    @SuppressWarnings("unchecked")
109
    public List<DocumentManager> getDocumentManagers() {
110
        Iterator<Extension> iterator =
111
            ToolsLocator.getExtensionPointManager()
112
                .get(KEY_DOCUMENTS_FACTORIES).iterator();
113
        List<DocumentManager> factories = new ArrayList<DocumentManager>();
114
        while (iterator.hasNext()) {
115
            Extension extension = iterator.next();
116
            try {
117
                factories.add((DocumentManager) extension.create());
118
            } catch (InstantiationException e) {
119
                logger.error("Can't access to project document factory ("
120
                    + extension.getName() + ").");
121
            } catch (IllegalAccessException e) {
122
                logger.error("Can't access to project document factory ("
123
                    + extension.getName() + ").");
124
            }
125
        }
126
        return factories;
127
    }
128

    
129
    /**
130
     * @deprecated use {@link #getDocumentManager(String)} instead.
131
     */
132
    public DocumentManager getDocumentManagers(String type) {
133
        return getDocumentManager(type);
134
    }
135

    
136
    public DocumentManager getDocumentManager(String type) {
137
        DocumentManager factory = null;
138
        try {
139
            factory =
140
                (DocumentManager) ToolsLocator.getExtensionPointManager()
141
                    .get(KEY_DOCUMENTS_FACTORIES).create(type);
142
        } catch (Exception ex) {
143
            logger
144
                .warn(
145
                    MessageFormat.format(
146
                        "Unable to locate factory for documents of type {0}",
147
                        type), ex);
148
        }
149
        return factory;
150
    }
151

    
152
    public Document createDocument(String type) {
153
        Document doc = getDocumentManager(type).createDocument();
154
        return doc;
155
    }
156

    
157
    public Document createDocument(String type, String name) {
158
        Document doc = createDocument(type);
159
        doc.setName(name);
160
        return doc;
161
    }
162

    
163
    /**
164
     * @deprecated use {@link #createDocumentsByUser(String)} instead
165
     */
166
    public Document createDocumentByUser(String type) {
167
        Document doc = getDocumentManager(type).createDocumentByUser();
168
        return doc;
169
    }
170

    
171
    /**
172
     * Creates a group of documents of a given type through the user interface.
173
     * 
174
     * @param type
175
     *            the type of documents to create
176
     * @return the created documents
177
     */
178
    public Iterator<? extends Document> createDocumentsByUser(String type) {
179
        logger.info("createDocumentsByUser('{}')", type);
180
        return getDocumentManager(type).createDocumentsByUser();
181
    }
182

    
183
    public Project createProject() {
184
        return new DefaultProject();
185
    }
186

    
187
    public ProjectExtent createExtent() {
188
        return new ProjectExtent();
189
    }
190

    
191
    public void registerDocumentFactory(DocumentManager documentManager) {
192
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
193
        manager.add(KEY_DOCUMENTS_FACTORIES).append(
194
            documentManager.getTypeName(), null, documentManager);
195
        notifyObservers();
196
    }
197

    
198
    public void registerDocumentFactoryAlias(String typeName, String alias) {
199
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
200

    
201
        manager.get(KEY_DOCUMENTS_FACTORIES).addAlias(typeName, alias);
202
    }
203

    
204
    public void registerDocumentAction(String typeName, DocumentAction action) {
205
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
206

    
207
        String key = KEY_DOCUMENTS_ACTIONS + "." + typeName;
208
        String description =
209
            MessageFormat.format("Actions for {1} documents ", typeName);
210

    
211
        manager.add(key, description).append(action.getId(),
212
            action.getDescription(), action);
213
    }
214

    
215
    /**
216
     * Gets a list of actions for the document type especified.
217
     * 
218
     * La lista esta ordenada deacuerdo al orden especificado en
219
     * las acciones y grupos de acciones involucrados.
220
     * 
221
     * @param doctype
222
     * @return list of actions as List<DocumentAction>
223
     */
224
    @SuppressWarnings("unchecked")
225
    public List<DocumentAction> getDocumentActions(String doctype) {
226

    
227
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
228

    
229
        String key = KEY_DOCUMENTS_ACTIONS + "." + doctype;
230
        ExtensionPoint extensionPoint = manager.get(key);
231
        if (extensionPoint == null) {
232
            // No hay acciones registradas para ese tipo de documento
233
            return null;
234
        }
235
        List<DocumentAction> actions = new ArrayList<DocumentAction>();
236
        Iterator<Extension> it = extensionPoint.iterator();
237
        while (it.hasNext()) {
238
            try {
239
                DocumentAction action;
240
                action = (DocumentAction) it.next().create();
241
                actions.add(action);
242
            } catch (InstantiationException e) {
243
                logger.warn("Can't retrieve document action", e);
244
            } catch (IllegalAccessException e) {
245
                logger.warn("Can't retrieve document action", e);
246
            }
247
        }
248
        if (actions.size() < 1) {
249
            return null;
250
        }
251
        Collections.sort(actions, new CompareAction());
252
        return actions;
253
    }
254

    
255
    private class CompareAction implements Comparator<DocumentAction> {
256

    
257
        public int compare(DocumentAction action1, DocumentAction action2) {
258
            // FIXME: flata formatear los enteros!!!!
259
            String key1 =
260
                MessageFormat.format("{1}.{2}.{3}", action1.getGroup()
261
                    .getOrder(), action1.getGroup().getTitle(), action1
262
                    .getOrder());
263
            String key2 =
264
                MessageFormat.format("{1}.{2}.{3}", action2.getGroup()
265
                    .getOrder(), action2.getGroup().getTitle(), action2
266
                    .getOrder());
267
            return key1.compareTo(key2);
268
        }
269
    }
270

    
271
    /**
272
     * Create, add and return a new action for documents.
273
     * 
274
     * If action already exists don't create and return this.
275
     * 
276
     * @param unique
277
     *            identifier for the action
278
     * @param title
279
     * @param description
280
     * @param order
281
     * @return
282
     */
283
    public DocumentActionGroup addDocumentActionGroup(String id, String title,
284
        String description, int order) {
285
        DocumentActionGroup group = this.documentActionGroups.get(id);
286
        if (group != null) {
287
            return group;
288
        }
289
        group = new DefaultDocumentActionGroup(id, title, description, order);
290
        this.documentActionGroups.put(id, group);
291
        return group;
292
    }
293

    
294
    public ProjectPreferences getProjectPreferences() {
295
        return DefaultProject.getPreferences();
296
    }
297
    
298
}