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

History | View | Annotate | Download (8.27 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
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2009 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
* 
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
* 
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
* 
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
42
* MA  02110-1301, USA.
43
* 
44
*/
45

    
46
/*
47
* AUTHORS (In addition to CIT):
48
* 2004-2009 IVER TI
49
*   
50
*/
51

    
52
package org.gvsig.app.project.documents;
53

    
54
import java.lang.reflect.Constructor;
55
import java.util.ArrayList;
56
import java.util.HashSet;
57
import java.util.Iterator;
58
import java.util.List;
59
import java.util.Map;
60
import java.util.Set;
61

    
62
import javax.swing.ImageIcon;
63

    
64
import org.slf4j.Logger;
65
import org.slf4j.LoggerFactory;
66

    
67
import org.gvsig.andami.PluginServices;
68
import org.gvsig.andami.ui.mdiManager.IWindow;
69
import org.gvsig.app.project.Project;
70
import org.gvsig.app.project.documents.gui.IDocumentWindow;
71
import org.gvsig.tools.dynobject.DynStruct;
72
import org.gvsig.tools.extensionpoint.ExtensionBuilder;
73
import org.gvsig.tools.persistence.PersistenceManager;
74
import org.gvsig.tools.persistence.PersistentState;
75
import org.gvsig.tools.persistence.exception.PersistenceException;
76

    
77

    
78

    
79
/**
80
 * Factory of ProjectDocument.
81
 *
82
 * @author 2005-         Vicente Caballero
83
 * @author 2009-         Joaquin del Cerro
84
 * 
85
 */
86

    
87
public abstract class AbstractDocumentManager implements DocumentManager {
88

    
89
    private static final Logger logger = LoggerFactory.getLogger(AbstractDocumentManager.class);
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
    public AbstractDocument createDocumentByUser() {
130
        return createDocument();
131
    }
132

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

    
139
    /**
140
     * @see ExtensionBuilder
141
     */
142
    public Object create() {
143
        return this;
144
    }
145

    
146
    /**
147
     * @see ExtensionBuilder
148
     */
149
    public Object create(Object[] args) {
150
        return this;
151
    }
152

    
153
    /**
154
     * @see ExtensionBuilder
155
     */
156
    @SuppressWarnings("rawtypes")
157
    public Object create(Map args) {
158
        return this;
159
    }
160

    
161
    public IWindow getMainWindow(Document doc) {
162
        return getMainWindow(doc, null);
163
    }
164

    
165

    
166
    /**
167
     * Return true if the name exists to another document.
168
     *
169
     * @param project
170
     * @param documentName
171
     *
172
     * @return True if the name exists.
173
     * @deprecated use instead  project.getProjectDocument
174
     */
175
    public boolean existName(Project project, String documentName) {
176
        return project.getDocument(documentName, getTypeName())!=null;
177
    }
178

    
179
    /**
180
     * Return the class or interface for the documents managed by this factory.
181
     * @return The document class;
182
     */
183
    @SuppressWarnings("rawtypes")
184
    abstract protected Class getDocumentClass();
185

    
186
    public Object createFromState(PersistentState state) throws PersistenceException {
187
        Document doc = this.createDocument();
188
        return doc;
189
    }
190

    
191
    public void loadFromState(PersistentState state, Object object) throws PersistenceException {
192
        Document doc = (Document) object;
193
        doc.loadFromState(state);
194
    }
195

    
196
    public void saveToState(PersistentState state, Object obj) throws PersistenceException {
197
        Document doc = (Document) obj;
198
        doc.saveToState(state);
199

    
200
    }
201

    
202
    @SuppressWarnings({ "rawtypes", "unchecked" })
203
    public boolean manages(Class theClass) {
204
        return this.getDocumentClass().isAssignableFrom(theClass);
205
    }
206

    
207
    @SuppressWarnings("rawtypes")
208
    public boolean manages(PersistentState state) {
209
        try {
210
            Class theClass;
211
            theClass = (Class) Class.forName(state.getTheClassName());
212
            return manages(theClass);
213
        } catch (ClassNotFoundException e) {
214
            return false;
215
        }
216
    }
217

    
218
    public List<DynStruct> getDefinitions() {
219
        DynStruct definition = this.getDefinition(this.getDocumentClass().getName());
220
        List<DynStruct> definitions = new ArrayList<DynStruct>();
221
        definitions.add(definition);
222
        return definitions;
223
    }
224

    
225
    public String getDomainName() {
226
        return PersistenceManager.DEFAULT_DOMAIN_NAME;
227
    }
228

    
229
    public String getDomainURL() {
230
        return PersistenceManager.DEFAULT_DOMAIN_URL;
231
    }
232

    
233
    @SuppressWarnings({ "unchecked", "rawtypes" })
234
    public List getManagedClasses() {
235
        List classes = new ArrayList();
236
        classes.add(this.getDocumentClass());
237
        return classes;
238
    }
239

    
240
    @SuppressWarnings("rawtypes")
241
    public Class getManagedClass(Object object) {
242
        return this.getDocumentClass();
243
    }
244

    
245
    @SuppressWarnings("rawtypes")
246
    public Class getManagedClass(PersistentState state) {
247
        return this.getDocumentClass();
248
    }
249

    
250
    @SuppressWarnings("rawtypes")
251
    public Class getManagedClass(String name) {
252
        return this.getDocumentClass();
253
    }
254

    
255
    @SuppressWarnings("rawtypes")
256
    public String getManagedClassName(Object object) {
257
        Class clazz = this.getManagedClass(object);
258
        if (clazz != null){
259
            return clazz.getName();
260
        }
261
        return null;
262
    }
263

    
264
    @SuppressWarnings("rawtypes")
265
    protected IDocumentWindow createDocumentWindow(Document document) {
266
        IDocumentWindow documentWindow = null;
267
        Class windowClass = getMainWindowClass();
268

    
269
        try {
270

    
271
            try {
272
                Constructor constructor;
273
                constructor =
274
                    windowClass.getConstructor(new Class[] { Document.class });
275
                documentWindow =
276
                    (IDocumentWindow) constructor
277
                        .newInstance(new Object[] { document });
278
            } catch (NoSuchMethodException e1) {
279
                documentWindow = (IDocumentWindow) windowClass.newInstance();
280
                documentWindow.setDocument(document);
281
            }
282
        } catch (Exception e) {
283
            logger.warn("Can't create the document window.", e);
284
            return null;
285
        }
286
        return documentWindow;
287
    }
288
}