Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / ProjectTable.java @ 9392

History | View | Annotate | Download (24.5 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.project.Project;
75
import com.iver.cit.gvsig.project.documents.ProjectDocument;
76
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
77
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
78
import com.iver.cit.gvsig.project.documents.table.gui.Column;
79
import com.iver.cit.gvsig.project.documents.table.gui.Columns;
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
        /**
113
         * Establece a true el bit index-?simo del bitmap de campos visibles. Los
114
         * campos cuyos bits est?n a true se considerar?n visibles y viceversa
115
         *
116
         * @param index
117
         *            indice del bit que se quiere establecer a true
118
         */
119
        public void set(int index) {
120
                // TODO implementar bien
121
                // modelo.set(index);
122

    
123
                change.firePropertyChange("visibles", true, true);
124
        }
125

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

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

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

    
165

    
166

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

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

    
191
        }
192

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

    
200
        }
201

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

    
212
                setName(esModel.getRecordset().getName());
213
                setCreationDate(DateFormat.getInstance().format(new Date()));
214
                change.firePropertyChange("model", esModel, esModel);
215
        }
216

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

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

    
255
                change.firePropertyChange("model", original, esModel);
256
        }
257

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

    
293
                                                return;
294
                                        }
295
                                }
296

    
297
                                lyr.setRecordset(original.getRecordset());
298

    
299
                                lyr.setIsJoined(false);
300
                        }
301
                }
302

    
303
                setModel(original);
304
                original = null;
305
                createAlias();
306

    
307
                change.firePropertyChange("model", original, esModel);
308
        }
309

    
310
        /**
311
         * DOCUMENT ME!
312
         *
313
         * @return DOCUMENT ME!
314
         * @throws SaveException
315
         *
316
         * @throws XMLException
317
         */
318
        public XMLEntity getXMLEntity() throws SaveException {
319
                XMLEntity xml = super.getXMLEntity();
320
                try {
321
                        // xml.putProperty("nameClass", this.getClass().getName());
322
                        xml.putProperty("numTables", numTables);
323

    
324
                        if (getLinkTable() != null) {
325
                                xml.putProperty("linkTable", linkTable);
326
                                xml.putProperty("field1", field1);
327
                                xml.putProperty("field2", field2);
328
                        }
329

    
330
                        if (getOriginal() != null) {
331
                                xml.addChild(getOriginal().getRecordset().getXMLEntity());
332
                        }
333
                        xml.addChild(esModel.getRecordset().getXMLEntity());
334

    
335
//                        Object di = LayerFactory.getDataSourceFactory().getDriverInfo(
336
//                                        esModel.getRecordset().getName());
337

    
338
                        if (associatedTable != null) {
339
                                xml.putProperty("layerName", ((FLayer) associatedTable)
340
                                                .getName());
341
                                xml.putProperty("viewName", project
342
                                                .getView((FLayer) associatedTable));
343
                        }
344

    
345
                        xml.putProperty("mapping", mapping);
346
                        xml.putProperty("aliases", alias);
347
                } catch (Exception e) {
348
                        throw new SaveException(e, this.getClass().getName());
349
                }
350

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

    
360
        /**
361
         * DOCUMENT ME!
362
         *
363
         * @param xml
364
         *            DOCUMENT ME!
365
         * @param p
366
         *            DOCUMENT ME!
367
         *
368
         * @throws XMLException
369
         * @throws DriverException
370
         *
371
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
372
         */
373
        public void setXMLEntity03(XMLEntity xml)
374
                throws XMLException, DriverException, DriverIOException{
375
                super.setXMLEntity03(xml);
376
                numTables = xml.getIntProperty("numTables");
377

    
378
                if (xml.getStringProperty("type").equals("otherDriverFile")) {
379
                        LayerFactory.getDataSourceFactory().addFileDataSource(
380
                                        xml.getStringProperty("driverName"),
381
                                        xml.getStringProperty("gdbmsname"),
382
                                        xml.getStringProperty("file"));
383

    
384
                        setSelectableDataSource03(xml);
385
                } else if (xml.getStringProperty("type").equals("sameDriverFile")) {
386
                        String layerName = xml.getStringProperty("layerName");
387

    
388
                        ProjectView vista =(ProjectView) project.getProjectDocumentByName(xml
389
                                        .getStringProperty("viewName"), ProjectViewFactory.registerName);
390
                        FLayer layer = vista.getMapContext().getLayers()
391
                                        .getLayer(layerName);
392

    
393
                        setTheModel((VectorialEditableAdapter) ((FLyrVect) layer).getSource());
394
                        associatedTable = (AlphanumericData) layer;
395

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

    
409
                        setSelectableDataSource03(xml);
410
                }
411

    
412
                setName(xml.getStringProperty("name"));
413
        }
414

    
415
        /**
416
         * DOCUMENT ME!
417
         *
418
         * @param xml
419
         *            DOCUMENT ME!
420
         * @param p
421
         *            DOCUMENT ME!
422
         *
423
         * @throws XMLException
424
         * @throws DriverException
425
         * @throws OpenException
426
         *
427
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
428
         */
429
        public void setXMLEntity(XMLEntity xml) throws XMLException,
430
                        DriverException, OpenException {
431
                try {
432
                        super.setXMLEntity(xml);
433
                        setName(xml.getStringProperty("name"));
434
                        numTables = xml.getIntProperty("numTables");
435

    
436
                        setSelectableDataSource(xml);
437

    
438
                        /*
439
                         * if (xml.getStringProperty("type").equals("otherDriverFile")) { }
440
                         * else if (xml.getStringProperty("type").equals("sameDriverFile")) {
441
                         * String layerName = xml.getStringProperty("layerName");
442
                         * ProjectView vista = project.getViewByName(xml.getStringProperty(
443
                         * "viewName")); FLayer layer =
444
                         * vista.getMapContext().getLayers().getLayer(layerName);
445
                         *
446
                         * modelo = ((AlphanumericData) layer).getRecordset();
447
                         * associatedTable = (AlphanumericData) layer; } else if
448
                         * (xml.getStringProperty("type").equals("db")) {
449
                         * setSelectableDataSource(xml); }
450
                         */
451
                        setName(xml.getStringProperty("name"));
452

    
453
                        if (xml.contains("linkTable")) {
454
                                setLinkTable(xml.getStringProperty("linkTable"), xml
455
                                                .getStringProperty("field1"), xml
456
                                                .getStringProperty("field2"));
457
                        }
458

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

    
473
                                }
474

    
475
                        } else {
476
                                try {
477
                                        createAlias();
478
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
479
                                        throw new XMLException(e);
480
                                }
481
                        }
482
                } catch (Exception e) {
483
                        throw new OpenException(e, this.getClass().getName());
484
                }
485

    
486
                // for (int i=1;i<xml.getNumChild();i++){
487
                // columns.add(Column.createColumn(xml.getChild(i)));
488
                // }
489
                if (xml.contains("columns")) {
490
                        columns.clear();
491
                        columns = Columns
492
                                        .createColumns(xml.getChild(xml.getChildrenCount() - 1));
493
                }
494

    
495

    
496
        }
497

    
498
        /**
499
         * DOCUMENT ME!
500
         *
501
         * @param xml
502
         *            DOCUMENT ME!
503
         *
504
         * @throws XMLException
505
         *             DOCUMENT ME!
506
         * @throws DriverException
507
         *             DOCUMENT ME!
508
         */
509
        private void setSelectableDataSource03(XMLEntity xml) throws XMLException {
510
                String layerName = null;
511

    
512
                if (xml.contains("layerName")) {
513
                        layerName = xml.getStringProperty("layerName");
514
                }
515

    
516
                if (layerName == null) {
517
                        DataSource dataSource;
518

    
519
                        try {
520
                                dataSource = LayerFactory.getDataSourceFactory()
521
                                                .createRandomDataSource(
522
                                                                xml.getStringProperty("gdbmsname"),
523
                                                                DataSourceFactory.AUTOMATIC_OPENING);
524

    
525
                                SelectableDataSource sds = new SelectableDataSource(dataSource);
526

    
527
                                sds.setXMLEntity03(xml.getChild(0));
528
                                EditableAdapter auxea = new EditableAdapter();
529
                                auxea.setOriginalDataSource(sds);
530
                                setDataSource(auxea);
531
                        } catch (NoSuchTableException e) {
532
                                throw new XMLException(e);
533
                        } catch (DriverLoadException e) {
534
                                throw new XMLException(e);
535
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
536
                                throw new XMLException(e);
537
                        }
538

    
539
                } else {
540
                        ProjectView vista = (ProjectView) project.getProjectDocumentByName(xml
541
                                        .getStringProperty("viewName"), ProjectViewFactory.registerName);
542
                        FLayer layer = vista.getMapContext().getLayers()
543
                                        .getLayer(layerName);
544

    
545
                        setTheModel((VectorialEditableAdapter) ((FLyrVect) layer).getSource());
546
                        associatedTable = (AlphanumericData) layer;
547
                }
548
        }
549

    
550
        /**
551
         * DOCUMENT ME!
552
         *
553
         * @param xml
554
         *            DOCUMENT ME!
555
         *
556
         * @throws XMLException
557
         *             DOCUMENT ME!
558
         * @throws DriverException
559
         *             DOCUMENT ME!
560
         */
561
        private void setSelectableDataSource(XMLEntity xml) throws DriverException {
562
                String layerName = null;
563
                boolean bNeedToReplace = false;
564
                XMLEntity xmlAux = null;
565

    
566
                try {
567
                        EditableAdapter es;
568

    
569
                        if (xml.contains("layerName")) {
570
                                layerName = xml.getStringProperty("layerName");
571

    
572
                                ProjectView vista = (ProjectView) project.getProjectDocumentByName(xml
573
                                                .getStringProperty("viewName"), ProjectViewFactory.registerName);
574
                                FLayer layer = getLayer(vista.getMapContext().getLayers(),
575
                                                layerName);
576
                                EditableAdapter ea = new EditableAdapter();
577
                                SelectableDataSource sds = ((FLyrVect) layer).getRecordset();
578
                                // sds.setSelectionSupport(((FLyrVect)
579
                                // layer).getSelectionSupport());
580
                                ea.setOriginalDataSource(sds);
581
                                associatedTable = (AlphanumericData) layer;
582

    
583
                                es = ea;
584
                        } else {
585
                                es = new EditableAdapter();
586
                                es.setOriginalDataSource(SelectableDataSource
587
                                                .createSelectableDataSource(xml.getChild(0)));
588
                        }
589

    
590
                        setDataSource(es);
591

    
592
                        if (xml.getChildrenCount() == 2 && !(xml.contains("columns"))
593
                                        || (xml.contains("columns") && (xml.getChildrenCount() == 3))) {
594
                                bNeedToReplace = true;
595
                                xmlAux = xml.getChild(1);
596
                                es = new EditableAdapter();
597
                                // es.setRecordSet(SelectableDataSource.createSelectableDataSource(xmlAux));
598

    
599
                                // replaceDataSource(SelectableDataSource.createSelectableDataSource(xml.getChild(1)));
600
                        }
601

    
602
                        if (bNeedToReplace) {
603
                                if (layerName != null) {
604
                                        ProjectView vista = (ProjectView) project.getProjectDocumentByName(xml
605
                                                        .getStringProperty("viewName"), ProjectViewFactory.registerName);
606
                                        FLayer layer = getLayer(vista.getMapContext().getLayers(),
607
                                                        layerName);
608

    
609
                                        // modelo = ((AlphanumericData) layer).getRecordset();
610
                                        associatedTable = (AlphanumericData) layer;
611
                                }
612

    
613
                                EditableAdapter auxea = new EditableAdapter();
614
                                auxea.setOriginalDataSource(SelectableDataSource
615
                                                .createSelectableDataSource(xmlAux));
616
                                replaceDataSource(auxea);
617
                        }
618
                } catch (DriverLoadException e) {
619
                        throw new DriverException(e);
620
                } catch (NoSuchTableException e) {
621
                        throw new DriverException(e);
622
                } catch (ParseException e) {
623
                        throw new DriverException(e);
624
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
625
                        throw new DriverException(e);
626
                } catch (SemanticException e) {
627
                        throw new DriverException(e);
628
                } catch (IOException e) {
629
                        throw new DriverException(e);
630
                } catch (XMLException e) {
631
                        throw new DriverException(e);
632
                }
633
        }
634

    
635
        private FLayer getLayer(FLayers layers, String name) {
636
                for (int i = 0; i < layers.getLayersCount(); i++) {
637
                        if (layers.getLayer(i) instanceof FLayers) {
638
                                return getLayer((FLayers) layers.getLayer(i), name);
639
                        } else if (layers.getLayer(i).getName().equals(name)) {
640
                                return layers.getLayer(i);
641
                        }
642
                }
643
                return null;
644
        }
645

    
646
        /**
647
         * DOCUMENT ME!
648
         *
649
         * @return DOCUMENT ME!
650
         */
651
        public AlphanumericData getAssociatedTable() {
652
                return associatedTable;
653
        }
654

    
655
        /**
656
         * DOCUMENT ME!
657
         *
658
         * @param associatedTable
659
         *            DOCUMENT ME!
660
         */
661
        public void setAssociatedTable(AlphanumericData associatedTable) {
662
                this.associatedTable = associatedTable;
663
        }
664

    
665
        /**
666
         * Obtiene la fuente de datos original de la tabla si se ha invocado
667
         * replaceDataSource. Si no se invoc? este m?todo o se invoc? posteriormente
668
         * restoreDataSource se devuelve null
669
         *
670
         * @return Returns the original.
671
         */
672
        public IEditableSource getOriginal() {
673
                return original;
674
        }
675

    
676
        /**
677
         * Devuelve el identificador de la tabla que contiene el link.
678
         *
679
         * @return identificador ?nico de la tabla.
680
         */
681
        public String getLinkTable() {
682
                return linkTable;
683
        }
684

    
685
        /**
686
         * Devuelve el nombre del campo de la tabla a enlazar.
687
         *
688
         * @return Nombre del campo de la tabla a enlazar.
689
         */
690
        public String getField1() {
691
                return field1;
692
        }
693

    
694
        /**
695
         * Devuelve el nombre del campo de la tabla enlazada.
696
         *
697
         * @return Nombre del campo de la tabla enlazada.
698
         */
699
        public String getField2() {
700
                return field2;
701
        }
702

    
703
        /**
704
         * Inserta el identificador de la tabla, el campo de la primera tabla y el
705
         * campo de la segunda tabla.
706
         *
707
         * @param lt
708
         *            identificado de la tabla.
709
         * @param f1
710
         *            nombre del campo de la primera tabla.
711
         * @param f2
712
         *            nombre del campo de la segunda tabla.
713
         */
714
        public void setLinkTable(String lt, String f1, String f2) {
715
                linkTable = lt;
716
                field1 = f1;
717
                field2 = f2;
718
        }
719

    
720
        /**
721
         * Borra el identificador de la tabla y elimina del array de listener los
722
         * listener que sean del tipo: LinkSelectionListenr
723
         */
724
        public void removeLinkTable() {
725
                linkTable = null;
726
                try {
727
                        getModelo().getRecordset()
728
                                        .removeLinksSelectionListener();
729
                } catch (DriverLoadException e) {
730
                        e.printStackTrace();
731
                }
732
        }
733

    
734
        public String[] getAliases() {
735
                return alias;
736
        }
737

    
738
        public void setAliases(String[] alias) {
739
                this.alias = alias;
740
        }
741

    
742
        public int[] getMapping() {
743
                if (mapping == null) {
744
                        mapping = new int[alias.length];
745
                        for (int i = 0; i < mapping.length; i++) {
746
                                mapping[i] = i;
747
                        }
748
                }
749
                return mapping;
750
        }
751

    
752
        public void setMapping(int[] mapping) {
753
                this.mapping = mapping;
754
        }
755

    
756
        public void setModel(IEditableSource ies) {
757
                setTheModel(ies);
758
                try {
759
                        createAlias();
760
                } catch (DriverLoadException e) {
761
                        e.printStackTrace();
762
                        NotificationManager.addError(e);
763
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
764
                        e.printStackTrace();
765
                        NotificationManager.addError(e);
766
                }
767
        }
768

    
769
        public Column getColumn(int i) {
770
//                if (i==0){
771
//                        Column column=new Column();
772
//                        column.setWidth(45);
773
//                        return column;
774
//                }
775
//                i--;
776
                        return (Column) columns.get(getMapping()[i]);
777
        }
778

    
779
        public void addColumn(Column column) {
780
                columns.add(column);
781
        }
782

    
783
        public int getColumnCount() {
784
                return columns.size();
785
        }
786

    
787
        public IWindow createWindow() {
788
                com.iver.cit.gvsig.project.documents.table.gui.Table table = new com.iver.cit.gvsig.project.documents.table.gui.Table();
789
                table.setModel(this);
790
                return table;
791
        }
792
        public IWindow getProperties() {
793
                return new TableProperties(this);
794
        }
795

    
796
        public void afterRemove() {
797
                // TODO Auto-generated method stub
798

    
799
        }
800

    
801
        public void afterAdd() {
802
                // TODO Auto-generated method stub
803

    
804
        }
805

    
806
        private void setTheModel(IEditableSource es) {
807
                this.esModel = es;
808

    
809
        }
810

    
811
        public void exportToXML(XMLEntity root, Project project)  throws SaveException {
812
                XMLEntity tableRoot = project.getExportXMLTypeRootNode(root,ProjectTableFactory.registerName);
813
                project.exportToXMLDataSource(root,this.getModelo().getRecordset().getName());
814
                tableRoot.addChild(this.getXMLEntity());
815
        }
816

    
817
        public void importFromXML(XMLEntity root, XMLEntity typeRoot,int elementIndex ,Project project, boolean removeDocumentsFromRoot) throws XMLException, DriverException, OpenException {
818
                XMLEntity element = typeRoot.getChild(elementIndex);
819
                this.setXMLEntity(element);
820
                if (removeDocumentsFromRoot) {
821
                        typeRoot.removeChild(elementIndex);
822
                }
823
                project.addDocument(this);
824

    
825
        }
826

    
827

    
828

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