svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / ProjectManager.java @ 36443
History | View | Annotate | Download (9.4 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
package org.gvsig.app.project; |
23 |
|
24 |
import java.text.MessageFormat; |
25 |
import java.util.ArrayList; |
26 |
import java.util.Collections; |
27 |
import java.util.Comparator; |
28 |
import java.util.HashMap; |
29 |
import java.util.Iterator; |
30 |
import java.util.List; |
31 |
import java.util.Map; |
32 |
|
33 |
import org.slf4j.Logger; |
34 |
import org.slf4j.LoggerFactory; |
35 |
|
36 |
import org.gvsig.andami.PluginServices; |
37 |
import org.gvsig.app.extension.ProjectExtension; |
38 |
import org.gvsig.app.project.documents.DefaultDocumentActionGroup; |
39 |
import org.gvsig.app.project.documents.Document; |
40 |
import org.gvsig.app.project.documents.DocumentAction; |
41 |
import org.gvsig.app.project.documents.DocumentActionGroup; |
42 |
import org.gvsig.app.project.documents.DocumentManager; |
43 |
import org.gvsig.app.project.documents.gui.ProjectWindow; |
44 |
import org.gvsig.tools.ToolsLocator; |
45 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
46 |
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
47 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
48 |
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable; |
49 |
|
50 |
public class ProjectManager extends BaseWeakReferencingObservable { |
51 |
|
52 |
private static final Logger logger = LoggerFactory |
53 |
.getLogger(ProjectManager.class); |
54 |
private static ProjectManager factory = null; |
55 |
|
56 |
// private static final String KEY_PROJECT = "app.project";
|
57 |
// private static final String KEY_DOCUMENTS = "app.project.documents";
|
58 |
private static final String KEY_DOCUMENTS_FACTORIES = |
59 |
"app.project.documents.factories";
|
60 |
private static final String KEY_DOCUMENTS_ACTIONS = |
61 |
"app.project.documents.actions";
|
62 |
|
63 |
private Map<String, DocumentActionGroup> documentActionGroups; |
64 |
|
65 |
public static ProjectManager getInstance() { |
66 |
if (factory == null) { |
67 |
factory = new ProjectManager();
|
68 |
} |
69 |
return factory;
|
70 |
} |
71 |
|
72 |
private ProjectManager() {
|
73 |
this.documentActionGroups = new HashMap<String, DocumentActionGroup>(); |
74 |
} |
75 |
|
76 |
public Project getCurrentProject() {
|
77 |
return ((ProjectExtension) PluginServices
|
78 |
.getExtension(ProjectExtension.class)).getProject(); |
79 |
} |
80 |
|
81 |
public ProjectWindow getCurrentProjectWindow() {
|
82 |
ProjectExtension projectExtension = |
83 |
(ProjectExtension) PluginServices |
84 |
.getExtension(ProjectExtension.class); |
85 |
ProjectWindow window = |
86 |
(ProjectWindow) projectExtension.getProjectWindow(); |
87 |
return window;
|
88 |
} |
89 |
|
90 |
/**
|
91 |
* @deprecated use {@link #getDocumentManagers()} instead.
|
92 |
*/
|
93 |
public List<DocumentManager> getDocumentManager() { |
94 |
return getDocumentManagers();
|
95 |
} |
96 |
|
97 |
@SuppressWarnings("unchecked") |
98 |
public List<DocumentManager> getDocumentManagers() { |
99 |
Iterator<Extension> iterator =
|
100 |
ToolsLocator.getExtensionPointManager() |
101 |
.get(KEY_DOCUMENTS_FACTORIES).iterator(); |
102 |
List<DocumentManager> factories = new ArrayList<DocumentManager>(); |
103 |
while (iterator.hasNext()) {
|
104 |
Extension extension = iterator.next(); |
105 |
try {
|
106 |
factories.add((DocumentManager) extension.create()); |
107 |
} catch (InstantiationException e) { |
108 |
logger.error("Can't access to project document factory ("
|
109 |
+ extension.getName() + ").");
|
110 |
} catch (IllegalAccessException e) { |
111 |
logger.error("Can't access to project document factory ("
|
112 |
+ extension.getName() + ").");
|
113 |
} |
114 |
} |
115 |
return factories;
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* @deprecated use {@link #getDocumentManager(String)} instead.
|
120 |
*/
|
121 |
public DocumentManager getDocumentManagers(String type) { |
122 |
return getDocumentManager(type);
|
123 |
} |
124 |
|
125 |
public DocumentManager getDocumentManager(String type) { |
126 |
DocumentManager factory = null;
|
127 |
try {
|
128 |
factory = |
129 |
(DocumentManager) ToolsLocator.getExtensionPointManager() |
130 |
.get(KEY_DOCUMENTS_FACTORIES).create(type); |
131 |
} catch (Exception ex) { |
132 |
logger |
133 |
.warn( |
134 |
MessageFormat.format(
|
135 |
"Unable to locate factory for documents of type {0}",
|
136 |
type), ex); |
137 |
} |
138 |
return factory;
|
139 |
} |
140 |
|
141 |
public Document createDocument(String type) { |
142 |
Document doc = getDocumentManager(type).createDocument();
|
143 |
return doc;
|
144 |
} |
145 |
|
146 |
public Document createDocument(String type, String name) { |
147 |
Document doc = createDocument(type);
|
148 |
doc.setName(name); |
149 |
return doc;
|
150 |
} |
151 |
|
152 |
public Document createDocumentByUser(String type) { |
153 |
Document doc = getDocumentManager(type).createDocumentByUser();
|
154 |
return doc;
|
155 |
} |
156 |
|
157 |
public Project createProject() {
|
158 |
return new DefaultProject(); |
159 |
} |
160 |
|
161 |
public ProjectExtent createExtent() {
|
162 |
return new ProjectExtent(); |
163 |
} |
164 |
|
165 |
public void registerDocumentFactory(DocumentManager documentManager) { |
166 |
ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
167 |
manager.add(KEY_DOCUMENTS_FACTORIES).append( |
168 |
documentManager.getTypeName(), null, documentManager);
|
169 |
notifyObservers(); |
170 |
} |
171 |
|
172 |
public void registerDocumentFactoryAlias(String typeName, String alias) { |
173 |
ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
174 |
|
175 |
manager.get(KEY_DOCUMENTS_FACTORIES).addAlias(typeName, alias); |
176 |
} |
177 |
|
178 |
public void registerDocumentAction(String typeName, DocumentAction action) { |
179 |
ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
180 |
|
181 |
String key = KEY_DOCUMENTS_ACTIONS + "." + typeName; |
182 |
String description =
|
183 |
MessageFormat.format("Actions for {1} documents ", typeName); |
184 |
|
185 |
manager.add(key, description).append(action.getId(), |
186 |
action.getDescription(), action); |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Gets a list of actions for the document type especified.
|
191 |
*
|
192 |
* La lista esta ordenada deacuerdo al orden especificado en
|
193 |
* las acciones y grupos de acciones involucrados.
|
194 |
*
|
195 |
* @param doctype
|
196 |
* @return list of actions as List<DocumentAction>
|
197 |
*/
|
198 |
@SuppressWarnings("unchecked") |
199 |
public List<DocumentAction> getDocumentActions(String doctype) { |
200 |
|
201 |
ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
202 |
|
203 |
String key = KEY_DOCUMENTS_ACTIONS + "." + doctype; |
204 |
ExtensionPoint extensionPoint = manager.get(key); |
205 |
if (extensionPoint == null) { |
206 |
// No hay acciones registradas para ese tipo de documento
|
207 |
return null; |
208 |
} |
209 |
List<DocumentAction> actions = new ArrayList<DocumentAction>(); |
210 |
Iterator<Extension> it = extensionPoint.iterator();
|
211 |
while (it.hasNext()) {
|
212 |
try {
|
213 |
DocumentAction action; |
214 |
action = (DocumentAction) it.next().create(); |
215 |
actions.add(action); |
216 |
} catch (InstantiationException e) { |
217 |
logger.warn("Can't retrieve document action", e);
|
218 |
} catch (IllegalAccessException e) { |
219 |
logger.warn("Can't retrieve document action", e);
|
220 |
} |
221 |
} |
222 |
if (actions.size() < 1) { |
223 |
return null; |
224 |
} |
225 |
Collections.sort(actions, new CompareAction()); |
226 |
return actions;
|
227 |
} |
228 |
|
229 |
private class CompareAction implements Comparator<DocumentAction> { |
230 |
|
231 |
public int compare(DocumentAction action1, DocumentAction action2) { |
232 |
// FIXME: flata formatear los enteros!!!!
|
233 |
String key1 =
|
234 |
MessageFormat.format("{1}.{2}.{3}", action1.getGroup() |
235 |
.getOrder(), action1.getGroup().getTitle(), action1 |
236 |
.getOrder()); |
237 |
String key2 =
|
238 |
MessageFormat.format("{1}.{2}.{3}", action2.getGroup() |
239 |
.getOrder(), action2.getGroup().getTitle(), action2 |
240 |
.getOrder()); |
241 |
return key1.compareTo(key2);
|
242 |
} |
243 |
} |
244 |
|
245 |
/**
|
246 |
* Create, add and return a new action for documents.
|
247 |
*
|
248 |
* If action already exists don't create and return this.
|
249 |
*
|
250 |
* @param unique
|
251 |
* identifier for the action
|
252 |
* @param title
|
253 |
* @param description
|
254 |
* @param order
|
255 |
* @return
|
256 |
*/
|
257 |
public DocumentActionGroup addDocumentActionGroup(String id, String title, |
258 |
String description, int order) { |
259 |
DocumentActionGroup group = this.documentActionGroups.get(id);
|
260 |
if (group != null) { |
261 |
return group;
|
262 |
} |
263 |
group = new DefaultDocumentActionGroup(id, title, description, order);
|
264 |
this.documentActionGroups.put(id, group);
|
265 |
return group;
|
266 |
} |
267 |
|
268 |
} |