Statistics
| Revision:

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 @ 40596

History | View | Annotate | Download (8.81 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
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

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

    
46

    
47

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

    
58
        /**
59
         * 
60
         */
61
        private static final long serialVersionUID = 3335040973071555406L;
62

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

    
76
        private DocumentManager factory;
77

    
78
        private WindowLayout windowLayout = null;
79
        
80
        private List<ProjectDocumentListener> projectDocListener = new ArrayList<ProjectDocumentListener>();
81

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

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

    
105
        /**
106
         * return the name of the document
107
         *
108
         * @return name as String
109
         */
110
        public String getName() {
111
                return name;
112
        }
113

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

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

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

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

    
174
        /**
175
         * Sets the creator name of the document
176
         *
177
         * @param string
178
         */
179
        public void setOwner(String string) {
180
                String oldOwner = owner;
181
                owner = string;
182
                change.firePropertyChange("owner", oldOwner, owner);
183
        }
184

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

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

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

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

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

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

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

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

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

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

    
307
        public DocumentManager getFactory() {
308
                return factory;
309
        }
310
        
311
        public boolean isModified() {
312
                return isModified;
313
        }
314

    
315
        public void setModified(boolean modified) {
316
                isModified=modified;
317
        }
318

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

    
329
        /**
330
         * @deprecated use {link {@link #raiseEventCreateWindow(IWindow)}
331
         */
332
        protected void callCreateWindow(IWindow window) {
333
                raiseEventCreateWindow(window);
334
        }
335

    
336
        public void afterAdd() {
337
                // do nothing by default
338
        }
339

    
340
        public void afterRemove() {
341
                // do nothing by default
342
        }
343

    
344
        public String exportDocumentAsText() {
345
                throw new UnsupportedOperationException();
346
        }
347

    
348
        public void setStateFromText(String text) {
349
                throw new UnsupportedOperationException();
350
        }
351

    
352
        public IWindow getPropertiesWindow() {
353
                return this.getFactory().getPropertiesWindow(this);
354
        }
355
        
356
        public IWindow getMainWindow() {
357
                return this.getFactory().getMainWindow(this);
358
        }
359
        
360

    
361
}