svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / layers / LayerFactory.java @ 43020
History | View | Annotate | Download (6.32 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.mapcontext.layers; |
25 |
|
26 |
import java.util.HashMap; |
27 |
import java.util.Iterator; |
28 |
import java.util.Map; |
29 |
import java.util.Map.Entry; |
30 |
|
31 |
import org.cresques.cts.IProjection; |
32 |
import org.gvsig.fmap.dal.DALLocator; |
33 |
import org.gvsig.fmap.dal.DataManager; |
34 |
import org.gvsig.fmap.dal.DataStore; |
35 |
import org.gvsig.fmap.dal.DataStoreParameters; |
36 |
import org.gvsig.fmap.dal.exception.InitializeException; |
37 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
38 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
39 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
40 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
41 |
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer; |
42 |
|
43 |
/**
|
44 |
* Crea un adaptador del driver que se le pasa como par?metro en los m?todos
|
45 |
* createLayer. Si hay memoria suficiente se crea un FLyrMemory que pasa todas
|
46 |
* las features del driver a memoria
|
47 |
*/
|
48 |
public class LayerFactory { |
49 |
// final static private Logger logger = LoggerFactory.getLogger(LayerFactory.class);
|
50 |
|
51 |
private static LayerFactory instance = null; |
52 |
|
53 |
|
54 |
|
55 |
public static LayerFactory getInstance() { |
56 |
if (instance == null) { |
57 |
instance = new LayerFactory();
|
58 |
} |
59 |
return instance;
|
60 |
} |
61 |
|
62 |
/**
|
63 |
* Guarda registro de que clase de capa debe usar para un determinado Store
|
64 |
*
|
65 |
* como clave usamos el nombre de registro del dataStore
|
66 |
*/
|
67 |
private Map layersToUseForStore = new HashMap(); |
68 |
|
69 |
/**
|
70 |
* Registra que clase tiene que usar para un {@link DataStore} determinado. <br>
|
71 |
* Por defecto, si el
|
72 |
*
|
73 |
*
|
74 |
* @param dataStoreName
|
75 |
* Nombre de registro del {@link DataStore} dentro del
|
76 |
* {@link DataManager}
|
77 |
* @param layerClassToUse
|
78 |
* clase que implementa {@link SingleLayer}
|
79 |
* @return
|
80 |
*/
|
81 |
|
82 |
public boolean registerLayerToUseForStore(String dataStoreName, |
83 |
Class layerClassToUse) {
|
84 |
|
85 |
DataManager dm = DALLocator.getDataManager(); |
86 |
DataStoreParameters dsparams; |
87 |
try {
|
88 |
dsparams = dm.createStoreParameters(dataStoreName); |
89 |
} catch (InitializeException e) {
|
90 |
e.printStackTrace(); |
91 |
return false; |
92 |
} catch (ProviderNotRegisteredException e) {
|
93 |
e.printStackTrace(); |
94 |
return false; |
95 |
} |
96 |
if (!layerClassToUse.isAssignableFrom(SingleLayer.class)) {
|
97 |
return false; |
98 |
} |
99 |
this.layersToUseForStore.put(dsparams.getDataStoreName(),
|
100 |
layerClassToUse); |
101 |
|
102 |
return true; |
103 |
} |
104 |
|
105 |
public boolean registerLayerToUseForStore(Class storeClass, |
106 |
Class layerClassToUse) {
|
107 |
|
108 |
// DataManager dm = DALLocator.getDataManager();
|
109 |
if (!DataStore.class.isAssignableFrom(storeClass)) {
|
110 |
return false; |
111 |
} |
112 |
|
113 |
if (!SingleLayer.class.isAssignableFrom(layerClassToUse)
|
114 |
|| !FLayer.class.isAssignableFrom(layerClassToUse)) { |
115 |
return false; |
116 |
} |
117 |
this.layersToUseForStore.put(storeClass,
|
118 |
layerClassToUse); |
119 |
|
120 |
return true; |
121 |
} |
122 |
|
123 |
private Class getLayerClassFor(DataStore store) { |
124 |
Class result = (Class) this.layersToUseForStore.get(store.getName()); |
125 |
if (result == null) { |
126 |
Iterator iter = this.layersToUseForStore.entrySet().iterator(); |
127 |
Map.Entry entry;
|
128 |
Class key;
|
129 |
while (iter.hasNext()) {
|
130 |
entry = (Entry) iter.next(); |
131 |
if (entry.getKey() instanceof Class) { |
132 |
key = (Class) entry.getKey();
|
133 |
if (key.isAssignableFrom(store.getClass())) {
|
134 |
result = (Class) entry.getValue();
|
135 |
break;
|
136 |
} |
137 |
} |
138 |
} |
139 |
} |
140 |
return result;
|
141 |
|
142 |
} |
143 |
|
144 |
|
145 |
|
146 |
/**
|
147 |
* @deprecated to be removed in gvSIG 2.1
|
148 |
* @see {@link MapContextManager}.
|
149 |
*/
|
150 |
public FLayer createLayer(String layerName, |
151 |
DataStoreParameters storeParameters) throws LoadLayerException {
|
152 |
// Se obtiene el driver que lee
|
153 |
try{
|
154 |
DataManager dataManager=DALLocator.getDataManager(); |
155 |
DataStore dataStore=dataManager.openStore(storeParameters.getDataStoreName(), storeParameters); |
156 |
return createLayer(layerName, dataStore);
|
157 |
}catch (Exception e) { |
158 |
throw new LoadLayerException(layerName,e); |
159 |
} |
160 |
} |
161 |
|
162 |
/**
|
163 |
* @deprecated to be removed in gvSIG 2.1
|
164 |
* @see {@link MapContextManager}.
|
165 |
*/
|
166 |
public FLayer createLayer(String layerName, DataStore dataStore) throws LoadLayerException{ |
167 |
try{
|
168 |
Class layerClass = this.getLayerClassFor(dataStore); |
169 |
if (layerClass == null) { |
170 |
throw new CantRetrieveLayerByStoreException(layerName,dataStore.getName()); |
171 |
} |
172 |
FLayer layer; |
173 |
try {
|
174 |
layer = (FLayer) layerClass.newInstance(); |
175 |
} catch (InstantiationException e) { |
176 |
throw new LoadLayerException(layerName, e); |
177 |
} catch (IllegalAccessException e) { |
178 |
throw new LoadLayerException(layerName, e); |
179 |
} |
180 |
|
181 |
layer.setName(layerName); |
182 |
((SingleLayer) layer).setDataStore(dataStore); |
183 |
IProjection proj = (IProjection)dataStore.getDynValue(FeatureStore.METADATA_CRS); |
184 |
if (proj != null) { |
185 |
layer.setProjection(proj); |
186 |
} |
187 |
layer.load(); |
188 |
return layer;
|
189 |
} catch (Exception e) { |
190 |
throw new LoadLayerException(layerName,e); |
191 |
} |
192 |
} |
193 |
|
194 |
private class CantRetrieveLayerByStoreException extends LoadLayerException { |
195 |
|
196 |
/**
|
197 |
*
|
198 |
*/
|
199 |
private static final long serialVersionUID = 1442450896900126712L; |
200 |
|
201 |
CantRetrieveLayerByStoreException(String layerName, String storeName) { |
202 |
super(
|
203 |
"Can't retrieve the class leyer of %(layer) to use for store %(store).",
|
204 |
null,
|
205 |
"_Cant_retrieve_the_class_leyer_of_XlayerX_to_use_for_store_XstoreX",
|
206 |
serialVersionUID |
207 |
); |
208 |
setValue("layer", layerName);
|
209 |
setValue("store", storeName);
|
210 |
} |
211 |
} |
212 |
|
213 |
} |