root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / LayerFactory.java @ 33030
History | View | Annotate | Download (5.43 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 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences; |
20 |
import org.slf4j.Logger; |
21 |
import org.slf4j.LoggerFactory; |
22 |
|
23 |
/**
|
24 |
* Crea un adaptador del driver que se le pasa como par?metro en los m?todos
|
25 |
* createLayer. Si hay memoria suficiente se crea un FLyrMemory que pasa todas
|
26 |
* las features del driver a memoria
|
27 |
*/
|
28 |
public class LayerFactory { |
29 |
final static private Logger logger = LoggerFactory.getLogger(LayerFactory.class); |
30 |
|
31 |
private static LayerFactory instance = null; |
32 |
|
33 |
|
34 |
|
35 |
public static LayerFactory getInstance() { |
36 |
if (instance == null) { |
37 |
instance = new LayerFactory();
|
38 |
} |
39 |
return instance;
|
40 |
} |
41 |
|
42 |
/**
|
43 |
* Guarda registro de que clase de capa debe usar para un determinado Store
|
44 |
*
|
45 |
* como clave usamos el nombre de registro del dataStore
|
46 |
*/
|
47 |
private Map layersToUseForStore = new HashMap(); |
48 |
|
49 |
/**
|
50 |
* Registra que clase tiene que usar para un {@link DataStore} determinado. <br>
|
51 |
* Por defecto, si el
|
52 |
*
|
53 |
*
|
54 |
* @param dataStoreName
|
55 |
* Nombre de registro del {@link DataStore} dentro del
|
56 |
* {@link DataManager}
|
57 |
* @param layerClassToUse
|
58 |
* clase que implementa {@link SingleLayer}
|
59 |
* @return
|
60 |
*/
|
61 |
|
62 |
public boolean registerLayerToUseForStore(String dataStoreName, |
63 |
Class layerClassToUse) {
|
64 |
|
65 |
DataManager dm = DALLocator.getDataManager(); |
66 |
DataStoreParameters dsparams; |
67 |
try {
|
68 |
dsparams = dm.createStoreParameters(dataStoreName); |
69 |
} catch (InitializeException e) {
|
70 |
e.printStackTrace(); |
71 |
return false; |
72 |
} catch (ProviderNotRegisteredException e) {
|
73 |
e.printStackTrace(); |
74 |
return false; |
75 |
} |
76 |
if (!layerClassToUse.isAssignableFrom(SingleLayer.class)) {
|
77 |
return false; |
78 |
} |
79 |
this.layersToUseForStore.put(dsparams.getDataStoreName(),
|
80 |
layerClassToUse); |
81 |
|
82 |
return true; |
83 |
} |
84 |
|
85 |
public boolean registerLayerToUseForStore(Class storeClass, |
86 |
Class layerClassToUse) {
|
87 |
|
88 |
DataManager dm = DALLocator.getDataManager(); |
89 |
if (!DataStore.class.isAssignableFrom(storeClass)) {
|
90 |
return false; |
91 |
} |
92 |
|
93 |
if (!SingleLayer.class.isAssignableFrom(layerClassToUse)
|
94 |
|| !FLayer.class.isAssignableFrom(layerClassToUse)) { |
95 |
return false; |
96 |
} |
97 |
this.layersToUseForStore.put(storeClass,
|
98 |
layerClassToUse); |
99 |
|
100 |
return true; |
101 |
} |
102 |
|
103 |
private Class getLayerClassFor(DataStore store) { |
104 |
Class result = (Class) this.layersToUseForStore.get(store.getName()); |
105 |
if (result == null) { |
106 |
Iterator iter = this.layersToUseForStore.entrySet().iterator(); |
107 |
Map.Entry entry;
|
108 |
Class key;
|
109 |
while (iter.hasNext()) {
|
110 |
entry = (Entry) iter.next(); |
111 |
if (entry.getKey() instanceof Class) { |
112 |
key = (Class) entry.getKey();
|
113 |
if (key.isAssignableFrom(store.getClass())) {
|
114 |
result = (Class) entry.getValue();
|
115 |
break;
|
116 |
} |
117 |
} |
118 |
} |
119 |
} |
120 |
return result;
|
121 |
|
122 |
} |
123 |
|
124 |
|
125 |
|
126 |
/**
|
127 |
* @deprecated to be removed in gvSIG 2.1
|
128 |
* @see {@link MapContextManager}.
|
129 |
*/
|
130 |
public FLayer createLayer(String layerName, |
131 |
DataStoreParameters storeParameters) throws LoadLayerException {
|
132 |
// Se obtiene el driver que lee
|
133 |
try{
|
134 |
DataManager dataManager=DALLocator.getDataManager(); |
135 |
DataStore dataStore=dataManager.createStore(storeParameters); |
136 |
return createLayer(layerName, dataStore);
|
137 |
}catch (Exception e) { |
138 |
throw new LoadLayerException(layerName,e); |
139 |
} |
140 |
} |
141 |
|
142 |
/**
|
143 |
* @deprecated to be removed in gvSIG 2.1
|
144 |
* @see {@link MapContextManager}.
|
145 |
*/
|
146 |
public FLayer createLayer(String layerName, DataStore dataStore) throws LoadLayerException{ |
147 |
try{
|
148 |
Class layerClass = this.getLayerClassFor(dataStore); |
149 |
if (layerClass == null) { |
150 |
throw new CantRetrieveLayerByStoreException(layerName,dataStore.getName()); |
151 |
} |
152 |
FLayer layer; |
153 |
try {
|
154 |
layer = (FLayer) layerClass.newInstance(); |
155 |
} catch (InstantiationException e) { |
156 |
throw new LoadLayerException(layerName, e); |
157 |
} catch (IllegalAccessException e) { |
158 |
throw new LoadLayerException(layerName, e); |
159 |
} |
160 |
|
161 |
layer.setName(layerName); |
162 |
((SingleLayer) layer).setDataStore(dataStore); |
163 |
IProjection proj = (IProjection)dataStore.getDynValue(FeatureStore.METADATA_CRS); |
164 |
if (proj != null) { |
165 |
layer.setProjection(proj); |
166 |
} |
167 |
return layer;
|
168 |
} catch (Exception e) { |
169 |
throw new LoadLayerException(layerName,e); |
170 |
} |
171 |
} |
172 |
|
173 |
private class CantRetrieveLayerByStoreException extends LoadLayerException { |
174 |
|
175 |
/**
|
176 |
*
|
177 |
*/
|
178 |
private static final long serialVersionUID = 1442450896900126712L; |
179 |
|
180 |
CantRetrieveLayerByStoreException(String layerName, String storeName) { |
181 |
super(
|
182 |
"Can't retrieve the class leyer of %(layer) to use for store %(store).",
|
183 |
null,
|
184 |
"_Cant_retrieve_the_class_leyer_of_XlayerX_to_use_for_store_XstoreX",
|
185 |
serialVersionUID |
186 |
); |
187 |
setValue("layer", layerName);
|
188 |
setValue("store", storeName);
|
189 |
} |
190 |
} |
191 |
|
192 |
} |