Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / ProjectManager.java @ 33408

History | View | Annotate | Download (8.71 KB)

1 1103 fjp
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41 29598 jpiera
package org.gvsig.app.project;
42 312 fernando
43 31496 jjdelcerro
import java.text.MessageFormat;
44
import java.util.ArrayList;
45
import java.util.Collections;
46
import java.util.Comparator;
47
import java.util.HashMap;
48
import java.util.Iterator;
49
import java.util.List;
50
import java.util.Map;
51
52
import org.gvsig.andami.PluginServices;
53 31547 jjdelcerro
import org.gvsig.app.ApplicationLocator;
54 31496 jjdelcerro
import org.gvsig.app.extension.ProjectExtension;
55
import org.gvsig.app.project.documents.DefaultDocumentActionGroup;
56
import org.gvsig.app.project.documents.DocumentAction;
57
import org.gvsig.app.project.documents.DocumentActionGroup;
58
import org.gvsig.app.project.documents.Document;
59
import org.gvsig.app.project.documents.DocumentManager;
60
import org.gvsig.app.project.documents.AbstractDocumentManager;
61
import org.gvsig.app.project.documents.gui.ProjectWindow;
62 24956 jmvivo
import org.gvsig.tools.ToolsLocator;
63 31496 jjdelcerro
import org.gvsig.tools.extensionpoint.ExtensionPoint;
64
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
65
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
66
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
68 20994 jmvivo
69 312 fernando
70
71
72 31496 jjdelcerro
public class ProjectManager {
73
74
        private static final  Logger logger = LoggerFactory.getLogger(ProjectManager.class);
75
        private static ProjectManager factory = null;
76
77
//        private static final String KEY_PROJECT = "app.project";
78
//        private static final String KEY_DOCUMENTS = "app.project.documents";
79
        private static final String KEY_DOCUMENTS_FACTORIES = "app.project.documents.factories";
80
        private static final String KEY_DOCUMENTS_ACTIONS = "app.project.documents.actions";
81
82
        private Map<String,DocumentActionGroup> documentActionGroups;
83
84
        public static ProjectManager getInstance() {
85
                if( factory == null ) {
86
                        factory = new ProjectManager();
87 7343 caballero
                }
88 31496 jjdelcerro
                return factory;
89 312 fernando
        }
90 31496 jjdelcerro
91
        private ProjectManager() {
92
                this.documentActionGroups = new HashMap<String,DocumentActionGroup>();
93
        }
94
95
        public Project getCurrentProject() {
96
                return ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
97
        }
98 3940 caballero
99 31496 jjdelcerro
        public ProjectWindow getCurrentProjectWindow() {
100
                ProjectExtension projectExtension = (ProjectExtension)PluginServices.getExtension(ProjectExtension.class);
101
                ProjectWindow window = (ProjectWindow) projectExtension.getProjectWindow();
102
                return window;
103 312 fernando
        }
104 31496 jjdelcerro
105 3940 caballero
106 31496 jjdelcerro
        @SuppressWarnings("unchecked")
107
        public List<DocumentManager> getDocumentManager() {
108
                Iterator<Extension> iterator = ToolsLocator.getExtensionPointManager().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 ("+ extension.getName() + ").");
116
                        } catch (IllegalAccessException e) {
117
                                logger.error("Can't access to project document factory ("+ extension.getName() + ").");
118
                        }
119
                }
120
                return factories;
121 312 fernando
        }
122 31496 jjdelcerro
123
        public DocumentManager getDocumentManagers(String type) {
124
                DocumentManager factory = null;
125 7343 caballero
                try {
126 31496 jjdelcerro
                        factory = (DocumentManager) ToolsLocator.getExtensionPointManager().get(KEY_DOCUMENTS_FACTORIES).create(type);
127
                } catch (Exception ex) {
128
                        logger.warn(
129
                                        MessageFormat.format("Can't locate factory for documents of type {1}",type),
130
                                        ex
131
                        );
132 7343 caballero
                }
133 31496 jjdelcerro
                return factory;
134 312 fernando
        }
135 3940 caballero
136 31496 jjdelcerro
        public Document createDocument(String type) {
137
                Document  doc = getDocumentManagers(type).createDocument();
138
                return doc;
139 312 fernando
        }
140 31496 jjdelcerro
141
        public Document createDocument(String type, String name) {
142
                Document  doc = createDocument(type);
143
                doc.setName(name);
144
                return doc;
145
        }
146
147
        public Document createDocumentByUser(String type) {
148
                Document  doc = getDocumentManagers(type).createDocumentByUser();
149
                return doc;
150
        }
151
152
        public Project createProject(){
153
                return new DefaultProject();
154
        }
155 3940 caballero
156 31496 jjdelcerro
        public ProjectExtent createExtent(){
157 412 vcaballero
                return new ProjectExtent();
158 312 fernando
        }
159 3940 caballero
160 31496 jjdelcerro
        @SuppressWarnings("unchecked")
161
        public void registerDocumentFactory(Class factoryClass) {
162
            ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
163
            AbstractDocumentManager factory;
164
                try {
165
                        factory = (AbstractDocumentManager) factoryClass.newInstance();
166
                } catch (InstantiationException e) {
167
                        throw new IllegalArgumentException("Can't create factory.", e);
168
                } catch (IllegalAccessException e) {
169
                        throw new IllegalArgumentException("Can't create factory.", e);
170
                }
171
            manager.add(KEY_DOCUMENTS_FACTORIES).append(factory.getTypeName(),null,factory);
172
    }
173 3940 caballero
174 31496 jjdelcerro
    public void registerDocumentFactoryAlias(String typeName, String alias) {
175
            ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
176 10626 caballero
177 31496 jjdelcerro
            manager.get(KEY_DOCUMENTS_FACTORIES).addAlias(typeName, alias);
178
    }
179
180
    public void registerDocumentAction(String typeName, DocumentAction action) {
181
            ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
182
183
        String key = KEY_DOCUMENTS_ACTIONS + "." + typeName;
184
            String description = MessageFormat.format( "Actions for {1} documents ", typeName);
185
186
            manager.add(key,description).append(
187
                            action.getId(),
188
                            action.getDescription(),
189
                            action
190
                    );
191
    }
192
193
    /**
194
     * Gets a list of actions for the document type especified.
195
     *
196
     *  La lista esta ordenada deacuerdo al orden especificado en
197
     *  las acciones y grupos de acciones involucrados.
198
     *
199
     * @param doctype
200
     * @return list of actions as List<DocumentAction>
201
     */
202
    @SuppressWarnings("unchecked")
203
        public List<DocumentAction> getDocumentActions(String doctype) {
204
205
            ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
206
207
            String key = KEY_DOCUMENTS_ACTIONS + "." + doctype;
208
            ExtensionPoint extensionPoint = manager.get(key);
209
            if( extensionPoint == null ) {
210
                    // No hay acciones registradas para ese tipo de documento
211
                    return null;
212
            }
213
            List<DocumentAction> actions = new ArrayList<DocumentAction>();
214
            Iterator<Extension> it = extensionPoint.iterator();
215
            while( it.hasNext() ) {
216
                        try {
217
                            DocumentAction action;
218
                                action = (DocumentAction) it.next().create();
219
                                   actions.add(action);
220
                        } catch (InstantiationException e) {
221
                                logger.warn("Can't retrieve document action", e);
222
                        } catch (IllegalAccessException e) {
223
                                logger.warn("Can't retrieve document action", e);
224
                        }
225
            }
226
            if( actions.size() < 1 ) {
227
                    return null;
228
            }
229
            Collections.sort(actions,new CompareAction());
230
            return actions;
231
    }
232
233
        private class CompareAction implements Comparator<DocumentAction>{
234
                public int compare(DocumentAction action1, DocumentAction action2) {
235
                        //FIXME: flata formatear los enteros!!!!
236
                        String key1 = MessageFormat.format(
237
                                        "{1}.{2}.{3}",
238
                                        action1.getGroup().getOrder(),
239
                                        action1.getGroup().getTitle(),
240
                                        action1.getOrder()
241
                        );
242
                        String key2 = MessageFormat.format(
243
                                        "{1}.{2}.{3}",
244
                                        action2.getGroup().getOrder(),
245
                                        action2.getGroup().getTitle(),
246
                                        action2.getOrder()
247
                        );
248
                        return key1.compareTo(key2);
249
                }
250
        }
251
252
    /**
253
     * Create, add and return a new action for documents.
254
     *
255
     * If action already exists don't create and return this.
256
     *
257
     * @param unique identifier for the action
258
     * @param title
259
     * @param description
260
     * @param order
261
     * @return
262
     */
263
    public DocumentActionGroup addDocumentActionGroup(String id, String title, String description, int order){
264
            DocumentActionGroup group = this.documentActionGroups.get(id);
265
            if( group != null ) {
266
                    return group;
267
            }
268
            group = new DefaultDocumentActionGroup(id, title, description, order);
269
            this.documentActionGroups.put(id, group);
270
            return group;
271
    }
272
273 312 fernando
}