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

History | View | Annotate | Download (12.7 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 IWindow getPropertiesWindow(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
        return null;
195
    }
196
    
197
    
198
    
199
    @Override
200
    public JComponent getMainComponent(Document doc) {
201
        ApplicationManager application = ApplicationLocator.getManager();
202
        Document activeDoc = application.getActiveDocument();
203
        if( activeDoc == doc ) { // Ojo, hay que cerciorarse de que esta comparacion funciona.
204
            IWindow win = this.getMainWindow(doc);
205
            if( win instanceof Component ) {
206
                return ((Component)win).asJComponent();
207
            }
208
            return (JComponent)win;
209
        }
210
        if( activeDoc instanceof DocumentsContainer ) {
211
            DocumentsContainer container = (DocumentsContainer)activeDoc;
212
            return this.getMainComponent(container, doc);
213
        }
214
        return null;
215
    }
216
    
217
    public JComponent getMainComponent(DocumentsContainer container, Document doc) {
218
        Formatter f = new Formatter();
219
        f.format("08X",container.hashCode());
220
        f.format("08X",doc.hashCode());
221
        String key = f.toString();
222
        f.close();
223
        return mainComponents.get(key);
224
    }
225

    
226
    public void unregisterMainComponent(DocumentsContainer container, Document doc) {
227
        Formatter f = new Formatter();
228
        f.format("08X",container.hashCode());
229
        f.format("08X",doc.hashCode());
230
        String key = f.toString();
231
        f.close();
232
        mainComponents.remove(key);
233
    }
234

    
235
    public void registerMainComponent(DocumentsContainer container, Document doc, JComponent component) {
236
        Formatter f = new Formatter();
237
        f.format("08X",container.hashCode());
238
        f.format("08X",doc.hashCode());
239
        String key = f.toString();
240
        f.close();
241
        mainComponents.put(key,component);
242
    }
243
    
244
    /**
245
     * Return true if the name exists to another document.
246
     *
247
     * @param project
248
     * @param documentName
249
     *
250
     * @return True if the name exists.
251
     * @deprecated use instead  project.getProjectDocument
252
     */
253
    public boolean existName(Project project, String documentName) {
254
        return project.getDocument(documentName, getTypeName())!=null;
255
    }
256

    
257
    /**
258
     * Return the class or interface for the documents managed by this factory.
259
     * @return The document class;
260
     */
261
    @SuppressWarnings("rawtypes")
262
    abstract protected Class getDocumentClass();
263

    
264
    public Object createFromState(PersistentState state) throws PersistenceException {
265
        Document doc = this.createDocument();
266
        return doc;
267
    }
268

    
269
    public void loadFromState(PersistentState state, Object object) throws PersistenceException {
270
        Document doc = (Document) object;
271
        doc.loadFromState(state);
272
    }
273

    
274
    public void saveToState(PersistentState state, Object obj) throws PersistenceException {
275
        Document doc = (Document) obj;
276
        doc.saveToState(state);
277

    
278
    }
279

    
280
    @SuppressWarnings({ "rawtypes", "unchecked" })
281
    public boolean manages(Class theClass) {
282
        return this.getDocumentClass().isAssignableFrom(theClass);
283
    }
284

    
285
    @SuppressWarnings("rawtypes")
286
    public boolean manages(PersistentState state) {
287
        try {
288
            Class theClass;
289
            theClass = (Class) Class.forName(state.getTheClassName());
290
            return manages(theClass);
291
        } catch (ClassNotFoundException e) {
292
            return false;
293
        }
294
    }
295

    
296
    public List<DynStruct> getDefinitions() {
297
        DynStruct definition = this.getDefinition(this.getDocumentClass().getName());
298
        List<DynStruct> definitions = new ArrayList<DynStruct>();
299
        definitions.add(definition);
300
        return definitions;
301
    }
302

    
303
    public String getDomainName() {
304
        return PersistenceManager.DEFAULT_DOMAIN_NAME;
305
    }
306

    
307
    public String getDomainURL() {
308
        return PersistenceManager.DEFAULT_DOMAIN_URL;
309
    }
310

    
311
    @SuppressWarnings({ "unchecked", "rawtypes" })
312
    public List getManagedClasses() {
313
        List classes = new ArrayList();
314
        classes.add(this.getDocumentClass());
315
        return classes;
316
    }
317

    
318
    @SuppressWarnings("rawtypes")
319
    public Class getManagedClass(Object object) {
320
        return this.getDocumentClass();
321
    }
322

    
323
    @SuppressWarnings("rawtypes")
324
    public Class getManagedClass(PersistentState state) {
325
        return this.getDocumentClass();
326
    }
327

    
328
    @SuppressWarnings("rawtypes")
329
    public Class getManagedClass(String name) {
330
        return this.getDocumentClass();
331
    }
332

    
333
    @SuppressWarnings("rawtypes")
334
    public String getManagedClassName(Object object) {
335
        Class clazz = this.getManagedClass(object);
336
        if (clazz != null){
337
            return clazz.getName();
338
        }
339
        return null;
340
    }
341

    
342
    @SuppressWarnings("rawtypes")
343
    protected IDocumentWindow createDocumentWindow(Document document) {
344
        IDocumentWindow documentWindow = null;
345
        Class windowClass = getMainWindowClass();
346

    
347
        try {
348

    
349
            try {
350
                Constructor constructor;
351
                constructor =
352
                    windowClass.getConstructor(new Class[] { Document.class });
353
                documentWindow =
354
                    (IDocumentWindow) constructor
355
                        .newInstance(new Object[] { document });
356
            } catch (NoSuchMethodException e1) {
357
                documentWindow = (IDocumentWindow) windowClass.newInstance();
358
                documentWindow.setDocument(document);
359
            }
360
        } catch (Exception e) {
361
            logger.warn("Can't create the document window.", e);
362
            return null;
363
        }
364
        return documentWindow;
365
    }
366

    
367
    public void update(Observable observable, Object notification) {
368
        // Do nothing
369
    }
370
    
371

    
372
    public void addObserver(Observer o) {
373
        this.observableHelper.addObserver(o);
374
    }
375

    
376
    public void deleteObserver(Observer o) {
377
        this.observableHelper.deleteObserver(o);
378
    }
379

    
380
    public void deleteObservers() {
381
        this.observableHelper.deleteObservers();
382
    }
383
    
384
    protected Notification notifyObservers(String type, Document doc) {
385
        Notification notification = new BaseNotification(type, new Object[] {doc});
386
        this.observableHelper.notifyObservers(notification);
387
        return notification;
388
    }
389

    
390
    protected Notification notifyObservers(String type, IWindow doc) {
391
        Notification notification = new BaseNotification(type, new Object[] {doc});
392
        this.observableHelper.notifyObservers(notification);
393
        return notification;
394
    }
395

    
396
    @Override
397
    public Object getProperty(Object key) {
398
        return this.propertiesHelper.getProperty(key);
399
    }
400

    
401
    @Override
402
    public void setProperty(Object key, Object obj) {
403
        this.propertiesHelper.setProperty(key, obj);
404
    }
405

    
406
    @Override
407
    public Map getExtendedProperties() {
408
        return this.propertiesHelper.getExtendedProperties();
409
    }
410
    
411
}