Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2020 / applications / appgvSIG / src / org / gvsig / app / project / ProjectManager.java @ 33813

History | View | Annotate | Download (8.35 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.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55
import org.gvsig.andami.PluginServices;
56
import org.gvsig.app.extension.ProjectExtension;
57
import org.gvsig.app.project.documents.DefaultDocumentActionGroup;
58
import org.gvsig.app.project.documents.Document;
59
import org.gvsig.app.project.documents.DocumentAction;
60
import org.gvsig.app.project.documents.DocumentActionGroup;
61
import org.gvsig.app.project.documents.DocumentManager;
62
import org.gvsig.app.project.documents.gui.ProjectWindow;
63
import org.gvsig.tools.ToolsLocator;
64
import org.gvsig.tools.extensionpoint.ExtensionPoint;
65
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
66
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
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(
129
                        "Unable to locate factory for documents of type {0}",
130
                        type),
131
                                        ex
132
                        );
133
                }
134
                return factory;
135
        }
136

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

    
157
        public ProjectExtent createExtent(){
158
                return new ProjectExtent();
159
        }
160

    
161
        public void registerDocumentFactory(DocumentManager documentManager) {
162
            ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
163
            manager.add(KEY_DOCUMENTS_FACTORIES).append(documentManager.getTypeName(),null,documentManager);
164
    }
165

    
166
    public void registerDocumentFactoryAlias(String typeName, String alias) {
167
            ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
168

    
169
            manager.get(KEY_DOCUMENTS_FACTORIES).addAlias(typeName, alias);
170
    }
171

    
172
    public void registerDocumentAction(String typeName, DocumentAction action) {
173
            ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
174

    
175
        String key = KEY_DOCUMENTS_ACTIONS + "." + typeName; 
176
            String description = MessageFormat.format( "Actions for {1} documents ", typeName);
177
            
178
            manager.add(key,description).append(
179
                            action.getId(), 
180
                            action.getDescription(),
181
                            action
182
                    );
183
    }
184

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

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

    
265
}