Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / ProjectDocument.java @ 21132

History | View | Annotate | Download (11.9 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 org.gvsig.data.ReadException;
52
import org.gvsig.fmap.drivers.reading.DriverIOException;
53
import org.gvsig.fmap.mapcontext.layers.XMLException;
54

    
55
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
56
import com.hardcode.gdbms.engine.data.driver.DriverException;
57
import com.iver.andami.PluginServices;
58
import com.iver.andami.messages.NotificationManager;
59
import com.iver.andami.ui.mdiManager.IWindow;
60
import com.iver.cit.gvsig.project.Project;
61
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
62
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
63
import com.iver.cit.gvsig.project.documents.gui.WindowData;
64
import com.iver.utiles.XMLEntity;
65
import com.iver.utiles.extensionPoints.ExtensionPoint;
66
import com.iver.utiles.extensionPoints.ExtensionPoints;
67
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
68

    
69

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
239
        return pe;
240
    }
241

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

    
280
        }
281

    
282
        /**
283
         * DOCUMENT ME!
284
         *
285
         * @param xml DOCUMENT ME!
286
         * @param p DOCUMENT ME!
287
         *
288
         * @throws XMLException
289
         * @throws OpenException
290
         * @throws ReadDriverException
291
         */
292
        public void setXMLEntity(XMLEntity xml)
293
                throws XMLException, OpenException, ReadDriverException{
294

    
295
                this.setComment(xml.getStringProperty("comment"));
296
                this.setCreationDate(xml.getStringProperty("creationDate"));
297
                this.setName(xml.getStringProperty("name"));
298
                this.setOwner(xml.getStringProperty("owner"));
299

    
300
        }
301

    
302
        /**
303
         * DOCUMENT ME!
304
         *
305
         * @param xml DOCUMENT ME!
306
         * @param p DOCUMENT ME!
307
         *
308
         * @throws XMLException
309
         * @throws ReadDriverException
310
         * @throws DriverIOException
311
         */
312
        public void setXMLEntity03(XMLEntity xml)
313
                throws XMLException, ReadDriverException{
314

    
315
                        this.setComment(xml.getStringProperty("comment"));
316
                        this.setCreationDate(xml.getStringProperty("creationDate"));
317
                        this.setName(xml.getStringProperty("name"));
318
                        this.setOwner(xml.getStringProperty("owner"));
319

    
320
                }
321

    
322
        /**
323
         * DOCUMENT ME!
324
         *
325
         * @return DOCUMENT ME!
326
         */
327
        public Project getProject() {
328
                return project;
329
        }
330
        /**
331
         * DOCUMENT ME!
332
         *
333
         * @param project DOCUMENT ME!
334
         */
335
        public void setProject(Project project, int index) {
336
                this.project = project;
337
                this.index = index;
338
        }
339
        public int getIndex() {
340
                return index;
341
        }
342

    
343
        /**
344
         * Locks this project element protecting it from deleting from the project.
345
         */
346
        public void lock() {
347
                locked = true;
348
        }
349

    
350
        /**
351
         * Unlocks this element. So, from now on, it can be removed from the project.
352
         */
353
        public void unlock() {
354
                locked = false;
355
        }
356

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

    
369
        public abstract IWindow createWindow();
370
        public abstract IWindow getProperties();
371
        public abstract void afterRemove();
372
        public abstract void afterAdd();
373

    
374

    
375
        public void setProjectDocumentFactory(
376
                        ProjectDocumentFactory projectDocumentFactory) {
377
                this.projectDocumentFactory = projectDocumentFactory;
378
        }
379

    
380
        public ProjectDocumentFactory getProjectDocumentFactory() {
381
                return projectDocumentFactory;
382
        }
383

    
384
        public abstract void exportToXML(XMLEntity root, Project project)  throws SaveException ;
385

    
386
        public abstract void importFromXML(XMLEntity root, XMLEntity typeRoot,int elementIndex ,Project project, boolean removeDocumentsFromRoot) throws XMLException, OpenException, ReadException;
387

    
388

    
389
        public void importFromXML(XMLEntity root, XMLEntity typeRoot,int elementIndex ,Project project) throws XMLException, OpenException, ReadException{
390
                importFromXML(root,typeRoot, elementIndex,project,false);
391
        }
392

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

    
405
        /**
406
         * Store the layout properties (size, position, state of the components)
407
         * of the window associated with this ProjectDocument.
408
         * This is used to re-open the window with the same properties it had
409
         * when it was closed.
410
         */
411
        public void storeWindowData(WindowData data) {
412
                windowData = data;
413
        }
414

    
415
        public boolean isModified() {
416
                return isModified;
417
        }
418

    
419
        public void setModified(boolean modified) {
420
                isModified=modified;
421
        }
422

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