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 / documents / AbstractDocumentManager.java @ 44644
History | View | Annotate | Download (12.4 KB)
1 | 40558 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | |
25 | |||
26 | package org.gvsig.app.project.documents; |
||
27 | |||
28 | import java.lang.reflect.Constructor; |
||
29 | import java.util.ArrayList; |
||
30 | 41248 | jjdelcerro | import java.util.Formatter; |
31 | import java.util.HashMap; |
||
32 | 40435 | jjdelcerro | import java.util.HashSet; |
33 | import java.util.Iterator; |
||
34 | import java.util.List; |
||
35 | import java.util.Map; |
||
36 | import java.util.Set; |
||
37 | |||
38 | import javax.swing.ImageIcon; |
||
39 | 41248 | jjdelcerro | import javax.swing.JComponent; |
40 | 40435 | jjdelcerro | |
41 | import org.slf4j.Logger; |
||
42 | import org.slf4j.LoggerFactory; |
||
43 | |||
44 | import org.gvsig.andami.PluginServices; |
||
45 | import org.gvsig.andami.ui.mdiManager.IWindow; |
||
46 | 41248 | jjdelcerro | import org.gvsig.app.ApplicationLocator; |
47 | import org.gvsig.app.ApplicationManager; |
||
48 | import org.gvsig.app.project.DocumentsContainer; |
||
49 | 40435 | jjdelcerro | import org.gvsig.app.project.Project; |
50 | import org.gvsig.app.project.documents.gui.IDocumentWindow; |
||
51 | 42873 | jjdelcerro | import org.gvsig.app.project.documents.gui.WindowLayout; |
52 | 43633 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper; |
53 | 42873 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
54 | 40435 | jjdelcerro | import org.gvsig.tools.dynobject.DynStruct; |
55 | import org.gvsig.tools.extensionpoint.ExtensionBuilder; |
||
56 | 42873 | jjdelcerro | import org.gvsig.tools.identitymanagement.SimpleIdentityManager; |
57 | import org.gvsig.tools.identitymanagement.UnauthorizedException; |
||
58 | 41634 | jjdelcerro | import org.gvsig.tools.observer.BaseNotification; |
59 | import org.gvsig.tools.observer.Notification; |
||
60 | 41248 | jjdelcerro | import org.gvsig.tools.observer.Observable; |
61 | import org.gvsig.tools.observer.Observer; |
||
62 | 41634 | jjdelcerro | import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable; |
63 | 40435 | jjdelcerro | import org.gvsig.tools.persistence.PersistenceManager; |
64 | import org.gvsig.tools.persistence.PersistentState; |
||
65 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
66 | 41248 | jjdelcerro | import org.gvsig.tools.swing.api.Component; |
67 | 40435 | jjdelcerro | |
68 | |||
69 | |||
70 | /**
|
||
71 | * Factory of ProjectDocument.
|
||
72 | *
|
||
73 | */
|
||
74 | |||
75 | 41248 | jjdelcerro | public abstract class AbstractDocumentManager implements DocumentManager, Observer { |
76 | 40435 | jjdelcerro | |
77 | private static final Logger logger = LoggerFactory.getLogger(AbstractDocumentManager.class); |
||
78 | |||
79 | 41248 | jjdelcerro | |
80 | private Map<String, JComponent> mainComponents = new HashMap<String, JComponent>(); |
||
81 | 41634 | jjdelcerro | |
82 | private DelegateWeakReferencingObservable observableHelper = null; |
||
83 | |||
84 | 43633 | jjdelcerro | private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper(); |
85 | |||
86 | 41634 | jjdelcerro | protected AbstractDocumentManager() {
|
87 | this.observableHelper = new DelegateWeakReferencingObservable(this); |
||
88 | } |
||
89 | 41248 | jjdelcerro | |
90 | 40435 | jjdelcerro | /**
|
91 | * Returns the type of document priority.
|
||
92 | *
|
||
93 | * This priority is used when order document factories in UI.
|
||
94 | *
|
||
95 | * @return Priority.
|
||
96 | */
|
||
97 | public int getPriority() { |
||
98 | return 10; |
||
99 | } |
||
100 | |||
101 | /**
|
||
102 | * Returns the icon for the type of document.
|
||
103 | *
|
||
104 | * @return Image.
|
||
105 | */
|
||
106 | public ImageIcon getIcon() { |
||
107 | return PluginServices.getIconTheme().get("document-icon-sel"); |
||
108 | } |
||
109 | |||
110 | /**
|
||
111 | * Returns the icon for the type of document when is selected
|
||
112 | *
|
||
113 | * @return Image.
|
||
114 | */
|
||
115 | public ImageIcon getIconSelected() { |
||
116 | return PluginServices.getIconTheme().get("document-icon"); |
||
117 | } |
||
118 | |||
119 | /**
|
||
120 | * Returns the title of type of document.
|
||
121 | *
|
||
122 | * @return String title for type of document
|
||
123 | */
|
||
124 | public String getTitle() { |
||
125 | return PluginServices.getText(this, "documento"); |
||
126 | } |
||
127 | |||
128 | public AbstractDocument createDocumentByUser() { |
||
129 | return createDocument();
|
||
130 | } |
||
131 | |||
132 | public Iterator<? extends Document> createDocumentsByUser() { |
||
133 | Set<AbstractDocument> doc = new HashSet<AbstractDocument>(1); |
||
134 | doc.add(createDocumentByUser()); |
||
135 | return doc.iterator();
|
||
136 | } |
||
137 | |||
138 | /**
|
||
139 | * @see ExtensionBuilder
|
||
140 | */
|
||
141 | public Object create() { |
||
142 | return this; |
||
143 | } |
||
144 | |||
145 | /**
|
||
146 | * @see ExtensionBuilder
|
||
147 | */
|
||
148 | public Object create(Object[] args) { |
||
149 | return this; |
||
150 | } |
||
151 | |||
152 | /**
|
||
153 | * @see ExtensionBuilder
|
||
154 | */
|
||
155 | @SuppressWarnings("rawtypes") |
||
156 | public Object create(Map args) { |
||
157 | return this; |
||
158 | } |
||
159 | |||
160 | 42873 | jjdelcerro | @Override
|
161 | 40435 | jjdelcerro | public IWindow getMainWindow(Document doc) { |
162 | return getMainWindow(doc, null); |
||
163 | } |
||
164 | 42873 | jjdelcerro | |
165 | @Override
|
||
166 | public IWindow getMainWindow(Document doc, WindowLayout layout) { |
||
167 | SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager(); |
||
168 | if( ! identityManager.getCurrentIdentity().isAuthorized(Document.ACCESS_DOCUMENT_AUTHORIZATION, this, this.getTypeName()) ) { |
||
169 | throw new UnauthorizedException(Document.ACCESS_DOCUMENT_AUTHORIZATION, this, this.getTypeName()); |
||
170 | } |
||
171 | IDocumentWindow win; |
||
172 | |||
173 | win = (IDocumentWindow) PluginServices.getMDIManager().getSingletonWindow(getMainWindowClass(), doc); |
||
174 | return win;
|
||
175 | } |
||
176 | |||
177 | @Override
|
||
178 | public IWindow getPropertiesWindow(Document doc) { |
||
179 | SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager(); |
||
180 | if( ! identityManager.getCurrentIdentity().isAuthorized(Document.ACCESS_DOCUMENT_AUTHORIZATION, this, this.getTypeName()) ) { |
||
181 | throw new UnauthorizedException(Document.ACCESS_DOCUMENT_AUTHORIZATION, this, this.getTypeName()); |
||
182 | } |
||
183 | return null; |
||
184 | } |
||
185 | 41248 | jjdelcerro | |
186 | 42873 | jjdelcerro | |
187 | |||
188 | @Override
|
||
189 | 41248 | jjdelcerro | public JComponent getMainComponent(Document doc) { |
190 | ApplicationManager application = ApplicationLocator.getManager(); |
||
191 | Document activeDoc = application.getActiveDocument();
|
||
192 | if( activeDoc == doc ) { // Ojo, hay que cerciorarse de que esta comparacion funciona. |
||
193 | IWindow win = this.getMainWindow(doc);
|
||
194 | if( win instanceof Component ) { |
||
195 | return ((Component)win).asJComponent(); |
||
196 | } |
||
197 | return (JComponent)win; |
||
198 | } |
||
199 | if( activeDoc instanceof DocumentsContainer ) { |
||
200 | DocumentsContainer container = (DocumentsContainer)activeDoc; |
||
201 | return this.getMainComponent(container, doc); |
||
202 | } |
||
203 | return null; |
||
204 | } |
||
205 | |||
206 | public JComponent getMainComponent(DocumentsContainer container, Document doc) { |
||
207 | Formatter f = new Formatter(); |
||
208 | f.format("08X",container.hashCode());
|
||
209 | f.format("08X",doc.hashCode());
|
||
210 | String key = f.toString();
|
||
211 | f.close(); |
||
212 | return mainComponents.get(key);
|
||
213 | } |
||
214 | 40435 | jjdelcerro | |
215 | 41248 | jjdelcerro | public void unregisterMainComponent(DocumentsContainer container, Document doc) { |
216 | Formatter f = new Formatter(); |
||
217 | f.format("08X",container.hashCode());
|
||
218 | f.format("08X",doc.hashCode());
|
||
219 | String key = f.toString();
|
||
220 | f.close(); |
||
221 | mainComponents.remove(key); |
||
222 | } |
||
223 | 40435 | jjdelcerro | |
224 | 41248 | jjdelcerro | public void registerMainComponent(DocumentsContainer container, Document doc, JComponent component) { |
225 | Formatter f = new Formatter(); |
||
226 | f.format("08X",container.hashCode());
|
||
227 | f.format("08X",doc.hashCode());
|
||
228 | String key = f.toString();
|
||
229 | f.close(); |
||
230 | mainComponents.put(key,component); |
||
231 | } |
||
232 | |||
233 | 40435 | jjdelcerro | /**
|
234 | * Return true if the name exists to another document.
|
||
235 | *
|
||
236 | * @param project
|
||
237 | * @param documentName
|
||
238 | *
|
||
239 | * @return True if the name exists.
|
||
240 | * @deprecated use instead project.getProjectDocument
|
||
241 | */
|
||
242 | public boolean existName(Project project, String documentName) { |
||
243 | return project.getDocument(documentName, getTypeName())!=null; |
||
244 | } |
||
245 | |||
246 | /**
|
||
247 | * Return the class or interface for the documents managed by this factory.
|
||
248 | * @return The document class;
|
||
249 | */
|
||
250 | @SuppressWarnings("rawtypes") |
||
251 | abstract protected Class getDocumentClass(); |
||
252 | |||
253 | public Object createFromState(PersistentState state) throws PersistenceException { |
||
254 | Document doc = this.createDocument(); |
||
255 | return doc;
|
||
256 | } |
||
257 | |||
258 | public void loadFromState(PersistentState state, Object object) throws PersistenceException { |
||
259 | Document doc = (Document) object; |
||
260 | doc.loadFromState(state); |
||
261 | } |
||
262 | |||
263 | public void saveToState(PersistentState state, Object obj) throws PersistenceException { |
||
264 | Document doc = (Document) obj; |
||
265 | doc.saveToState(state); |
||
266 | |||
267 | } |
||
268 | |||
269 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
||
270 | public boolean manages(Class theClass) { |
||
271 | return this.getDocumentClass().isAssignableFrom(theClass); |
||
272 | } |
||
273 | |||
274 | @SuppressWarnings("rawtypes") |
||
275 | public boolean manages(PersistentState state) { |
||
276 | try {
|
||
277 | Class theClass;
|
||
278 | theClass = (Class) Class.forName(state.getTheClassName()); |
||
279 | return manages(theClass);
|
||
280 | } catch (ClassNotFoundException e) { |
||
281 | return false; |
||
282 | } |
||
283 | } |
||
284 | |||
285 | public List<DynStruct> getDefinitions() { |
||
286 | DynStruct definition = this.getDefinition(this.getDocumentClass().getName()); |
||
287 | List<DynStruct> definitions = new ArrayList<DynStruct>(); |
||
288 | definitions.add(definition); |
||
289 | return definitions;
|
||
290 | } |
||
291 | |||
292 | public String getDomainName() { |
||
293 | return PersistenceManager.DEFAULT_DOMAIN_NAME;
|
||
294 | } |
||
295 | |||
296 | public String getDomainURL() { |
||
297 | return PersistenceManager.DEFAULT_DOMAIN_URL;
|
||
298 | } |
||
299 | |||
300 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
||
301 | public List getManagedClasses() { |
||
302 | List classes = new ArrayList(); |
||
303 | classes.add(this.getDocumentClass());
|
||
304 | return classes;
|
||
305 | } |
||
306 | |||
307 | @SuppressWarnings("rawtypes") |
||
308 | public Class getManagedClass(Object object) { |
||
309 | return this.getDocumentClass(); |
||
310 | } |
||
311 | |||
312 | @SuppressWarnings("rawtypes") |
||
313 | public Class getManagedClass(PersistentState state) { |
||
314 | return this.getDocumentClass(); |
||
315 | } |
||
316 | |||
317 | @SuppressWarnings("rawtypes") |
||
318 | public Class getManagedClass(String name) { |
||
319 | return this.getDocumentClass(); |
||
320 | } |
||
321 | |||
322 | @SuppressWarnings("rawtypes") |
||
323 | public String getManagedClassName(Object object) { |
||
324 | Class clazz = this.getManagedClass(object); |
||
325 | if (clazz != null){ |
||
326 | return clazz.getName();
|
||
327 | } |
||
328 | return null; |
||
329 | } |
||
330 | |||
331 | @SuppressWarnings("rawtypes") |
||
332 | protected IDocumentWindow createDocumentWindow(Document document) { |
||
333 | IDocumentWindow documentWindow = null;
|
||
334 | Class windowClass = getMainWindowClass();
|
||
335 | |||
336 | try {
|
||
337 | |||
338 | try {
|
||
339 | Constructor constructor;
|
||
340 | constructor = |
||
341 | windowClass.getConstructor(new Class[] { Document.class }); |
||
342 | documentWindow = |
||
343 | (IDocumentWindow) constructor |
||
344 | .newInstance(new Object[] { document }); |
||
345 | } catch (NoSuchMethodException e1) { |
||
346 | documentWindow = (IDocumentWindow) windowClass.newInstance(); |
||
347 | documentWindow.setDocument(document); |
||
348 | } |
||
349 | } catch (Exception e) { |
||
350 | logger.warn("Can't create the document window.", e);
|
||
351 | return null; |
||
352 | } |
||
353 | return documentWindow;
|
||
354 | } |
||
355 | 41248 | jjdelcerro | |
356 | public void update(Observable observable, Object notification) { |
||
357 | // Do nothing
|
||
358 | } |
||
359 | |||
360 | 41634 | jjdelcerro | |
361 | public void addObserver(Observer o) { |
||
362 | this.observableHelper.addObserver(o);
|
||
363 | } |
||
364 | |||
365 | public void deleteObserver(Observer o) { |
||
366 | this.observableHelper.deleteObserver(o);
|
||
367 | } |
||
368 | |||
369 | public void deleteObservers() { |
||
370 | this.observableHelper.deleteObservers();
|
||
371 | } |
||
372 | 41248 | jjdelcerro | |
373 | 41634 | jjdelcerro | protected Notification notifyObservers(String type, Document doc) { |
374 | Notification notification = new BaseNotification(type, new Object[] {doc}); |
||
375 | this.observableHelper.notifyObservers(notification);
|
||
376 | return notification;
|
||
377 | } |
||
378 | |||
379 | protected Notification notifyObservers(String type, IWindow doc) { |
||
380 | Notification notification = new BaseNotification(type, new Object[] {doc}); |
||
381 | this.observableHelper.notifyObservers(notification);
|
||
382 | return notification;
|
||
383 | } |
||
384 | 43633 | jjdelcerro | |
385 | @Override
|
||
386 | public Object getProperty(Object key) { |
||
387 | return this.propertiesHelper.getProperty(key); |
||
388 | } |
||
389 | |||
390 | @Override
|
||
391 | public void setProperty(Object key, Object obj) { |
||
392 | this.propertiesHelper.setProperty(key, obj);
|
||
393 | } |
||
394 | |||
395 | @Override
|
||
396 | public Map getExtendedProperties() { |
||
397 | return this.propertiesHelper.getExtendedProperties(); |
||
398 | } |
||
399 | |||
400 | 40435 | jjdelcerro | } |