Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / ProjectTable.java @ 9532

History | View | Annotate | Download (24.6 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
        private boolean isModified=false;
113

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

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

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

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

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

    
167

    
168

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

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

    
193
        }
194

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

    
202
        }
203

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

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

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

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

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

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

    
295
                                                return;
296
                                        }
297
                                }
298

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

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

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

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

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

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

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

    
337
//                        Object di = LayerFactory.getDataSourceFactory().getDriverInfo(
338
//                                        esModel.getRecordset().getName());
339

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

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

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

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

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

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

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

    
395
                        setTheModel((VectorialEditableAdapter) ((FLyrVect) layer).getSource());
396
                        associatedTable = (AlphanumericData) layer;
397

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

    
411
                        setSelectableDataSource03(xml);
412
                }
413

    
414
                setName(xml.getStringProperty("name"));
415
        }
416

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

    
438
                        setSelectableDataSource(xml);
439

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

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

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

    
475
                                }
476

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

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

    
497

    
498
        }
499

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

    
514
                if (xml.contains("layerName")) {
515
                        layerName = xml.getStringProperty("layerName");
516
                }
517

    
518
                if (layerName == null) {
519
                        DataSource dataSource;
520

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

    
527
                                SelectableDataSource sds = new SelectableDataSource(dataSource);
528

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

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

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

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

    
568
                try {
569
                        EditableAdapter es;
570

    
571
                        if (xml.contains("layerName")) {
572
                                layerName = xml.getStringProperty("layerName");
573

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

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

    
592
                        setDataSource(es);
593

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

    
601
                                // replaceDataSource(SelectableDataSource.createSelectableDataSource(xml.getChild(1)));
602
                        }
603

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

    
611
                                        // modelo = ((AlphanumericData) layer).getRecordset();
612
                                        associatedTable = (AlphanumericData) layer;
613
                                }
614

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

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

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

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

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

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

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

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

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

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

    
736
        public String[] getAliases() {
737
                return alias;
738
        }
739

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

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

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

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

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

    
781
        public void addColumn(Column column) {
782
                columns.add(column);
783
        }
784

    
785
        public int getColumnCount() {
786
                return columns.size();
787
        }
788

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

    
798
        public void afterRemove() {
799
                // TODO Auto-generated method stub
800

    
801
        }
802

    
803
        public void afterAdd() {
804
                // TODO Auto-generated method stub
805

    
806
        }
807

    
808
        private void setTheModel(IEditableSource es) {
809
                this.esModel = es;
810

    
811
        }
812

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

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

    
827
        }
828

    
829
        public boolean isModified() {
830
                return isModified;
831
        }
832

    
833
        public void setModified(boolean modified) {
834
                isModified=modified;
835
        }
836

    
837

    
838

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