root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toc / actions / CopyPasteLayersUtiles.java @ 21299
History | View | Annotate | Download (11 KB)
1 |
package com.iver.cit.gvsig.project.documents.view.toc.actions; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
|
5 |
import org.gvsig.data.ReadException; |
6 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
7 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
8 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
9 |
import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
10 |
import org.gvsig.fmap.mapcontext.layers.XMLException; |
11 |
import org.gvsig.fmap.mapcontext.layers.operations.AlphanumericData; |
12 |
|
13 |
import com.iver.andami.PluginServices; |
14 |
import com.iver.cit.gvsig.ProjectExtension; |
15 |
import com.iver.cit.gvsig.Version; |
16 |
import com.iver.cit.gvsig.project.Project; |
17 |
import com.iver.cit.gvsig.project.documents.ProjectDocument; |
18 |
import com.iver.cit.gvsig.project.documents.exceptions.OpenException; |
19 |
import com.iver.cit.gvsig.project.documents.exceptions.SaveException; |
20 |
import com.iver.cit.gvsig.project.documents.table.ProjectTable; |
21 |
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory; |
22 |
import com.iver.utiles.XMLEntity; |
23 |
|
24 |
public class CopyPasteLayersUtiles { |
25 |
private static CopyPasteLayersUtiles theInstance = null; |
26 |
|
27 |
public static CopyPasteLayersUtiles getInstance() { |
28 |
if (theInstance == null) { |
29 |
theInstance = new CopyPasteLayersUtiles();
|
30 |
} |
31 |
return theInstance;
|
32 |
} |
33 |
|
34 |
public XMLEntity generateXMLCopyLayers(FLayer[] actives) { |
35 |
|
36 |
XMLEntity xml = this.newRootNode();
|
37 |
XMLEntity xmlLayers = this.newLayersNode();
|
38 |
XMLEntity xmlTables = this.newTablesNode();
|
39 |
XMLEntity xmlDataSources = this.newDataSourcesNode();
|
40 |
|
41 |
for (int i=0;i < actives.length; i++) { |
42 |
if (!this.addToXMLLayer(actives[i],xmlLayers ,xmlTables,xmlDataSources)) return null; |
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 ) return false; |
78 |
|
79 |
// Se pegan las tablas igualmente
|
80 |
/*
|
81 |
Project project = this.getProject();
|
82 |
|
83 |
Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
|
84 |
*/
|
85 |
|
86 |
|
87 |
if (xmlDataSources != null) { |
88 |
if (!this.registerDataSources(xmlDataSources)) return false; |
89 |
} |
90 |
|
91 |
if (!this.addLayers(xmlLayers,root)) return false; |
92 |
|
93 |
if (xmlTables != null) { |
94 |
if (!this.addTables(xmlTables)) return false; |
95 |
} |
96 |
|
97 |
return true; |
98 |
|
99 |
} |
100 |
|
101 |
public boolean loadTablesFromXML(XMLEntity xml) { |
102 |
XMLEntity xmlTables = xml.firstChild("type","tables"); |
103 |
XMLEntity xmlDataSources = xml.firstChild("type","dataSources"); |
104 |
|
105 |
|
106 |
if (xmlTables == null ) return false; |
107 |
|
108 |
/*
|
109 |
Project project = this.getProject();
|
110 |
|
111 |
Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
|
112 |
*/
|
113 |
|
114 |
if (xmlDataSources != null) { |
115 |
if (!this.registerDataSources(xmlDataSources)) return false; |
116 |
} |
117 |
|
118 |
|
119 |
|
120 |
return this.addTables(xmlTables); |
121 |
} |
122 |
|
123 |
|
124 |
public boolean checkXMLRootNode(XMLEntity xml) { |
125 |
if (!xml.contains("applicationName")) return false; |
126 |
if (!xml.getStringProperty("applicationName").equalsIgnoreCase("gvSIG")) return false; |
127 |
|
128 |
if (!xml.contains("version")) return false; |
129 |
if (!xml.getStringProperty("version").equalsIgnoreCase(Version.format())) return false; |
130 |
|
131 |
return true; |
132 |
} |
133 |
|
134 |
private void fillXMLRootNode(XMLEntity xml) { |
135 |
xml.putProperty("applicationName","gvSIG"); |
136 |
xml.putProperty("version",Version.format());
|
137 |
} |
138 |
|
139 |
private boolean registerDataSources(XMLEntity xmlDataSources) { |
140 |
try {
|
141 |
int numDataSources = xmlDataSources.getChildrenCount();
|
142 |
|
143 |
if (numDataSources == 0) return true; |
144 |
DataSourceFactory dsFactory = LayerFactory.getDataSourceFactory(); |
145 |
|
146 |
for (int i = 0; i < numDataSources; i++) { |
147 |
XMLEntity child = xmlDataSources.getChild(i); |
148 |
String name = child.getStringProperty("gdbmsname"); |
149 |
|
150 |
if (dsFactory.getDriverInfo(name) == null) { |
151 |
if (child.getStringProperty("type").equals("otherDriverFile")) { |
152 |
LayerFactory.getDataSourceFactory().addFileDataSource( |
153 |
child.getStringProperty("driverName"),
|
154 |
name, |
155 |
child.getStringProperty("file")
|
156 |
); |
157 |
|
158 |
|
159 |
} else if (child.getStringProperty("type").equals("sameDriverFile")) { |
160 |
/* String layerName = child.getStringProperty("layerName");
|
161 |
ProjectView vista = project.getViewByName(child.getStringProperty(
|
162 |
"viewName"));
|
163 |
FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
|
164 |
|
165 |
modelo = ((AlphanumericData) layer).getRecordset();
|
166 |
associatedTable = (AlphanumericData) layer;
|
167 |
*/
|
168 |
} else if (child.getStringProperty("type").equals("db")) { |
169 |
LayerFactory.getDataSourceFactory().addDBDataSourceByTable( |
170 |
name, |
171 |
child.getStringProperty("host"),
|
172 |
child.getIntProperty("port"),
|
173 |
child.getStringProperty("user"),
|
174 |
child.getStringProperty("password"),
|
175 |
child.getStringProperty("dbName"),
|
176 |
child.getStringProperty("tableName"),
|
177 |
child.getStringProperty("driverInfo")
|
178 |
); |
179 |
} |
180 |
} |
181 |
} |
182 |
|
183 |
return true; |
184 |
} catch (Exception e) { |
185 |
e.printStackTrace(); |
186 |
return false; |
187 |
} |
188 |
} |
189 |
|
190 |
private boolean addTables(XMLEntity xmlTables) { |
191 |
try {
|
192 |
int numTables = xmlTables.getChildrenCount();
|
193 |
if (numTables == 0) return true; |
194 |
|
195 |
Project project = this.getProject();
|
196 |
|
197 |
for (int i = 0; i < numTables; i++) { |
198 |
try{
|
199 |
ProjectTable ptable = (ProjectTable) ProjectTable.createFromXML(xmlTables.getChild(i), project); |
200 |
project.addDocument(ptable); |
201 |
/*
|
202 |
if (ptable.getSeedViewInfo()!=null && ptable.getAndamiView()!=null) { // open the view, if it was open, and restore its dimensions
|
203 |
PluginServices.getMDIManager().addView(ptable.getAndamiView());
|
204 |
PluginServices.getMDIManager().changeViewInfo(ptable.getAndamiView(), ptable.getSeedViewInfo());
|
205 |
}
|
206 |
*/
|
207 |
}catch(OpenException e){
|
208 |
e.printStackTrace(); |
209 |
return false; |
210 |
} |
211 |
} |
212 |
|
213 |
project.setLinkTable(); |
214 |
|
215 |
return true; |
216 |
} catch (Exception e) { |
217 |
e.printStackTrace(); |
218 |
return false; |
219 |
} |
220 |
} |
221 |
|
222 |
private boolean addLayers(XMLEntity xmlLayers,FLayers root) { |
223 |
try {
|
224 |
XMLEntity child; |
225 |
int numLayers = xmlLayers.getChildrenCount();
|
226 |
for (int i = 0; i < numLayers; i++) { |
227 |
child = xmlLayers.getChild(i); |
228 |
if (!root.addLayerFromXMLEntity(child,null)) return false; |
229 |
} |
230 |
return true; |
231 |
} catch (Exception e) { |
232 |
e.printStackTrace(); |
233 |
return false; |
234 |
} |
235 |
|
236 |
} |
237 |
|
238 |
|
239 |
|
240 |
private Project getProject() {
|
241 |
return ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
|
242 |
} |
243 |
|
244 |
private boolean addToXMLDataSource(SourceInfo source,XMLEntity xmlDataSources, Project project) { |
245 |
if (project== null) { |
246 |
project = this.getProject();
|
247 |
} |
248 |
xmlDataSources.addChild(project.getSourceInfoXMLEntity(source)); |
249 |
|
250 |
return true; |
251 |
} |
252 |
|
253 |
private boolean addToXMLTable(ProjectTable pt,XMLEntity xmlTables,XMLEntity xmlDataSources,Project project) { |
254 |
if (project== null) { |
255 |
project = this.getProject();
|
256 |
} |
257 |
if (xmlTables.findChildren("name",pt.getName()) != null) return true; |
258 |
XMLEntity xmlTable = null;
|
259 |
try {
|
260 |
xmlTable = pt.getXMLEntity(); |
261 |
|
262 |
xmlTables.addChild(xmlTable); |
263 |
|
264 |
if (pt.getAssociatedTable() != null) { |
265 |
this.addToXMLDataSource(pt.getAssociatedTable().getRecordset().getSourceInfo(),xmlDataSources,project);
|
266 |
} |
267 |
|
268 |
if (pt.getLinkTable() != null) { |
269 |
if (xmlTables.findChildren("name",pt.getLinkTable()) == null) { |
270 |
ProjectTable ptLink = (ProjectTable)project.getProjectDocumentByName(pt.getLinkTable(),ProjectTableFactory.registerName); |
271 |
if (!this.addToXMLTable(ptLink,xmlTables,xmlDataSources,project)) return false; |
272 |
} |
273 |
} |
274 |
} catch (SaveException e) {
|
275 |
e.printStackTrace(); |
276 |
return false; |
277 |
} catch (ReadException e) {
|
278 |
e.printStackTrace(); |
279 |
return false; |
280 |
} |
281 |
|
282 |
return true; |
283 |
} |
284 |
|
285 |
private boolean addToXMLLayerDependencies(FLayer lyr,XMLEntity xmlTables,XMLEntity xmlDataSources) { |
286 |
try {
|
287 |
Project project = this.getProject();
|
288 |
|
289 |
if (lyr instanceof FLayers) { |
290 |
FLayers lyrs = (FLayers)lyr; |
291 |
int count = lyrs.getLayersCount();
|
292 |
for (int i=0;i < count;i++) { |
293 |
FLayer subLyr = lyrs.getLayer(i); |
294 |
this.addToXMLLayerDependencies(subLyr,xmlTables,xmlDataSources);
|
295 |
} |
296 |
|
297 |
} else if (lyr instanceof AlphanumericData){ |
298 |
if (!this.addToXMLDataSource( |
299 |
((AlphanumericData)lyr).getRecordset().getSourceInfo(), |
300 |
xmlDataSources, |
301 |
project |
302 |
|
303 |
)) return false; |
304 |
|
305 |
ProjectTable pt = project.getTable((AlphanumericData) lyr); |
306 |
if (pt != null) { |
307 |
if (!this.addToXMLTable(pt,xmlTables,xmlDataSources,project)) return false; |
308 |
} |
309 |
|
310 |
} |
311 |
|
312 |
} catch (ReadException e) {
|
313 |
// TODO Auto-generated catch block
|
314 |
e.printStackTrace(); |
315 |
return false; |
316 |
} catch (Exception e) { |
317 |
e.printStackTrace(); |
318 |
return false; |
319 |
|
320 |
} |
321 |
return true; |
322 |
|
323 |
} |
324 |
|
325 |
private boolean addToXMLLayer(FLayer lyr,XMLEntity xmlLayers,XMLEntity xmlTables,XMLEntity xmlDataSources) { |
326 |
try {
|
327 |
xmlLayers.addChild(lyr.getXMLEntity()); |
328 |
|
329 |
return this.addToXMLLayerDependencies(lyr,xmlTables,xmlDataSources); |
330 |
|
331 |
} catch (XMLException e) {
|
332 |
e.printStackTrace(); |
333 |
return false; |
334 |
} catch (Exception e) { |
335 |
e.printStackTrace(); |
336 |
return false; |
337 |
} |
338 |
} |
339 |
|
340 |
private XMLEntity newRootNode() {
|
341 |
XMLEntity xml = new XMLEntity();
|
342 |
fillXMLRootNode(xml); |
343 |
return xml;
|
344 |
} |
345 |
|
346 |
private XMLEntity newLayersNode() {
|
347 |
XMLEntity xmlLayers = new XMLEntity();
|
348 |
xmlLayers.putProperty("type","layers"); |
349 |
return xmlLayers;
|
350 |
} |
351 |
|
352 |
private XMLEntity newDataSourcesNode() {
|
353 |
XMLEntity xmlDataSources = new XMLEntity();
|
354 |
xmlDataSources.putProperty("type","dataSources"); |
355 |
return xmlDataSources;
|
356 |
} |
357 |
|
358 |
private XMLEntity newTablesNode() {
|
359 |
XMLEntity xmlTables = new XMLEntity();
|
360 |
xmlTables.putProperty("type","tables"); |
361 |
return xmlTables;
|
362 |
} |
363 |
|
364 |
public boolean removeLayers(FLayer[] actives) { |
365 |
for (int i = actives.length-1; i>=0; i--){ |
366 |
try {
|
367 |
//actives[i].getParentLayer().removeLayer(actives[i]);
|
368 |
//FLayers lyrs=getMapContext().getLayers();
|
369 |
//lyrs.addLayer(actives[i]);
|
370 |
actives[i].getParentLayer().removeLayer(actives[i]); |
371 |
|
372 |
if (actives[i] instanceof AlphanumericData){ |
373 |
Project project = ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject(); |
374 |
ProjectTable pt = project.getTable((AlphanumericData) actives[i]); |
375 |
|
376 |
ArrayList tables = project.getDocumentsByType(ProjectTableFactory.registerName);
|
377 |
for (int j = 0; j < tables.size(); j++) { |
378 |
if (tables.get(j) == pt){
|
379 |
project.delDocument((ProjectDocument)tables.get(j)); |
380 |
break;
|
381 |
} |
382 |
} |
383 |
|
384 |
PluginServices.getMDIManager().closeSingletonWindow(pt); |
385 |
} |
386 |
|
387 |
|
388 |
} catch (CancelationException e1) {
|
389 |
e1.printStackTrace(); |
390 |
return false; |
391 |
} |
392 |
} |
393 |
return true; |
394 |
} |
395 |
|
396 |
} |