Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toc / actions / CopyPasteLayersUtiles.java @ 24759

History | View | Annotate | Download (11.2 KB)

1
package com.iver.cit.gvsig.project.documents.view.toc.actions;
2

    
3
import java.util.ArrayList;
4

    
5
import org.gvsig.fmap.mapcontext.layers.CancelationException;
6
import org.gvsig.fmap.mapcontext.layers.FLayer;
7
import org.gvsig.fmap.mapcontext.layers.FLayers;
8
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
9

    
10
import com.iver.andami.PluginServices;
11
import com.iver.cit.gvsig.ProjectExtension;
12
import com.iver.cit.gvsig.Version;
13
import com.iver.cit.gvsig.project.Project;
14
import com.iver.cit.gvsig.project.documents.ProjectDocument;
15
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
16
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
17
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
18
import com.iver.utiles.XMLEntity;
19
import com.iver.utiles.XMLException;
20

    
21
//TODO comentado para que compile
22
public class CopyPasteLayersUtiles {
23
        private static CopyPasteLayersUtiles theInstance = null;
24

    
25
        public static CopyPasteLayersUtiles getInstance() {
26
                if (theInstance == null) {
27
                        theInstance = new CopyPasteLayersUtiles();
28
                }
29
                return theInstance;
30
        }
31

    
32
        public XMLEntity generateXMLCopyLayers(FLayer[] actives) {
33

    
34
                XMLEntity xml = this.newRootNode();
35
                XMLEntity xmlLayers = this.newLayersNode();
36
                XMLEntity xmlTables = this.newTablesNode();
37
                XMLEntity xmlDataSources = this.newDataSourcesNode();
38

    
39
                for (int i=0;i < actives.length; i++) {
40
                        if (!this.addToXMLLayer(actives[i],xmlLayers ,xmlTables,xmlDataSources)) {
41
                                return null;
42
                        }
43

    
44
                }
45

    
46
                if (xmlDataSources.getChildrenCount() > 0) {
47
                        xml.addChild(xmlDataSources);
48
                }
49
                if (xmlLayers.getChildrenCount() > 0) {
50
                        xml.addChild(xmlLayers);
51
                }
52
                if (xmlTables.getChildrenCount() > 0) {
53
                        xml.addChild(xmlTables);
54
                }
55

    
56
                return xml;
57

    
58
        }
59

    
60

    
61

    
62
        /*
63
         *
64
         *
65
         *
66
         * Funciones Publicas de carga de un XML (pegar)
67
         *
68
         *
69
         *
70
        */
71

    
72
        public boolean loadLayersFromXML(XMLEntity xml, FLayers root) {
73
                XMLEntity xmlLayers = xml.firstChild("type","layers");
74
                XMLEntity xmlTables = xml.firstChild("type","tables");
75
                XMLEntity xmlDataSources = xml.firstChild("type","dataSources");
76

    
77
                if (xmlLayers == null ) {
78
                        return false;
79
                }
80

    
81
                // Se pegan las tablas igualmente
82
                /*
83
                Project project = this.getProject();
84

85
                Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
86
                */
87

    
88

    
89
                if (xmlDataSources != null)  {
90
                        if (!this.registerDataSources(xmlDataSources)) {
91
                                return false;
92
                        }
93
                }
94

    
95
                if (!this.addLayers(xmlLayers,root)) {
96
                        return false;
97
                }
98

    
99
                if (xmlTables != null)  {
100
                        if (!this.addTables(xmlTables)) {
101
                                return false;
102
                        }
103
                }
104

    
105
                return true;
106

    
107
        }
108

    
109
        public boolean loadTablesFromXML(XMLEntity xml) {
110
                XMLEntity xmlTables = xml.firstChild("type","tables");
111
                XMLEntity xmlDataSources = xml.firstChild("type","dataSources");
112

    
113

    
114
                if (xmlTables == null ) {
115
                        return false;
116
                }
117

    
118
                /*
119
                Project project = this.getProject();
120

121
                Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
122
                */
123

    
124
                if (xmlDataSources != null)  {
125
                        if (!this.registerDataSources(xmlDataSources)) {
126
                                return false;
127
                        }
128
                }
129

    
130

    
131

    
132
                return this.addTables(xmlTables);
133
        }
134

    
135

    
136
        public boolean checkXMLRootNode(XMLEntity xml) {
137
                if (!xml.contains("applicationName")) {
138
                        return false;
139
                }
140
                if (!xml.getStringProperty("applicationName").equalsIgnoreCase("gvSIG")) {
141
                        return false;
142
                }
143

    
144
                if (!xml.contains("version")) {
145
                        return false;
146
                }
147
                if (!xml.getStringProperty("version").equalsIgnoreCase(Version.format())) {
148
                        return false;
149
                }
150

    
151
                return true;
152
        }
153

    
154
        private void fillXMLRootNode(XMLEntity xml) {
155
                xml.putProperty("applicationName","gvSIG");
156
                xml.putProperty("version",Version.format());
157
        }
158

    
159
        private boolean registerDataSources(XMLEntity xmlDataSources) {
160
//                try {
161
//                        int numDataSources = xmlDataSources.getChildrenCount();
162
//
163
//                        if (numDataSources == 0) return true;
164
//                        DataSourceFactory dsFactory = LayerFactory.getDataSourceFactory();
165
//
166
//                        for (int i = 0; i < numDataSources; i++) {
167
//                                XMLEntity child = xmlDataSources.getChild(i);
168
//                                String name = child.getStringProperty("gdbmsname");
169
//
170
//                                if (dsFactory.getDriverInfo(name) == null) {
171
//                                        if (child.getStringProperty("type").equals("otherDriverFile")) {
172
//                                                LayerFactory.getDataSourceFactory().addFileDataSource(
173
//                                                                child.getStringProperty("driverName"),
174
//                                                                name,
175
//                                                                child.getStringProperty("file")
176
//                                                );
177
//
178
//
179
//                                        } else if (child.getStringProperty("type").equals("sameDriverFile")) {
180
//                                                /*                                String layerName = child.getStringProperty("layerName");
181
//                                                 ProjectView vista = project.getViewByName(child.getStringProperty(
182
//                                                 "viewName"));
183
//                                                 FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
184
//
185
//                                                 modelo = ((AlphanumericData) layer).getRecordset();
186
//                                                 associatedTable = (AlphanumericData) layer;
187
//                                                 */
188
//                                        } else if (child.getStringProperty("type").equals("db")) {
189
//                                                LayerFactory.getDataSourceFactory().addDBDataSourceByTable(
190
//                                                                name,
191
//                                                                child.getStringProperty("host"),
192
//                                                                child.getIntProperty("port"),
193
//                                                                child.getStringProperty("user"),
194
//                                                                child.getStringProperty("password"),
195
//                                                                child.getStringProperty("dbName"),
196
//                                                                child.getStringProperty("tableName"),
197
//                                                                child.getStringProperty("driverInfo")
198
//                                                );
199
//                                        }
200
//                                }
201
//                        }
202
//
203
//                        return true;
204
//                } catch (Exception e) {
205
//                        e.printStackTrace();
206
//                        return false;
207
//                }
208
                return false;
209
        }
210

    
211
        private boolean addTables(XMLEntity xmlTables) {
212
                try {
213
                        int numTables = xmlTables.getChildrenCount();
214
                        if (numTables == 0) {
215
                                return true;
216
                        }
217

    
218
                        Project project = this.getProject();
219

    
220
                        for (int i = 0; i < numTables; i++) {
221
                                try{
222
                                        ProjectTable ptable = (ProjectTable) ProjectTable.createFromXML(xmlTables.getChild(i), project);
223
                                        project.addDocument(ptable);
224
                                        /*
225
                                        if (ptable.getSeedViewInfo()!=null && ptable.getAndamiView()!=null) { // open the view, if it was open, and restore its dimensions
226
                                                PluginServices.getMDIManager().addView(ptable.getAndamiView());
227
                                                PluginServices.getMDIManager().changeViewInfo(ptable.getAndamiView(), ptable.getSeedViewInfo());
228
                                        }
229
                                        */
230
                                }catch(OpenException e){
231
                                        e.printStackTrace();
232
                                        return false;
233
                                }
234
                        }
235

    
236
                        project.setLinkTable();
237

    
238
                        return true;
239
                } catch (Exception e) {
240
                        e.printStackTrace();
241
                        return false;
242
                }
243
        }
244

    
245
        private boolean addLayers(XMLEntity xmlLayers,FLayers root) {
246
                try {
247
                        XMLEntity child;
248
                        int numLayers = xmlLayers.getChildrenCount();
249
                        for (int i = 0; i < numLayers; i++) {
250
                                child = xmlLayers.getChild(i);
251
//                                if (!root.addLayerFromXMLEntity(child,null)) return false;
252
                        }
253
                        return true;
254
                } catch (Exception e) {
255
                        e.printStackTrace();
256
                        return false;
257
                }
258

    
259
        }
260

    
261

    
262

    
263
        private Project getProject() {
264
                 return ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
265
        }
266

    
267
//        private boolean addToXMLDataSource(SourceInfo source,XMLEntity xmlDataSources, Project project) {
268
//                if (project== null) {
269
//                        project = this.getProject();
270
//                }
271
//                    xmlDataSources.addChild(project.getSourceInfoXMLEntity(source));
272
//
273
//                    return true;
274
//        }
275

    
276
        private boolean addToXMLTable(ProjectTable pt,XMLEntity xmlTables,XMLEntity xmlDataSources,Project project) {
277
//                if (project== null) {
278
//                        project = this.getProject();
279
//                }
280
//                if (xmlTables.findChildren("name",pt.getName()) != null) return true;
281
//                XMLEntity xmlTable = null;
282
//                try {
283
//                        xmlTable = pt.getXMLEntity();
284
//
285
//                        xmlTables.addChild(xmlTable);
286
//
287
//                        if (pt.getAssociatedTable() != null) {
288
//                                this.addToXMLDataSource(pt.getAssociatedTable().getRecordset().getSourceInfo(),xmlDataSources,project);
289
//                        }
290
//
291
//                        if (pt.getLinkTable() != null) {
292
//                                if (xmlTables.findChildren("name",pt.getLinkTable()) == null)  {
293
//                                        ProjectTable ptLink = (ProjectTable)project.getProjectDocumentByName(pt.getLinkTable(),ProjectTableFactory.registerName);
294
//                                        if (!this.addToXMLTable(ptLink,xmlTables,xmlDataSources,project)) return false;
295
//                                }
296
//                        }
297
//                } catch (SaveException e) {
298
//                        e.printStackTrace();
299
//                        return false;
300
//                } catch (ReadException e) {
301
//                        e.printStackTrace();
302
//                        return false;
303
//                }
304

    
305
                    return true;
306
        }
307

    
308
        private boolean addToXMLLayerDependencies(FLayer lyr,XMLEntity xmlTables,XMLEntity xmlDataSources) {
309
//                try {
310
//                        Project project = this.getProject();
311
//
312
//                        if (lyr instanceof FLayers) {
313
//                                FLayers lyrs = (FLayers)lyr;
314
//                                int count = lyrs.getLayersCount();
315
//                                for (int i=0;i < count;i++) {
316
//                                        FLayer subLyr = lyrs.getLayer(i);
317
//                                        this.addToXMLLayerDependencies(subLyr,xmlTables,xmlDataSources);
318
//                                }
319
//
320
//                    } else if (lyr instanceof FLyrVect){
321
//                    if (!this.addToXMLDataSource(
322
//                                ((FLyrVect)lyr).getRecordset().getSourceInfo(),
323
//                                xmlDataSources,
324
//                                project
325
//
326
//                    )) return false;
327
//
328
//                ProjectTable pt = project.getTable((FLyrVect) lyr);
329
//                if (pt != null) {
330
//                        if (!this.addToXMLTable(pt,xmlTables,xmlDataSources,project)) return false;
331
//                }
332
//
333
//            }
334
//
335
//                } catch (ReadException e) {
336
//                        // TODO Auto-generated catch block
337
//                        e.printStackTrace();
338
//                        return false;
339
//                } catch (Exception e) {
340
//                        e.printStackTrace();
341
//                        return false;
342
//
343
//                }
344
                return true;
345

    
346
        }
347

    
348
        private boolean addToXMLLayer(FLayer lyr,XMLEntity xmlLayers,XMLEntity xmlTables,XMLEntity xmlDataSources) {
349
                try {
350
                        xmlLayers.addChild(lyr.getXMLEntity());
351

    
352
                        return this.addToXMLLayerDependencies(lyr,xmlTables,xmlDataSources);
353

    
354
                } catch (XMLException e) {
355
                        e.printStackTrace();
356
                        return false;
357
                } catch (Exception e) {
358
                        e.printStackTrace();
359
                        return false;
360
                }
361
        }
362

    
363
        private XMLEntity newRootNode() {
364
                XMLEntity xml = new XMLEntity();
365
                fillXMLRootNode(xml);
366
                return xml;
367
        }
368

    
369
        private XMLEntity newLayersNode() {
370
                XMLEntity xmlLayers = new XMLEntity();
371
                xmlLayers.putProperty("type","layers");
372
                return xmlLayers;
373
        }
374

    
375
        private XMLEntity newDataSourcesNode() {
376
                XMLEntity xmlDataSources = new XMLEntity();
377
                xmlDataSources.putProperty("type","dataSources");
378
                return xmlDataSources;
379
        }
380

    
381
        private XMLEntity newTablesNode() {
382
                XMLEntity xmlTables = new XMLEntity();
383
                xmlTables.putProperty("type","tables");
384
                return xmlTables;
385
        }
386

    
387
        public boolean removeLayers(FLayer[] actives) {
388
            for (int i = actives.length-1; i>=0; i--){
389
                try {
390
                                //actives[i].getParentLayer().removeLayer(actives[i]);
391
                                //FLayers lyrs=getMapContext().getLayers();
392
                                //lyrs.addLayer(actives[i]);
393
                                actives[i].getParentLayer().removeLayer(actives[i]);
394

    
395
                if (actives[i] instanceof FLyrVect){
396
                    Project project = ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
397
                    ProjectTable pt = project.getTable((FLyrVect) actives[i]);
398

    
399
                    ArrayList tables = project.getDocumentsByType(ProjectTableFactory.registerName);
400
                    for (int j = 0; j < tables.size(); j++) {
401
                        if (tables.get(j) == pt){
402
                            project.delDocument((ProjectDocument)tables.get(j));
403
                            break;
404
                        }
405
                    }
406

    
407
                    PluginServices.getMDIManager().closeSingletonWindow(pt);
408
                }
409

    
410

    
411
                    } catch (CancelationException e1) {
412
                            e1.printStackTrace();
413
                            return false;
414
                    }
415
            }
416
                return true;
417
        }
418

    
419
}