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

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 static ProjectManager getInstance() {
81
        if (factory == null) {
82
            factory = new ProjectManager();
83
        }
84
        return factory;
85
    }
86

    
87
    private ProjectManager() {
88
        this.documentActionGroups = new HashMap<String, DocumentActionGroup>();
89
        this.newProjectListeners = new BaseListenerSupport();
90
    }
91

    
92
    
93
    public Project getCurrentProject() {
94
        ProjectExtension ext = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
95
        if( ext==null ) {
96
            return null;
97
        }
98
        return ext.getProject();
99
    }
100
    
101
    public void setCurrentProject(Project project) {
102
        ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).setProject(project);
103
    }
104

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
240
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
241

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

    
268
    private class CompareAction implements Comparator<DocumentAction> {
269

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

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

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

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

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

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

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