Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / AbstractDocumentManager.java @ 38608

History | View | Annotate | Download (7.33 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2009 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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 2
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
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2004-2009 IVER TI
26
*   
27
*/
28

    
29
package org.gvsig.app.project.documents;
30

    
31
import java.lang.reflect.Constructor;
32
import java.util.ArrayList;
33
import java.util.HashSet;
34
import java.util.Iterator;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.Set;
38

    
39
import javax.swing.ImageIcon;
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.project.Project;
47
import org.gvsig.app.project.documents.gui.IDocumentWindow;
48
import org.gvsig.tools.dynobject.DynStruct;
49
import org.gvsig.tools.extensionpoint.ExtensionBuilder;
50
import org.gvsig.tools.persistence.PersistenceManager;
51
import org.gvsig.tools.persistence.PersistentState;
52
import org.gvsig.tools.persistence.exception.PersistenceException;
53

    
54

    
55

    
56
/**
57
 * Factory of ProjectDocument.
58
 *
59
 * @author 2005-         Vicente Caballero
60
 * @author 2009-         Joaquin del Cerro
61
 * 
62
 */
63

    
64
public abstract class AbstractDocumentManager implements DocumentManager {
65

    
66
    private static final Logger logger = LoggerFactory.getLogger(AbstractDocumentManager.class);
67

    
68
    /**
69
     * Returns the type of document priority.
70
     *
71
     * This priority is used when order document factories in UI.
72
     * 
73
     * @return Priority.
74
     */
75
    public int getPriority() {
76
        return 10;
77
    }
78

    
79
    /**
80
     * Returns the icon for the type of document.
81
     *
82
     * @return Image.
83
     */
84
    public ImageIcon getIcon() {
85
        return PluginServices.getIconTheme().get("document-icon-sel");
86
    }
87

    
88
    /**
89
     * Returns the icon for the type of document when is selected
90
     *
91
     * @return Image.
92
     */
93
    public ImageIcon getIconSelected() {
94
        return PluginServices.getIconTheme().get("document-icon");
95
    }
96

    
97
    /**
98
     * Returns the title of type of document.
99
     *
100
     * @return String title for type of document
101
     */
102
    public String getTitle() {
103
        return PluginServices.getText(this, "documento");
104
    }
105

    
106
    public AbstractDocument createDocumentByUser() {
107
        return createDocument();
108
    }
109

    
110
    public Iterator<? extends Document> createDocumentsByUser() {
111
        Set<AbstractDocument> doc = new HashSet<AbstractDocument>(1);
112
        doc.add(createDocumentByUser());
113
        return doc.iterator();
114
    }
115

    
116
    /**
117
     * @see ExtensionBuilder
118
     */
119
    public Object create() {
120
        return this;
121
    }
122

    
123
    /**
124
     * @see ExtensionBuilder
125
     */
126
    public Object create(Object[] args) {
127
        return this;
128
    }
129

    
130
    /**
131
     * @see ExtensionBuilder
132
     */
133
    @SuppressWarnings("rawtypes")
134
    public Object create(Map args) {
135
        return this;
136
    }
137

    
138
    public IWindow getMainWindow(Document doc) {
139
        return getMainWindow(doc, null);
140
    }
141

    
142

    
143
    /**
144
     * Return true if the name exists to another document.
145
     *
146
     * @param project
147
     * @param documentName
148
     *
149
     * @return True if the name exists.
150
     * @deprecated use instead  project.getProjectDocument
151
     */
152
    public boolean existName(Project project, String documentName) {
153
        return project.getDocument(documentName, getTypeName())!=null;
154
    }
155

    
156
    /**
157
     * Return the class or interface for the documents managed by this factory.
158
     * @return The document class;
159
     */
160
    @SuppressWarnings("rawtypes")
161
    abstract protected Class getDocumentClass();
162

    
163
    public Object createFromState(PersistentState state) throws PersistenceException {
164
        Document doc = this.createDocument();
165
        return doc;
166
    }
167

    
168
    public void loadFromState(PersistentState state, Object object) throws PersistenceException {
169
        Document doc = (Document) object;
170
        doc.loadFromState(state);
171
    }
172

    
173
    public void saveToState(PersistentState state, Object obj) throws PersistenceException {
174
        Document doc = (Document) obj;
175
        doc.saveToState(state);
176

    
177
    }
178

    
179
    @SuppressWarnings({ "rawtypes", "unchecked" })
180
    public boolean manages(Class theClass) {
181
        return this.getDocumentClass().isAssignableFrom(theClass);
182
    }
183

    
184
    @SuppressWarnings("rawtypes")
185
    public boolean manages(PersistentState state) {
186
        try {
187
            Class theClass;
188
            theClass = (Class) Class.forName(state.getTheClassName());
189
            return manages(theClass);
190
        } catch (ClassNotFoundException e) {
191
            return false;
192
        }
193
    }
194

    
195
    public List<DynStruct> getDefinitions() {
196
        DynStruct definition = this.getDefinition(this.getDocumentClass().getName());
197
        List<DynStruct> definitions = new ArrayList<DynStruct>();
198
        definitions.add(definition);
199
        return definitions;
200
    }
201

    
202
    public String getDomainName() {
203
        return PersistenceManager.DEFAULT_DOMAIN_NAME;
204
    }
205

    
206
    public String getDomainURL() {
207
        return PersistenceManager.DEFAULT_DOMAIN_URL;
208
    }
209

    
210
    @SuppressWarnings({ "unchecked", "rawtypes" })
211
    public List getManagedClasses() {
212
        List classes = new ArrayList();
213
        classes.add(this.getDocumentClass());
214
        return classes;
215
    }
216

    
217
    @SuppressWarnings("rawtypes")
218
    public Class getManagedClass(Object object) {
219
        return this.getDocumentClass();
220
    }
221

    
222
    @SuppressWarnings("rawtypes")
223
    public Class getManagedClass(PersistentState state) {
224
        return this.getDocumentClass();
225
    }
226

    
227
    @SuppressWarnings("rawtypes")
228
    public Class getManagedClass(String name) {
229
        return this.getDocumentClass();
230
    }
231

    
232
    @SuppressWarnings("rawtypes")
233
    public String getManagedClassName(Object object) {
234
        Class clazz = this.getManagedClass(object);
235
        if (clazz != null){
236
            return clazz.getName();
237
        }
238
        return null;
239
    }
240

    
241
    @SuppressWarnings("rawtypes")
242
    protected IDocumentWindow createDocumentWindow(Document document) {
243
        IDocumentWindow documentWindow = null;
244
        Class windowClass = getMainWindowClass();
245

    
246
        try {
247

    
248
            try {
249
                Constructor constructor;
250
                constructor =
251
                    windowClass.getConstructor(new Class[] { Document.class });
252
                documentWindow =
253
                    (IDocumentWindow) constructor
254
                        .newInstance(new Object[] { document });
255
            } catch (NoSuchMethodException e1) {
256
                documentWindow = (IDocumentWindow) windowClass.newInstance();
257
                documentWindow.setDocument(document);
258
            }
259
        } catch (Exception e) {
260
            logger.warn("Can't create the document window.", e);
261
            return null;
262
        }
263
        return documentWindow;
264
    }
265
}