svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / ProjectManager.java @ 43913
History | View | Annotate | Download (11.4 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.project; |
25 |
|
26 |
import java.text.MessageFormat; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Collections; |
29 |
import java.util.Comparator; |
30 |
import java.util.EventListener; |
31 |
import java.util.HashMap; |
32 |
import java.util.Iterator; |
33 |
import java.util.List; |
34 |
import java.util.Map; |
35 |
|
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
import org.gvsig.andami.PluginServices; |
40 |
import org.gvsig.app.extension.ProjectExtension; |
41 |
import org.gvsig.app.project.documents.DefaultDocumentActionGroup; |
42 |
import org.gvsig.app.project.documents.Document; |
43 |
import org.gvsig.app.project.documents.DocumentAction; |
44 |
import org.gvsig.app.project.documents.DocumentActionGroup; |
45 |
import org.gvsig.app.project.documents.DocumentManager; |
46 |
import org.gvsig.app.project.documents.gui.ProjectWindow; |
47 |
import org.gvsig.tools.ToolsLocator; |
48 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
49 |
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
50 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
51 |
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable; |
52 |
import org.gvsig.tools.util.BaseListenerSupport; |
53 |
import org.gvsig.tools.util.BaseListenerSupport.NotificationListener; |
54 |
|
55 |
public class ProjectManager extends BaseWeakReferencingObservable { |
56 |
|
57 |
private static final Logger logger = LoggerFactory |
58 |
.getLogger(ProjectManager.class); |
59 |
private static ProjectManager factory = null; |
60 |
|
61 |
// private static final String KEY_PROJECT = "app.project";
|
62 |
// private static final String KEY_DOCUMENTS = "app.project.documents";
|
63 |
private static final String KEY_DOCUMENTS_FACTORIES = |
64 |
"app.project.documents.factories";
|
65 |
private static final String KEY_DOCUMENTS_ACTIONS = |
66 |
"app.project.documents.actions";
|
67 |
|
68 |
private Map<String, DocumentActionGroup> documentActionGroups; |
69 |
|
70 |
private final BaseListenerSupport newProjectListeners; |
71 |
|
72 |
public interface ProjectEvent extends EventListener { |
73 |
public Project getProject();
|
74 |
} |
75 |
|
76 |
public interface NewProjectEvent extends ProjectEvent { |
77 |
|
78 |
} |
79 |
|
80 |
public static ProjectManager getInstance() { |
81 |
if (factory == null) { |
82 |
factory = new ProjectManager();
|
83 |
} |
84 |
return factory;
|
85 |
} |
86 |
|
87 |
private ProjectManager() {
|
88 |
this.documentActionGroups = new HashMap<String, DocumentActionGroup>(); |
89 |
this.newProjectListeners = new BaseListenerSupport(); |
90 |
} |
91 |
|
92 |
|
93 |
public Project getCurrentProject() {
|
94 |
return ((ProjectExtension) PluginServices
|
95 |
.getExtension(ProjectExtension.class)).getProject(); |
96 |
} |
97 |
|
98 |
public void setCurrentProject(Project project) { |
99 |
((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).setProject(project); |
100 |
} |
101 |
|
102 |
public ProjectWindow getCurrentProjectWindow() {
|
103 |
ProjectExtension projectExtension = |
104 |
(ProjectExtension) PluginServices |
105 |
.getExtension(ProjectExtension.class); |
106 |
ProjectWindow window = |
107 |
(ProjectWindow) projectExtension.getProjectWindow(); |
108 |
return window;
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* @deprecated use {@link #getDocumentManagers()} instead.
|
113 |
*/
|
114 |
public List<DocumentManager> getDocumentManager() { |
115 |
return getDocumentManagers();
|
116 |
} |
117 |
|
118 |
@SuppressWarnings("unchecked") |
119 |
public List<DocumentManager> getDocumentManagers() { |
120 |
Iterator<Extension> iterator =
|
121 |
ToolsLocator.getExtensionPointManager() |
122 |
.get(KEY_DOCUMENTS_FACTORIES).iterator(); |
123 |
List<DocumentManager> factories = new ArrayList<DocumentManager>(); |
124 |
while (iterator.hasNext()) {
|
125 |
Extension extension = iterator.next(); |
126 |
try {
|
127 |
factories.add((DocumentManager) extension.create()); |
128 |
} catch (InstantiationException e) { |
129 |
logger.error("Can't access to project document factory ("
|
130 |
+ extension.getName() + ").");
|
131 |
} catch (IllegalAccessException e) { |
132 |
logger.error("Can't access to project document factory ("
|
133 |
+ extension.getName() + ").");
|
134 |
} |
135 |
} |
136 |
return factories;
|
137 |
} |
138 |
|
139 |
/**
|
140 |
* @deprecated use {@link #getDocumentManager(String)} instead.
|
141 |
*/
|
142 |
public DocumentManager getDocumentManagers(String type) { |
143 |
return getDocumentManager(type);
|
144 |
} |
145 |
|
146 |
public DocumentManager getDocumentManager(String type) { |
147 |
DocumentManager factory = null;
|
148 |
try {
|
149 |
factory = |
150 |
(DocumentManager) ToolsLocator.getExtensionPointManager() |
151 |
.get(KEY_DOCUMENTS_FACTORIES).create(type); |
152 |
} catch (Exception ex) { |
153 |
logger |
154 |
.warn( |
155 |
MessageFormat.format(
|
156 |
"Unable to locate factory for documents of type {0}",
|
157 |
type), ex); |
158 |
} |
159 |
return factory;
|
160 |
} |
161 |
|
162 |
public Document createDocument(String type) { |
163 |
Document doc = getDocumentManager(type).createDocument();
|
164 |
return doc;
|
165 |
} |
166 |
|
167 |
public Document createDocument(String type, String name) { |
168 |
Document doc = createDocument(type);
|
169 |
doc.setName(name); |
170 |
return doc;
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* @deprecated use {@link #createDocumentsByUser(String)} instead
|
175 |
*/
|
176 |
public Document createDocumentByUser(String type) { |
177 |
Document doc = getDocumentManager(type).createDocumentByUser();
|
178 |
return doc;
|
179 |
} |
180 |
|
181 |
/**
|
182 |
* Creates a group of documents of a given type through the user interface.
|
183 |
*
|
184 |
* @param type
|
185 |
* the type of documents to create
|
186 |
* @return the created documents
|
187 |
*/
|
188 |
public Iterator<? extends Document> createDocumentsByUser(String type) { |
189 |
logger.info("createDocumentsByUser('{}')", type);
|
190 |
return getDocumentManager(type).createDocumentsByUser();
|
191 |
} |
192 |
|
193 |
public Project createProject() {
|
194 |
return new DefaultProject(); |
195 |
} |
196 |
|
197 |
public ProjectExtent createExtent() {
|
198 |
return new ProjectExtent(); |
199 |
} |
200 |
|
201 |
public void registerDocumentFactory(DocumentManager documentManager) { |
202 |
ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
203 |
manager.add(KEY_DOCUMENTS_FACTORIES).append( |
204 |
documentManager.getTypeName(), null, documentManager);
|
205 |
notifyObservers(); |
206 |
} |
207 |
|
208 |
public void registerDocumentFactoryAlias(String typeName, String alias) { |
209 |
ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
210 |
|
211 |
manager.get(KEY_DOCUMENTS_FACTORIES).addAlias(typeName, alias); |
212 |
} |
213 |
|
214 |
public void registerDocumentAction(String typeName, DocumentAction action) { |
215 |
ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
216 |
|
217 |
String key = KEY_DOCUMENTS_ACTIONS + "." + typeName; |
218 |
String description =
|
219 |
MessageFormat.format("Actions for {1} documents ", typeName); |
220 |
|
221 |
manager.add(key, description).append(action.getId(), |
222 |
action.getDescription(), action); |
223 |
} |
224 |
|
225 |
/**
|
226 |
* Gets a list of actions for the document type especified.
|
227 |
*
|
228 |
* La lista esta ordenada deacuerdo al orden especificado en
|
229 |
* las acciones y grupos de acciones involucrados.
|
230 |
*
|
231 |
* @param doctype
|
232 |
* @return list of actions as List<DocumentAction>
|
233 |
*/
|
234 |
@SuppressWarnings("unchecked") |
235 |
public List<DocumentAction> getDocumentActions(String doctype) { |
236 |
|
237 |
ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
238 |
|
239 |
String key = KEY_DOCUMENTS_ACTIONS + "." + doctype; |
240 |
ExtensionPoint extensionPoint = manager.get(key); |
241 |
if (extensionPoint == null) { |
242 |
// No hay acciones registradas para ese tipo de documento
|
243 |
return null; |
244 |
} |
245 |
List<DocumentAction> actions = new ArrayList<DocumentAction>(); |
246 |
Iterator<Extension> it = extensionPoint.iterator();
|
247 |
while (it.hasNext()) {
|
248 |
try {
|
249 |
DocumentAction action; |
250 |
action = (DocumentAction) it.next().create(); |
251 |
actions.add(action); |
252 |
} catch (InstantiationException e) { |
253 |
logger.warn("Can't retrieve document action", e);
|
254 |
} catch (IllegalAccessException e) { |
255 |
logger.warn("Can't retrieve document action", e);
|
256 |
} |
257 |
} |
258 |
if (actions.size() < 1) { |
259 |
return null; |
260 |
} |
261 |
Collections.sort(actions, new CompareAction()); |
262 |
return actions;
|
263 |
} |
264 |
|
265 |
private class CompareAction implements Comparator<DocumentAction> { |
266 |
|
267 |
public int compare(DocumentAction action1, DocumentAction action2) { |
268 |
// FIXME: flata formatear los enteros!!!!
|
269 |
String key1 =
|
270 |
MessageFormat.format("{1}.{2}.{3}", action1.getGroup() |
271 |
.getOrder(), action1.getGroup().getTitle(), action1 |
272 |
.getOrder()); |
273 |
String key2 =
|
274 |
MessageFormat.format("{1}.{2}.{3}", action2.getGroup() |
275 |
.getOrder(), action2.getGroup().getTitle(), action2 |
276 |
.getOrder()); |
277 |
return key1.compareTo(key2);
|
278 |
} |
279 |
} |
280 |
|
281 |
/**
|
282 |
* Create, add and return a new action for documents.
|
283 |
*
|
284 |
* If action already exists don't create and return this.
|
285 |
*
|
286 |
* @param unique
|
287 |
* identifier for the action
|
288 |
* @param title
|
289 |
* @param description
|
290 |
* @param order
|
291 |
* @return
|
292 |
*/
|
293 |
public DocumentActionGroup addDocumentActionGroup(String id, String title, |
294 |
String description, int order) { |
295 |
DocumentActionGroup group = this.documentActionGroups.get(id);
|
296 |
if (group != null) { |
297 |
return group;
|
298 |
} |
299 |
group = new DefaultDocumentActionGroup(id, title, description, order);
|
300 |
this.documentActionGroups.put(id, group);
|
301 |
return group;
|
302 |
} |
303 |
|
304 |
public ProjectPreferences getProjectPreferences() {
|
305 |
return DefaultProject.getPreferences();
|
306 |
} |
307 |
|
308 |
public void addProjectListener(NotificationListener listener) { |
309 |
this.newProjectListeners.addListener(listener);
|
310 |
} |
311 |
|
312 |
public NotificationListener[] getProjectListeners() { |
313 |
return (NotificationListener[]) this.newProjectListeners.getListeners(); |
314 |
} |
315 |
|
316 |
public boolean hasProjectListeners() { |
317 |
return this.newProjectListeners.hasListeners(); |
318 |
} |
319 |
|
320 |
public void removeProjectListener(NotificationListener listener) { |
321 |
this.newProjectListeners.removeListener(listener);
|
322 |
} |
323 |
|
324 |
public void removeAllProjectListener() { |
325 |
this.newProjectListeners.removeAllListener();
|
326 |
} |
327 |
|
328 |
public void notifyProjectEvent(ProjectEvent event) { |
329 |
this.newProjectListeners.notifyEvent(event);
|
330 |
} |
331 |
} |