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