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

History | View | Annotate | Download (10.2 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 ProjectWindow getCurrentProjectWindow() {
89
        ProjectExtension projectExtension =
90
            (ProjectExtension) PluginServices
91
                .getExtension(ProjectExtension.class);
92
        ProjectWindow window =
93
            (ProjectWindow) projectExtension.getProjectWindow();
94
        return window;
95
    }
96

    
97
    /**
98
     * @deprecated use {@link #getDocumentManagers()} instead.
99
     */
100
    public List<DocumentManager> getDocumentManager() {
101
        return getDocumentManagers();
102
    }
103

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

    
125
    /**
126
     * @deprecated use {@link #getDocumentManager(String)} instead.
127
     */
128
    public DocumentManager getDocumentManagers(String type) {
129
        return getDocumentManager(type);
130
    }
131

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

    
148
    public Document createDocument(String type) {
149
        Document doc = getDocumentManager(type).createDocument();
150
        return doc;
151
    }
152

    
153
    public Document createDocument(String type, String name) {
154
        Document doc = createDocument(type);
155
        doc.setName(name);
156
        return doc;
157
    }
158

    
159
    /**
160
     * @deprecated use {@link #createDocumentsByUser(String)} instead
161
     */
162
    public Document createDocumentByUser(String type) {
163
        Document doc = getDocumentManager(type).createDocumentByUser();
164
        return doc;
165
    }
166

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

    
179
    public Project createProject() {
180
        return new DefaultProject();
181
    }
182

    
183
    public ProjectExtent createExtent() {
184
        return new ProjectExtent();
185
    }
186

    
187
    public void registerDocumentFactory(DocumentManager documentManager) {
188
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
189
        manager.add(KEY_DOCUMENTS_FACTORIES).append(
190
            documentManager.getTypeName(), null, documentManager);
191
        notifyObservers();
192
    }
193

    
194
    public void registerDocumentFactoryAlias(String typeName, String alias) {
195
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
196

    
197
        manager.get(KEY_DOCUMENTS_FACTORIES).addAlias(typeName, alias);
198
    }
199

    
200
    public void registerDocumentAction(String typeName, DocumentAction action) {
201
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
202

    
203
        String key = KEY_DOCUMENTS_ACTIONS + "." + typeName;
204
        String description =
205
            MessageFormat.format("Actions for {1} documents ", typeName);
206

    
207
        manager.add(key, description).append(action.getId(),
208
            action.getDescription(), action);
209
    }
210

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

    
223
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
224

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

    
251
    private class CompareAction implements Comparator<DocumentAction> {
252

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

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

    
290
    public ProjectPreferences getProjectPreferences() {
291
        return DefaultProject.getPreferences();
292
    }
293
    
294
}