Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / ProjectTable.java @ 10666

History | View | Annotate | Download (24.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.table;
42

    
43
import java.io.IOException;
44
import java.text.DateFormat;
45
import java.util.Date;
46

    
47
import javax.swing.JOptionPane;
48

    
49
import com.hardcode.driverManager.DriverLoadException;
50
import com.hardcode.gdbms.engine.data.DataSource;
51
import com.hardcode.gdbms.engine.data.DataSourceFactory;
52
import com.hardcode.gdbms.engine.data.NoSuchTableException;
53
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
54
import com.hardcode.gdbms.engine.instruction.SemanticException;
55
import com.hardcode.gdbms.parser.ParseException;
56
import com.iver.andami.PluginServices;
57
import com.iver.andami.messages.NotificationManager;
58
import com.iver.andami.ui.mdiManager.IWindow;
59
import com.iver.cit.gvsig.fmap.DriverException;
60
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
61
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
62
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
63
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
64
import com.iver.cit.gvsig.fmap.layers.FLayer;
65
import com.iver.cit.gvsig.fmap.layers.FLayers;
66
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
67
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
68
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
69
import com.iver.cit.gvsig.fmap.layers.XMLException;
70
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
71
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
72
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend;
73
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
74
import com.iver.cit.gvsig.gui.tables.Column;
75
import com.iver.cit.gvsig.gui.tables.Columns;
76
import com.iver.cit.gvsig.project.Project;
77
import com.iver.cit.gvsig.project.documents.ProjectDocument;
78
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
79
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
80
import com.iver.cit.gvsig.project.documents.table.gui.TableProperties;
81
import com.iver.cit.gvsig.project.documents.view.ProjectView;
82
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
83
import com.iver.utiles.XMLEntity;
84

    
85
/**
86
 * Tabla del proyecto
87
 *
88
 * @author Fernando Gonz?lez Cort?s
89
 */
90
public class ProjectTable extends ProjectDocument {
91
        //public static int numTables = 0;
92

    
93
        private IEditableSource esModel;
94

    
95
        private IEditableSource original;
96

    
97
        private String linkTable;
98

    
99
        private String field1;
100

    
101
        private String field2;
102

    
103
        /* No es necesaria para operar, s?lo para guardar el proyecto */
104
        private AlphanumericData associatedTable;
105

    
106
        private int[] mapping;
107

    
108
        private String[] alias;
109

    
110
        private Columns columns = new Columns();
111
        
112
        private boolean isModified=false;
113
        
114

    
115
        /**
116
         * Establece a true el bit index-?simo del bitmap de campos visibles. Los
117
         * campos cuyos bits est?n a true se considerar?n visibles y viceversa
118
         *
119
         * @param index
120
         *            indice del bit que se quiere establecer a true
121
         */
122
        public void set(int index) {
123
                // TODO implementar bien
124
                // modelo.set(index);
125

    
126
                change.firePropertyChange("visibles", true, true);
127
        }
128

    
129
        /**
130
         * Obtiene el valor del bit index-?simo del bitmap de campos visibles
131
         *
132
         * @param index
133
         *            indice del bit que se quiere obtener
134
         *
135
         * @return devuelve true si el campo index-?simo es visible y false en caso
136
         *         contrario
137
         */
138
        public boolean get(int index) {
139
                // TODO implementar bien
140
                // return modelo.get(index);
141
                return false;
142
        }
143

    
144
        /**
145
         * Obtiene el modelo de la Tabla. Es decir, una clase con capacidad para
146
         * leer la informaci?n de la tabla
147
         *
148
         * @return
149
         */
150
        public IEditableSource getModelo() {
151
                return esModel;
152
        }
153

    
154
        /**
155
         * Establece el valor del bit index-?simo al valor 'value'
156
         *
157
         * @param bitIndex
158
         *            indice del bit que se quiere tocar
159
         * @param value
160
         *            valor que se quiere establecer en el bit indicado
161
         */
162
        public void set(int bitIndex, boolean value) {
163
                // TODO implementar bien
164
                // modelo.set(bitIndex, value);
165
                change.firePropertyChange("visibles", value, value);
166
        }
167

    
168

    
169

    
170
        public void createAlias()
171
                        throws com.hardcode.gdbms.engine.data.driver.DriverException,
172
                        DriverLoadException {
173
                SelectableDataSource sds = esModel.getRecordset();
174
//                mapping = new int[sds.getFieldCount()+1];
175
//                alias = new String[sds.getFieldCount()+1];
176
//                for (int i = 0; i < sds.getFieldCount()+1; i++) {
177
                mapping = new int[sds.getFieldCount()];
178
                alias = new String[sds.getFieldCount()];
179
                for (int i = 0; i < sds.getFieldCount(); i++) {
180
                        mapping[i]=i;
181
                        alias[i]=sds.getFieldName(i);
182

    
183
//                        if (i==0){
184
//                                mapping[i]=i;
185
//                                alias[i]=" ";
186
//                        }else{
187
//
188
//                                mapping[i] = i;
189
//                                alias[i] = sds.getFieldName(i-1);
190
//                        }
191
                }
192
                recalculateColumnsFromAliases();
193

    
194
        }
195

    
196
        public void recalculateColumnsFromAliases()
197
        {
198
                columns.clear();
199
                for (int i = 0; i < alias.length; i++) {
200
                        addColumn(new Column());
201
                }
202

    
203
        }
204

    
205
        /**
206
         * DOCUMENT ME!
207
         *
208
         * @param sds
209
         *            DOCUMENT ME!
210
         * @throws DriverLoadException
211
         */
212
        public void setDataSource(IEditableSource es) throws DriverLoadException {
213
                setModel(es);
214

    
215
                setName(esModel.getRecordset().getName());
216
                setCreationDate(DateFormat.getInstance().format(new Date()));
217
                change.firePropertyChange("model", esModel, esModel);
218
        }
219

    
220
        /**
221
         * DOCUMENT ME!
222
         *
223
         * @param ds
224
         *            DOCUMENT ME!
225
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException
226
         */
227
        public void replaceDataSource(IEditableSource es)
228
                        throws com.hardcode.gdbms.engine.data.driver.DriverException {
229
                if (original == null) {
230
                        original = esModel;
231
                }
232
                setModel(es);
233
                try {
234
                         es.getRecordset().setSelectionSupport((original
235
                                                        .getRecordset()).getSelectionSupport());
236

    
237
                        createAlias();
238
                } catch (DriverLoadException e1) {
239
                        throw new com.hardcode.gdbms.engine.data.driver.DriverException(e1);
240
                }
241
                // FJP:
242
                // Si la tabla proviene de un layer, cambiamos su recordset
243
                if (associatedTable != null) {
244
                        if (associatedTable instanceof FLyrVect) {
245
                                try {
246
                                        // ((EditableAdapter)((FLyrVect)
247
                                        // associatedTable).getSource()).setRecordSet((SelectableDataSource)es.getRecordset());
248
                                        FLyrVect lyrVect = (FLyrVect) associatedTable;
249
                                        lyrVect.setRecordset(es.getRecordset());
250
                                        ((FLyrVect) associatedTable).setIsJoined(true);
251
                                } catch (DriverLoadException e) {
252
                                        throw new com.hardcode.gdbms.engine.data.driver.DriverException(
253
                                                        e);
254
                                }
255
                        }
256
                }
257

    
258
                change.firePropertyChange("model", original, esModel);
259
        }
260

    
261
        /**
262
         * DOCUMENT ME!
263
         *
264
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException
265
         * @throws DriverLoadException
266
         */
267
        public void restoreDataSource()
268
                        throws com.hardcode.gdbms.engine.data.driver.DriverException,
269
                        DriverLoadException {
270
                // FJP:
271
                // Si la tabla proviene de un layer, cambiamos su recordset
272
                if (associatedTable != null) {
273
                        if (associatedTable instanceof FLyrVect) {
274
                                // Miramos si la leyenda que est? usando es una
275
                                // leyenda basada en un campo de los de la uni?n.
276
                                // Si lo es, no dejamos pegarle el cambiazo al recordset
277
                                FLyrVect lyr = ((FLyrVect) associatedTable);
278
                                if (lyr.getLegend() instanceof ClassifiedLegend) {
279
                                        ClassifiedLegend legend = (ClassifiedLegend) lyr
280
                                                        .getLegend();
281
                                        VectorialLegend aux = (VectorialLegend) legend;
282
                                        String fieldName = legend.getFieldName();
283
                                        int idField = original.getRecordset().getFieldIndexByName(
284
                                                        fieldName);
285
                                        int idLabelField = -2;
286
                                        if (aux.getLabelField() != null) {
287
                                                idLabelField = original.getRecordset()
288
                                                                .getFieldIndexByName(aux.getLabelField());
289
                                        }
290
                                        if ((idField == -1) || (idLabelField == -1)) {
291
                                                // No se ha encontrado ese campo, o se est? etiquetando
292
                                                // por ese campo
293
                                                JOptionPane.showMessageDialog(null, PluginServices
294
                                                                .getText(this, "leyenda_campo_unido"));
295

    
296
                                                return;
297
                                        }
298
                                }
299

    
300
                                lyr.setRecordset(original.getRecordset());
301

    
302
                                lyr.setIsJoined(false);
303
                        }
304
                }
305

    
306
                setModel(original);
307
                original = null;
308
                createAlias();
309

    
310
                change.firePropertyChange("model", original, esModel);
311
        }
312

    
313
        /**
314
         * DOCUMENT ME!
315
         *
316
         * @return DOCUMENT ME!
317
         * @throws SaveException
318
         *
319
         * @throws XMLException
320
         */
321
        public XMLEntity getXMLEntity() throws SaveException {
322
                XMLEntity xml = super.getXMLEntity();
323
                try {
324
                        // xml.putProperty("nameClass", this.getClass().getName());
325
                        int numTables=((Integer)ProjectDocument.NUMS.get(ProjectTableFactory.registerName)).intValue();
326
                        xml.putProperty("numTables", numTables);
327

    
328
                        if (getLinkTable() != null) {
329
                                xml.putProperty("linkTable", linkTable);
330
                                xml.putProperty("field1", field1);
331
                                xml.putProperty("field2", field2);
332
                        }
333

    
334
                        if (getOriginal() != null) {
335
                                xml.addChild(getOriginal().getRecordset().getXMLEntity());
336
                        }
337
                        xml.addChild(esModel.getRecordset().getXMLEntity());
338

    
339
//                        Object di = LayerFactory.getDataSourceFactory().getDriverInfo(
340
//                                        esModel.getRecordset().getName());
341

    
342
                        if (associatedTable != null) {
343
                                xml.putProperty("layerName", ((FLayer) associatedTable)
344
                                                .getName());
345
                                xml.putProperty("viewName", project
346
                                                .getView((FLayer) associatedTable));
347
                        }
348

    
349
                        xml.putProperty("mapping", mapping);
350
                        xml.putProperty("aliases", alias);
351
                } catch (Exception e) {
352
                        throw new SaveException(e, this.getClass().getName());
353
                }
354

    
355
                // for (int i=0;i<columns.size();i++){
356
                // Column column=(Column)columns.get(i);
357
                // xml.addChild(column.getXMLEntity());
358
                // }
359
                xml.addChild(columns.getXMLEntity());
360
                xml.putProperty("columns", true);
361
                return xml;
362
        }
363

    
364
        /**
365
         * DOCUMENT ME!
366
         *
367
         * @param xml
368
         *            DOCUMENT ME!
369
         * @param p
370
         *            DOCUMENT ME!
371
         *
372
         * @throws XMLException
373
         * @throws DriverException
374
         *
375
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
376
         */
377
        public void setXMLEntity03(XMLEntity xml)
378
                throws XMLException, DriverException, DriverIOException{
379
                super.setXMLEntity03(xml);
380
                int numTables = xml.getIntProperty("numTables");
381
                ProjectDocument.NUMS.put(ProjectTableFactory.registerName,new Integer(numTables));
382

    
383
                if (xml.getStringProperty("type").equals("otherDriverFile")) {
384
                        LayerFactory.getDataSourceFactory().addFileDataSource(
385
                                        xml.getStringProperty("driverName"),
386
                                        xml.getStringProperty("gdbmsname"),
387
                                        xml.getStringProperty("file"));
388

    
389
                        setSelectableDataSource03(xml);
390
                } else if (xml.getStringProperty("type").equals("sameDriverFile")) {
391
                        String layerName = xml.getStringProperty("layerName");
392

    
393
                        ProjectView vista =(ProjectView) project.getProjectDocumentByName(xml
394
                                        .getStringProperty("viewName"), ProjectViewFactory.registerName);
395
                        FLayer layer = vista.getMapContext().getLayers()
396
                                        .getLayer(layerName);
397

    
398
                        setTheModel((VectorialEditableAdapter) ((FLyrVect) layer).getSource());
399
                        associatedTable = (AlphanumericData) layer;
400

    
401
                        LayerFactory.getDataSourceFactory().addDataSource(
402
                                        (ObjectDriver) ((SingleLayer) layer).getSource()
403
                                                        .getDriver(), xml.getStringProperty("gdbmsname"));
404
                } else if (xml.getStringProperty("type").equals("db")) {
405
                        LayerFactory.getDataSourceFactory().addDBDataSourceByTable(
406
                                        xml.getStringProperty("gdbmsname"),
407
                                        xml.getStringProperty("host"), xml.getIntProperty("port"),
408
                                        xml.getStringProperty("user"),
409
                                        xml.getStringProperty("password"),
410
                                        xml.getStringProperty("dbName"),
411
                                        xml.getStringProperty("tableName"),
412
                                        xml.getStringProperty("driverInfo"));
413

    
414
                        setSelectableDataSource03(xml);
415
                }
416

    
417
                setName(xml.getStringProperty("name"));
418
        }
419

    
420
        /**
421
         * DOCUMENT ME!
422
         *
423
         * @param xml
424
         *            DOCUMENT ME!
425
         * @param p
426
         *            DOCUMENT ME!
427
         *
428
         * @throws XMLException
429
         * @throws DriverException
430
         * @throws OpenException
431
         *
432
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
433
         */
434
        public void setXMLEntity(XMLEntity xml) throws XMLException,
435
                        DriverException, OpenException {
436
                try {
437
                        super.setXMLEntity(xml);
438
                        setName(xml.getStringProperty("name"));
439
                        int numTables = xml.getIntProperty("numTables");
440
                        ProjectDocument.NUMS.put(ProjectTableFactory.registerName,new Integer(numTables));
441

    
442
                        setSelectableDataSource(xml);
443

    
444
                        /*
445
                         * if (xml.getStringProperty("type").equals("otherDriverFile")) { }
446
                         * else if (xml.getStringProperty("type").equals("sameDriverFile")) {
447
                         * String layerName = xml.getStringProperty("layerName");
448
                         * ProjectView vista = project.getViewByName(xml.getStringProperty(
449
                         * "viewName")); FLayer layer =
450
                         * vista.getMapContext().getLayers().getLayer(layerName);
451
                         *
452
                         * modelo = ((AlphanumericData) layer).getRecordset();
453
                         * associatedTable = (AlphanumericData) layer; } else if
454
                         * (xml.getStringProperty("type").equals("db")) {
455
                         * setSelectableDataSource(xml); }
456
                         */
457
                        setName(xml.getStringProperty("name"));
458

    
459
                        if (xml.contains("linkTable")) {
460
                                setLinkTable(xml.getStringProperty("linkTable"), xml
461
                                                .getStringProperty("field1"), xml
462
                                                .getStringProperty("field2"));
463
                        }
464

    
465
                        if (xml.contains("mapping")) {
466
                                mapping = xml.getIntArrayProperty("mapping");
467
                                alias = xml.getStringArrayProperty("aliases");
468
                                // we check if all fields are real there (may be some external program has changed them.
469
                                // If we detect any change, we discard all mapping and aliases.
470
                                if (mapping.length != getModelo().getRecordset().getFieldCount())
471
                                {
472
                                        createAlias();
473
//                                        columns.clear();
474
//                                        for (int i = 0; i < esModel.getRecordset().getFieldCount(); i++) {
475
//                                                addColumn(new Column());
476
//                                        }
477
                                        return;
478

    
479
                                }
480

    
481
                        } else {
482
                                try {
483
                                        createAlias();
484
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
485
                                        throw new XMLException(e);
486
                                }
487
                        }
488
                } catch (Exception e) {
489
                        throw new OpenException(e, this.getClass().getName());
490
                }
491

    
492
                // for (int i=1;i<xml.getNumChild();i++){
493
                // columns.add(Column.createColumn(xml.getChild(i)));
494
                // }
495
                if (xml.contains("columns")) {
496
                        columns.clear();
497
                        columns = Columns
498
                                        .createColumns(xml.getChild(xml.getChildrenCount() - 1));
499
                }
500

    
501

    
502
        }
503

    
504
        /**
505
         * DOCUMENT ME!
506
         *
507
         * @param xml
508
         *            DOCUMENT ME!
509
         *
510
         * @throws XMLException
511
         *             DOCUMENT ME!
512
         * @throws DriverException
513
         *             DOCUMENT ME!
514
         */
515
        private void setSelectableDataSource03(XMLEntity xml) throws XMLException {
516
                String layerName = null;
517

    
518
                if (xml.contains("layerName")) {
519
                        layerName = xml.getStringProperty("layerName");
520
                }
521

    
522
                if (layerName == null) {
523
                        DataSource dataSource;
524

    
525
                        try {
526
                                dataSource = LayerFactory.getDataSourceFactory()
527
                                                .createRandomDataSource(
528
                                                                xml.getStringProperty("gdbmsname"),
529
                                                                DataSourceFactory.AUTOMATIC_OPENING);
530

    
531
                                SelectableDataSource sds = new SelectableDataSource(dataSource);
532

    
533
                                sds.setXMLEntity03(xml.getChild(0));
534
                                EditableAdapter auxea = new EditableAdapter();
535
                                auxea.setOriginalDataSource(sds);
536
                                setDataSource(auxea);
537
                        } catch (NoSuchTableException e) {
538
                                throw new XMLException(e);
539
                        } catch (DriverLoadException e) {
540
                                throw new XMLException(e);
541
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
542
                                throw new XMLException(e);
543
                        }
544

    
545
                } else {
546
                        ProjectView vista = (ProjectView) project.getProjectDocumentByName(xml
547
                                        .getStringProperty("viewName"), ProjectViewFactory.registerName);
548
                        FLayer layer = vista.getMapContext().getLayers()
549
                                        .getLayer(layerName);
550

    
551
                        setTheModel((VectorialEditableAdapter) ((FLyrVect) layer).getSource());
552
                        associatedTable = (AlphanumericData) layer;
553
                }
554
        }
555

    
556
        /**
557
         * DOCUMENT ME!
558
         *
559
         * @param xml
560
         *            DOCUMENT ME!
561
         *
562
         * @throws XMLException
563
         *             DOCUMENT ME!
564
         * @throws DriverException
565
         *             DOCUMENT ME!
566
         */
567
        private void setSelectableDataSource(XMLEntity xml) throws DriverException {
568
                String layerName = null;
569
                boolean bNeedToReplace = false;
570
                XMLEntity xmlAux = null;
571

    
572
                try {
573
                        EditableAdapter es;
574

    
575
                        if (xml.contains("layerName")) {
576
                                layerName = xml.getStringProperty("layerName");
577

    
578
                                ProjectView vista = (ProjectView) project.getProjectDocumentByName(xml
579
                                                .getStringProperty("viewName"), ProjectViewFactory.registerName);
580
                                FLayer layer = getLayer(vista.getMapContext().getLayers(),
581
                                                layerName);
582
                                EditableAdapter ea = new EditableAdapter();
583
                                SelectableDataSource sds = ((FLyrVect) layer).getRecordset();
584
                                // sds.setSelectionSupport(((FLyrVect)
585
                                // layer).getSelectionSupport());
586
                                ea.setOriginalDataSource(sds);
587
                                associatedTable = (AlphanumericData) layer;
588

    
589
                                es = ea;
590
                        } else {
591
                                es = new EditableAdapter();
592
                                es.setOriginalDataSource(SelectableDataSource
593
                                                .createSelectableDataSource(xml.getChild(0)));
594
                        }
595

    
596
                        setDataSource(es);
597

    
598
                        if (xml.getChildrenCount() == 2 && !(xml.contains("columns"))
599
                                        || (xml.contains("columns") && (xml.getChildrenCount() == 3))) {
600
                                bNeedToReplace = true;
601
                                xmlAux = xml.getChild(1);
602
                                es = new EditableAdapter();
603
                                // es.setRecordSet(SelectableDataSource.createSelectableDataSource(xmlAux));
604

    
605
                                // replaceDataSource(SelectableDataSource.createSelectableDataSource(xml.getChild(1)));
606
                        }
607

    
608
                        if (bNeedToReplace) {
609
                                if (layerName != null) {
610
                                        ProjectView vista = (ProjectView) project.getProjectDocumentByName(xml
611
                                                        .getStringProperty("viewName"), ProjectViewFactory.registerName);
612
                                        FLayer layer = getLayer(vista.getMapContext().getLayers(),
613
                                                        layerName);
614

    
615
                                        // modelo = ((AlphanumericData) layer).getRecordset();
616
                                        associatedTable = (AlphanumericData) layer;
617
                                }
618

    
619
                                EditableAdapter auxea = new EditableAdapter();
620
                                auxea.setOriginalDataSource(SelectableDataSource
621
                                                .createSelectableDataSource(xmlAux));
622
                                replaceDataSource(auxea);
623
                        }
624
                } catch (DriverLoadException e) {
625
                        throw new DriverException(e);
626
                } catch (NoSuchTableException e) {
627
                        throw new DriverException(e);
628
                } catch (ParseException e) {
629
                        throw new DriverException(e);
630
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
631
                        throw new DriverException(e);
632
                } catch (SemanticException e) {
633
                        throw new DriverException(e);
634
                } catch (IOException e) {
635
                        throw new DriverException(e);
636
                } catch (XMLException e) {
637
                        throw new DriverException(e);
638
                }
639
        }
640

    
641
        private FLayer getLayer(FLayers layers, String name) {
642
                for (int i = 0; i < layers.getLayersCount(); i++) {
643
                        if (layers.getLayer(i) instanceof FLayers) {
644
                                return getLayer((FLayers) layers.getLayer(i), name);
645
                        } else if (layers.getLayer(i).getName().equals(name)) {
646
                                return layers.getLayer(i);
647
                        }
648
                }
649
                return null;
650
        }
651

    
652
        /**
653
         * DOCUMENT ME!
654
         *
655
         * @return DOCUMENT ME!
656
         */
657
        public AlphanumericData getAssociatedTable() {
658
                return associatedTable;
659
        }
660

    
661
        /**
662
         * DOCUMENT ME!
663
         *
664
         * @param associatedTable
665
         *            DOCUMENT ME!
666
         */
667
        public void setAssociatedTable(AlphanumericData associatedTable) {
668
                this.associatedTable = associatedTable;
669
        }
670

    
671
        /**
672
         * Obtiene la fuente de datos original de la tabla si se ha invocado
673
         * replaceDataSource. Si no se invoc? este m?todo o se invoc? posteriormente
674
         * restoreDataSource se devuelve null
675
         *
676
         * @return Returns the original.
677
         */
678
        public IEditableSource getOriginal() {
679
                return original;
680
        }
681

    
682
        /**
683
         * Devuelve el identificador de la tabla que contiene el link.
684
         *
685
         * @return identificador ?nico de la tabla.
686
         */
687
        public String getLinkTable() {
688
                return linkTable;
689
        }
690

    
691
        /**
692
         * Devuelve el nombre del campo de la tabla a enlazar.
693
         *
694
         * @return Nombre del campo de la tabla a enlazar.
695
         */
696
        public String getField1() {
697
                return field1;
698
        }
699

    
700
        /**
701
         * Devuelve el nombre del campo de la tabla enlazada.
702
         *
703
         * @return Nombre del campo de la tabla enlazada.
704
         */
705
        public String getField2() {
706
                return field2;
707
        }
708

    
709
        /**
710
         * Inserta el identificador de la tabla, el campo de la primera tabla y el
711
         * campo de la segunda tabla.
712
         *
713
         * @param lt
714
         *            identificado de la tabla.
715
         * @param f1
716
         *            nombre del campo de la primera tabla.
717
         * @param f2
718
         *            nombre del campo de la segunda tabla.
719
         */
720
        public void setLinkTable(String lt, String f1, String f2) {
721
                linkTable = lt;
722
                field1 = f1;
723
                field2 = f2;
724
        }
725

    
726
        /**
727
         * Borra el identificador de la tabla y elimina del array de listener los
728
         * listener que sean del tipo: LinkSelectionListenr
729
         */
730
        public void removeLinkTable() {
731
                linkTable = null;
732
                try {
733
                        getModelo().getRecordset()
734
                                        .removeLinksSelectionListener();
735
                } catch (DriverLoadException e) {
736
                        e.printStackTrace();
737
                }
738
        }
739

    
740
        public String[] getAliases() {
741
                return alias;
742
        }
743

    
744
        public void setAliases(String[] alias) {
745
                this.alias = alias;
746
        }
747

    
748
        public int[] getMapping() {
749
                if (mapping == null) {
750
                        mapping = new int[alias.length];
751
                        for (int i = 0; i < mapping.length; i++) {
752
                                mapping[i] = i;
753
                        }
754
                }
755
                return mapping;
756
        }
757

    
758
        public void setMapping(int[] mapping) {
759
                this.mapping = mapping;
760
        }
761

    
762
        public void setModel(IEditableSource ies) {
763
                setTheModel(ies);
764
                try {
765
                        createAlias();
766
                } catch (DriverLoadException e) {
767
                        e.printStackTrace();
768
                        NotificationManager.addError(e);
769
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
770
                        e.printStackTrace();
771
                        NotificationManager.addError(e);
772
                }
773
        }
774

    
775
        public Column getColumn(int i) {
776
//                if (i==0){
777
//                        Column column=new Column();
778
//                        column.setWidth(45);
779
//                        return column;
780
//                }
781
//                i--;
782
                        return (Column) columns.get(getMapping()[i]);
783
        }
784

    
785
        public void addColumn(Column column) {
786
                columns.add(column);
787
        }
788

    
789
        public int getColumnCount() {
790
                return columns.size();
791
        }
792

    
793
        public IWindow createWindow() {
794
                com.iver.cit.gvsig.project.documents.table.gui.Table table = new com.iver.cit.gvsig.project.documents.table.gui.Table();
795
                table.setModel(this);
796
                return table;
797
        }
798
        public IWindow getProperties() {
799
                return new TableProperties(this);
800
        }
801

    
802
        public void afterRemove() {
803
                // TODO Auto-generated method stub
804

    
805
        }
806

    
807
        public void afterAdd() {
808
                // TODO Auto-generated method stub
809

    
810
        }
811

    
812
        private void setTheModel(IEditableSource es) {
813
                this.esModel = es;
814

    
815
        }
816

    
817
        public void exportToXML(XMLEntity root, Project project)  throws SaveException {
818
                XMLEntity tableRoot = project.getExportXMLTypeRootNode(root,ProjectTableFactory.registerName);
819
                project.exportToXMLDataSource(root,this.getModelo().getRecordset().getName());
820
                tableRoot.addChild(this.getXMLEntity());
821
        }
822

    
823
        public void importFromXML(XMLEntity root, XMLEntity typeRoot,int elementIndex ,Project project, boolean removeDocumentsFromRoot) throws XMLException, DriverException, OpenException {
824
                XMLEntity element = typeRoot.getChild(elementIndex);
825
                this.setXMLEntity(element);
826
                if (removeDocumentsFromRoot) {
827
                        typeRoot.removeChild(elementIndex);
828
                }
829
                project.addDocument(this);
830

    
831
        }
832

    
833
        public boolean isModified() {
834
                return isModified;
835
        }
836

    
837
        public void setModified(boolean modified) {
838
                isModified=modified;
839
        }
840

    
841

    
842
//        public int computeSignature() {
843
//                int result = 17;
844
//
845
//                Class clazz = getClass();
846
//                Field[] fields = clazz.getDeclaredFields();
847
//                for (int i = 0; i < fields.length; i++) {
848
//                        try {
849
//                                String type = fields[i].getType().getName();
850
//                                if (type.equals("boolean")) {
851
//                                        result += 37 + ((fields[i].getBoolean(this)) ? 1 : 0);
852
//                                } else if (type.equals("java.lang.String")) {
853
//                                        Object v = fields[i].get(this);
854
//                                        if (v == null) {
855
//                                                result += 37;
856
//                                                continue;
857
//                                        }
858
//                                        char[] chars = ((String) v).toCharArray();
859
//                                        for (int j = 0; j < chars.length; j++) {
860
//                                                result += 37 + (int) chars[i];
861
//                                        }
862
//                                } else if (type.equals("byte")) {
863
//                                        result += 37 + (int) fields[i].getByte(this);
864
//                                } else if (type.equals("char")) {
865
//                                        result += 37 + (int) fields[i].getChar(this);
866
//                                } else if (type.equals("short")) {
867
//                                        result += 37 + (int) fields[i].getShort(this);
868
//                                } else if (type.equals("int")) {
869
//                                        result += 37 + fields[i].getInt(this);
870
//                                } else if (type.equals("long")) {
871
//                                        long f = fields[i].getLong(this) ;
872
//                                        result += 37 + (f ^ (f >>> 32));
873
//                                } else if (type.equals("float")) {
874
//                                        result += 37 + Float.floatToIntBits(fields[i].getFloat(this));
875
//                                } else if (type.equals("double")) {
876
//                                        long f = Double.doubleToLongBits(fields[i].getDouble(this));
877
//                                        result += 37 + (f ^ (f >>> 32));
878
//                                } else {
879
//                                        Object obj = fields[i].get(this);
880
//                                        result += 37 + ((obj != null)? obj.hashCode() : 0);
881
//                                }
882
//                        } catch (Exception e) { e.printStackTrace(); }
883
//
884
//                }
885
//                return result;
886
//        }
887
}