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 / AbstractDocument.java @ 40558
History | View | Annotate | Download (9.75 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 |
package org.gvsig.app.project.documents; |
52 |
|
53 |
import java.beans.PropertyChangeListener; |
54 |
import java.beans.PropertyChangeSupport; |
55 |
import java.io.Serializable; |
56 |
import java.text.DateFormat; |
57 |
import java.util.ArrayList; |
58 |
import java.util.Date; |
59 |
import java.util.List; |
60 |
|
61 |
import org.gvsig.andami.PluginServices; |
62 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
63 |
import org.gvsig.app.project.Project; |
64 |
import org.gvsig.app.project.documents.gui.WindowLayout; |
65 |
import org.gvsig.tools.ToolsLocator; |
66 |
import org.gvsig.tools.dynobject.DynStruct; |
67 |
import org.gvsig.tools.persistence.PersistenceManager; |
68 |
import org.gvsig.tools.persistence.Persistent; |
69 |
import org.gvsig.tools.persistence.PersistentState; |
70 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
71 |
|
72 |
|
73 |
|
74 |
/**
|
75 |
* Clase base de los elementos del proyecto (mapas, tablas y vistas)
|
76 |
*
|
77 |
* @author 2006-2009 Jose Manuel Vivo
|
78 |
* @author 2005- Vicente Caballero
|
79 |
* @author 2009- Joaquin del Cerro
|
80 |
*
|
81 |
*/
|
82 |
public abstract class AbstractDocument implements Serializable, Persistent, Document { |
83 |
|
84 |
/**
|
85 |
*
|
86 |
*/
|
87 |
private static final long serialVersionUID = 3335040973071555406L; |
88 |
|
89 |
public static final String PERSISTENCE_DEFINITION_NAME = "AbstractDocument"; |
90 |
|
91 |
protected PropertyChangeSupport change; |
92 |
|
93 |
private Project project;
|
94 |
|
95 |
private String name; |
96 |
private String creationDate; |
97 |
private String owner; |
98 |
private String comment; |
99 |
private boolean locked = false; |
100 |
private boolean isModified=false; |
101 |
|
102 |
private DocumentManager factory;
|
103 |
|
104 |
private WindowLayout windowLayout = null; |
105 |
|
106 |
private List<ProjectDocumentListener> projectDocListener = new ArrayList<ProjectDocumentListener>(); |
107 |
|
108 |
/**
|
109 |
* Creates a new ProjectElement object.
|
110 |
*/
|
111 |
public AbstractDocument() { |
112 |
creationDate = DateFormat.getDateInstance().format(new Date()); |
113 |
change = new PropertyChangeSupport(this); |
114 |
this.factory = null; |
115 |
this.project = null; |
116 |
this.name = PluginServices.getText(this, "untitled"); |
117 |
} |
118 |
|
119 |
public AbstractDocument(DocumentManager factory) { |
120 |
this();
|
121 |
this.factory = factory;
|
122 |
} |
123 |
|
124 |
/**
|
125 |
* @see java.lang.Object#toString()
|
126 |
*/
|
127 |
public String toString() { |
128 |
return name;
|
129 |
} |
130 |
|
131 |
/**
|
132 |
* return the name of the document
|
133 |
*
|
134 |
* @return name as String
|
135 |
*/
|
136 |
public String getName() { |
137 |
return name;
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* Return the name of the type of documents
|
142 |
*
|
143 |
* @return name as String
|
144 |
*/
|
145 |
public String getTypeName() { |
146 |
return this.factory.getTypeName(); |
147 |
} |
148 |
|
149 |
/**
|
150 |
* Sets the nane of the document
|
151 |
*
|
152 |
* @param name as string
|
153 |
*/
|
154 |
public void setName(String name) { |
155 |
if (isLocked()) {
|
156 |
throw new RuntimeException("this document is locked"); |
157 |
} |
158 |
if( name.length()==0) { |
159 |
name = null;
|
160 |
} |
161 |
if( project != null ) { |
162 |
Document doc = project.getDocument(name, this.getTypeName()); |
163 |
if( doc != null && !this.equals(doc) ) { |
164 |
throw new RuntimeException("document name already exists in project"); |
165 |
} |
166 |
} |
167 |
String previousName = this.name; |
168 |
this.name = name;
|
169 |
change.firePropertyChange("name", previousName, name);
|
170 |
} |
171 |
|
172 |
/**
|
173 |
* Get the creation date of the document.
|
174 |
*
|
175 |
* @return creation date as String
|
176 |
*/
|
177 |
public String getCreationDate() { |
178 |
return creationDate;
|
179 |
} |
180 |
|
181 |
/**
|
182 |
* Get the creator name of the document.
|
183 |
*
|
184 |
* @return creator name as String
|
185 |
*/
|
186 |
public String getOwner() { |
187 |
return owner;
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* Set the creation date of the document.
|
192 |
*
|
193 |
* @param string
|
194 |
*/
|
195 |
public void setCreationDate(String string) { |
196 |
creationDate = string; |
197 |
change.firePropertyChange("creationDate", creationDate, creationDate);
|
198 |
} |
199 |
|
200 |
/**
|
201 |
* Sets the creator name of the document
|
202 |
*
|
203 |
* @param string
|
204 |
*/
|
205 |
public void setOwner(String string) { |
206 |
String oldOwner = owner;
|
207 |
owner = string; |
208 |
change.firePropertyChange("owner", oldOwner, owner);
|
209 |
} |
210 |
|
211 |
/**
|
212 |
* Gets the comments asociateds to the document
|
213 |
*
|
214 |
* @return comments as String
|
215 |
*/
|
216 |
public String getComment() { |
217 |
return comment;
|
218 |
} |
219 |
|
220 |
/**
|
221 |
* Gets the comments asociateds to the document
|
222 |
*
|
223 |
* @param string
|
224 |
*/
|
225 |
public void setComment(String string) { |
226 |
String oldComment = comment;
|
227 |
comment = string; |
228 |
change.firePropertyChange("comment", oldComment, comment);
|
229 |
} |
230 |
|
231 |
/**
|
232 |
* Add a listener to monitor the changes in the properties of the document
|
233 |
*
|
234 |
* @param listener
|
235 |
*/
|
236 |
public synchronized void addPropertyChangeListener( |
237 |
PropertyChangeListener listener) {
|
238 |
change.addPropertyChangeListener(listener); |
239 |
} |
240 |
|
241 |
/**
|
242 |
* Register a ProjectDocumentListener.
|
243 |
* @param listener ProjectDocumentListener
|
244 |
*/
|
245 |
public void addListener(ProjectDocumentListener listener) { |
246 |
if(this.projectDocListener.indexOf(listener) == -1) |
247 |
this.projectDocListener.add(listener);
|
248 |
} |
249 |
|
250 |
|
251 |
public WindowLayout getWindowLayout() {
|
252 |
return this.windowLayout; |
253 |
} |
254 |
|
255 |
public void setWindowLayout(WindowLayout layout) { |
256 |
this.windowLayout = layout;
|
257 |
} |
258 |
|
259 |
public void loadFromState(PersistentState state) |
260 |
throws PersistenceException {
|
261 |
this.setComment( state.getString("comment") ); |
262 |
this.setCreationDate( state.getString("creationDate") ); |
263 |
this.setName( state.getString("name") ); |
264 |
this.setOwner( state.getString("owner") ); |
265 |
} |
266 |
|
267 |
public void saveToState(PersistentState state) throws PersistenceException { |
268 |
state.set("comment", comment);
|
269 |
state.set("creationDate", creationDate);
|
270 |
state.set("name", name);
|
271 |
state.set("owner", owner);
|
272 |
state.set("locked", locked);
|
273 |
} |
274 |
|
275 |
public static void registerPersistent() { |
276 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
277 |
DynStruct definition = manager.getDefinition(PERSISTENCE_DEFINITION_NAME); |
278 |
if ( definition == null ){ |
279 |
definition = manager.addDefinition( |
280 |
AbstractDocument.class,
|
281 |
PERSISTENCE_DEFINITION_NAME, |
282 |
"Document persistence definition",
|
283 |
null,
|
284 |
null
|
285 |
); |
286 |
definition.addDynFieldString("comment").setMandatory(false); |
287 |
definition.addDynFieldString("creationDate").setMandatory(true); |
288 |
definition.addDynFieldString("name").setMandatory(true); |
289 |
definition.addDynFieldString("owner").setMandatory(false); |
290 |
definition.addDynFieldBoolean("locked").setMandatory(true); |
291 |
} |
292 |
} |
293 |
|
294 |
public Project getProject() {
|
295 |
return project;
|
296 |
} |
297 |
|
298 |
public void setProject(Project project) { |
299 |
this.project = project;
|
300 |
} |
301 |
|
302 |
|
303 |
/**
|
304 |
* Locks this project element protecting it from deleting from the project.
|
305 |
*/
|
306 |
public void lock() { |
307 |
boolean oldLocked = locked;
|
308 |
locked = Boolean.TRUE;
|
309 |
change.firePropertyChange("locked", oldLocked, locked);
|
310 |
} |
311 |
|
312 |
/**
|
313 |
* Unlocks this element. So, from now on, it can be removed from the project.
|
314 |
*/
|
315 |
public void unlock() { |
316 |
boolean oldLocked = locked;
|
317 |
locked = Boolean.FALSE;
|
318 |
change.firePropertyChange("locked", oldLocked, locked);
|
319 |
} |
320 |
|
321 |
/**
|
322 |
* Tells whether if this project's element is locked/protected or not. A protected
|
323 |
* element cannot be removed from the current project.
|
324 |
*
|
325 |
* @see <b>lock()</b> and <b>unlock()</b> methods.
|
326 |
*
|
327 |
* @return true if it is locked, false otherwise
|
328 |
*/
|
329 |
public boolean isLocked() { |
330 |
return locked;
|
331 |
} |
332 |
|
333 |
public DocumentManager getFactory() {
|
334 |
return factory;
|
335 |
} |
336 |
|
337 |
public boolean isModified() { |
338 |
return isModified;
|
339 |
} |
340 |
|
341 |
public void setModified(boolean modified) { |
342 |
isModified=modified; |
343 |
} |
344 |
|
345 |
/**
|
346 |
* Throw this event when a new window is created
|
347 |
* @param window
|
348 |
* IWindow created
|
349 |
*/
|
350 |
public void raiseEventCreateWindow(IWindow window) { |
351 |
for (int i = 0; i < projectDocListener.size(); i++) |
352 |
projectDocListener.get(i).windowCreated(window); |
353 |
} |
354 |
|
355 |
/**
|
356 |
* @deprecated use {link {@link #raiseEventCreateWindow(IWindow)}
|
357 |
*/
|
358 |
protected void callCreateWindow(IWindow window) { |
359 |
raiseEventCreateWindow(window); |
360 |
} |
361 |
|
362 |
public void afterAdd() { |
363 |
// do nothing by default
|
364 |
} |
365 |
|
366 |
public void afterRemove() { |
367 |
// do nothing by default
|
368 |
} |
369 |
|
370 |
public String exportDocumentAsText() { |
371 |
throw new UnsupportedOperationException(); |
372 |
} |
373 |
|
374 |
public void setStateFromText(String text) { |
375 |
throw new UnsupportedOperationException(); |
376 |
} |
377 |
|
378 |
public IWindow getPropertiesWindow() {
|
379 |
return this.getFactory().getPropertiesWindow(this); |
380 |
} |
381 |
|
382 |
public IWindow getMainWindow() {
|
383 |
return this.getFactory().getMainWindow(this); |
384 |
} |
385 |
|
386 |
|
387 |
} |