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

History | View | Annotate | Download (9.04 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
import javax.swing.JComponent;
35

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

    
47

    
48

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

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

    
64
        public static final String PERSISTENCE_DEFINITION_NAME = "AbstractDocument";
65

    
66
        protected PropertyChangeSupport change;
67

    
68
        private Project project;
69

    
70
        private String name;
71
        private String creationDate;
72
        private String owner;
73
        private String comment;
74
        private boolean locked = false;
75
        private boolean isModified=false;
76

    
77
        private DocumentManager factory;
78

    
79
        private WindowLayout windowLayout = null;
80

    
81
        private List<ProjectDocumentListener> projectDocListener = new ArrayList<ProjectDocumentListener>();
82

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

    
94
        public AbstractDocument(DocumentManager factory) {
95
                this();
96
                this.factory = factory;
97
        }
98

    
99
        /**
100
         * @see java.lang.Object#toString()
101
         */
102
        public String toString() {
103
                return name;
104
        }
105

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

    
115
        /**
116
         * Return the name of the type of documents
117
         *
118
         * @return name as String
119
         */
120
        public String getTypeName() {
121
                return this.factory.getTypeName();
122
        }
123

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

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

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

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

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

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

    
195
        /**
196
         * Gets the comments asociateds to the document
197
         *
198
         * @param string
199
         */
200
        public void setComment(String string) {
201
                String oldComment = comment;
202
                comment = string;
203
                change.firePropertyChange("comment", oldComment, 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.getDefinition(PERSISTENCE_DEFINITION_NAME);
253
                if ( definition == null ){
254
                    definition = manager.addDefinition(
255
                        AbstractDocument.class,
256
                        PERSISTENCE_DEFINITION_NAME,
257
                        "Document persistence definition",
258
                        null,
259
                        null
260
                    );
261
                    definition.addDynFieldString("comment").setMandatory(false);
262
                    definition.addDynFieldString("creationDate").setMandatory(true);
263
                    definition.addDynFieldString("name").setMandatory(true);
264
                    definition.addDynFieldString("owner").setMandatory(false);
265
                    definition.addDynFieldBoolean("locked").setMandatory(true);
266
                }
267
        }
268

    
269
        public Project getProject() {
270
                return project;
271
        }
272

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

    
277

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

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

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

    
308
        public DocumentManager getFactory() {
309
                return factory;
310
        }
311

    
312
        public boolean isModified() {
313
                return isModified;
314
        }
315

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

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

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

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

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

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

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

    
353
        public IWindow getPropertiesWindow() {
354
                return this.getFactory().getPropertiesWindow(this);
355
        }
356

    
357
        public IWindow getMainWindow() {
358
                return this.getFactory().getMainWindow(this);
359
        }
360

    
361
    public JComponent getMainComponent() {
362
                return this.getFactory().getMainComponent(this);
363
    }
364

    
365
    public boolean isTemporary() {
366
        return false;
367
    }
368

    
369
    public boolean isAvailable() {
370
        return true;
371
    }
372

    
373

    
374
}