root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / LayerFactory.java @ 38581
History | View | Annotate | Download (5.33 KB)
1 |
package org.gvsig.fmap.mapcontext.layers; |
---|---|
2 |
|
3 |
import java.util.HashMap; |
4 |
import java.util.Iterator; |
5 |
import java.util.Map; |
6 |
import java.util.Map.Entry; |
7 |
|
8 |
import org.cresques.cts.IProjection; |
9 |
import org.gvsig.fmap.dal.DALLocator; |
10 |
import org.gvsig.fmap.dal.DataManager; |
11 |
import org.gvsig.fmap.dal.DataStore; |
12 |
import org.gvsig.fmap.dal.DataStoreParameters; |
13 |
import org.gvsig.fmap.dal.exception.InitializeException; |
14 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
15 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
16 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
17 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
18 |
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer; |
19 |
|
20 |
/**
|
21 |
* Crea un adaptador del driver que se le pasa como par?metro en los m?todos
|
22 |
* createLayer. Si hay memoria suficiente se crea un FLyrMemory que pasa todas
|
23 |
* las features del driver a memoria
|
24 |
*/
|
25 |
public class LayerFactory { |
26 |
// final static private Logger logger = LoggerFactory.getLogger(LayerFactory.class);
|
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 DataManager}
|
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 = DALLocator.getDataManager(); |
63 |
DataStoreParameters dsparams; |
64 |
try {
|
65 |
dsparams = dm.createStoreParameters(dataStoreName); |
66 |
} catch (InitializeException e) {
|
67 |
e.printStackTrace(); |
68 |
return false; |
69 |
} catch (ProviderNotRegisteredException e) {
|
70 |
e.printStackTrace(); |
71 |
return false; |
72 |
} |
73 |
if (!layerClassToUse.isAssignableFrom(SingleLayer.class)) {
|
74 |
return false; |
75 |
} |
76 |
this.layersToUseForStore.put(dsparams.getDataStoreName(),
|
77 |
layerClassToUse); |
78 |
|
79 |
return true; |
80 |
} |
81 |
|
82 |
public boolean registerLayerToUseForStore(Class storeClass, |
83 |
Class layerClassToUse) {
|
84 |
|
85 |
// DataManager dm = DALLocator.getDataManager();
|
86 |
if (!DataStore.class.isAssignableFrom(storeClass)) {
|
87 |
return false; |
88 |
} |
89 |
|
90 |
if (!SingleLayer.class.isAssignableFrom(layerClassToUse)
|
91 |
|| !FLayer.class.isAssignableFrom(layerClassToUse)) { |
92 |
return false; |
93 |
} |
94 |
this.layersToUseForStore.put(storeClass,
|
95 |
layerClassToUse); |
96 |
|
97 |
return true; |
98 |
} |
99 |
|
100 |
private Class getLayerClassFor(DataStore store) { |
101 |
Class result = (Class) this.layersToUseForStore.get(store.getName()); |
102 |
if (result == null) { |
103 |
Iterator iter = this.layersToUseForStore.entrySet().iterator(); |
104 |
Map.Entry entry;
|
105 |
Class key;
|
106 |
while (iter.hasNext()) {
|
107 |
entry = (Entry) iter.next(); |
108 |
if (entry.getKey() instanceof Class) { |
109 |
key = (Class) entry.getKey();
|
110 |
if (key.isAssignableFrom(store.getClass())) {
|
111 |
result = (Class) entry.getValue();
|
112 |
break;
|
113 |
} |
114 |
} |
115 |
} |
116 |
} |
117 |
return result;
|
118 |
|
119 |
} |
120 |
|
121 |
|
122 |
|
123 |
/**
|
124 |
* @deprecated to be removed in gvSIG 2.1
|
125 |
* @see {@link MapContextManager}.
|
126 |
*/
|
127 |
public FLayer createLayer(String layerName, |
128 |
DataStoreParameters storeParameters) throws LoadLayerException {
|
129 |
// Se obtiene el driver que lee
|
130 |
try{
|
131 |
DataManager dataManager=DALLocator.getDataManager(); |
132 |
DataStore dataStore=dataManager.createStore(storeParameters); |
133 |
return createLayer(layerName, dataStore);
|
134 |
}catch (Exception e) { |
135 |
throw new LoadLayerException(layerName,e); |
136 |
} |
137 |
} |
138 |
|
139 |
/**
|
140 |
* @deprecated to be removed in gvSIG 2.1
|
141 |
* @see {@link MapContextManager}.
|
142 |
*/
|
143 |
public FLayer createLayer(String layerName, DataStore dataStore) throws LoadLayerException{ |
144 |
try{
|
145 |
Class layerClass = this.getLayerClassFor(dataStore); |
146 |
if (layerClass == null) { |
147 |
throw new CantRetrieveLayerByStoreException(layerName,dataStore.getName()); |
148 |
} |
149 |
FLayer layer; |
150 |
try {
|
151 |
layer = (FLayer) layerClass.newInstance(); |
152 |
} catch (InstantiationException e) { |
153 |
throw new LoadLayerException(layerName, e); |
154 |
} catch (IllegalAccessException e) { |
155 |
throw new LoadLayerException(layerName, e); |
156 |
} |
157 |
|
158 |
layer.setName(layerName); |
159 |
((SingleLayer) layer).setDataStore(dataStore); |
160 |
IProjection proj = (IProjection)dataStore.getDynValue(FeatureStore.METADATA_CRS); |
161 |
if (proj != null) { |
162 |
layer.setProjection(proj); |
163 |
} |
164 |
layer.load(); |
165 |
return layer;
|
166 |
} catch (Exception e) { |
167 |
throw new LoadLayerException(layerName,e); |
168 |
} |
169 |
} |
170 |
|
171 |
private class CantRetrieveLayerByStoreException extends LoadLayerException { |
172 |
|
173 |
/**
|
174 |
*
|
175 |
*/
|
176 |
private static final long serialVersionUID = 1442450896900126712L; |
177 |
|
178 |
CantRetrieveLayerByStoreException(String layerName, String storeName) { |
179 |
super(
|
180 |
"Can't retrieve the class leyer of %(layer) to use for store %(store).",
|
181 |
null,
|
182 |
"_Cant_retrieve_the_class_leyer_of_XlayerX_to_use_for_store_XstoreX",
|
183 |
serialVersionUID |
184 |
); |
185 |
setValue("layer", layerName);
|
186 |
setValue("store", storeName);
|
187 |
} |
188 |
} |
189 |
|
190 |
} |