Statistics
| Revision:

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 @ 47036

History | View | Annotate | Download (13.2 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

    
25

    
26
package org.gvsig.app.project.documents;
27

    
28
import java.lang.reflect.Constructor;
29
import java.util.ArrayList;
30
import java.util.Formatter;
31
import java.util.HashMap;
32
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
import javax.swing.JComponent;
40

    
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
import org.gvsig.app.ApplicationLocator;
47
import org.gvsig.app.ApplicationManager;
48
import org.gvsig.app.project.DocumentsContainer;
49
import org.gvsig.app.project.Project;
50
import org.gvsig.app.project.documents.gui.IDocumentWindow;
51
import org.gvsig.app.project.documents.gui.WindowLayout;
52
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
53
import org.gvsig.tools.ToolsLocator;
54
import org.gvsig.tools.dynobject.DynStruct;
55
import org.gvsig.tools.extensionpoint.ExtensionBuilder;
56
import org.gvsig.tools.identitymanagement.SimpleIdentityManager;
57
import org.gvsig.tools.identitymanagement.UnauthorizedException;
58
import org.gvsig.tools.observer.BaseNotification;
59
import org.gvsig.tools.observer.Notification;
60
import org.gvsig.tools.observer.Observable;
61
import org.gvsig.tools.observer.Observer;
62
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
63
import org.gvsig.tools.persistence.PersistenceManager;
64
import org.gvsig.tools.persistence.PersistentState;
65
import org.gvsig.tools.persistence.exception.PersistenceException;
66
import org.gvsig.tools.swing.api.Component;
67
import org.gvsig.tools.util.Invocable;
68

    
69

    
70

    
71
/**
72
 * Factory of ProjectDocument.
73
 * 
74
 */
75

    
76
public abstract class AbstractDocumentManager implements DocumentManager, Observer {
77

    
78
    private static final Logger logger = LoggerFactory.getLogger(AbstractDocumentManager.class);
79

    
80
    
81
    private Map<String, JComponent> mainComponents = new HashMap<String, JComponent>();
82

    
83
    private DelegateWeakReferencingObservable observableHelper = null;
84

    
85
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
86
    
87
    protected AbstractDocumentManager() {
88
        this.observableHelper = new DelegateWeakReferencingObservable(this);
89
    }
90
    
91
    /**
92
     * Returns the type of document priority.
93
     *
94
     * This priority is used when order document factories in UI.
95
     * 
96
     * @return Priority.
97
     */
98
    public int getPriority() {
99
        return 10;
100
    }
101

    
102
    /**
103
     * Returns the icon for the type of document.
104
     *
105
     * @return Image.
106
     */
107
    public ImageIcon getIcon() {
108
        return PluginServices.getIconTheme().get("document-icon-sel");
109
    }
110

    
111
    /**
112
     * Returns the icon for the type of document when is selected
113
     *
114
     * @return Image.
115
     */
116
    public ImageIcon getIconSelected() {
117
        return PluginServices.getIconTheme().get("document-icon");
118
    }
119

    
120
    /**
121
     * Returns the title of type of document.
122
     *
123
     * @return String title for type of document
124
     */
125
    public String getTitle() {
126
        return PluginServices.getText(this, "documento");
127
    }
128

    
129
    @Override
130
    public AbstractDocument createDocumentByUser() {
131
        return createDocument();
132
    }
133

    
134
    @Override
135
    public Iterator<? extends Document> createDocumentsByUser() {
136
        Set<AbstractDocument> doc = new HashSet<>(1);
137
        doc.add(createDocumentByUser());
138
        return doc.iterator();
139
    }
140

    
141
    @Override
142
    public Iterator<? extends Document> createDocumentsByUser(Invocable whenDocumentsLoaded) {
143
        Set<AbstractDocument> docs = new HashSet<>(1);
144
        docs.add(createDocumentByUser());
145
        return docs.iterator();
146
    }
147

    
148
    /**
149
     * @see ExtensionBuilder
150
     */
151
    @Override
152
    public Object create() {
153
        return this;
154
    }
155

    
156
    /**
157
     * @see ExtensionBuilder
158
     */
159
    public Object create(Object[] args) {
160
        return this;
161
    }
162

    
163
    /**
164
     * @see ExtensionBuilder
165
     */
166
    @SuppressWarnings("rawtypes")
167
    public Object create(Map args) {
168
        return this;
169
    }
170

    
171
    @Override
172
    public IWindow getMainWindow(Document doc) {
173
        return getMainWindow(doc, null);
174
    }
175

    
176
    @Override
177
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
178
        SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager();
179
        if( ! identityManager.getCurrentIdentity().isAuthorized(Document.ACCESS_DOCUMENT_AUTHORIZATION, this, this.getTypeName()) ) {
180
            throw new UnauthorizedException(Document.ACCESS_DOCUMENT_AUTHORIZATION, this, this.getTypeName());
181
        }   
182
        IDocumentWindow win;
183

    
184
        win = (IDocumentWindow) PluginServices.getMDIManager().getSingletonWindow(getMainWindowClass(), doc);
185
        return win;
186
    }
187

    
188
    @Override
189
    public boolean hasMainWindow(Document doc) {
190
        SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager();
191
        if( ! identityManager.getCurrentIdentity().isAuthorized(Document.ACCESS_DOCUMENT_AUTHORIZATION, this, this.getTypeName()) ) {
192
            throw new UnauthorizedException(Document.ACCESS_DOCUMENT_AUTHORIZATION, this, this.getTypeName());
193
        }   
194
        IDocumentWindow win;
195

    
196
        win = (IDocumentWindow) PluginServices.getMDIManager().getSingletonWindow(getMainWindowClass(), doc);
197
        return win!=null;
198
    }
199

    
200
    @Override
201
    public IWindow getPropertiesWindow(Document doc) {
202
        SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager();
203
        if( ! identityManager.getCurrentIdentity().isAuthorized(Document.ACCESS_DOCUMENT_AUTHORIZATION, this, this.getTypeName()) ) {
204
            throw new UnauthorizedException(Document.ACCESS_DOCUMENT_AUTHORIZATION, this, this.getTypeName());
205
        }   
206
        return null;
207
    }
208
    
209
    
210
    
211
    @Override
212
    public JComponent getMainComponent(Document doc) {
213
        ApplicationManager application = ApplicationLocator.getManager();
214
        Document activeDoc = application.getActiveDocument(this.getDocumentClass());
215
        if( activeDoc == doc ) { // Ojo, hay que cerciorarse de que esta comparacion funciona.
216
            IWindow win = this.getMainWindow(doc);
217
            if( win instanceof Component ) {
218
                return ((Component)win).asJComponent();
219
            }
220
            return (JComponent)win;
221
        }
222
        if( activeDoc instanceof DocumentsContainer ) {
223
            DocumentsContainer container = (DocumentsContainer)activeDoc;
224
            return this.getMainComponent(container, doc);
225
        }
226
        return null;
227
    }
228
    
229
    public JComponent getMainComponent(DocumentsContainer container, Document doc) {
230
        Formatter f = new Formatter();
231
        f.format("08X",container.hashCode());
232
        f.format("08X",doc.hashCode());
233
        String key = f.toString();
234
        f.close();
235
        return mainComponents.get(key);
236
    }
237

    
238
    public void unregisterMainComponent(DocumentsContainer container, Document doc) {
239
        Formatter f = new Formatter();
240
        f.format("08X",container.hashCode());
241
        f.format("08X",doc.hashCode());
242
        String key = f.toString();
243
        f.close();
244
        mainComponents.remove(key);
245
    }
246

    
247
    public void registerMainComponent(DocumentsContainer container, Document doc, JComponent component) {
248
        Formatter f = new Formatter();
249
        f.format("08X",container.hashCode());
250
        f.format("08X",doc.hashCode());
251
        String key = f.toString();
252
        f.close();
253
        mainComponents.put(key,component);
254
    }
255
    
256
    /**
257
     * Return true if the name exists to another document.
258
     *
259
     * @param project
260
     * @param documentName
261
     *
262
     * @return True if the name exists.
263
     * @deprecated use instead  project.getProjectDocument
264
     */
265
    public boolean existName(Project project, String documentName) {
266
        return project.getDocument(documentName, getTypeName())!=null;
267
    }
268

    
269
    /**
270
     * Return the class or interface for the documents managed by this factory.
271
     * @return The document class;
272
     */
273
    @SuppressWarnings("rawtypes")
274
    abstract protected Class getDocumentClass();
275

    
276
    public Object createFromState(PersistentState state) throws PersistenceException {
277
        Document doc = this.createDocument();
278
        return doc;
279
    }
280

    
281
    public void loadFromState(PersistentState state, Object object) throws PersistenceException {
282
        Document doc = (Document) object;
283
        doc.loadFromState(state);
284
    }
285

    
286
    public void saveToState(PersistentState state, Object obj) throws PersistenceException {
287
        Document doc = (Document) obj;
288
        doc.saveToState(state);
289

    
290
    }
291

    
292
    @SuppressWarnings({ "rawtypes", "unchecked" })
293
    public boolean manages(Class theClass) {
294
        return this.getDocumentClass().isAssignableFrom(theClass);
295
    }
296

    
297
    @SuppressWarnings("rawtypes")
298
    public boolean manages(PersistentState state) {
299
        try {
300
            Class theClass;
301
            theClass = (Class) Class.forName(state.getTheClassName());
302
            return manages(theClass);
303
        } catch (ClassNotFoundException e) {
304
            return false;
305
        }
306
    }
307

    
308
    public List<DynStruct> getDefinitions() {
309
        DynStruct definition = this.getDefinition(this.getDocumentClass().getName());
310
        List<DynStruct> definitions = new ArrayList<DynStruct>();
311
        definitions.add(definition);
312
        return definitions;
313
    }
314

    
315
    public String getDomainName() {
316
        return PersistenceManager.DEFAULT_DOMAIN_NAME;
317
    }
318

    
319
    public String getDomainURL() {
320
        return PersistenceManager.DEFAULT_DOMAIN_URL;
321
    }
322

    
323
    @SuppressWarnings({ "unchecked", "rawtypes" })
324
    public List getManagedClasses() {
325
        List classes = new ArrayList();
326
        classes.add(this.getDocumentClass());
327
        return classes;
328
    }
329

    
330
    @SuppressWarnings("rawtypes")
331
    public Class getManagedClass(Object object) {
332
        return this.getDocumentClass();
333
    }
334

    
335
    @SuppressWarnings("rawtypes")
336
    public Class getManagedClass(PersistentState state) {
337
        return this.getDocumentClass();
338
    }
339

    
340
    @SuppressWarnings("rawtypes")
341
    public Class getManagedClass(String name) {
342
        return this.getDocumentClass();
343
    }
344

    
345
    @SuppressWarnings("rawtypes")
346
    public String getManagedClassName(Object object) {
347
        Class clazz = this.getManagedClass(object);
348
        if (clazz != null){
349
            return clazz.getName();
350
        }
351
        return null;
352
    }
353

    
354
    @SuppressWarnings("rawtypes")
355
    protected IDocumentWindow createDocumentWindow(Document document) {
356
        IDocumentWindow documentWindow = null;
357
        Class windowClass = getMainWindowClass();
358

    
359
        try {
360

    
361
            try {
362
                Constructor constructor;
363
                constructor =
364
                    windowClass.getConstructor(new Class[] { Document.class });
365
                documentWindow =
366
                    (IDocumentWindow) constructor
367
                        .newInstance(new Object[] { document });
368
            } catch (NoSuchMethodException e1) {
369
                documentWindow = (IDocumentWindow) windowClass.newInstance();
370
                documentWindow.setDocument(document);
371
            }
372
        } catch (Exception e) {
373
            logger.warn("Can't create the document window.", e);
374
            return null;
375
        }
376
        return documentWindow;
377
    }
378

    
379
    public void update(Observable observable, Object notification) {
380
        // Do nothing
381
    }
382
    
383

    
384
    public void addObserver(Observer o) {
385
        this.observableHelper.addObserver(o);
386
    }
387

    
388
    public void deleteObserver(Observer o) {
389
        this.observableHelper.deleteObserver(o);
390
    }
391

    
392
    public void deleteObservers() {
393
        this.observableHelper.deleteObservers();
394
    }
395
    
396
    protected Notification notifyObservers(String type, Document doc) {
397
        Notification notification = new BaseNotification(type, new Object[] {doc});
398
        this.observableHelper.notifyObservers(notification);
399
        return notification;
400
    }
401

    
402
    protected Notification notifyObservers(String type, IWindow doc) {
403
        Notification notification = new BaseNotification(type, new Object[] {doc});
404
        this.observableHelper.notifyObservers(notification);
405
        return notification;
406
    }
407

    
408
    @Override
409
    public Object getProperty(Object key) {
410
        return this.propertiesHelper.getProperty(key);
411
    }
412

    
413
    @Override
414
    public void setProperty(Object key, Object obj) {
415
        this.propertiesHelper.setProperty(key, obj);
416
    }
417

    
418
    @Override
419
    public Map getExtendedProperties() {
420
        return this.propertiesHelper.getExtendedProperties();
421
    }
422
    
423
}