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