Statistics
| Revision:

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

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.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
                        int numTables=((Integer)ProjectDocument.NUMS.get(ProjectTableFactory.registerName)).intValue();
325

    
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

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