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 31496 jjdelcerro
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2009 Infrastructures and Transports Department
4 31544 cordinyana
* of the Valencian Government (CIT)
5 31496 jjdelcerro
*
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 29598 jpiera
package org.gvsig.app.project.documents;
30 7343 caballero
31 35193 jjdelcerro
import java.lang.reflect.Constructor;
32 33659 fdiaz
import java.util.ArrayList;
33 36631 cordinyana
import java.util.HashSet;
34
import java.util.Iterator;
35 33659 fdiaz
import java.util.List;
36 7738 jaume
import java.util.Map;
37 36631 cordinyana
import java.util.Set;
38 7343 caballero
39 7738 jaume
import javax.swing.ImageIcon;
40 7343 caballero
41 35193 jjdelcerro
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43
44 29598 jpiera
import org.gvsig.andami.PluginServices;
45 31496 jjdelcerro
import org.gvsig.andami.ui.mdiManager.IWindow;
46 29598 jpiera
import org.gvsig.app.project.Project;
47 35193 jjdelcerro
import org.gvsig.app.project.documents.gui.IDocumentWindow;
48 33659 fdiaz
import org.gvsig.tools.dynobject.DynStruct;
49 24958 jmvivo
import org.gvsig.tools.extensionpoint.ExtensionBuilder;
50 33659 fdiaz
import org.gvsig.tools.persistence.PersistenceManager;
51
import org.gvsig.tools.persistence.PersistentState;
52
import org.gvsig.tools.persistence.exception.PersistenceException;
53 24958 jmvivo
54 7343 caballero
55
56
/**
57
 * Factory of ProjectDocument.
58
 *
59 31496 jjdelcerro
 * @author 2005-         Vicente Caballero
60
 * @author 2009-         Joaquin del Cerro
61
 *
62 7343 caballero
 */
63 31496 jjdelcerro
64 33659 fdiaz
public abstract class AbstractDocumentManager implements DocumentManager {
65 31496 jjdelcerro
66 35193 jjdelcerro
    private static final Logger logger = LoggerFactory.getLogger(AbstractDocumentManager.class);
67
68 7343 caballero
    /**
69 31496 jjdelcerro
     * Returns the type of document priority.
70 7343 caballero
     *
71 31496 jjdelcerro
     * This priority is used when order document factories in UI.
72
     *
73 7343 caballero
     * @return Priority.
74
     */
75
    public int getPriority() {
76
        return 10;
77
    }
78 9690 caballero
79 8159 sbayarri
    /**
80 31496 jjdelcerro
     * Returns the icon for the type of document.
81 8159 sbayarri
     *
82 31496 jjdelcerro
     * @return Image.
83 8159 sbayarri
     */
84 31496 jjdelcerro
    public ImageIcon getIcon() {
85 34114 fdiaz
        return PluginServices.getIconTheme().get("document-icon-sel");
86 8159 sbayarri
    }
87 7343 caballero
88
    /**
89 31496 jjdelcerro
     * Returns the icon for the type of document when is selected
90 7343 caballero
     *
91 31496 jjdelcerro
     * @return Image.
92 7343 caballero
     */
93 31496 jjdelcerro
    public ImageIcon getIconSelected() {
94 34114 fdiaz
        return PluginServices.getIconTheme().get("document-icon");
95 31496 jjdelcerro
    }
96 7343 caballero
97
    /**
98 31496 jjdelcerro
     * Returns the title of type of document.
99 7343 caballero
     *
100 31496 jjdelcerro
     * @return String title for type of document
101 7343 caballero
     */
102 31496 jjdelcerro
    public String getTitle() {
103 7343 caballero
        return PluginServices.getText(this, "documento");
104
    }
105
106 31496 jjdelcerro
    public AbstractDocument createDocumentByUser() {
107
        return createDocument();
108 7343 caballero
    }
109
110 36631 cordinyana
    public Iterator<? extends Document> createDocumentsByUser() {
111
        Set<AbstractDocument> doc = new HashSet<AbstractDocument>(1);
112
        doc.add(createDocumentByUser());
113
        return doc.iterator();
114
    }
115
116 7343 caballero
    /**
117 31496 jjdelcerro
     * @see ExtensionBuilder
118 7343 caballero
     */
119
    public Object create() {
120
        return this;
121
    }
122
123
    /**
124 34114 fdiaz
     * @see ExtensionBuilder
125
     */
126 7343 caballero
    public Object create(Object[] args) {
127
        return this;
128
    }
129
130
    /**
131 31496 jjdelcerro
     * @see ExtensionBuilder
132 7343 caballero
     */
133 34114 fdiaz
    @SuppressWarnings("rawtypes")
134 33659 fdiaz
    public Object create(Map args) {
135 7343 caballero
        return this;
136
    }
137
138 31496 jjdelcerro
    public IWindow getMainWindow(Document doc) {
139 34114 fdiaz
        return getMainWindow(doc, null);
140
    }
141 7343 caballero
142 34114 fdiaz
143 7755 jmvivo
    /**
144 34114 fdiaz
     * 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 33659 fdiaz
156 34114 fdiaz
    /**
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 33659 fdiaz
163 34114 fdiaz
    public Object createFromState(PersistentState state) throws PersistenceException {
164
        Document doc = this.createDocument();
165
        return doc;
166
    }
167 33659 fdiaz
168 34114 fdiaz
    public void loadFromState(PersistentState state, Object object) throws PersistenceException {
169
        Document doc = (Document) object;
170
        doc.loadFromState(state);
171
    }
172 33659 fdiaz
173 34114 fdiaz
    public void saveToState(PersistentState state, Object obj) throws PersistenceException {
174
        Document doc = (Document) obj;
175
        doc.saveToState(state);
176 33659 fdiaz
177 34114 fdiaz
    }
178 33659 fdiaz
179 34114 fdiaz
    @SuppressWarnings({ "rawtypes", "unchecked" })
180
    public boolean manages(Class theClass) {
181
        return this.getDocumentClass().isAssignableFrom(theClass);
182
    }
183 33659 fdiaz
184 34114 fdiaz
    @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 33659 fdiaz
195 34114 fdiaz
    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 33659 fdiaz
202 34114 fdiaz
    public String getDomainName() {
203
        return PersistenceManager.DEFAULT_DOMAIN_NAME;
204
    }
205 33659 fdiaz
206 34114 fdiaz
    public String getDomainURL() {
207
        return PersistenceManager.DEFAULT_DOMAIN_URL;
208
    }
209 33659 fdiaz
210 34114 fdiaz
    @SuppressWarnings({ "unchecked", "rawtypes" })
211
    public List getManagedClasses() {
212
        List classes = new ArrayList();
213
        classes.add(this.getDocumentClass());
214
        return classes;
215
    }
216 33659 fdiaz
217 34114 fdiaz
    @SuppressWarnings("rawtypes")
218
    public Class getManagedClass(Object object) {
219
        return this.getDocumentClass();
220
    }
221 33659 fdiaz
222 34114 fdiaz
    @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 35193 jjdelcerro
    @SuppressWarnings("rawtypes")
242
    protected IDocumentWindow createDocumentWindow(Document document) {
243
        IDocumentWindow documentWindow = null;
244
        Class windowClass = getMainWindowClass();
245 34114 fdiaz
246 35193 jjdelcerro
        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 7343 caballero
}