svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / LayerFactory.java @ 23750
History | View | Annotate | Download (3.73 KB)
1 |
package org.gvsig.fmap.mapcontext.layers; |
---|---|
2 |
|
3 |
import java.util.HashMap; |
4 |
import java.util.Map; |
5 |
|
6 |
import org.apache.log4j.Logger; |
7 |
import org.cresques.cts.IProjection; |
8 |
import org.gvsig.fmap.data.DataManager; |
9 |
import org.gvsig.fmap.data.DataStore; |
10 |
import org.gvsig.fmap.data.DataStoreParameters; |
11 |
import org.gvsig.fmap.data.exceptions.InitializeException; |
12 |
import org.gvsig.fmap.data.feature.FeatureStore; |
13 |
import org.gvsig.fmap.data.impl.DefaultDataManager; |
14 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
15 |
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer; |
16 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
17 |
|
18 |
|
19 |
/**
|
20 |
* Crea un adaptador del driver que se le pasa como par?metro en los m?todos
|
21 |
* createLayer. Si hay memoria suficiente se crea un FLyrMemory que pasa todas
|
22 |
* las features del driver a memoria
|
23 |
*/
|
24 |
public class LayerFactory { |
25 |
private static Logger logger = Logger.getLogger(LayerFactory.class |
26 |
.getName()); |
27 |
|
28 |
private static LayerFactory instance = null; |
29 |
|
30 |
|
31 |
|
32 |
public static LayerFactory getInstance() { |
33 |
if (instance == null) { |
34 |
instance = new LayerFactory();
|
35 |
} |
36 |
return instance;
|
37 |
} |
38 |
|
39 |
/**
|
40 |
* Guarda registro de que clase de capa debe usar para un determinado Store
|
41 |
*
|
42 |
* como clave usamos el nombre de registro del dataStore
|
43 |
*/
|
44 |
private Map layersToUseForStore = new HashMap(); |
45 |
|
46 |
/**
|
47 |
* Registra que clase tiene que usar para un {@link DataStore} determinado. <br>
|
48 |
* Por defecto, si el
|
49 |
*
|
50 |
*
|
51 |
* @param dataStoreName
|
52 |
* Nombre de registro del {@link DataStore} dentro del
|
53 |
* {@link DefaultDataManager}
|
54 |
* @param layerClassToUse
|
55 |
* clase que implementa {@link SingleLayer}
|
56 |
* @return
|
57 |
*/
|
58 |
|
59 |
public boolean registerLayerToUseForStore(String dataStoreName, |
60 |
Class layerClassToUse) {
|
61 |
|
62 |
DataManager dm = DefaultDataManager.getManager(); |
63 |
DataStoreParameters dsparams; |
64 |
try {
|
65 |
dsparams = dm |
66 |
.createDataStoreParameters(dataStoreName); |
67 |
} catch (InitializeException e) {
|
68 |
return false; |
69 |
} |
70 |
if (!layerClassToUse.isAssignableFrom(SingleLayer.class)) {
|
71 |
return false; |
72 |
} |
73 |
this.layersToUseForStore.put(dsparams.getDataStoreName(),
|
74 |
layerClassToUse); |
75 |
|
76 |
return true; |
77 |
} |
78 |
|
79 |
private Class getLayerClassFor(DataStoreParameters params) { |
80 |
Class result = (Class) this.layersToUseForStore.get(params |
81 |
.getDataStoreName()); |
82 |
return result;
|
83 |
|
84 |
} |
85 |
|
86 |
|
87 |
|
88 |
/*
|
89 |
* TODO Documentation
|
90 |
*
|
91 |
* @param layerName Nombre de la capa. @param driverName Nombre del driver.
|
92 |
*
|
93 |
* @param f fichero. @param proj Proyecci?n.
|
94 |
*
|
95 |
* @return FLayer. @throws DriverException
|
96 |
*
|
97 |
* @throws DriverException @throws DriverIOException
|
98 |
*/
|
99 |
public FLayer createLayer(String layerName, |
100 |
DataStoreParameters storeParameters, |
101 |
IProjection proj) throws LoadLayerException {
|
102 |
// Se obtiene el driver que lee
|
103 |
try{
|
104 |
DataManager dataManager=DefaultDataManager.getManager(); |
105 |
DataStore dataStore=dataManager.createDataStore(storeParameters); |
106 |
Class layerClass = this.getLayerClassFor(storeParameters); |
107 |
if (layerClass == null) { |
108 |
if (dataStore instanceof FeatureStore) { |
109 |
layerClass = FLyrVect.class; |
110 |
} else {
|
111 |
throw new LoadLayerException("No_layer_class_to_use", |
112 |
new Exception()); |
113 |
} |
114 |
|
115 |
} |
116 |
FLayer layer; |
117 |
try {
|
118 |
layer = (FLayer) layerClass.newInstance(); |
119 |
} catch (InstantiationException e) { |
120 |
throw new LoadLayerException(layerName, e); |
121 |
} catch (IllegalAccessException e) { |
122 |
throw new LoadLayerException(layerName, e); |
123 |
} |
124 |
|
125 |
((SingleLayer) layer).setDataStore(dataStore); |
126 |
layer.setProjection(proj); |
127 |
layer.setName(layerName); |
128 |
|
129 |
|
130 |
return layer;
|
131 |
} catch (InitializeException e1) {
|
132 |
throw new LoadLayerException(layerName,e1); |
133 |
} |
134 |
} |
135 |
|
136 |
} |