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

History | View | Annotate | Download (12.1 KB)

1 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40558 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 40435 jjdelcerro
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8 40558 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * of the License, or (at your option) any later version.
10 40558 jjdelcerro
 *
11 40435 jjdelcerro
 * 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 40558 jjdelcerro
 *
16 40435 jjdelcerro
 * 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 40558 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 40435 jjdelcerro
 * MA  02110-1301, USA.
20 40558 jjdelcerro
 *
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 40435 jjdelcerro
 */
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 43879 jjdelcerro
import java.util.EventListener;
31 40435 jjdelcerro
import java.util.HashMap;
32
import java.util.Iterator;
33
import java.util.List;
34
import java.util.Map;
35
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38
39
import org.gvsig.andami.PluginServices;
40
import org.gvsig.app.extension.ProjectExtension;
41
import org.gvsig.app.project.documents.DefaultDocumentActionGroup;
42
import org.gvsig.app.project.documents.Document;
43
import org.gvsig.app.project.documents.DocumentAction;
44
import org.gvsig.app.project.documents.DocumentActionGroup;
45
import org.gvsig.app.project.documents.DocumentManager;
46
import org.gvsig.app.project.documents.gui.ProjectWindow;
47
import org.gvsig.tools.ToolsLocator;
48
import org.gvsig.tools.extensionpoint.ExtensionPoint;
49
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
50
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
51
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable;
52 43879 jjdelcerro
import org.gvsig.tools.util.BaseListenerSupport;
53
import org.gvsig.tools.util.BaseListenerSupport.NotificationListener;
54 45931 jjdelcerro
import org.gvsig.tools.util.Invocable;
55 40435 jjdelcerro
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 43879 jjdelcerro
    private final BaseListenerSupport newProjectListeners;
72
73
    public interface ProjectEvent extends EventListener {
74 45978 jjdelcerro
        public int getId();
75 43879 jjdelcerro
        public Project getProject();
76
    }
77 45978 jjdelcerro
78
    public static class ProjectEventImpl implements ProjectEvent {
79
        private final Project project;
80
        private final int id;
81
        public ProjectEventImpl(Project project, int id) {
82
            this.project = project;
83
            this.id = id;
84
        }
85
        @Override
86
        public int getId() { return this.id; }
87
        @Override
88
        public Project getProject() { return this.project; }
89
    }
90 43879 jjdelcerro
91 40435 jjdelcerro
    public static ProjectManager getInstance() {
92
        if (factory == null) {
93
            factory = new ProjectManager();
94
        }
95
        return factory;
96
    }
97
98
    private ProjectManager() {
99
        this.documentActionGroups = new HashMap<String, DocumentActionGroup>();
100 43879 jjdelcerro
        this.newProjectListeners = new BaseListenerSupport();
101 40435 jjdelcerro
    }
102
103 41311 jjdelcerro
104 40435 jjdelcerro
    public Project getCurrentProject() {
105 45739 jjdelcerro
        ProjectExtension ext = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
106
        if( ext==null ) {
107
            return null;
108
        }
109
        return ext.getProject();
110 40435 jjdelcerro
    }
111 43328 jjdelcerro
112
    public void setCurrentProject(Project project) {
113
        ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).setProject(project);
114
    }
115 40435 jjdelcerro
116
    public ProjectWindow getCurrentProjectWindow() {
117
        ProjectExtension projectExtension =
118
            (ProjectExtension) PluginServices
119
                .getExtension(ProjectExtension.class);
120
        ProjectWindow window =
121
            (ProjectWindow) projectExtension.getProjectWindow();
122
        return window;
123
    }
124
125
    /**
126
     * @deprecated use {@link #getDocumentManagers()} instead.
127
     */
128
    public List<DocumentManager> getDocumentManager() {
129
        return getDocumentManagers();
130
    }
131
132
    @SuppressWarnings("unchecked")
133
    public List<DocumentManager> getDocumentManagers() {
134
        Iterator<Extension> iterator =
135
            ToolsLocator.getExtensionPointManager()
136
                .get(KEY_DOCUMENTS_FACTORIES).iterator();
137
        List<DocumentManager> factories = new ArrayList<DocumentManager>();
138
        while (iterator.hasNext()) {
139
            Extension extension = iterator.next();
140
            try {
141
                factories.add((DocumentManager) extension.create());
142
            } catch (InstantiationException e) {
143
                logger.error("Can't access to project document factory ("
144
                    + extension.getName() + ").");
145
            } catch (IllegalAccessException e) {
146
                logger.error("Can't access to project document factory ("
147
                    + extension.getName() + ").");
148
            }
149
        }
150
        return factories;
151
    }
152
153
    /**
154
     * @deprecated use {@link #getDocumentManager(String)} instead.
155
     */
156
    public DocumentManager getDocumentManagers(String type) {
157
        return getDocumentManager(type);
158
    }
159
160
    public DocumentManager getDocumentManager(String type) {
161
        DocumentManager factory = null;
162
        try {
163
            factory =
164
                (DocumentManager) ToolsLocator.getExtensionPointManager()
165
                    .get(KEY_DOCUMENTS_FACTORIES).create(type);
166
        } catch (Exception ex) {
167
            logger
168
                .warn(
169
                    MessageFormat.format(
170
                        "Unable to locate factory for documents of type {0}",
171
                        type), ex);
172
        }
173
        return factory;
174
    }
175
176
    public Document createDocument(String type) {
177
        Document doc = getDocumentManager(type).createDocument();
178
        return doc;
179
    }
180
181
    public Document createDocument(String type, String name) {
182
        Document doc = createDocument(type);
183
        doc.setName(name);
184
        return doc;
185
    }
186
187
    /**
188
     * @deprecated use {@link #createDocumentsByUser(String)} instead
189
     */
190
    public Document createDocumentByUser(String type) {
191
        Document doc = getDocumentManager(type).createDocumentByUser();
192
        return doc;
193
    }
194
195
    /**
196
     * Creates a group of documents of a given type through the user interface.
197
     *
198
     * @param type
199
     *            the type of documents to create
200
     * @return the created documents
201
     */
202
    public Iterator<? extends Document> createDocumentsByUser(String type) {
203
        logger.info("createDocumentsByUser('{}')", type);
204
        return getDocumentManager(type).createDocumentsByUser();
205
    }
206
207 45931 jjdelcerro
    public Iterator<? extends Document> createDocumentsByUser(String type, Invocable whenDocumentsLoadeds) {
208
        logger.info("createDocumentsByUser('{}')", type);
209
        return getDocumentManager(type).createDocumentsByUser(whenDocumentsLoadeds);
210
    }
211
212 40435 jjdelcerro
    public Project createProject() {
213
        return new DefaultProject();
214
    }
215
216
    public ProjectExtent createExtent() {
217
        return new ProjectExtent();
218
    }
219
220
    public void registerDocumentFactory(DocumentManager documentManager) {
221
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
222
        manager.add(KEY_DOCUMENTS_FACTORIES).append(
223
            documentManager.getTypeName(), null, documentManager);
224
        notifyObservers();
225
    }
226
227
    public void registerDocumentFactoryAlias(String typeName, String alias) {
228
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
229
230
        manager.get(KEY_DOCUMENTS_FACTORIES).addAlias(typeName, alias);
231
    }
232
233
    public void registerDocumentAction(String typeName, DocumentAction action) {
234
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
235
236
        String key = KEY_DOCUMENTS_ACTIONS + "." + typeName;
237
        String description =
238
            MessageFormat.format("Actions for {1} documents ", typeName);
239
240
        manager.add(key, description).append(action.getId(),
241
            action.getDescription(), action);
242
    }
243
244
    /**
245
     * Gets a list of actions for the document type especified.
246
     *
247
     * La lista esta ordenada deacuerdo al orden especificado en
248
     * las acciones y grupos de acciones involucrados.
249
     *
250
     * @param doctype
251
     * @return list of actions as List<DocumentAction>
252
     */
253
    @SuppressWarnings("unchecked")
254
    public List<DocumentAction> getDocumentActions(String doctype) {
255
256
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
257
258
        String key = KEY_DOCUMENTS_ACTIONS + "." + doctype;
259
        ExtensionPoint extensionPoint = manager.get(key);
260
        if (extensionPoint == null) {
261
            // No hay acciones registradas para ese tipo de documento
262
            return null;
263
        }
264
        List<DocumentAction> actions = new ArrayList<DocumentAction>();
265
        Iterator<Extension> it = extensionPoint.iterator();
266
        while (it.hasNext()) {
267
            try {
268
                DocumentAction action;
269
                action = (DocumentAction) it.next().create();
270
                actions.add(action);
271
            } catch (InstantiationException e) {
272
                logger.warn("Can't retrieve document action", e);
273
            } catch (IllegalAccessException e) {
274
                logger.warn("Can't retrieve document action", e);
275
            }
276
        }
277
        if (actions.size() < 1) {
278
            return null;
279
        }
280
        Collections.sort(actions, new CompareAction());
281
        return actions;
282
    }
283
284
    private class CompareAction implements Comparator<DocumentAction> {
285
286
        public int compare(DocumentAction action1, DocumentAction action2) {
287
            // FIXME: flata formatear los enteros!!!!
288
            String key1 =
289
                MessageFormat.format("{1}.{2}.{3}", action1.getGroup()
290
                    .getOrder(), action1.getGroup().getTitle(), action1
291
                    .getOrder());
292
            String key2 =
293
                MessageFormat.format("{1}.{2}.{3}", action2.getGroup()
294
                    .getOrder(), action2.getGroup().getTitle(), action2
295
                    .getOrder());
296
            return key1.compareTo(key2);
297
        }
298
    }
299
300
    /**
301
     * Create, add and return a new action for documents.
302
     *
303
     * If action already exists don't create and return this.
304
     *
305
     * @param unique
306
     *            identifier for the action
307
     * @param title
308
     * @param description
309
     * @param order
310
     * @return
311
     */
312
    public DocumentActionGroup addDocumentActionGroup(String id, String title,
313
        String description, int order) {
314
        DocumentActionGroup group = this.documentActionGroups.get(id);
315
        if (group != null) {
316
            return group;
317
        }
318
        group = new DefaultDocumentActionGroup(id, title, description, order);
319
        this.documentActionGroups.put(id, group);
320
        return group;
321
    }
322
323 41311 jjdelcerro
    public ProjectPreferences getProjectPreferences() {
324
        return DefaultProject.getPreferences();
325
    }
326 43879 jjdelcerro
327
    public void addProjectListener(NotificationListener listener) {
328
        this.newProjectListeners.addListener(listener);
329
    }
330
331
    public NotificationListener[] getProjectListeners() {
332
        return (NotificationListener[]) this.newProjectListeners.getListeners();
333
    }
334
335
    public boolean hasProjectListeners() {
336
        return this.newProjectListeners.hasListeners();
337
    }
338 41350 jjdelcerro
339 43879 jjdelcerro
    public void removeProjectListener(NotificationListener listener) {
340
        this.newProjectListeners.removeListener(listener);
341
    }
342
343
    public void removeAllProjectListener() {
344
        this.newProjectListeners.removeAllListener();
345
    }
346
347
    public void notifyProjectEvent(ProjectEvent event) {
348
        this.newProjectListeners.notifyEvent(event);
349
    }
350 40435 jjdelcerro
}