Statistics
| Revision:

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

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.text.DateFormat;
44
import java.util.ArrayList;
45
import java.util.Date;
46

    
47
import javax.swing.JOptionPane;
48

    
49
import com.hardcode.driverManager.DriverLoadException;
50
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
51
import com.hardcode.gdbms.engine.data.DataSource;
52
import com.hardcode.gdbms.engine.data.DataSourceFactory;
53
import com.hardcode.gdbms.engine.data.NoSuchTableException;
54
import com.hardcode.gdbms.engine.data.driver.DriverException;
55
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
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.edition.EditableAdapter;
60
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
61
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
62
import com.iver.cit.gvsig.fmap.layers.FLayer;
63
import com.iver.cit.gvsig.fmap.layers.FLayers;
64
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
65
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
66
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
67
import com.iver.cit.gvsig.fmap.layers.XMLException;
68
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
69
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
70
import com.iver.cit.gvsig.fmap.rendering.IClassifiedVectorialLegend;
71
import com.iver.cit.gvsig.fmap.rendering.IVectorialLegend;
72
import com.iver.cit.gvsig.project.Project;
73
import com.iver.cit.gvsig.project.documents.ProjectDocument;
74
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
75
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
76
import com.iver.cit.gvsig.project.documents.table.gui.TableProperties;
77
import com.iver.cit.gvsig.project.documents.table.gui.tablemodel.Column;
78
import com.iver.cit.gvsig.project.documents.table.gui.tablemodel.Columns;
79
import com.iver.cit.gvsig.project.documents.view.ProjectView;
80
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
81
import com.iver.utiles.XMLEntity;
82

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

    
91
        private IEditableSource esModel;
92

    
93
        private IEditableSource original;
94

    
95
        private String linkTable;
96

    
97
        private String field1;
98

    
99
        private String field2;
100

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

    
104
        private int[] mapping;
105

    
106
        private String[] alias;
107

    
108
        private Columns columns = new Columns();
109
        private long[] orderIndexes = null;
110

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

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

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

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

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

    
164

    
165

    
166
        public void createAlias() throws ReadDriverException {
167
                SelectableDataSource sds = esModel.getRecordset();
168
//                mapping = new int[sds.getFieldCount()+1];
169
//                alias = new String[sds.getFieldCount()+1];
170
//                for (int i = 0; i < sds.getFieldCount()+1; i++) {
171
                mapping = new int[sds.getFieldCount()];
172
                alias = new String[sds.getFieldCount()];
173
                for (int i = 0; i < sds.getFieldCount(); i++) {
174
                        mapping[i]=i;
175
                        alias[i]=sds.getFieldName(i);
176

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

    
188
        }
189

    
190
        public void recalculateColumnsFromAliases()
191
        {
192
                ArrayList columnsAux=new ArrayList();
193
                columnsAux.addAll(columns);
194
                columns.clear();
195
                for (int i = 0; i < alias.length; i++) {
196
                        if (columnsAux.size()>i) {
197
                                columns.add(columnsAux.get(i));
198
                        }else {
199
                                addColumn(new Column());
200
                        }
201
                }
202

    
203
        }
204

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

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

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

    
237
                        createAlias();
238
                // FJP:
239
                // Si la tabla proviene de un layer, cambiamos su recordset
240
                if (associatedTable != null) {
241
                        if (associatedTable instanceof FLyrVect) {
242
                                // ((EditableAdapter)((FLyrVect)
243
                                // associatedTable).getSource()).setRecordSet((SelectableDataSource)es.getRecordset());
244
                                FLyrVect lyrVect = (FLyrVect) associatedTable;
245
                                lyrVect.setRecordset(es.getRecordset());
246
                                ((FLyrVect) associatedTable).setIsJoined(true);
247
                        }
248
                }
249

    
250
                change.firePropertyChange("model", original, esModel);
251
        }
252

    
253
        /**
254
         * DOCUMENT ME!
255
         *
256
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException
257
         * @throws DriverLoadException
258
         */
259
        public void restoreDataSource()
260
                        throws ReadDriverException,
261
                        DriverLoadException {
262
                // FJP:
263
                // Si la tabla proviene de un layer, cambiamos su recordset
264
                if (associatedTable != null) {
265
                        if (associatedTable instanceof FLyrVect) {
266
                                // Miramos si la leyenda que est? usando es una
267
                                // leyenda basada en un campo de los de la uni?n.
268
                                // Si lo es, no dejamos pegarle el cambiazo al recordset
269
                                FLyrVect lyr = ((FLyrVect) associatedTable);
270
                                if (lyr.getLegend() instanceof IClassifiedVectorialLegend) {
271
                                        IClassifiedVectorialLegend legend = (IClassifiedVectorialLegend) lyr
272
                                                        .getLegend();
273
                                        IVectorialLegend aux = (IVectorialLegend) legend;
274
                                        int idField = original.getRecordset().getFieldIndexByName(
275
                                                        legend.getFieldNames()[0]);
276
                                        int idLabelField = -2;
277

    
278
                                        // TODO
279
                                        NotificationManager.addWarning("Piece of code remains unrefactored in methdo com.iver.cit.gvsig.project.documents.table.ProjectTable.restoreDataSource()", null);
280
                                        // TODO
281
//                                        if (aux.getLabelField() != null) {
282
//                                                idLabelField = original.getRecordset()
283
//                                                                .getFieldIndexByName(aux.getLabelField());
284
//                                        }
285
//                                        if ((idField == -1) || (idLabelField == -1)) {
286
//                                                // No se ha encontrado ese campo, o se est? etiquetando
287
//                                                // por ese campo
288
//                                                JOptionPane.showMessageDialog(null, PluginServices
289
//                                                                .getText(this, "leyenda_campo_unido"));
290
//
291
//                                                return;
292
//                                        }
293
                                }
294

    
295
                                lyr.setRecordset(original.getRecordset());
296

    
297
                                lyr.setIsJoined(false);
298
                        }
299
                }
300

    
301
                setModel(original);
302
                original = null;
303
                createAlias();
304

    
305
                change.firePropertyChange("model", original, esModel);
306
        }
307

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

    
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 ReadDriverException
370
         * @throws DriverException
371
         *
372
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
373
         */
374
        public void setXMLEntity03(XMLEntity xml)
375
                throws XMLException, ReadDriverException{
376
                super.setXMLEntity03(xml);
377
                int numTables = xml.getIntProperty("numTables");
378
                ProjectDocument.NUMS.put(ProjectTableFactory.registerName,new Integer(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
                        ReadDriverException, OpenException {
433
                try {
434
                        super.setXMLEntity(xml);
435
                        setName(xml.getStringProperty("name"));
436
                        int numTables = xml.getIntProperty("numTables");
437
                        ProjectDocument.NUMS.put(ProjectTableFactory.registerName,new Integer(numTables));
438

    
439
                        setSelectableDataSource(xml);
440

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

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

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

    
476
                                }
477

    
478
                        } else {
479
                                try {
480
                                        createAlias();
481
                                } catch (ReadDriverException e) {
482
                                        throw new XMLException(e);
483
                                }
484
                        }
485
                } catch (Exception e) {
486
                        throw new OpenException(e, this.getClass().getName());
487
                }
488

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

    
498

    
499
        }
500

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

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

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

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

    
528
                                SelectableDataSource sds = new SelectableDataSource(dataSource);
529

    
530
                                sds.setXMLEntity03(xml.getChild(0));
531
                                EditableAdapter auxea = new EditableAdapter();
532
                                auxea.setOriginalDataSource(sds);
533
                                setDataSource(auxea);
534
                        } catch (NoSuchTableException e) {
535
                                throw new XMLException(e);
536
                        } catch (DriverLoadException e) {
537
                                throw new XMLException(e);
538
                        } catch (ReadDriverException e) {
539
                                throw new XMLException(e);
540
                        }
541

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

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

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

    
569
                try {
570
                        EditableAdapter es;
571

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

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

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

    
593
                        setDataSource(es);
594

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

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

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

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

    
616
                                EditableAdapter auxea = new EditableAdapter();
617
                                auxea.setOriginalDataSource(SelectableDataSource
618
                                                .createSelectableDataSource(xmlAux));
619
                                replaceDataSource(auxea);
620
                        }
621
                } catch (DriverLoadException e) {
622
                        throw new ReadDriverException(getName(),e);
623
                } catch (XMLException e) {
624
                        throw new ReadDriverException(getName(),e);
625
                }
626
        }
627

    
628
        private FLayer getLayer(FLayers layers, String name) {
629
                for (int i = 0; i < layers.getLayersCount(); i++) {
630
                        if (layers.getLayer(i) instanceof FLayers) {
631
                                return getLayer((FLayers) layers.getLayer(i), name);
632
                        } else if (layers.getLayer(i).getName().equals(name)) {
633
                                return layers.getLayer(i);
634
                        }
635
                }
636
                return null;
637
        }
638

    
639
        /**
640
         * DOCUMENT ME!
641
         *
642
         * @return DOCUMENT ME!
643
         */
644
        public AlphanumericData getAssociatedTable() {
645
                return associatedTable;
646
        }
647

    
648
        /**
649
         * DOCUMENT ME!
650
         *
651
         * @param associatedTable
652
         *            DOCUMENT ME!
653
         */
654
        public void setAssociatedTable(AlphanumericData associatedTable) {
655
                this.associatedTable = associatedTable;
656
        }
657

    
658
        /**
659
         * Obtiene la fuente de datos original de la tabla si se ha invocado
660
         * replaceDataSource. Si no se invoc? este m?todo o se invoc? posteriormente
661
         * restoreDataSource se devuelve null
662
         *
663
         * @return Returns the original.
664
         */
665
        public IEditableSource getOriginal() {
666
                return original;
667
        }
668

    
669
        /**
670
         * Devuelve el identificador de la tabla que contiene el link.
671
         *
672
         * @return identificador ?nico de la tabla.
673
         */
674
        public String getLinkTable() {
675
                return linkTable;
676
        }
677

    
678
        /**
679
         * Devuelve el nombre del campo de la tabla a enlazar.
680
         *
681
         * @return Nombre del campo de la tabla a enlazar.
682
         */
683
        public String getField1() {
684
                return field1;
685
        }
686

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

    
696
        /**
697
         * Inserta el identificador de la tabla, el campo de la primera tabla y el
698
         * campo de la segunda tabla.
699
         *
700
         * @param lt
701
         *            identificado de la tabla.
702
         * @param f1
703
         *            nombre del campo de la primera tabla.
704
         * @param f2
705
         *            nombre del campo de la segunda tabla.
706
         */
707
        public void setLinkTable(String lt, String f1, String f2) {
708
                linkTable = lt;
709
                field1 = f1;
710
                field2 = f2;
711
        }
712

    
713
        /**
714
         * Borra el identificador de la tabla y elimina del array de listener los
715
         * listener que sean del tipo: LinkSelectionListenr
716
         */
717
        public void removeLinkTable() {
718
                linkTable = null;
719
                try {
720
                        getModelo().getRecordset()
721
                                        .removeLinksSelectionListener();
722
                } catch (ReadDriverException e) {
723
                        e.printStackTrace();
724
                }
725
        }
726

    
727
        public String[] getAliases() {
728
                return alias;
729
        }
730

    
731
        public void setAliases(String[] alias) {
732
                this.alias = alias;
733
        }
734

    
735
        public int[] getMapping() {
736
                if (mapping == null) {
737
                        mapping = new int[alias.length];
738
                        for (int i = 0; i < mapping.length; i++) {
739
                                mapping[i] = i;
740
                        }
741
                }
742
                return mapping;
743
        }
744

    
745
        public void setMapping(int[] mapping) {
746
                this.mapping = mapping;
747
        }
748

    
749
        public void setModel(IEditableSource ies) {
750
                setTheModel(ies);
751
                try {
752
                        if (mapping==null) {
753
                                createAlias();
754
                        }
755
                } catch (ReadDriverException e) {
756
                        e.printStackTrace();
757
                        NotificationManager.addError(e);
758
                }
759
        }
760

    
761
        public Column getColumn(int i) {
762
//                if (i==0){
763
//                        Column column=new Column();
764
//                        column.setWidth(45);
765
//                        return column;
766
//                }
767
//                i--;
768
                        return (Column) columns.get(getMapping()[i]);
769
        }
770

    
771
        public void addColumn(Column column) {
772
                columns.add(column);
773
        }
774

    
775
        public int getColumnCount() {
776
                return columns.size();
777
        }
778

    
779
        public IWindow createWindow() {
780
                com.iver.cit.gvsig.project.documents.table.gui.Table table = new com.iver.cit.gvsig.project.documents.table.gui.Table();
781
                table.setModel(this);
782
                return table;
783
        }
784
        public IWindow getProperties() {
785
                return new TableProperties(this);
786
        }
787

    
788
        public void afterRemove() {
789
                // TODO Auto-generated method stub
790

    
791
        }
792

    
793
        public void afterAdd() {
794
                // TODO Auto-generated method stub
795

    
796
        }
797

    
798
        private void setTheModel(IEditableSource es) {
799
                this.esModel = es;
800

    
801
        }
802

    
803
        public void exportToXML(XMLEntity root, Project project)  throws SaveException {
804
                XMLEntity tableRoot = project.getExportXMLTypeRootNode(root,ProjectTableFactory.registerName);
805
                try {
806
                        project.exportToXMLDataSource(root,this.getModelo().getRecordset().getName());
807
                } catch (ReadDriverException e) {
808
                        throw new SaveException();
809
                }
810
                tableRoot.addChild(this.getXMLEntity());
811
        }
812

    
813
        public void importFromXML(XMLEntity root, XMLEntity typeRoot,int elementIndex ,Project project, boolean removeDocumentsFromRoot) throws XMLException, ReadDriverException, OpenException {
814
                XMLEntity element = typeRoot.getChild(elementIndex);
815
                this.setXMLEntity(element);
816
                if (removeDocumentsFromRoot) {
817
                        typeRoot.removeChild(elementIndex);
818
                }
819
                project.addDocument(this);
820

    
821
        }
822

    
823
        public long[] getOrderIndexes() {
824
                return orderIndexes;
825
        }
826

    
827
        public void setOrderIndexes(long[] orderIndexes) {
828
                this.orderIndexes = orderIndexes;
829
        }
830

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