Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / ProjectManager.java @ 38601

History | View | Annotate | Download (9.89 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.app.project;
23

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

    
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35

    
36
import org.gvsig.andami.PluginServices;
37
import org.gvsig.app.extension.ProjectExtension;
38
import org.gvsig.app.project.documents.DefaultDocumentActionGroup;
39
import org.gvsig.app.project.documents.Document;
40
import org.gvsig.app.project.documents.DocumentAction;
41
import org.gvsig.app.project.documents.DocumentActionGroup;
42
import org.gvsig.app.project.documents.DocumentManager;
43
import org.gvsig.app.project.documents.gui.ProjectWindow;
44
import org.gvsig.tools.ToolsLocator;
45
import org.gvsig.tools.extensionpoint.ExtensionPoint;
46
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
47
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
48
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable;
49

    
50
public class ProjectManager extends BaseWeakReferencingObservable {
51

    
52
    private static final Logger logger = LoggerFactory
53
        .getLogger(ProjectManager.class);
54
    private static ProjectManager factory = null;
55

    
56
    // private static final String KEY_PROJECT = "app.project";
57
    // private static final String KEY_DOCUMENTS = "app.project.documents";
58
    private static final String KEY_DOCUMENTS_FACTORIES =
59
        "app.project.documents.factories";
60
    private static final String KEY_DOCUMENTS_ACTIONS =
61
        "app.project.documents.actions";
62

    
63
    private Map<String, DocumentActionGroup> documentActionGroups;
64

    
65
    public static ProjectManager getInstance() {
66
        if (factory == null) {
67
            factory = new ProjectManager();
68
        }
69
        return factory;
70
    }
71

    
72
    private ProjectManager() {
73
        this.documentActionGroups = new HashMap<String, DocumentActionGroup>();
74
    }
75

    
76
    public Project getCurrentProject() {
77
        return ((ProjectExtension) PluginServices
78
            .getExtension(ProjectExtension.class)).getProject();
79
    }
80

    
81
    public ProjectWindow getCurrentProjectWindow() {
82
        ProjectExtension projectExtension =
83
            (ProjectExtension) PluginServices
84
                .getExtension(ProjectExtension.class);
85
        ProjectWindow window =
86
            (ProjectWindow) projectExtension.getProjectWindow();
87
        return window;
88
    }
89

    
90
    /**
91
     * @deprecated use {@link #getDocumentManagers()} instead.
92
     */
93
    public List<DocumentManager> getDocumentManager() {
94
        return getDocumentManagers();
95
    }
96

    
97
    @SuppressWarnings("unchecked")
98
    public List<DocumentManager> getDocumentManagers() {
99
        Iterator<Extension> iterator =
100
            ToolsLocator.getExtensionPointManager()
101
                .get(KEY_DOCUMENTS_FACTORIES).iterator();
102
        List<DocumentManager> factories = new ArrayList<DocumentManager>();
103
        while (iterator.hasNext()) {
104
            Extension extension = iterator.next();
105
            try {
106
                factories.add((DocumentManager) extension.create());
107
            } catch (InstantiationException e) {
108
                logger.error("Can't access to project document factory ("
109
                    + extension.getName() + ").");
110
            } catch (IllegalAccessException e) {
111
                logger.error("Can't access to project document factory ("
112
                    + extension.getName() + ").");
113
            }
114
        }
115
        return factories;
116
    }
117

    
118
    /**
119
     * @deprecated use {@link #getDocumentManager(String)} instead.
120
     */
121
    public DocumentManager getDocumentManagers(String type) {
122
        return getDocumentManager(type);
123
    }
124

    
125
    public DocumentManager getDocumentManager(String type) {
126
        DocumentManager factory = null;
127
        try {
128
            factory =
129
                (DocumentManager) ToolsLocator.getExtensionPointManager()
130
                    .get(KEY_DOCUMENTS_FACTORIES).create(type);
131
        } catch (Exception ex) {
132
            logger
133
                .warn(
134
                    MessageFormat.format(
135
                        "Unable to locate factory for documents of type {0}",
136
                        type), ex);
137
        }
138
        return factory;
139
    }
140

    
141
    public Document createDocument(String type) {
142
        Document doc = getDocumentManager(type).createDocument();
143
        return doc;
144
    }
145

    
146
    public Document createDocument(String type, String name) {
147
        Document doc = createDocument(type);
148
        doc.setName(name);
149
        return doc;
150
    }
151

    
152
    /**
153
     * @deprecated use {@link #createDocumentsByUser(String)} instead
154
     */
155
    public Document createDocumentByUser(String type) {
156
        Document doc = getDocumentManager(type).createDocumentByUser();
157
        return doc;
158
    }
159

    
160
    /**
161
     * Creates a group of documents of a given type through the user interface.
162
     * 
163
     * @param type
164
     *            the type of documents to create
165
     * @return the created documents
166
     */
167
    public Iterator<? extends Document> createDocumentsByUser(String type) {
168
        logger.info("createDocumentsByUser('{}')", type);
169
        return getDocumentManager(type).createDocumentsByUser();
170
    }
171

    
172
    public Project createProject() {
173
        return new DefaultProject();
174
    }
175

    
176
    public ProjectExtent createExtent() {
177
        return new ProjectExtent();
178
    }
179

    
180
    public void registerDocumentFactory(DocumentManager documentManager) {
181
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
182
        manager.add(KEY_DOCUMENTS_FACTORIES).append(
183
            documentManager.getTypeName(), null, documentManager);
184
        notifyObservers();
185
    }
186

    
187
    public void registerDocumentFactoryAlias(String typeName, String alias) {
188
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
189

    
190
        manager.get(KEY_DOCUMENTS_FACTORIES).addAlias(typeName, alias);
191
    }
192

    
193
    public void registerDocumentAction(String typeName, DocumentAction action) {
194
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
195

    
196
        String key = KEY_DOCUMENTS_ACTIONS + "." + typeName;
197
        String description =
198
            MessageFormat.format("Actions for {1} documents ", typeName);
199

    
200
        manager.add(key, description).append(action.getId(),
201
            action.getDescription(), action);
202
    }
203

    
204
    /**
205
     * Gets a list of actions for the document type especified.
206
     * 
207
     * La lista esta ordenada deacuerdo al orden especificado en
208
     * las acciones y grupos de acciones involucrados.
209
     * 
210
     * @param doctype
211
     * @return list of actions as List<DocumentAction>
212
     */
213
    @SuppressWarnings("unchecked")
214
    public List<DocumentAction> getDocumentActions(String doctype) {
215

    
216
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
217

    
218
        String key = KEY_DOCUMENTS_ACTIONS + "." + doctype;
219
        ExtensionPoint extensionPoint = manager.get(key);
220
        if (extensionPoint == null) {
221
            // No hay acciones registradas para ese tipo de documento
222
            return null;
223
        }
224
        List<DocumentAction> actions = new ArrayList<DocumentAction>();
225
        Iterator<Extension> it = extensionPoint.iterator();
226
        while (it.hasNext()) {
227
            try {
228
                DocumentAction action;
229
                action = (DocumentAction) it.next().create();
230
                actions.add(action);
231
            } catch (InstantiationException e) {
232
                logger.warn("Can't retrieve document action", e);
233
            } catch (IllegalAccessException e) {
234
                logger.warn("Can't retrieve document action", e);
235
            }
236
        }
237
        if (actions.size() < 1) {
238
            return null;
239
        }
240
        Collections.sort(actions, new CompareAction());
241
        return actions;
242
    }
243

    
244
    private class CompareAction implements Comparator<DocumentAction> {
245

    
246
        public int compare(DocumentAction action1, DocumentAction action2) {
247
            // FIXME: flata formatear los enteros!!!!
248
            String key1 =
249
                MessageFormat.format("{1}.{2}.{3}", action1.getGroup()
250
                    .getOrder(), action1.getGroup().getTitle(), action1
251
                    .getOrder());
252
            String key2 =
253
                MessageFormat.format("{1}.{2}.{3}", action2.getGroup()
254
                    .getOrder(), action2.getGroup().getTitle(), action2
255
                    .getOrder());
256
            return key1.compareTo(key2);
257
        }
258
    }
259

    
260
    /**
261
     * Create, add and return a new action for documents.
262
     * 
263
     * If action already exists don't create and return this.
264
     * 
265
     * @param unique
266
     *            identifier for the action
267
     * @param title
268
     * @param description
269
     * @param order
270
     * @return
271
     */
272
    public DocumentActionGroup addDocumentActionGroup(String id, String title,
273
        String description, int order) {
274
        DocumentActionGroup group = this.documentActionGroups.get(id);
275
        if (group != null) {
276
            return group;
277
        }
278
        group = new DefaultDocumentActionGroup(id, title, description, order);
279
        this.documentActionGroups.put(id, group);
280
        return group;
281
    }
282

    
283
}