Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / AbstractDocumentManager.java @ 36443

History | View | Annotate | Download (7.24 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.List;
34
import java.util.Map;
35

    
36
import javax.swing.ImageIcon;
37

    
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

    
41
import org.gvsig.andami.PluginServices;
42
import org.gvsig.andami.ui.mdiManager.IWindow;
43
import org.gvsig.app.project.Project;
44
import org.gvsig.app.project.documents.gui.IDocumentWindow;
45
import org.gvsig.tools.dynobject.DynStruct;
46
import org.gvsig.tools.extensionpoint.ExtensionBuilder;
47
import org.gvsig.tools.persistence.PersistenceManager;
48
import org.gvsig.tools.persistence.PersistentState;
49
import org.gvsig.tools.persistence.exception.PersistenceException;
50

    
51

    
52

    
53
/**
54
 * Factory of ProjectDocument.
55
 *
56
 * @author 2005-         Vicente Caballero
57
 * @author 2009-         Joaquin del Cerro
58
 * 
59
 */
60

    
61
public abstract class AbstractDocumentManager implements DocumentManager {
62

    
63
    private static final Logger logger = LoggerFactory.getLogger(AbstractDocumentManager.class);
64

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

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

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

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

    
103
    /**
104
     * Introdece a gui to be able from the characteristics that we want a ProjectDocument
105
     *
106
     * @param project present Project.
107
     *
108
     * @return new ProjectDocument.
109
     */
110
    public AbstractDocument createDocumentByUser() {
111
        return createDocument();
112
    }
113

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

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

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

    
136
    public IWindow getMainWindow(Document doc) {
137
        return getMainWindow(doc, null);
138
    }
139

    
140

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

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

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

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

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

    
175
    }
176

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

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

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

    
200
    public String getDomainName() {
201
        return PersistenceManager.DEFAULT_DOMAIN_NAME;
202
    }
203

    
204
    public String getDomainURL() {
205
        return PersistenceManager.DEFAULT_DOMAIN_URL;
206
    }
207

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

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

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

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

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

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

    
244
        try {
245

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