Statistics
| Revision:

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

History | View | Annotate | Download (24.7 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.ClassifiedLegend;
71
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
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
                try {
235
                         es.getRecordset().setSelectionSupport((original
236
                                                        .getRecordset()).getSelectionSupport());
237

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

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

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

    
296
                                                return;
297
                                        }
298
                                }
299

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

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

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

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

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

    
327
                        xml.putProperty("numTables", numTables);
328

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

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

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

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

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

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

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

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

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

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

    
400
                        setTheModel((VectorialEditableAdapter) ((FLyrVect) layer).getSource());
401
                        associatedTable = (AlphanumericData) layer;
402

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

    
416
                        setSelectableDataSource03(xml);
417
                }
418

    
419
                setName(xml.getStringProperty("name"));
420
        }
421

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

    
444
                        setSelectableDataSource(xml);
445

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

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

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

    
481
                                }
482

    
483
                        } else {
484
                                try {
485
                                        createAlias();
486
                                } catch (ReadDriverException e) {
487
                                        throw new XMLException(e);
488
                                }
489
                        }
490
                } catch (Exception e) {
491
                        throw new OpenException(e, this.getClass().getName());
492
                }
493

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

    
503

    
504
        }
505

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

    
520
                if (xml.contains("layerName")) {
521
                        layerName = xml.getStringProperty("layerName");
522
                }
523

    
524
                if (layerName == null) {
525
                        DataSource dataSource;
526

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

    
533
                                SelectableDataSource sds = new SelectableDataSource(dataSource);
534

    
535
                                sds.setXMLEntity03(xml.getChild(0));
536
                                EditableAdapter auxea = new EditableAdapter();
537
                                auxea.setOriginalDataSource(sds);
538
                                setDataSource(auxea);
539
                        } catch (NoSuchTableException e) {
540
                                throw new XMLException(e);
541
                        } catch (DriverLoadException e) {
542
                                throw new XMLException(e);
543
                        } catch (ReadDriverException e) {
544
                                throw new XMLException(e);
545
                        }
546

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

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

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

    
574
                try {
575
                        EditableAdapter es;
576

    
577
                        if (xml.contains("layerName")) {
578
                                layerName = xml.getStringProperty("layerName");
579

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

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

    
598
                        setDataSource(es);
599

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

    
607
                                // replaceDataSource(SelectableDataSource.createSelectableDataSource(xml.getChild(1)));
608
                        }
609

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

    
617
                                        // modelo = ((AlphanumericData) layer).getRecordset();
618
                                        associatedTable = (AlphanumericData) layer;
619
                                }
620

    
621
                                EditableAdapter auxea = new EditableAdapter();
622
                                auxea.setOriginalDataSource(SelectableDataSource
623
                                                .createSelectableDataSource(xmlAux));
624
                                replaceDataSource(auxea);
625
                        }
626
                } catch (DriverLoadException e) {
627
                        throw new ReadDriverException(getName(),e);
628
                } catch (XMLException e) {
629
                        throw new ReadDriverException(getName(),e);
630
                }
631
        }
632

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

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

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

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

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

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

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

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

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

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

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

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

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

    
756
        public void setModel(IEditableSource ies) {
757
                setTheModel(ies);
758
                try {
759
                        if (mapping==null) {
760
                                createAlias();
761
                        }
762
                } catch (DriverLoadException e) {
763
                        e.printStackTrace();
764
                        NotificationManager.addError(e);
765
                } catch (ReadDriverException 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
                try {
816
                        project.exportToXMLDataSource(root,this.getModelo().getRecordset().getName());
817
                } catch (ReadDriverException e) {
818
                        throw new SaveException();
819
                }
820
                tableRoot.addChild(this.getXMLEntity());
821
        }
822

    
823
        public void importFromXML(XMLEntity root, XMLEntity typeRoot,int elementIndex ,Project project, boolean removeDocumentsFromRoot) throws XMLException, ReadDriverException, 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 long[] getOrderIndexes() {
834
                return orderIndexes;
835
        }
836

    
837
        public void setOrderIndexes(long[] orderIndexes) {
838
                this.orderIndexes = orderIndexes;
839
        }
840

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