root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / ProjectManager.java @ 38562
History | View | Annotate | Download (9.89 KB)
1 | 36435 | cordinyana | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | 1103 | fjp | *
|
3 | 36435 | cordinyana | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | 1103 | fjp | * 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 | 36435 | cordinyana | *
|
11 | 1103 | fjp | * 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 | 36435 | cordinyana | *
|
16 | 1103 | fjp | * 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 | 36435 | cordinyana | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | 1103 | fjp | */
|
22 | 29598 | jpiera | package org.gvsig.app.project; |
23 | 312 | fernando | |
24 | 31496 | jjdelcerro | 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 | 33748 | cordinyana | import org.slf4j.Logger; |
34 | import org.slf4j.LoggerFactory; |
||
35 | |||
36 | 31496 | jjdelcerro | import org.gvsig.andami.PluginServices; |
37 | import org.gvsig.app.extension.ProjectExtension; |
||
38 | import org.gvsig.app.project.documents.DefaultDocumentActionGroup; |
||
39 | 33659 | fdiaz | import org.gvsig.app.project.documents.Document; |
40 | 31496 | jjdelcerro | 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 | 24956 | jmvivo | import org.gvsig.tools.ToolsLocator; |
45 | 31496 | jjdelcerro | import org.gvsig.tools.extensionpoint.ExtensionPoint; |
46 | import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
||
47 | 33659 | fdiaz | import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
48 | 36443 | cordinyana | import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable; |
49 | 20994 | jmvivo | |
50 | 36443 | cordinyana | public class ProjectManager extends BaseWeakReferencingObservable { |
51 | 312 | fernando | |
52 | 36435 | cordinyana | private static final Logger logger = LoggerFactory |
53 | .getLogger(ProjectManager.class); |
||
54 | private static ProjectManager factory = null; |
||
55 | 312 | fernando | |
56 | 36435 | cordinyana | // 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 | 312 | fernando | |
63 | 36435 | cordinyana | private Map<String, DocumentActionGroup> documentActionGroups; |
64 | 31496 | jjdelcerro | |
65 | 36435 | cordinyana | public static ProjectManager getInstance() { |
66 | if (factory == null) { |
||
67 | factory = new ProjectManager();
|
||
68 | } |
||
69 | return factory;
|
||
70 | } |
||
71 | 31496 | jjdelcerro | |
72 | 36435 | cordinyana | private ProjectManager() {
|
73 | this.documentActionGroups = new HashMap<String, DocumentActionGroup>(); |
||
74 | } |
||
75 | 3940 | caballero | |
76 | 36435 | cordinyana | 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 | 36434 | cordinyana | /**
|
91 | * @deprecated use {@link #getDocumentManagers()} instead.
|
||
92 | */
|
||
93 | public List<DocumentManager> getDocumentManager() { |
||
94 | return getDocumentManagers();
|
||
95 | } |
||
96 | 3940 | caballero | |
97 | 36435 | cordinyana | @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 | 36434 | cordinyana | /**
|
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 | 36435 | cordinyana | 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 | 33748 | cordinyana | MessageFormat.format(
|
135 | "Unable to locate factory for documents of type {0}",
|
||
136 | 36435 | cordinyana | type), ex); |
137 | } |
||
138 | return factory;
|
||
139 | } |
||
140 | 3940 | caballero | |
141 | 36435 | cordinyana | public Document createDocument(String type) { |
142 | Document doc = getDocumentManager(type).createDocument();
|
||
143 | return doc;
|
||
144 | } |
||
145 | 3940 | caballero | |
146 | 36435 | cordinyana | public Document createDocument(String type, String name) { |
147 | Document doc = createDocument(type);
|
||
148 | doc.setName(name); |
||
149 | return doc;
|
||
150 | } |
||
151 | 3940 | caballero | |
152 | 36631 | cordinyana | /**
|
153 | * @deprecated use {@link #createDocumentsByUser(String)} instead
|
||
154 | */
|
||
155 | 36435 | cordinyana | public Document createDocumentByUser(String type) { |
156 | Document doc = getDocumentManager(type).createDocumentByUser();
|
||
157 | return doc;
|
||
158 | 31496 | jjdelcerro | } |
159 | 3940 | caballero | |
160 | 36631 | cordinyana | /**
|
161 | * Creates a group of documents of a given type through the user interface.
|
||
162 | *
|
||
163 | * @param type
|
||
164 | * the type of documents to create
|
||
165 | * @return the created documents
|
||
166 | */
|
||
167 | public Iterator<? extends Document> createDocumentsByUser(String type) { |
||
168 | 37827 | jjdelcerro | logger.info("createDocumentsByUser('{}')", type);
|
169 | 36631 | cordinyana | return getDocumentManager(type).createDocumentsByUser();
|
170 | } |
||
171 | |||
172 | 36435 | cordinyana | public Project createProject() {
|
173 | return new DefaultProject(); |
||
174 | } |
||
175 | |||
176 | public ProjectExtent createExtent() {
|
||
177 | return new ProjectExtent(); |
||
178 | } |
||
179 | |||
180 | public void registerDocumentFactory(DocumentManager documentManager) { |
||
181 | ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
||
182 | manager.add(KEY_DOCUMENTS_FACTORIES).append( |
||
183 | documentManager.getTypeName(), null, documentManager);
|
||
184 | 36443 | cordinyana | notifyObservers(); |
185 | 36435 | cordinyana | } |
186 | |||
187 | 31496 | jjdelcerro | public void registerDocumentFactoryAlias(String typeName, String alias) { |
188 | 36435 | cordinyana | ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
189 | 10626 | caballero | |
190 | 36435 | cordinyana | manager.get(KEY_DOCUMENTS_FACTORIES).addAlias(typeName, alias); |
191 | 31496 | jjdelcerro | } |
192 | |||
193 | public void registerDocumentAction(String typeName, DocumentAction action) { |
||
194 | 36435 | cordinyana | ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
195 | 31496 | jjdelcerro | |
196 | 36435 | cordinyana | String key = KEY_DOCUMENTS_ACTIONS + "." + typeName; |
197 | String description =
|
||
198 | MessageFormat.format("Actions for {1} documents ", typeName); |
||
199 | |||
200 | manager.add(key, description).append(action.getId(), |
||
201 | action.getDescription(), action); |
||
202 | 31496 | jjdelcerro | } |
203 | |||
204 | /**
|
||
205 | * Gets a list of actions for the document type especified.
|
||
206 | 36435 | cordinyana | *
|
207 | * La lista esta ordenada deacuerdo al orden especificado en
|
||
208 | * las acciones y grupos de acciones involucrados.
|
||
209 | *
|
||
210 | 31496 | jjdelcerro | * @param doctype
|
211 | * @return list of actions as List<DocumentAction>
|
||
212 | */
|
||
213 | @SuppressWarnings("unchecked") |
||
214 | 36435 | cordinyana | public List<DocumentAction> getDocumentActions(String doctype) { |
215 | 31496 | jjdelcerro | |
216 | 36435 | cordinyana | ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
217 | |||
218 | String key = KEY_DOCUMENTS_ACTIONS + "." + doctype; |
||
219 | ExtensionPoint extensionPoint = manager.get(key); |
||
220 | if (extensionPoint == null) { |
||
221 | // No hay acciones registradas para ese tipo de documento
|
||
222 | return null; |
||
223 | } |
||
224 | List<DocumentAction> actions = new ArrayList<DocumentAction>(); |
||
225 | Iterator<Extension> it = extensionPoint.iterator();
|
||
226 | while (it.hasNext()) {
|
||
227 | try {
|
||
228 | DocumentAction action; |
||
229 | action = (DocumentAction) it.next().create(); |
||
230 | actions.add(action); |
||
231 | } catch (InstantiationException e) { |
||
232 | logger.warn("Can't retrieve document action", e);
|
||
233 | } catch (IllegalAccessException e) { |
||
234 | logger.warn("Can't retrieve document action", e);
|
||
235 | } |
||
236 | } |
||
237 | if (actions.size() < 1) { |
||
238 | return null; |
||
239 | } |
||
240 | Collections.sort(actions, new CompareAction()); |
||
241 | return actions;
|
||
242 | 31496 | jjdelcerro | } |
243 | 36435 | cordinyana | |
244 | private class CompareAction implements Comparator<DocumentAction> { |
||
245 | |||
246 | public int compare(DocumentAction action1, DocumentAction action2) { |
||
247 | // FIXME: flata formatear los enteros!!!!
|
||
248 | String key1 =
|
||
249 | MessageFormat.format("{1}.{2}.{3}", action1.getGroup() |
||
250 | .getOrder(), action1.getGroup().getTitle(), action1 |
||
251 | .getOrder()); |
||
252 | String key2 =
|
||
253 | MessageFormat.format("{1}.{2}.{3}", action2.getGroup() |
||
254 | .getOrder(), action2.getGroup().getTitle(), action2 |
||
255 | .getOrder()); |
||
256 | return key1.compareTo(key2);
|
||
257 | } |
||
258 | } |
||
259 | |||
260 | 31496 | jjdelcerro | /**
|
261 | * Create, add and return a new action for documents.
|
||
262 | *
|
||
263 | * If action already exists don't create and return this.
|
||
264 | *
|
||
265 | 36435 | cordinyana | * @param unique
|
266 | * identifier for the action
|
||
267 | 31496 | jjdelcerro | * @param title
|
268 | * @param description
|
||
269 | * @param order
|
||
270 | * @return
|
||
271 | */
|
||
272 | 36435 | cordinyana | public DocumentActionGroup addDocumentActionGroup(String id, String title, |
273 | String description, int order) { |
||
274 | DocumentActionGroup group = this.documentActionGroups.get(id);
|
||
275 | if (group != null) { |
||
276 | return group;
|
||
277 | } |
||
278 | group = new DefaultDocumentActionGroup(id, title, description, order);
|
||
279 | this.documentActionGroups.put(id, group);
|
||
280 | return group;
|
||
281 | 31496 | jjdelcerro | } |
282 | |||
283 | 312 | fernando | } |