Statistics
| Revision:

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

History | View | Annotate | Download (8.81 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
        protected 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
                String oldOwner = owner;
184
                owner = string;
185
                change.firePropertyChange("owner", oldOwner, owner);
186
        }
187

    
188
        /**
189
         * Gets the comments asociateds to the document
190
         *
191
         * @return comments as String
192
         */
193
        public String getComment() {
194
                return comment;
195
        }
196

    
197
        /**
198
         * Gets the comments asociateds to the document
199
         *
200
         * @param string
201
         */
202
        public void setComment(String string) {
203
                String oldComment = comment;
204
                comment = string;
205
                change.firePropertyChange("comment", oldComment, comment);
206
        }
207

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

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

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

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

    
275
        public void setProject(Project project) {
276
                this.project = project;
277
        }
278
        
279

    
280
        /**
281
         * Locks this project element protecting it from deleting from the project.
282
         */
283
        public void lock() {                
284
                boolean oldLocked = locked;
285
                locked = Boolean.TRUE;
286
                change.firePropertyChange("locked", oldLocked, locked);
287
        }
288

    
289
        /**
290
         * Unlocks this element. So, from now on, it can be removed from the project.
291
         */
292
        public void unlock() {
293
                boolean oldLocked = locked;
294
                locked = Boolean.FALSE;
295
                change.firePropertyChange("locked", oldLocked, locked);
296
        }
297

    
298
        /**
299
         * Tells whether if this project's element is locked/protected or not. A protected
300
         * element cannot be removed from the current project.
301
         *
302
         * @see <b>lock()</b> and <b>unlock()</b> methods.
303
         *
304
         * @return true if it is locked, false otherwise
305
         */
306
        public boolean isLocked() {
307
                return locked;
308
        }
309

    
310
        public DocumentManager getFactory() {
311
                return factory;
312
        }
313
        
314
        public boolean isModified() {
315
                return isModified;
316
        }
317

    
318
        public void setModified(boolean modified) {
319
                isModified=modified;
320
        }
321

    
322
        /**
323
         * Throw this event when a new window is created
324
         * @param  window
325
         *         IWindow created
326
         */
327
        public void raiseEventCreateWindow(IWindow window) {
328
                for (int i = 0; i < projectDocListener.size(); i++)
329
                        projectDocListener.get(i).windowCreated(window);
330
        }
331

    
332
        /**
333
         * @deprecated use {link {@link #raiseEventCreateWindow(IWindow)}
334
         */
335
        protected void callCreateWindow(IWindow window) {
336
                raiseEventCreateWindow(window);
337
        }
338

    
339
        public void afterAdd() {
340
                // do nothing by default
341
        }
342

    
343
        public void afterRemove() {
344
                // do nothing by default
345
        }
346

    
347
        public String exportDocumentAsText() {
348
                throw new UnsupportedOperationException();
349
        }
350

    
351
        public void setStateFromText(String text) {
352
                throw new UnsupportedOperationException();
353
        }
354

    
355
        public IWindow getPropertiesWindow() {
356
                return this.getFactory().getPropertiesWindow(this);
357
        }
358
        
359
        public IWindow getMainWindow() {
360
                return this.getFactory().getMainWindow(this);
361
        }
362
        
363

    
364
}