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 @ 40596
History | View | Annotate | Download (7.33 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.HashSet; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
import java.util.Map; |
34 |
import java.util.Set; |
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 |
public AbstractDocument createDocumentByUser() { |
104 |
return createDocument();
|
105 |
} |
106 |
|
107 |
public Iterator<? extends Document> createDocumentsByUser() { |
108 |
Set<AbstractDocument> doc = new HashSet<AbstractDocument>(1); |
109 |
doc.add(createDocumentByUser()); |
110 |
return doc.iterator();
|
111 |
} |
112 |
|
113 |
/**
|
114 |
* @see ExtensionBuilder
|
115 |
*/
|
116 |
public Object create() { |
117 |
return this; |
118 |
} |
119 |
|
120 |
/**
|
121 |
* @see ExtensionBuilder
|
122 |
*/
|
123 |
public Object create(Object[] args) { |
124 |
return this; |
125 |
} |
126 |
|
127 |
/**
|
128 |
* @see ExtensionBuilder
|
129 |
*/
|
130 |
@SuppressWarnings("rawtypes") |
131 |
public Object create(Map args) { |
132 |
return this; |
133 |
} |
134 |
|
135 |
public IWindow getMainWindow(Document doc) { |
136 |
return getMainWindow(doc, null); |
137 |
} |
138 |
|
139 |
|
140 |
/**
|
141 |
* Return true if the name exists to another document.
|
142 |
*
|
143 |
* @param project
|
144 |
* @param documentName
|
145 |
*
|
146 |
* @return True if the name exists.
|
147 |
* @deprecated use instead project.getProjectDocument
|
148 |
*/
|
149 |
public boolean existName(Project project, String documentName) { |
150 |
return project.getDocument(documentName, getTypeName())!=null; |
151 |
} |
152 |
|
153 |
/**
|
154 |
* Return the class or interface for the documents managed by this factory.
|
155 |
* @return The document class;
|
156 |
*/
|
157 |
@SuppressWarnings("rawtypes") |
158 |
abstract protected Class getDocumentClass(); |
159 |
|
160 |
public Object createFromState(PersistentState state) throws PersistenceException { |
161 |
Document doc = this.createDocument(); |
162 |
return doc;
|
163 |
} |
164 |
|
165 |
public void loadFromState(PersistentState state, Object object) throws PersistenceException { |
166 |
Document doc = (Document) object; |
167 |
doc.loadFromState(state); |
168 |
} |
169 |
|
170 |
public void saveToState(PersistentState state, Object obj) throws PersistenceException { |
171 |
Document doc = (Document) obj; |
172 |
doc.saveToState(state); |
173 |
|
174 |
} |
175 |
|
176 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
177 |
public boolean manages(Class theClass) { |
178 |
return this.getDocumentClass().isAssignableFrom(theClass); |
179 |
} |
180 |
|
181 |
@SuppressWarnings("rawtypes") |
182 |
public boolean manages(PersistentState state) { |
183 |
try {
|
184 |
Class theClass;
|
185 |
theClass = (Class) Class.forName(state.getTheClassName()); |
186 |
return manages(theClass);
|
187 |
} catch (ClassNotFoundException e) { |
188 |
return false; |
189 |
} |
190 |
} |
191 |
|
192 |
public List<DynStruct> getDefinitions() { |
193 |
DynStruct definition = this.getDefinition(this.getDocumentClass().getName()); |
194 |
List<DynStruct> definitions = new ArrayList<DynStruct>(); |
195 |
definitions.add(definition); |
196 |
return definitions;
|
197 |
} |
198 |
|
199 |
public String getDomainName() { |
200 |
return PersistenceManager.DEFAULT_DOMAIN_NAME;
|
201 |
} |
202 |
|
203 |
public String getDomainURL() { |
204 |
return PersistenceManager.DEFAULT_DOMAIN_URL;
|
205 |
} |
206 |
|
207 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
208 |
public List getManagedClasses() { |
209 |
List classes = new ArrayList(); |
210 |
classes.add(this.getDocumentClass());
|
211 |
return classes;
|
212 |
} |
213 |
|
214 |
@SuppressWarnings("rawtypes") |
215 |
public Class getManagedClass(Object object) { |
216 |
return this.getDocumentClass(); |
217 |
} |
218 |
|
219 |
@SuppressWarnings("rawtypes") |
220 |
public Class getManagedClass(PersistentState state) { |
221 |
return this.getDocumentClass(); |
222 |
} |
223 |
|
224 |
@SuppressWarnings("rawtypes") |
225 |
public Class getManagedClass(String name) { |
226 |
return this.getDocumentClass(); |
227 |
} |
228 |
|
229 |
@SuppressWarnings("rawtypes") |
230 |
public String getManagedClassName(Object object) { |
231 |
Class clazz = this.getManagedClass(object); |
232 |
if (clazz != null){ |
233 |
return clazz.getName();
|
234 |
} |
235 |
return null; |
236 |
} |
237 |
|
238 |
@SuppressWarnings("rawtypes") |
239 |
protected IDocumentWindow createDocumentWindow(Document document) { |
240 |
IDocumentWindow documentWindow = null;
|
241 |
Class windowClass = getMainWindowClass();
|
242 |
|
243 |
try {
|
244 |
|
245 |
try {
|
246 |
Constructor constructor;
|
247 |
constructor = |
248 |
windowClass.getConstructor(new Class[] { Document.class }); |
249 |
documentWindow = |
250 |
(IDocumentWindow) constructor |
251 |
.newInstance(new Object[] { document }); |
252 |
} catch (NoSuchMethodException e1) { |
253 |
documentWindow = (IDocumentWindow) windowClass.newInstance(); |
254 |
documentWindow.setDocument(document); |
255 |
} |
256 |
} catch (Exception e) { |
257 |
logger.warn("Can't create the document window.", e);
|
258 |
return null; |
259 |
} |
260 |
return documentWindow;
|
261 |
} |
262 |
} |