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