Statistics
| Revision:

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

History | View | Annotate | Download (25.1 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 com.hardcode.driverManager.DriverLoadException;
48
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
49
import com.hardcode.gdbms.engine.data.DataSource;
50
import com.hardcode.gdbms.engine.data.DataSourceFactory;
51
import com.hardcode.gdbms.engine.data.NoSuchTableException;
52
import com.hardcode.gdbms.engine.data.driver.DriverException;
53
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
54
import com.iver.andami.messages.NotificationManager;
55
import com.iver.andami.ui.mdiManager.IWindow;
56
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
57
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
58
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
59
import com.iver.cit.gvsig.fmap.layers.FLayer;
60
import com.iver.cit.gvsig.fmap.layers.FLayers;
61
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
62
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
63
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
64
import com.iver.cit.gvsig.fmap.layers.XMLException;
65
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
66
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
67
import com.iver.cit.gvsig.fmap.rendering.IClassifiedVectorLegend;
68
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
69
import com.iver.cit.gvsig.project.Project;
70
import com.iver.cit.gvsig.project.documents.ProjectDocument;
71
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
72
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
73
import com.iver.cit.gvsig.project.documents.table.gui.TableProperties;
74
import com.iver.cit.gvsig.project.documents.table.gui.tablemodel.Column;
75
import com.iver.cit.gvsig.project.documents.table.gui.tablemodel.Columns;
76
import com.iver.cit.gvsig.project.documents.view.ProjectView;
77
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
78
import com.iver.utiles.XMLEntity;
79

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

    
88
        private IEditableSource esModel;
89

    
90
        private IEditableSource original;
91

    
92
        private String linkTable;
93

    
94
        private String field1;
95

    
96
        private String field2;
97

    
98
        /* No es necesaria para operar, s?lo para guardar el proyecto */
99
        private AlphanumericData associatedTable;
100

    
101
        private int[] mapping;
102

    
103
        private String[] alias;
104

    
105
        private Columns columns = new Columns();
106
        private long[] orderIndexes = null;
107

    
108
        private XMLEntity backupXMLEntity= null;
109

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

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

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

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

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

    
163

    
164

    
165
        public void createAlias() throws ReadDriverException {
166
                SelectableDataSource sds = esModel.getRecordset();
167
//                mapping = new int[sds.getFieldCount()+1];
168
//                alias = new String[sds.getFieldCount()+1];
169
//                for (int i = 0; i < sds.getFieldCount()+1; i++) {
170
//                if (mapping == null){
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
                }
187

    
188
                recalculateColumnsFromAliases();
189

    
190
        }
191

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

    
205
        }
206

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

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

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

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

    
252
                change.firePropertyChange("model", original, esModel);
253
        }
254

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

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

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

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

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

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

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

    
324
                        xml.putProperty("numTables", numTables);
325

    
326
                        if (this.getModelo() == null){
327
                                if (this.backupXMLEntity == null){
328
                                        return xml;
329
                                } else{
330
                                        return this.backupXMLEntity;
331
                                }
332
                        }
333

    
334
                        if (getLinkTable() != null) {
335
                                xml.putProperty("linkTable", linkTable);
336
                                xml.putProperty("field1", field1);
337
                                xml.putProperty("field2", field2);
338
                        }
339

    
340
                        if (getOriginal() != null) {
341
                                xml.addChild(getOriginal().getRecordset().getXMLEntity());
342
                        }
343
                        xml.addChild(esModel.getRecordset().getXMLEntity());
344

    
345
//                        Object di = LayerFactory.getDataSourceFactory().getDriverInfo(
346
//                                        esModel.getRecordset().getName());
347

    
348
                        if (associatedTable != null) {
349
                                xml.putProperty("layerName", ((FLayer) associatedTable)
350
                                                .getName());
351
                                xml.putProperty("viewName", project
352
                                                .getView((FLayer) associatedTable));
353
                        }
354

    
355
                        xml.putProperty("mapping", mapping);
356
                        xml.putProperty("aliases", alias);
357
                } catch (Exception e) {
358
                        throw new SaveException(e, this.getClass().getName());
359
                }
360

    
361
                // for (int i=0;i<columns.size();i++){
362
                // Column column=(Column)columns.get(i);
363
                // xml.addChild(column.getXMLEntity());
364
                // }
365
                xml.addChild(columns.getXMLEntity());
366
                xml.putProperty("columns", true);
367
                return xml;
368
        }
369

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

    
390
                if (xml.getStringProperty("type").equals("otherDriverFile")) {
391
                        LayerFactory.getDataSourceFactory().addFileDataSource(
392
                                        xml.getStringProperty("driverName"),
393
                                        xml.getStringProperty("gdbmsname"),
394
                                        xml.getStringProperty("file"));
395

    
396
                        setSelectableDataSource03(xml);
397
                } else if (xml.getStringProperty("type").equals("sameDriverFile")) {
398
                        String layerName = xml.getStringProperty("layerName");
399

    
400
                        ProjectView vista =(ProjectView) project.getProjectDocumentByName(xml
401
                                        .getStringProperty("viewName"), ProjectViewFactory.registerName);
402
                        FLayer layer = vista.getMapContext().getLayers()
403
                                        .getLayer(layerName);
404

    
405
                        setTheModel((VectorialEditableAdapter) ((FLyrVect) layer).getSource());
406
                        associatedTable = (AlphanumericData) layer;
407

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

    
421
                        setSelectableDataSource03(xml);
422
                }
423

    
424
                setName(xml.getStringProperty("name"));
425
        }
426

    
427
        private void fillAsEmpty(){
428
                this.esModel = null;
429
                mapping = new int[0];
430
                alias = new String[0];
431
                recalculateColumnsFromAliases();
432
        }
433

    
434
        /**
435
         * DOCUMENT ME!
436
         *
437
         * @param xml
438
         *            DOCUMENT ME!
439
         * @param p
440
         *            DOCUMENT ME!
441
         *
442
         * @throws XMLException
443
         * @throws DriverException
444
         * @throws OpenException
445
         *
446
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
447
         */
448
        public void setXMLEntity(XMLEntity xml) throws XMLException,
449
                        ReadDriverException, OpenException {
450
                try {
451
                        super.setXMLEntity(xml);
452
                        backupXMLEntity = xml;
453

    
454
                        setName(xml.getStringProperty("name"));
455
                        int numTables = xml.getIntProperty("numTables");
456
                        ProjectDocument.NUMS.put(ProjectTableFactory.registerName,new Integer(numTables));
457

    
458
                        if (xml.getChildrenCount() == 0){
459
                                fillAsEmpty();
460
                                return;
461

    
462
                        }
463
                        try{
464
                                setSelectableDataSource(xml);
465
                        } catch (ReadDriverException e){
466
                                fillAsEmpty();
467
                                throw e;
468
                        }
469

    
470
                        /*
471
                         * if (xml.getStringProperty("type").equals("otherDriverFile")) { }
472
                         * else if (xml.getStringProperty("type").equals("sameDriverFile")) {
473
                         * String layerName = xml.getStringProperty("layerName");
474
                         * ProjectView vista = project.getViewByName(xml.getStringProperty(
475
                         * "viewName")); FLayer layer =
476
                         * vista.getMapContext().getLayers().getLayer(layerName);
477
                         *
478
                         * modelo = ((AlphanumericData) layer).getRecordset();
479
                         * associatedTable = (AlphanumericData) layer; } else if
480
                         * (xml.getStringProperty("type").equals("db")) {
481
                         * setSelectableDataSource(xml); }
482
                         */
483
                        setName(xml.getStringProperty("name"));
484

    
485
                        if (xml.contains("linkTable")) {
486
                                setLinkTable(xml.getStringProperty("linkTable"), xml
487
                                                .getStringProperty("field1"), xml
488
                                                .getStringProperty("field2"));
489
                        }
490

    
491
                        if (xml.contains("mapping")) {
492
                                mapping = xml.getIntArrayProperty("mapping");
493
                                alias = xml.getStringArrayProperty("aliases");
494
                                // we check if all fields are real there (may be some external program has changed them.
495
                                // If we detect any change, we discard all mapping and aliases.
496
                                if (mapping.length != getModelo().getRecordset().getFieldCount())
497
                                {
498
                                        createAlias();
499
//                                        columns.clear();
500
//                                        for (int i = 0; i < esModel.getRecordset().getFieldCount(); i++) {
501
//                                                addColumn(new Column());
502
//                                        }
503
                                        return;
504

    
505
                                }
506

    
507
                        } else {
508
                                try {
509
                                        createAlias();
510
                                } catch (ReadDriverException e) {
511
                                        throw new XMLException(e);
512
                                }
513
                        }
514
                } catch (Exception e) {
515
                        throw new OpenException(e, this.getClass().getName());
516
                }
517

    
518
                // for (int i=1;i<xml.getNumChild();i++){
519
                // columns.add(Column.createColumn(xml.getChild(i)));
520
                // }
521
                if (xml.contains("columns")) {
522
                        columns.clear();
523
                        columns = Columns
524
                                        .createColumns(xml.getChild(xml.getChildrenCount() - 1));
525
                }
526

    
527

    
528
        }
529

    
530
        /**
531
         * DOCUMENT ME!
532
         *
533
         * @param xml
534
         *            DOCUMENT ME!
535
         *
536
         * @throws XMLException
537
         *             DOCUMENT ME!
538
         * @throws DriverException
539
         *             DOCUMENT ME!
540
         */
541
        private void setSelectableDataSource03(XMLEntity xml) throws XMLException {
542
                String layerName = null;
543

    
544
                if (xml.contains("layerName")) {
545
                        layerName = xml.getStringProperty("layerName");
546
                }
547

    
548
                if (layerName == null) {
549
                        DataSource dataSource;
550

    
551
                        try {
552
                                dataSource = LayerFactory.getDataSourceFactory()
553
                                                .createRandomDataSource(
554
                                                                xml.getStringProperty("gdbmsname"),
555
                                                                DataSourceFactory.AUTOMATIC_OPENING);
556

    
557
                                SelectableDataSource sds = new SelectableDataSource(dataSource);
558

    
559
                                sds.setXMLEntity03(xml.getChild(0));
560
                                EditableAdapter auxea = new EditableAdapter();
561
                                auxea.setOriginalDataSource(sds);
562
                                setDataSource(auxea);
563
                        } catch (NoSuchTableException e) {
564
                                throw new XMLException(e);
565
                        } catch (DriverLoadException e) {
566
                                throw new XMLException(e);
567
                        } catch (ReadDriverException e) {
568
                                throw new XMLException(e);
569
                        }
570

    
571
                } else {
572
                        ProjectView vista = (ProjectView) project.getProjectDocumentByName(xml
573
                                        .getStringProperty("viewName"), ProjectViewFactory.registerName);
574
                        FLayer layer = vista.getMapContext().getLayers()
575
                                        .getLayer(layerName);
576

    
577
                        setTheModel((VectorialEditableAdapter) ((FLyrVect) layer).getSource());
578
                        associatedTable = (AlphanumericData) layer;
579
                }
580
        }
581

    
582
        /**
583
         * DOCUMENT ME!
584
         *
585
         * @param xml
586
         *            DOCUMENT ME!
587
         *
588
         * @throws XMLException
589
         *             DOCUMENT ME!
590
         * @throws DriverException
591
         *             DOCUMENT ME!
592
         */
593
        private void setSelectableDataSource(XMLEntity xml) throws ReadDriverException {
594
                String layerName = null;
595
                boolean bNeedToReplace = false;
596
                XMLEntity xmlAux = null;
597

    
598
                try {
599
                        EditableAdapter es;
600

    
601
                        if (xml.contains("layerName")) {
602
                                layerName = xml.getStringProperty("layerName");
603

    
604
                                ProjectView vista = (ProjectView) project.getProjectDocumentByName(xml
605
                                                .getStringProperty("viewName"), ProjectViewFactory.registerName);
606
                                FLayer layer = getLayer(vista.getMapContext().getLayers(),
607
                                                layerName);
608
                                EditableAdapter ea = new EditableAdapter();
609
                                SelectableDataSource sds = ((FLyrVect) layer).getRecordset();
610
                                // sds.setSelectionSupport(((FLyrVect)
611
                                // layer).getSelectionSupport());
612
                                ea.setOriginalDataSource(sds);
613
                                associatedTable = (AlphanumericData) layer;
614

    
615
                                es = ea;
616
                        } else {
617
                                es = new EditableAdapter();
618
                                es.setOriginalDataSource(SelectableDataSource
619
                                                .createSelectableDataSource(xml.getChild(0)));
620
                        }
621

    
622
                        setDataSource(es);
623

    
624
                        if (xml.getChildrenCount() == 2 && !(xml.contains("columns"))
625
                                        || (xml.contains("columns") && (xml.getChildrenCount() == 3))) {
626
                                bNeedToReplace = true;
627
                                xmlAux = xml.getChild(1);
628
                                es = new EditableAdapter();
629
                                // es.setRecordSet(SelectableDataSource.createSelectableDataSource(xmlAux));
630

    
631
                                // replaceDataSource(SelectableDataSource.createSelectableDataSource(xml.getChild(1)));
632
                        }
633

    
634
                        if (bNeedToReplace) {
635
                                if (layerName != null) {
636
                                        ProjectView vista = (ProjectView) project.getProjectDocumentByName(xml
637
                                                        .getStringProperty("viewName"), ProjectViewFactory.registerName);
638
                                        FLayer layer = getLayer(vista.getMapContext().getLayers(),
639
                                                        layerName);
640

    
641
                                        // modelo = ((AlphanumericData) layer).getRecordset();
642
                                        associatedTable = (AlphanumericData) layer;
643
                                }
644

    
645
                                EditableAdapter auxea = new EditableAdapter();
646
                                auxea.setOriginalDataSource(SelectableDataSource
647
                                                .createSelectableDataSource(xmlAux));
648
                                replaceDataSource(auxea);
649
                        }
650
                } catch (DriverLoadException e) {
651
                        throw new ReadDriverException(getName(),e);
652
                } catch (XMLException e) {
653
                        throw new ReadDriverException(getName(),e);
654
                }
655
        }
656

    
657
        private FLayer getLayer(FLayers layers, String name) {
658
                FLayer aux;
659
                for (int i = 0; i < layers.getLayersCount(); i++) {
660
                        if (layers.getLayer(i) instanceof FLayers) {
661
                                aux = getLayer((FLayers) layers.getLayer(i), name);
662
                                if (aux != null){
663
                                        return aux;
664
                                }
665
                        } else if (layers.getLayer(i).getName().equals(name)) {
666
                                return layers.getLayer(i);
667
                        }
668
                }
669
                return null;
670
        }
671

    
672
        /**
673
         * DOCUMENT ME!
674
         *
675
         * @return DOCUMENT ME!
676
         */
677
        public AlphanumericData getAssociatedTable() {
678
                return associatedTable;
679
        }
680

    
681
        /**
682
         * DOCUMENT ME!
683
         *
684
         * @param associatedTable
685
         *            DOCUMENT ME!
686
         */
687
        public void setAssociatedTable(AlphanumericData associatedTable) {
688
                this.associatedTable = associatedTable;
689
        }
690

    
691
        /**
692
         * Obtiene la fuente de datos original de la tabla si se ha invocado
693
         * replaceDataSource. Si no se invoc? este m?todo o se invoc? posteriormente
694
         * restoreDataSource se devuelve null
695
         *
696
         * @return Returns the original.
697
         */
698
        public IEditableSource getOriginal() {
699
                return original;
700
        }
701

    
702
        /**
703
         * Devuelve el identificador de la tabla que contiene el link.
704
         *
705
         * @return identificador ?nico de la tabla.
706
         */
707
        public String getLinkTable() {
708
                return linkTable;
709
        }
710

    
711
        /**
712
         * Devuelve el nombre del campo de la tabla a enlazar.
713
         *
714
         * @return Nombre del campo de la tabla a enlazar.
715
         */
716
        public String getField1() {
717
                return field1;
718
        }
719

    
720
        /**
721
         * Devuelve el nombre del campo de la tabla enlazada.
722
         *
723
         * @return Nombre del campo de la tabla enlazada.
724
         */
725
        public String getField2() {
726
                return field2;
727
        }
728

    
729
        /**
730
         * Inserta el identificador de la tabla, el campo de la primera tabla y el
731
         * campo de la segunda tabla.
732
         *
733
         * @param lt
734
         *            identificado de la tabla.
735
         * @param f1
736
         *            nombre del campo de la primera tabla.
737
         * @param f2
738
         *            nombre del campo de la segunda tabla.
739
         */
740
        public void setLinkTable(String lt, String f1, String f2) {
741
                linkTable = lt;
742
                field1 = f1;
743
                field2 = f2;
744
        }
745

    
746
        /**
747
         * Borra el identificador de la tabla y elimina del array de listener los
748
         * listener que sean del tipo: LinkSelectionListenr
749
         */
750
        public void removeLinkTable() {
751
                linkTable = null;
752
                try {
753
                        getModelo().getRecordset()
754
                                        .removeLinksSelectionListener();
755
                } catch (ReadDriverException e) {
756
                        e.printStackTrace();
757
                }
758
        }
759

    
760
        public String[] getAliases() {
761
                return alias;
762
        }
763

    
764
        public void setAliases(String[] alias) {
765
                this.alias = alias;
766
        }
767

    
768
        public int[] getMapping() {
769
                if (mapping == null) {
770
                        mapping = new int[alias.length];
771
                        for (int i = 0; i < mapping.length; i++) {
772
                                mapping[i] = i;
773
                        }
774
                }
775
                return mapping;
776
        }
777

    
778
        public void setMapping(int[] mapping) {
779
                this.mapping = mapping;
780
        }
781

    
782
        public void setModel(IEditableSource ies) {
783
                setTheModel(ies);
784
                try {
785
                        if (mapping==null) {
786
                                createAlias();
787
                        }
788
                } catch (ReadDriverException e) {
789
                        e.printStackTrace();
790
                        NotificationManager.addError(e);
791
                }
792
        }
793

    
794
        public Column getColumn(int i) {
795
//                if (i==0){
796
//                        Column column=new Column();
797
//                        column.setWidth(45);
798
//                        return column;
799
//                }
800
//                i--;
801
                        return (Column) columns.get(getMapping()[i]);
802
        }
803

    
804
        public void addColumn(Column column) {
805
                columns.add(column);
806
        }
807

    
808
        public int getColumnCount() {
809
                return columns.size();
810
        }
811

    
812
        public IWindow createWindow() {
813
                if (this.getModelo() == null)
814
                        return null;
815
                com.iver.cit.gvsig.project.documents.table.gui.Table table = new com.iver.cit.gvsig.project.documents.table.gui.Table();
816
                table.setModel(this);
817
                callCreateWindow(table);
818
                return table;
819
        }
820
        public IWindow getProperties() {
821
                return new TableProperties(this);
822
        }
823

    
824
        public void afterRemove() {
825
                // TODO Auto-generated method stub
826

    
827
        }
828

    
829
        public void afterAdd() {
830
                // TODO Auto-generated method stub
831

    
832
        }
833

    
834
        private void setTheModel(IEditableSource es) {
835
                this.esModel = es;
836

    
837
        }
838

    
839
        public void exportToXML(XMLEntity root, Project project)  throws SaveException {
840
                XMLEntity tableRoot = project.getExportXMLTypeRootNode(root,ProjectTableFactory.registerName);
841
                try {
842
                        project.exportToXMLDataSource(root,this.getModelo().getRecordset().getName());
843
                } catch (ReadDriverException e) {
844
                        throw new SaveException();
845
                }
846
                tableRoot.addChild(this.getXMLEntity());
847
        }
848

    
849
        public void importFromXML(XMLEntity root, XMLEntity typeRoot,int elementIndex ,Project project, boolean removeDocumentsFromRoot) throws XMLException, ReadDriverException, OpenException {
850
                XMLEntity element = typeRoot.getChild(elementIndex);
851
                this.setXMLEntity(element);
852
                if (removeDocumentsFromRoot) {
853
                        typeRoot.removeChild(elementIndex);
854
                }
855
                project.addDocument(this);
856

    
857
        }
858

    
859
        public long[] getOrderIndexes() {
860
                return orderIndexes;
861
        }
862

    
863
        public void setOrderIndexes(long[] orderIndexes) {
864
                this.orderIndexes = orderIndexes;
865
        }
866

    
867
//        public int computeSignature() {
868
//                int result = 17;
869
//
870
//                Class clazz = getClass();
871
//                Field[] fields = clazz.getDeclaredFields();
872
//                for (int i = 0; i < fields.length; i++) {
873
//                        try {
874
//                                String type = fields[i].getType().getName();
875
//                                if (type.equals("boolean")) {
876
//                                        result += 37 + ((fields[i].getBoolean(this)) ? 1 : 0);
877
//                                } else if (type.equals("java.lang.String")) {
878
//                                        Object v = fields[i].get(this);
879
//                                        if (v == null) {
880
//                                                result += 37;
881
//                                                continue;
882
//                                        }
883
//                                        char[] chars = ((String) v).toCharArray();
884
//                                        for (int j = 0; j < chars.length; j++) {
885
//                                                result += 37 + (int) chars[i];
886
//                                        }
887
//                                } else if (type.equals("byte")) {
888
//                                        result += 37 + (int) fields[i].getByte(this);
889
//                                } else if (type.equals("char")) {
890
//                                        result += 37 + (int) fields[i].getChar(this);
891
//                                } else if (type.equals("short")) {
892
//                                        result += 37 + (int) fields[i].getShort(this);
893
//                                } else if (type.equals("int")) {
894
//                                        result += 37 + fields[i].getInt(this);
895
//                                } else if (type.equals("long")) {
896
//                                        long f = fields[i].getLong(this) ;
897
//                                        result += 37 + (f ^ (f >>> 32));
898
//                                } else if (type.equals("float")) {
899
//                                        result += 37 + Float.floatToIntBits(fields[i].getFloat(this));
900
//                                } else if (type.equals("double")) {
901
//                                        long f = Double.doubleToLongBits(fields[i].getDouble(this));
902
//                                        result += 37 + (f ^ (f >>> 32));
903
//                                } else {
904
//                                        Object obj = fields[i].get(this);
905
//                                        result += 37 + ((obj != null)? obj.hashCode() : 0);
906
//                                }
907
//                        } catch (Exception e) { e.printStackTrace(); }
908
//
909
//                }
910
//                return result;
911
//        }
912
}