Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / ProjectDocument.java @ 10661

History | View | Annotate | Download (11.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.project.documents;
42

    
43
import java.beans.PropertyChangeListener;
44
import java.beans.PropertyChangeSupport;
45
import java.io.Serializable;
46
import java.text.DateFormat;
47
import java.util.Date;
48
import java.util.HashMap;
49
import java.util.Iterator;
50

    
51
import com.hardcode.driverManager.DriverLoadException;
52
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
53
import com.iver.andami.messages.NotificationManager;
54
import com.iver.andami.ui.mdiManager.IWindow;
55
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
56
import com.iver.cit.gvsig.fmap.layers.CancelationException;
57
import com.iver.cit.gvsig.fmap.layers.XMLException;
58
import com.iver.cit.gvsig.project.Project;
59
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
60
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
61
import com.iver.cit.gvsig.project.documents.gui.WindowData;
62
import com.iver.utiles.XMLEntity;
63
import com.iver.utiles.extensionPoints.ExtensionPoint;
64
import com.iver.utiles.extensionPoints.ExtensionPoints;
65
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
66

    
67

    
68
/**
69
 * Clase base de los elementos del proyecto (mapas, tablas y vistas)
70
 *
71
 * @author Fernando Gonz?lez Cort?s
72
 */
73
public abstract class ProjectDocument implements Serializable {
74
        public static HashMap NUMS = new HashMap();
75
        protected PropertyChangeSupport change;
76
        protected Project project;
77
        protected int index;
78
        protected String name;
79
        protected String creationDate;
80
        protected String owner;
81
        protected String comment;
82
        private boolean locked = false;
83
        protected WindowData windowData = null;
84
        private boolean isModified=false;
85
        private ProjectDocumentFactory projectDocumentFactory;
86
        /**
87
         * Creates a new ProjectElement object.
88
         */
89
        public ProjectDocument() {
90
                creationDate = DateFormat.getDateInstance().format(new Date());
91
                change = new PropertyChangeSupport(this);
92
        }
93

    
94
        /**
95
         * @see java.lang.Object#toString()
96
         */
97
        public String toString() {
98
                return name;
99
        }
100

    
101
        /**
102
         * Obtiene el nombre del elemento
103
         *
104
         * @return
105
         */
106
        public String getName() {
107
                return name;
108
        }
109

    
110
        /**
111
         * Establece el nombre del elemento
112
         *
113
         * @param string
114
         */
115
        public void setName(String string) {
116
                String oldValue = name;
117
                name = string;
118
                change.firePropertyChange("name", oldValue, name);
119
        }
120

    
121
        /**
122
         * Obtiene la fecha de creaci?n del elemento
123
         *
124
         * @return
125
         */
126
        public String getCreationDate() {
127
                return creationDate;
128
        }
129

    
130
        /**
131
         * Obtiene el propietario del elemento
132
         *
133
         * @return
134
         */
135
        public String getOwner() {
136
                return owner;
137
        }
138

    
139
        /**
140
         * Establece la fecha de creaci?n del elemento.
141
         *
142
         * @param string
143
         */
144
        public void setCreationDate(String string) {
145
                creationDate = string;
146
                change.firePropertyChange("creationDate", creationDate, creationDate);
147
        }
148

    
149
        /**
150
         * Establece el propietario del elemento
151
         *
152
         * @param string
153
         */
154
        public void setOwner(String string) {
155
                owner = string;
156
                change.firePropertyChange("owner", owner, owner);
157
        }
158

    
159
        /**
160
         * Obtiene los comentarios del proyecto
161
         *
162
         * @return
163
         */
164
        public String getComment() {
165
                return comment;
166
        }
167

    
168
        /**
169
         * Establece los comentarios del proyecto
170
         *
171
         * @param string
172
         */
173
        public void setComment(String string) {
174
                comment = string;
175
                change.firePropertyChange("comment", comment, comment);
176
        }
177

    
178
        /**
179
         * A?ade un listener para los cambios en las bounded properties
180
         *
181
         * @param listener
182
         */
183
        public synchronized void addPropertyChangeListener(
184
                PropertyChangeListener listener) {
185
                change.addPropertyChangeListener(listener);
186
        }
187

    
188
        /**
189
         * DOCUMENT ME!
190
         *
191
         * @return DOCUMENT ME!
192
         * @throws XMLException
193
         * @throws SaveException
194
         */
195
        public XMLEntity getXMLEntity() throws SaveException {
196
                XMLEntity xml = new XMLEntity();
197
                try{
198
                //xml.putProperty("nameRegister",this.getRegisterName());
199
                xml.putProperty("className", projectDocumentFactory.getRegisterName());
200
                xml.putProperty("comment", comment);
201
                xml.putProperty("creationDate", creationDate);
202
                xml.putProperty("name", name);
203
                xml.putProperty("owner", owner);
204
                }catch (Exception e) {
205
                        throw new SaveException(e,this.getClass().getName());
206
                }
207
                return xml;
208
        }
209

    
210
    /**
211
     * DOCUMENT ME!
212
     *
213
     * @param xml DOCUMENT ME!
214
     * @param p DOCUMENT ME!
215
     *
216
     * @return DOCUMENT ME!
217
     * @throws XMLException
218
     */
219
    public static ProjectDocument createFromXML03(XMLEntity xml, Project p) throws XMLException{
220
        ProjectDocument pe = null;
221

    
222
            Class clase;
223
                        try {
224
                                clase = Class.forName(xml.getStringProperty("className"));
225
                        pe = (ProjectDocument) clase.newInstance();
226
                        } catch (ClassNotFoundException e) {
227
                    NotificationManager.addError("Clase de ProjectElement no reconocida",
228
                            e);
229
                        } catch (InstantiationException e) {
230
                    NotificationManager.addError("Clase de ProjectElement no reconocida",
231
                            e);
232
                        } catch (IllegalAccessException e) {
233
                    NotificationManager.addError("Clase de ProjectElement no reconocida",
234
                            e);
235
                        }
236

    
237
        return pe;
238
    }
239

    
240
        /**
241
         * DOCUMENT ME!
242
         *
243
         * @param xml DOCUMENT ME!
244
         * @param p DOCUMENT ME!
245
         *
246
         * @return DOCUMENT ME!
247
         *
248
         * @throws XMLException
249
         * @throws DriverException
250
         * @throws DriverIOException
251
         * @throws OpenException
252
         */
253
        public static ProjectDocument createFromXML(XMLEntity xml, Project p)
254
                throws XMLException, OpenException {
255
                ProjectDocumentFactory pde = null;
256
                try{
257
                        ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
258
                        ExtensionPoint extPoint=((ExtensionPoint)extensionPoints.get("Documents"));
259
                        try {
260
                                pde = (ProjectDocumentFactory) extPoint.create(xml.getStringProperty("className"));
261
                        } catch (InstantiationException e) {
262
                                NotificationManager.addError("Clase de ProjectDocument no reconocida",
263
                                                e);
264
                        } catch (IllegalAccessException e) {
265
                                NotificationManager.addError("Clase de ProjectDocument no reconocida",
266
                                        e);
267
                        }
268
                        ProjectDocument pe=pde.create(p);
269
                        pe.setProjectDocumentFactory(pde);
270
                        return pe;
271
                }catch (Exception e) {
272
                        throw new OpenException(e,pde.getNameType());
273
                }
274
        }
275

    
276
        /**
277
         * DOCUMENT ME!
278
         *
279
         * @param xml DOCUMENT ME!
280
         * @param p DOCUMENT ME!
281
         *
282
         * @throws XMLException
283
         * @throws OpenException
284
         * @throws ReadDriverException
285
         */
286
        public void setXMLEntity(XMLEntity xml)
287
                throws XMLException, OpenException, ReadDriverException{
288

    
289
                this.setComment(xml.getStringProperty("comment"));
290
                this.setCreationDate(xml.getStringProperty("creationDate"));
291
                this.setName(xml.getStringProperty("name"));
292
                this.setOwner(xml.getStringProperty("owner"));
293

    
294
        }
295

    
296
        /**
297
         * DOCUMENT ME!
298
         *
299
         * @param xml DOCUMENT ME!
300
         * @param p DOCUMENT ME!
301
         *
302
         * @throws XMLException
303
         * @throws ReadDriverException
304
         * @throws DriverIOException
305
         */
306
        public void setXMLEntity03(XMLEntity xml)
307
                throws XMLException, ReadDriverException{
308

    
309
                        this.setComment(xml.getStringProperty("comment"));
310
                        this.setCreationDate(xml.getStringProperty("creationDate"));
311
                        this.setName(xml.getStringProperty("name"));
312
                        this.setOwner(xml.getStringProperty("owner"));
313

    
314
                }
315

    
316
        /**
317
         * DOCUMENT ME!
318
         *
319
         * @return DOCUMENT ME!
320
         */
321
        public Project getProject() {
322
                return project;
323
        }
324
        /**
325
         * DOCUMENT ME!
326
         *
327
         * @param project DOCUMENT ME!
328
         */
329
        public void setProject(Project project, int index) {
330
                this.project = project;
331
                this.index = index;
332
        }
333
        public int getIndex() {
334
                return index;
335
        }
336

    
337
        /**
338
         * Locks this project element protecting it from deleting from the project.
339
         */
340
        public void lock() {
341
                locked = true;
342
        }
343

    
344
        /**
345
         * Unlocks this element. So, from now on, it can be removed from the project.
346
         */
347
        public void unlock() {
348
                locked = false;
349
        }
350

    
351
        /**
352
         * Tells whether if this project's element is locked/protected or not. A protected
353
         * element cannot be removed from the current project.
354
         *
355
         * @see <b>lock()</b> and <b>unlock()</b> methods.
356
         *
357
         * @return true if it is locked, false otherwise
358
         */
359
        public boolean isLocked() {
360
                return locked;
361
        }
362

    
363
        public abstract IWindow createWindow();
364
        public abstract IWindow getProperties();
365
        public abstract void afterRemove();
366
        public abstract void afterAdd();
367

    
368

    
369
        public void setProjectDocumentFactory(
370
                        ProjectDocumentFactory projectDocumentFactory) {
371
                this.projectDocumentFactory = projectDocumentFactory;
372
        }
373

    
374
        public ProjectDocumentFactory getProjectDocumentFactory() {
375
                return projectDocumentFactory;
376
        }
377

    
378
        public abstract void exportToXML(XMLEntity root, Project project)  throws SaveException ;
379

    
380
        public abstract void importFromXML(XMLEntity root, XMLEntity typeRoot,int elementIndex ,Project project, boolean removeDocumentsFromRoot) throws XMLException, OpenException, ReadDriverException;
381

    
382

    
383
        public void importFromXML(XMLEntity root, XMLEntity typeRoot,int elementIndex ,Project project) throws XMLException, OpenException, ReadDriverException{
384
                importFromXML(root,typeRoot, elementIndex,project,false);
385
        }
386

    
387
        /**
388
         * Get the layout properties (size, position, state of the components)
389
         * of the window associated with this ProjectDocument.
390
         * This is used to re-open the window with the same properties it had
391
         * when it was closed.
392
         *
393
         * @return A WindowData object storing the properties of the window.
394
         */
395
        public WindowData getWindowData() {
396
                return windowData;
397
        }
398

    
399
        /**
400
         * Store the layout properties (size, position, state of the components)
401
         * of the window associated with this ProjectDocument.
402
         * This is used to re-open the window with the same properties it had
403
         * when it was closed.
404
         */
405
        public void storeWindowData(WindowData data) {
406
                windowData = data;
407
        }
408

    
409
        public boolean isModified() {
410
                return isModified;
411
        }
412

    
413
        public void setModified(boolean modified) {
414
                isModified=modified;
415
        }
416

    
417
        public static void initializeNUMS() {
418
                NUMS.clear();
419
                ExtensionPoints extensionPoints =
420
                        ExtensionPointsSingleton.getInstance();
421
                ExtensionPoint extensionPoint =(ExtensionPoint)extensionPoints.get("Documents");
422
                Iterator iterator = extensionPoint.keySet().iterator();
423
                while (iterator.hasNext()) {
424
                        try {
425
                                ProjectDocumentFactory documentFactory = (ProjectDocumentFactory)extensionPoint.create((String)iterator.next());
426
                                NUMS.put(documentFactory.getRegisterName(),new Integer(0));
427
                        } catch (InstantiationException e) {
428
                                e.printStackTrace();
429
                        } catch (IllegalAccessException e) {
430
                                e.printStackTrace();
431
                        } catch (ClassCastException e) {
432
                                e.printStackTrace();
433
                        }
434
                }
435

    
436

    
437
        }
438
}