Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / ProjectManager.java @ 43888

History | View | Annotate | Download (11.5 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.EventListener;
31
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
import org.gvsig.tools.util.BaseListenerSupport;
53
import org.gvsig.tools.util.BaseListenerSupport.NotificationListener;
54

    
55
public class ProjectManager extends BaseWeakReferencingObservable {
56

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

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

    
68
    private Map<String, DocumentActionGroup> documentActionGroups;
69

    
70
    private final BaseListenerSupport newProjectListeners;
71
            
72
    public interface ProjectEvent extends EventListener {
73
        public Project getProject();
74
    }
75

    
76
    public interface NewProjectEvent extends ProjectEvent {
77
        
78
    }
79
    
80
    public interface ProjectLoadedEvent extends ProjectEvent {
81
        
82
    }    
83
    
84
    public static ProjectManager getInstance() {
85
        if (factory == null) {
86
            factory = new ProjectManager();
87
        }
88
        return factory;
89
    }
90

    
91
    private ProjectManager() {
92
        this.documentActionGroups = new HashMap<String, DocumentActionGroup>();
93
        this.newProjectListeners = new BaseListenerSupport();
94
    }
95

    
96
    
97
    public Project getCurrentProject() {
98
        return ((ProjectExtension) PluginServices
99
            .getExtension(ProjectExtension.class)).getProject();
100
    }
101
    
102
    public void setCurrentProject(Project project) {
103
        ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).setProject(project);
104
    }
105

    
106
    public ProjectWindow getCurrentProjectWindow() {
107
        ProjectExtension projectExtension =
108
            (ProjectExtension) PluginServices
109
                .getExtension(ProjectExtension.class);
110
        ProjectWindow window =
111
            (ProjectWindow) projectExtension.getProjectWindow();
112
        return window;
113
    }
114

    
115
    /**
116
     * @deprecated use {@link #getDocumentManagers()} instead.
117
     */
118
    public List<DocumentManager> getDocumentManager() {
119
        return getDocumentManagers();
120
    }
121

    
122
    @SuppressWarnings("unchecked")
123
    public List<DocumentManager> getDocumentManagers() {
124
        Iterator<Extension> iterator =
125
            ToolsLocator.getExtensionPointManager()
126
                .get(KEY_DOCUMENTS_FACTORIES).iterator();
127
        List<DocumentManager> factories = new ArrayList<DocumentManager>();
128
        while (iterator.hasNext()) {
129
            Extension extension = iterator.next();
130
            try {
131
                factories.add((DocumentManager) extension.create());
132
            } catch (InstantiationException e) {
133
                logger.error("Can't access to project document factory ("
134
                    + extension.getName() + ").");
135
            } catch (IllegalAccessException e) {
136
                logger.error("Can't access to project document factory ("
137
                    + extension.getName() + ").");
138
            }
139
        }
140
        return factories;
141
    }
142

    
143
    /**
144
     * @deprecated use {@link #getDocumentManager(String)} instead.
145
     */
146
    public DocumentManager getDocumentManagers(String type) {
147
        return getDocumentManager(type);
148
    }
149

    
150
    public DocumentManager getDocumentManager(String type) {
151
        DocumentManager factory = null;
152
        try {
153
            factory =
154
                (DocumentManager) ToolsLocator.getExtensionPointManager()
155
                    .get(KEY_DOCUMENTS_FACTORIES).create(type);
156
        } catch (Exception ex) {
157
            logger
158
                .warn(
159
                    MessageFormat.format(
160
                        "Unable to locate factory for documents of type {0}",
161
                        type), ex);
162
        }
163
        return factory;
164
    }
165

    
166
    public Document createDocument(String type) {
167
        Document doc = getDocumentManager(type).createDocument();
168
        return doc;
169
    }
170

    
171
    public Document createDocument(String type, String name) {
172
        Document doc = createDocument(type);
173
        doc.setName(name);
174
        return doc;
175
    }
176

    
177
    /**
178
     * @deprecated use {@link #createDocumentsByUser(String)} instead
179
     */
180
    public Document createDocumentByUser(String type) {
181
        Document doc = getDocumentManager(type).createDocumentByUser();
182
        return doc;
183
    }
184

    
185
    /**
186
     * Creates a group of documents of a given type through the user interface.
187
     * 
188
     * @param type
189
     *            the type of documents to create
190
     * @return the created documents
191
     */
192
    public Iterator<? extends Document> createDocumentsByUser(String type) {
193
        logger.info("createDocumentsByUser('{}')", type);
194
        return getDocumentManager(type).createDocumentsByUser();
195
    }
196

    
197
    public Project createProject() {
198
        return new DefaultProject();
199
    }
200

    
201
    public ProjectExtent createExtent() {
202
        return new ProjectExtent();
203
    }
204

    
205
    public void registerDocumentFactory(DocumentManager documentManager) {
206
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
207
        manager.add(KEY_DOCUMENTS_FACTORIES).append(
208
            documentManager.getTypeName(), null, documentManager);
209
        notifyObservers();
210
    }
211

    
212
    public void registerDocumentFactoryAlias(String typeName, String alias) {
213
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
214

    
215
        manager.get(KEY_DOCUMENTS_FACTORIES).addAlias(typeName, alias);
216
    }
217

    
218
    public void registerDocumentAction(String typeName, DocumentAction action) {
219
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
220

    
221
        String key = KEY_DOCUMENTS_ACTIONS + "." + typeName;
222
        String description =
223
            MessageFormat.format("Actions for {1} documents ", typeName);
224

    
225
        manager.add(key, description).append(action.getId(),
226
            action.getDescription(), action);
227
    }
228

    
229
    /**
230
     * Gets a list of actions for the document type especified.
231
     * 
232
     * La lista esta ordenada deacuerdo al orden especificado en
233
     * las acciones y grupos de acciones involucrados.
234
     * 
235
     * @param doctype
236
     * @return list of actions as List<DocumentAction>
237
     */
238
    @SuppressWarnings("unchecked")
239
    public List<DocumentAction> getDocumentActions(String doctype) {
240

    
241
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
242

    
243
        String key = KEY_DOCUMENTS_ACTIONS + "." + doctype;
244
        ExtensionPoint extensionPoint = manager.get(key);
245
        if (extensionPoint == null) {
246
            // No hay acciones registradas para ese tipo de documento
247
            return null;
248
        }
249
        List<DocumentAction> actions = new ArrayList<DocumentAction>();
250
        Iterator<Extension> it = extensionPoint.iterator();
251
        while (it.hasNext()) {
252
            try {
253
                DocumentAction action;
254
                action = (DocumentAction) it.next().create();
255
                actions.add(action);
256
            } catch (InstantiationException e) {
257
                logger.warn("Can't retrieve document action", e);
258
            } catch (IllegalAccessException e) {
259
                logger.warn("Can't retrieve document action", e);
260
            }
261
        }
262
        if (actions.size() < 1) {
263
            return null;
264
        }
265
        Collections.sort(actions, new CompareAction());
266
        return actions;
267
    }
268

    
269
    private class CompareAction implements Comparator<DocumentAction> {
270

    
271
        public int compare(DocumentAction action1, DocumentAction action2) {
272
            // FIXME: flata formatear los enteros!!!!
273
            String key1 =
274
                MessageFormat.format("{1}.{2}.{3}", action1.getGroup()
275
                    .getOrder(), action1.getGroup().getTitle(), action1
276
                    .getOrder());
277
            String key2 =
278
                MessageFormat.format("{1}.{2}.{3}", action2.getGroup()
279
                    .getOrder(), action2.getGroup().getTitle(), action2
280
                    .getOrder());
281
            return key1.compareTo(key2);
282
        }
283
    }
284

    
285
    /**
286
     * Create, add and return a new action for documents.
287
     * 
288
     * If action already exists don't create and return this.
289
     * 
290
     * @param unique
291
     *            identifier for the action
292
     * @param title
293
     * @param description
294
     * @param order
295
     * @return
296
     */
297
    public DocumentActionGroup addDocumentActionGroup(String id, String title,
298
        String description, int order) {
299
        DocumentActionGroup group = this.documentActionGroups.get(id);
300
        if (group != null) {
301
            return group;
302
        }
303
        group = new DefaultDocumentActionGroup(id, title, description, order);
304
        this.documentActionGroups.put(id, group);
305
        return group;
306
    }
307

    
308
    public ProjectPreferences getProjectPreferences() {
309
        return DefaultProject.getPreferences();
310
    }
311

    
312
    public void addProjectListener(NotificationListener listener) {
313
        this.newProjectListeners.addListener(listener);
314
    }
315

    
316
    public NotificationListener[] getProjectListeners() {
317
        return (NotificationListener[]) this.newProjectListeners.getListeners();
318
    }
319

    
320
    public boolean hasProjectListeners() {
321
        return this.newProjectListeners.hasListeners();
322
    }
323
    
324
    public void removeProjectListener(NotificationListener listener) {
325
        this.newProjectListeners.removeListener(listener);
326
    }
327

    
328
    public void removeAllProjectListener() {    
329
        this.newProjectListeners.removeAllListener();
330
    }
331
    
332
    public void notifyProjectEvent(ProjectEvent event) {
333
        this.newProjectListeners.notifyEvent(event);
334
    }
335
}