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

History | View | Annotate | Download (9.09 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
import org.apache.commons.lang3.StringUtils;
36

    
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
 *
57
 */
58
public abstract class AbstractDocument implements Serializable, Persistent, Document {
59

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

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

    
67
        protected PropertyChangeSupport change;
68

    
69
        private Project project;
70

    
71
        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

    
82
        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

    
100
        /**
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
         *
119
         * @return name as String
120
         */
121
        public String getTypeName() {
122
                return this.factory.getTypeName();
123
        }
124

    
125
        /**
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
                if( StringUtils.isEmpty(name) ) {
135
                        name = null;
136
                }
137
                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

    
227
        public WindowLayout getWindowLayout() {
228
                return this.windowLayout;
229
        }
230

    
231
        public void setWindowLayout(WindowLayout layout) {
232
                this.windowLayout = layout;
233
        }
234

    
235
        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

    
251
        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
                        null,
260
                        null
261
                    );
262
                    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

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

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

    
278

    
279
        /**
280
         * Locks this project element protecting it from deleting from the project.
281
         */
282
        public void lock() {
283
                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

    
313
        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

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

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

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

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

    
374

    
375
}