Statistics
| Revision:

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 21200 vcaballero
package org.gvsig.fmap.mapcontext.layers;
2
3 22207 jmvivo
import java.util.HashMap;
4
import java.util.Map;
5
6 21200 vcaballero
import org.apache.log4j.Logger;
7
import org.cresques.cts.IProjection;
8 21911 jmvivo
import org.gvsig.fmap.data.DataManager;
9
import org.gvsig.fmap.data.DataStore;
10
import org.gvsig.fmap.data.DataStoreParameters;
11 23750 jjdelcerro
import org.gvsig.fmap.data.exceptions.InitializeException;
12 22360 jmvivo
import org.gvsig.fmap.data.feature.FeatureStore;
13 23750 jjdelcerro
import org.gvsig.fmap.data.impl.DefaultDataManager;
14 21200 vcaballero
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
15 22207 jmvivo
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
16 21200 vcaballero
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 21659 vcaballero
28 22207 jmvivo
        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 23750 jjdelcerro
         *            {@link DefaultDataManager}
54 22207 jmvivo
         * @param layerClassToUse
55
         *            clase que implementa {@link SingleLayer}
56
         * @return
57
         */
58
59
        public boolean registerLayerToUseForStore(String dataStoreName,
60
                        Class layerClassToUse) {
61
62 23750 jjdelcerro
                DataManager dm = DefaultDataManager.getManager();
63 22207 jmvivo
                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 21200 vcaballero
        /*
89 22207 jmvivo
         * TODO Documentation
90 21200 vcaballero
         *
91
         * @param layerName Nombre de la capa. @param driverName Nombre del driver.
92 22207 jmvivo
         *
93 21200 vcaballero
         * @param f fichero. @param proj Proyecci?n.
94
         *
95
         * @return FLayer. @throws DriverException
96
         *
97
         * @throws DriverException @throws DriverIOException
98
         */
99 22207 jmvivo
        public FLayer createLayer(String layerName,
100
                        DataStoreParameters storeParameters,
101 21200 vcaballero
                        IProjection proj) throws LoadLayerException  {
102
                // Se obtiene el driver que lee
103
                try{
104 23750 jjdelcerro
                        DataManager dataManager=DefaultDataManager.getManager();
105 21200 vcaballero
                        DataStore dataStore=dataManager.createDataStore(storeParameters);
106 22207 jmvivo
                        Class layerClass = this.getLayerClassFor(storeParameters);
107
                        if (layerClass == null) {
108
                                if (dataStore instanceof FeatureStore) {
109
                                        layerClass = FLyrVect.class;
110 21673 jmvivo
                                } else {
111 22207 jmvivo
                                        throw new LoadLayerException("No_layer_class_to_use",
112
                                                        new Exception());
113 21673 jmvivo
                                }
114 21200 vcaballero
115
                        }
116 22207 jmvivo
                        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 21200 vcaballero
125 22207 jmvivo
                        ((SingleLayer) layer).setDataStore(dataStore);
126
                        layer.setProjection(proj);
127
                        layer.setName(layerName);
128 21200 vcaballero
129 22034 vcaballero
130 22207 jmvivo
                        return layer;
131 22034 vcaballero
                } catch (InitializeException e1) {
132
                        throw new LoadLayerException(layerName,e1);
133
                }
134
        }
135
136 21200 vcaballero
}