Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / vectorial / FLayerFileVectorial.java @ 30011

History | View | Annotate | Download (7.91 KB)

1
package org.gvsig.fmap.mapcontext.layers.vectorial;
2

    
3
import java.io.File;
4
import java.io.FileNotFoundException;
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.exception.ReadException;
10
import org.gvsig.fmap.dal.feature.FeatureStore;
11
import org.gvsig.fmap.mapcontext.MapContextException;
12
import org.gvsig.fmap.mapcontext.MapContextLocator;
13
import org.gvsig.fmap.mapcontext.exceptions.DriverLayerException;
14
import org.gvsig.fmap.mapcontext.exceptions.FileLayerException;
15
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException;
16
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
17
import org.gvsig.fmap.mapcontext.exceptions.NameLayerException;
18
import org.gvsig.fmap.mapcontext.exceptions.ProjectionLayerException;
19
import org.gvsig.fmap.mapcontext.exceptions.XMLLayerException;
20
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
21
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
22
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
23
import org.gvsig.tools.dynobject.exception.DynMethodException;
24
import org.gvsig.tools.exception.BaseException;
25
import org.gvsig.tools.locator.LocatorException;
26
import org.gvsig.utils.XMLEntity;
27
import org.gvsig.utils.XMLException;
28

    
29

    
30
public class FLayerFileVectorial extends FLyrVect{
31
        private boolean loaded = false;
32
        private File dataFile = null;
33
//        private FeatureStore featureStore = null;
34

    
35
        public FLayerFileVectorial() {
36
                super();
37
        }
38

    
39
        public FLayerFileVectorial(String name, String fileName,String driverName,String projectionName) throws Exception {
40
                super();
41

    
42
                this.setName(name);
43

    
44
                this.setFileName(fileName);
45

    
46
//                this.setDriverByName(driverName);
47

    
48
                this.setProjectionByName(projectionName);
49
        }
50

    
51
        /* Esto deberia ir en el FLyrDefault */
52
        public void setProjectionByName(String projectionName) throws Exception{
53
                IProjection proj = CRSFactory.getCRS(projectionName);
54
                if (proj == null) {
55
                        throw new Exception("No se ha encontrado la proyeccion: "+ projectionName);
56
                }
57
                this.setProjection(proj);
58

    
59
        }
60

    
61
        public void setFileName(String filePath) throws FileNotFoundException{
62
                if (dataFile != null) {
63
                        //TODO: que excepcion lanzar???
64
                        return;
65
                }
66
                File file = new File(filePath);
67
                if (!file.exists()) {
68
                        throw new FileNotFoundException(filePath);
69
                }
70
                this.dataFile = file;
71
        }
72

    
73
        public void setFile(File file) throws FileNotFoundException {
74
                if (dataFile != null) {
75
                        //TODO: que excepcion lanzar???
76
                        return;
77
                }
78
                if (!file.exists()) {
79
                        throw new FileNotFoundException(file.getAbsolutePath());
80
                }
81
                this.dataFile = new File(file.getAbsolutePath());
82
        }
83

    
84
        public String getFileName() {
85
                if (this.dataFile == null) {
86
                        return null;
87
                }
88
                return this.dataFile.getAbsolutePath();
89
        }
90

    
91

    
92
        public void setFeatureStore(FeatureStore fs) throws LoadLayerException {
93
                this.setDataStore(fs);
94
        }
95

    
96
//        public void setDriverByName(String driverName) throws DriverLoadException {
97
//                this.setDriver(
98
//                  (VectorialFileDriver)LayerFactory.getDM().getDriver(driverName)
99
//                );
100
//        }
101

    
102
//        public VectorialFileDriver getDriver() {
103
//                return this.fileDriver;
104
//        }
105

    
106
        /* FIXME: esto tendria que tener declarado un throws de algo*/
107
        public void wakeUp() throws LoadLayerException {
108
                if (!loaded) {
109
                        this.load();
110
                }
111

    
112
        }
113

    
114

    
115
        public void load() throws LoadLayerException {
116
                if (this.dataFile == null) {
117
                        this.setAvailable(false);
118
                        throw new FileLayerException(getName(),null);
119
                }
120
                if (this.getName() == null || this.getName().length() == 0) {
121
                        this.setAvailable(false);
122
                        throw new NameLayerException(getName(),null);
123
                }
124
                try {
125
                        if (this.getFeatureStore() == null) {
126
                                this.setAvailable(false);
127
                                throw new DriverLayerException(getName(),null);
128
                        }
129
                } catch (ReadException e2) {
130
                        throw new LoadLayerException(getName(),e2);
131
                } catch (DriverLayerException e2) {
132
                        throw new LoadLayerException(getName(),e2);
133
                }
134
                if (this.getProjection() == null) {
135
                        this.setAvailable(false);
136
                        throw new ProjectionLayerException(getName(),null);
137
                }
138
                try {
139
                        this.setDataStore(DALLocator.getDataManager().createStore(getFeatureStore().getParameters()));
140
                } catch (Exception e) {
141
                        throw new LoadLayerException(getName(),e);
142
                }
143

    
144
                        try {
145
                                this.putLoadSelection();
146
                                this.putLoadLegend();
147
                                this.initializeLegendDefault();
148
                        } catch (Exception e) {
149
                                this.setAvailable(false);
150
                                throw new LegendLayerException(getName(),e);
151
                        }
152
                this.cleanLoadOptions();
153
                this.loaded = true;
154
        }
155

    
156
        /* Esto deberia ir en FLyrVect */
157
        private void initializeLegendDefault() throws LegendLayerException, ReadException {
158
                if (this.getLegend() == null) {
159
//                        this.getFeatureStore().getMetadata().
160
                        Object obj=null;
161
                        try {
162
                                // FIXME arreglar esto
163
                                obj = this.getFeatureStore().getDynValue(
164
                                                "WithDefaultLegend");
165
                        } catch (BaseException e1) {
166
                                throw new ReadException(getName(), e1);
167
                        }
168
            if (obj!=null && ((Boolean)obj).booleanValue()) {
169
                    ILegend legend = null;
170
                                        try {
171
                                                legend = (ILegend) getFeatureStore().invokeDynMethod(
172
                                                        "defaultLegend", null);
173
                                        } catch (DynMethodException e) {
174
                                                throw new LegendLayerException(getName(),e);
175
                                        }
176

    
177
//                    WithDefaultLegend aux = (WithDefaultLegend) this.getRecordset().getDriver();
178
                this.setLegend((IVectorLegend)legend);
179

    
180

    
181

    
182
            } else {
183
                            IVectorLegend legend = MapContextLocator.getMapContextManager()
184
                                                        .createDefaultVectorLegend(getShapeType());
185
                                this.setLegend(legend);
186
            }
187
            obj = null;
188
//            try {
189
//                    // FIXME Arregla esto
190
//                                obj = this.getFeatureStore().getMetadata().getDynValue(
191
//                                                "LabelingStrategy");
192
//                        } catch (BaseException e) {
193
//                                throw new ReadException(getName(),e);
194
//                        }
195
            if (obj!=null && ((Boolean)obj).booleanValue()){
196
            ILabelingStrategy labeler=null;
197
                        try {
198
                                labeler = (ILabelingStrategy) getFeatureStore()
199
                                                        .invokeDynMethod("labelingStrategy", null);
200
                        } catch (DynMethodException e) {
201
                                throw new LegendLayerException(getName(),e);
202
                        }
203
            labeler.setLayer(this);
204

    
205
            this.setLabelingStrategy(labeler);
206
            }
207

    
208
                }
209

    
210

    
211

    
212

    
213
//                if (this.getLegend() == null) {
214
//            if (this.getRecordset().getDriver() instanceof WithDefaultLegend) {
215
//                WithDefaultLegend aux = (WithDefaultLegend) this.getRecordset().getDriver();
216
//                this.setLegend((IVectorLegend) aux.getDefaultLegend());
217
//
218
//                ILabelingStrategy labeler = aux.getDefaultLabelingStrategy();
219
//                if (labeler instanceof AttrInTableLabelingStrategy) {
220
//                        ((AttrInTableLabelingStrategy) labeler).setLayer(this);
221
//                }
222
//
223
//                this.setLabelingStrategy(labeler);
224
//            } else {
225
//                this.setLegend(LegendFactory.createSingleSymbolLegend(
226
//                        this.getShapeType()));
227
//            }
228
//                }
229
        }
230

    
231
        public void setXMLEntity(XMLEntity xml) throws XMLException {
232
        IProjection proj = null;
233
        if (xml.contains("proj")) {
234
            proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
235
        }
236
        else
237
        {
238
            proj = this.getMapContext().getViewPort().getProjection();
239
        }
240
                this.setName(xml.getName());
241
                this.setProjection(proj);
242
//                try {
243
//                        this.setDriver(
244
//                                (VectorialFileDriver)LayerFactory.getDM().getDriver(
245
//                                        xml.getStringProperty("driverName")
246
//                                )
247
//                        );
248
//                } catch (DriverLoadException e) {
249
//                        throw new XMLException(e);
250
//                } catch (ClassCastException e) {
251
//                        throw new XMLException(e);
252
//                }
253
                try {
254
                        this.setFileName(xml.getStringProperty("file"));
255
                } catch (FileNotFoundException e) {
256
                        throw new XMLLayerException(getClassName(),e);
257
                }
258

    
259
                super.setXMLEntityNew(xml);
260
        }
261
}