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