Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / AbstractDocument.java @ 33275

History | View | Annotate | Download (8.21 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
package org.gvsig.app.project.documents;
29

    
30
import java.beans.PropertyChangeListener;
31
import java.beans.PropertyChangeSupport;
32
import java.io.Serializable;
33
import java.text.DateFormat;
34
import java.util.ArrayList;
35
import java.util.Date;
36
import java.util.List;
37

    
38
import org.gvsig.andami.PluginServices;
39
import org.gvsig.andami.ui.mdiManager.IWindow;
40
import org.gvsig.app.project.Project;
41
import org.gvsig.app.project.documents.gui.WindowLayout;
42
import org.gvsig.tools.ToolsLocator;
43
import org.gvsig.tools.dynobject.DynStruct;
44
import org.gvsig.tools.persistence.PersistenceManager;
45
import org.gvsig.tools.persistence.Persistent;
46
import org.gvsig.tools.persistence.PersistentState;
47
import org.gvsig.tools.persistence.exception.PersistenceException;
48

    
49

    
50

    
51
/**
52
 * Clase base de los elementos del proyecto (mapas, tablas y vistas)
53
 *
54
 * @author 2006-2009 Jose Manuel Vivo
55
 * @author 2005-         Vicente Caballero
56
 * @author 2009-         Joaquin del Cerro
57
 * 
58
 */
59
public abstract class AbstractDocument implements Serializable, Persistent, Document {
60

    
61
        /**
62
         * 
63
         */
64
        private static final long serialVersionUID = 3335040973071555406L;
65

    
66
        public static final String PERSISTENCE_DEFINITION_NAME = "AbstractDocument";
67
        
68
        private PropertyChangeSupport change;
69
        
70
        private Project project;
71
        
72
        private String name;
73
        private String creationDate;
74
        private String owner;
75
        private String comment;
76
        private boolean locked = false;
77
        private boolean isModified=false;
78

    
79
        private DocumentManager factory;
80

    
81
        private WindowLayout windowLayout = null;
82
        
83
        private List<ProjectDocumentListener> projectDocListener = new ArrayList<ProjectDocumentListener>();
84

    
85
        /**
86
         * Creates a new ProjectElement object.
87
         */
88
        public AbstractDocument() {
89
                creationDate = DateFormat.getDateInstance().format(new Date());
90
                change = new PropertyChangeSupport(this);
91
                this.factory = null;
92
                this.project = null;
93
                this.name = PluginServices.getText(this, "untitled");
94
        }
95

    
96
        public AbstractDocument(DocumentManager factory) {
97
                this();
98
                this.factory = factory;
99
        }
100
        
101
        /**
102
         * @see java.lang.Object#toString()
103
         */
104
        public String toString() {
105
                return name;
106
        }
107

    
108
        /**
109
         * return the name of the document
110
         *
111
         * @return name as String
112
         */
113
        public String getName() {
114
                return name;
115
        }
116

    
117
        /**
118
         * Return the name of the type of documents
119
         * 
120
         * @return name as String
121
         */
122
        public String getTypeName() {
123
                return this.factory.getTypeName();
124
        }
125
        
126
        /**
127
         * Sets the nane of the document
128
         *
129
         * @param name as string
130
         */
131
        public void setName(String name) {
132
                if (isLocked()) {
133
                        throw new RuntimeException("this document is locked");
134
                }
135
                if( name.length()==0) {
136
                        name = null;
137
                } 
138
                if( project != null ) {
139
                        Document doc = project.getDocument(name, this.getTypeName());
140
                        if( doc != null && !this.equals(doc)  ) {
141
                                throw new RuntimeException("document name already exists in project");
142
                        }
143
                }
144
                String previousName = this.name;
145
                this.name = name;
146
                change.firePropertyChange("name", previousName, name);
147
        }
148

    
149
        /**
150
         * Get the creation date of the document.
151
         *
152
         * @return creation date as String
153
         */
154
        public String getCreationDate() {
155
                return creationDate;
156
        }
157

    
158
        /**
159
         * Get the creator name of the document.
160
         *
161
         * @return creator name as String
162
         */
163
        public String getOwner() {
164
                return owner;
165
        }
166

    
167
        /**
168
         * Set the creation date of the document.
169
         *
170
         * @param string
171
         */
172
        public void setCreationDate(String string) {
173
                creationDate = string;
174
                change.firePropertyChange("creationDate", creationDate, creationDate);
175
        }
176

    
177
        /**
178
         * Sets the creator name of the document
179
         *
180
         * @param string
181
         */
182
        public void setOwner(String string) {
183
                owner = string;
184
                change.firePropertyChange("owner", owner, 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
                comment = string;
203
                change.firePropertyChange("comment", comment, comment);
204
        }
205

    
206
        /**
207
         * Add a listener to monitor the changes in the properties of the document
208
         *
209
         * @param listener
210
         */
211
        public synchronized void addPropertyChangeListener(
212
                PropertyChangeListener listener) {
213
                change.addPropertyChangeListener(listener);
214
        }
215

    
216
        /**
217
         * Register a  ProjectDocumentListener.
218
         * @param  listener  ProjectDocumentListener
219
         */
220
        public void addListener(ProjectDocumentListener listener) {
221
                if(this.projectDocListener.indexOf(listener) == -1)
222
                        this.projectDocListener.add(listener);
223
        }
224

    
225
        
226
        public WindowLayout getWindowLayout() {
227
                return this.windowLayout;
228
        }
229
        
230
        public void setWindowLayout(WindowLayout layout) {
231
                this.windowLayout = layout;
232
        }
233
        
234
        public void loadFromState(PersistentState state)
235
                        throws PersistenceException {
236
                this.setComment( state.getString("comment") );
237
                this.setCreationDate( state.getString("creationDate") );
238
                this.setName( state.getString("name") );
239
                this.setOwner( state.getString("owner") );
240
        }
241

    
242
        public void saveToState(PersistentState state) throws PersistenceException {
243
                state.set("comment", comment);
244
                state.set("creationDate", creationDate);
245
                state.set("name", name);
246
                state.set("owner", owner);
247
                state.set("locked", locked);
248
        }
249
        
250
        public static void registerPersistent() {
251
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
252
                DynStruct definition = manager.addDefinition(
253
                                AbstractDocument.class,
254
                                PERSISTENCE_DEFINITION_NAME,
255
                                "Document persistence definition",
256
                                null, 
257
                                null
258
                );  
259
                definition.addDynFieldString("comment").setMandatory(true);
260
                definition.addDynFieldString("creationDate").setMandatory(true);
261
                definition.addDynFieldString("name").setMandatory(true);
262
                definition.addDynFieldString("owner").setMandatory(true);
263
                definition.addDynFieldString("locked").setMandatory(true);
264
        }
265
        
266
        public Project getProject() {
267
                return project;
268
        }
269

    
270
        public void setProject(Project project) {
271
                this.project = project;
272
        }
273
        
274

    
275
        /**
276
         * Locks this project element protecting it from deleting from the project.
277
         */
278
        public void lock() {
279
                locked = true;
280
        }
281

    
282
        /**
283
         * Unlocks this element. So, from now on, it can be removed from the project.
284
         */
285
        public void unlock() {
286
                locked = false;
287
        }
288

    
289
        /**
290
         * Tells whether if this project's element is locked/protected or not. A protected
291
         * element cannot be removed from the current project.
292
         *
293
         * @see <b>lock()</b> and <b>unlock()</b> methods.
294
         *
295
         * @return true if it is locked, false otherwise
296
         */
297
        public boolean isLocked() {
298
                return locked;
299
        }
300

    
301
        public DocumentManager getFactory() {
302
                return factory;
303
        }
304
        
305
        public boolean isModified() {
306
                return isModified;
307
        }
308

    
309
        public void setModified(boolean modified) {
310
                isModified=modified;
311
        }
312

    
313
        /**
314
         * Throw this event when a new window is created
315
         * @param  window
316
         *         IWindow created
317
         */
318
        public void raiseEventCreateWindow(IWindow window) {
319
                for (int i = 0; i < projectDocListener.size(); i++)
320
                        projectDocListener.get(i).windowCreated(window);
321
        }
322

    
323
        /**
324
         * @deprecated use {link {@link #raiseEventCreateWindow(IWindow)}
325
         */
326
        protected void callCreateWindow(IWindow window) {
327
                raiseEventCreateWindow(window);
328
        }
329

    
330
        public void afterAdd() {
331
                // do nothing by default
332
        }
333

    
334
        public void afterRemove() {
335
                // do nothing by default
336
        }
337

    
338
        public String exportDocumentAsText() {
339
                throw new UnsupportedOperationException();
340
        }
341

    
342
        public void setStateFromText(String text) {
343
                throw new UnsupportedOperationException();
344
        }
345

    
346
}