Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / ProjectManager.java @ 31496

History | View | Annotate | Download (8.67 KB)

1
/* 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
package org.gvsig.app.project;
42

    
43
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
import org.gvsig.app.extension.ProjectExtension;
54
import org.gvsig.app.project.documents.DefaultDocumentActionGroup;
55
import org.gvsig.app.project.documents.DocumentAction;
56
import org.gvsig.app.project.documents.DocumentActionGroup;
57
import org.gvsig.app.project.documents.Document;
58
import org.gvsig.app.project.documents.DocumentManager;
59
import org.gvsig.app.project.documents.AbstractDocumentManager;
60
import org.gvsig.app.project.documents.gui.ProjectWindow;
61
import org.gvsig.tools.ToolsLocator;
62
import org.gvsig.tools.extensionpoint.ExtensionPoint;
63
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
64
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
65
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
67

    
68

    
69

    
70

    
71
public class ProjectManager {
72
        
73
        private static final  Logger logger = LoggerFactory.getLogger(ProjectManager.class);
74
        private static ProjectManager factory = null;
75

    
76
//        private static final String KEY_PROJECT = "app.project";
77
//        private static final String KEY_DOCUMENTS = "app.project.documents";
78
        private static final String KEY_DOCUMENTS_FACTORIES = "app.project.documents.factories";
79
        private static final String KEY_DOCUMENTS_ACTIONS = "app.project.documents.actions";
80

    
81
        private Map<String,DocumentActionGroup> documentActionGroups;
82
        
83
        public static ProjectManager getInstance() {
84
                if( factory == null ) {
85
                        factory = new ProjectManager();
86
                }
87
                return factory;
88
        }
89
        
90
        private ProjectManager() {
91
                this.documentActionGroups = new HashMap<String,DocumentActionGroup>();
92
        }
93
        
94
        public Project getCurrentProject() {
95
                return ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
96
        }
97

    
98
        public ProjectWindow getCurrentProjectWindow() {
99
                ProjectExtension projectExtension = (ProjectExtension)PluginServices.getExtension(ProjectExtension.class);
100
                ProjectWindow window = (ProjectWindow) projectExtension.getProjectWindow();
101
                return window;
102
        }
103
    
104

    
105
        @SuppressWarnings("unchecked")
106
        public List<DocumentManager> getDocumentManager() {
107
                Iterator<Extension> iterator = ToolsLocator.getExtensionPointManager().get(KEY_DOCUMENTS_FACTORIES).iterator();
108
                List<DocumentManager> factories  = new ArrayList<DocumentManager>();
109
                while (iterator.hasNext()) {
110
                        Extension extension = iterator.next();
111
                        try {
112
                                factories.add( (DocumentManager) extension.create() );
113
                        } catch (InstantiationException e) {
114
                                logger.error("Can't access to project document factory ("+ extension.getName() + ").");
115
                        } catch (IllegalAccessException e) {
116
                                logger.error("Can't access to project document factory ("+ extension.getName() + ").");
117
                        }
118
                }
119
                return factories;
120
        }
121
        
122
        public DocumentManager getDocumentManagers(String type) {
123
                DocumentManager factory = null;
124
                try {
125
                        factory = (DocumentManager) ToolsLocator.getExtensionPointManager().get(KEY_DOCUMENTS_FACTORIES).create(type);
126
                } catch (Exception ex) {
127
                        logger.warn(
128
                                        MessageFormat.format("Can't locate factory for documents of type {1}",type), 
129
                                        ex
130
                        );
131
                }
132
                return factory;
133
        }
134

    
135
        public Document createDocument(String type) {
136
                Document  doc = getDocumentManagers(type).createDocument();
137
                return doc;
138
        }
139
        
140
        public Document createDocument(String type, String name) {
141
                Document  doc = createDocument(type);
142
                doc.setName(name);
143
                return doc;
144
        }
145
        
146
        public Document createDocumentByUser(String type) {
147
                Document  doc = getDocumentManagers(type).createDocumentByUser();
148
                return doc;
149
        }
150
        
151
        public Project createProject(){
152
                return new DefaultProject();
153
        }
154

    
155
        public ProjectExtent createExtent(){
156
                return new ProjectExtent();
157
        }
158

    
159
        @SuppressWarnings("unchecked")
160
        public void registerDocumentFactory(Class factoryClass) {
161
            ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
162
            AbstractDocumentManager factory;
163
                try {
164
                        factory = (AbstractDocumentManager) factoryClass.newInstance();
165
                } catch (InstantiationException e) {
166
                        throw new IllegalArgumentException("Can't create factory.", e);
167
                } catch (IllegalAccessException e) {
168
                        throw new IllegalArgumentException("Can't create factory.", e);
169
                } 
170
            manager.add(KEY_DOCUMENTS_FACTORIES).append(factory.getTypeName(),null,factory);
171
    }
172

    
173
    public void registerDocumentFactoryAlias(String typeName, String alias) {
174
            ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
175

    
176
            manager.get(KEY_DOCUMENTS_FACTORIES).addAlias(typeName, alias);
177
    }
178

    
179
    public void registerDocumentAction(String typeName, DocumentAction action) {
180
            ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
181

    
182
        String key = KEY_DOCUMENTS_ACTIONS + "." + typeName; 
183
            String description = MessageFormat.format( "Actions for {1} documents ", typeName);
184
            
185
            manager.add(key,description).append(
186
                            action.getId(), 
187
                            action.getDescription(),
188
                            action
189
                    );
190
    }
191

    
192
    /**
193
     * Gets a list of actions for the document type especified.
194
     *  
195
     *  La lista esta ordenada deacuerdo al orden especificado en 
196
     *  las acciones y grupos de acciones involucrados.
197
     *   
198
     * @param doctype
199
     * @return list of actions as List<DocumentAction>
200
     */
201
    @SuppressWarnings("unchecked")
202
        public List<DocumentAction> getDocumentActions(String doctype) {
203
                
204
            ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
205

    
206
            String key = KEY_DOCUMENTS_ACTIONS + "." + doctype;
207
            ExtensionPoint extensionPoint = manager.get(key);
208
            if( extensionPoint == null ) {
209
                    // No hay acciones registradas para ese tipo de documento
210
                    return null;
211
            }
212
            List<DocumentAction> actions = new ArrayList<DocumentAction>();
213
            Iterator<Extension> it = extensionPoint.iterator();
214
            while( it.hasNext() ) {
215
                        try {
216
                            DocumentAction action;
217
                                action = (DocumentAction) it.next().create();
218
                                   actions.add(action);
219
                        } catch (InstantiationException e) {
220
                                logger.warn("Can't retrieve document action", e);
221
                        } catch (IllegalAccessException e) {
222
                                logger.warn("Can't retrieve document action", e);
223
                        }
224
            }
225
            if( actions.size() < 1 ) {
226
                    return null;
227
            }
228
            Collections.sort(actions,new CompareAction());
229
            return actions;
230
    }
231
    
232
        private class CompareAction implements Comparator<DocumentAction>{
233
                public int compare(DocumentAction action1, DocumentAction action2) {
234
                        //FIXME: flata formatear los enteros!!!!
235
                        String key1 = MessageFormat.format(
236
                                        "{1}.{2}.{3}",
237
                                        action1.getGroup().getOrder(),
238
                                        action1.getGroup().getTitle(),
239
                                        action1.getOrder()
240
                        );
241
                        String key2 = MessageFormat.format(
242
                                        "{1}.{2}.{3}",
243
                                        action2.getGroup().getOrder(),
244
                                        action2.getGroup().getTitle(),
245
                                        action2.getOrder()
246
                        );
247
                        return key1.compareTo(key2);
248
                }
249
        }
250
        
251
    /**
252
     * Create, add and return a new action for documents.
253
     * 
254
     * If action already exists don't create and return this.
255
     * 
256
     * @param unique identifier for the action 
257
     * @param title
258
     * @param description
259
     * @param order
260
     * @return
261
     */
262
    public DocumentActionGroup addDocumentActionGroup(String id, String title, String description, int order){
263
            DocumentActionGroup group = this.documentActionGroups.get(id);
264
            if( group != null ) {
265
                    return group;
266
            }
267
            group = new DefaultDocumentActionGroup(id, title, description, order);
268
            this.documentActionGroups.put(id, group);
269
            return group;
270
    }
271

    
272
}