Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLayerFileVectorial.java @ 33261

History | View | Annotate | Download (6.06 KB)

1
package com.iver.cit.gvsig.fmap.layers;
2

    
3
import java.io.File;
4
import java.io.FileNotFoundException;
5

    
6
import org.cresques.cts.IProjection;
7
import org.gvsig.tools.file.PathGenerator;
8

    
9
import com.hardcode.driverManager.DriverLoadException;
10
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
11
import com.iver.cit.gvsig.exceptions.layers.DriverLayerException;
12
import com.iver.cit.gvsig.exceptions.layers.FileLayerException;
13
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException;
14
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
15
import com.iver.cit.gvsig.exceptions.layers.NameLayerException;
16
import com.iver.cit.gvsig.exceptions.layers.ProjectionLayerException;
17
import com.iver.cit.gvsig.exceptions.layers.XMLLayerException;
18
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
19
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
20
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
21
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
22
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
23
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.AttrInTableLabelingStrategy;
24
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.ILabelingStrategy;
25
import com.iver.utiles.XMLEntity;
26

    
27
public class FLayerFileVectorial extends FLyrVect{
28
        private boolean loaded = false;
29
        private File dataFile = null;
30
        private VectorialFileDriver fileDriver = null;
31
        private static PathGenerator pathGenerator=PathGenerator.getInstance();
32
        
33
        public FLayerFileVectorial() {
34
                super();
35
        }
36

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

    
40
                this.setName(name);
41

    
42
                this.setFileName(fileName);
43

    
44
                this.setDriverByName(driverName);
45

    
46
                this.setProjectionByName(projectionName);
47
        }
48

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

    
57
        }
58

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

    
72
        public void setFile(File file) throws FileNotFoundException {
73
                String path = pathGenerator.getPath(file.getAbsolutePath());
74
                if (dataFile != null) {
75
                        //TODO: que excepcion lanzar???
76
                        return;
77
                }
78
                if (!file.exists()) {
79
                        throw new FileNotFoundException(path);
80
                }
81
                this.dataFile = new File(path);
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 setDriver(VectorialFileDriver driver) {
93
                this.fileDriver = driver;
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
                if (this.fileDriver == null) {
125
                        this.setAvailable(false);
126
                        throw new DriverLayerException(getName(),null);
127
                }
128
                if (this.getProjection() == null) {
129
                        this.setAvailable(false);
130
                        throw new ProjectionLayerException(getName(),null);
131
                }
132

    
133
                        VectorialFileAdapter adapter = new VectorialFileAdapter(this.dataFile);
134
                        adapter.setDriver(this.fileDriver);
135

    
136
                        this.setSource(adapter);
137

    
138
                        try {
139
                                this.putLoadSelection();
140
                                this.putLoadLegend();
141
                                this.initializeLegendDefault();
142
                        } catch (LegendLayerException e) {
143
                                this.setAvailable(false);
144
                                throw new LegendLayerException(getName(),e);
145
                        } catch (XMLException e) {
146
                                this.setAvailable(false);
147
                                throw new XMLLayerException(getName(),e);
148
                        } catch (ReadDriverException e) {
149
                                this.setAvailable(false);
150
                                throw new LoadLayerException(getName(),e);
151
                        }
152
                this.cleanLoadOptions();
153
                this.loaded = true;
154
        }
155

    
156
        /* Esto deberia ir en FLyrVect */
157
        private void initializeLegendDefault() throws LegendLayerException, ReadDriverException {
158
                if (this.getLegend() == null) {
159
            if (this.getRecordset().getDriver() instanceof WithDefaultLegend) {
160
                WithDefaultLegend aux = (WithDefaultLegend) this.getRecordset().getDriver();
161
                this.setLegend((IVectorLegend) aux.getDefaultLegend());
162

    
163
                ILabelingStrategy labeler = aux.getDefaultLabelingStrategy();
164
                if (labeler instanceof AttrInTableLabelingStrategy) {
165
                        ((AttrInTableLabelingStrategy) labeler).setLayer(this);
166
                }
167

    
168
                this.setLabelingStrategy(labeler);
169
            } else {
170
                this.setLegend(LegendFactory.createSingleSymbolLegend(
171
                        this.getShapeType()));
172
            }
173
                }
174
        }
175

    
176
        public void setXMLEntity(XMLEntity xml) throws XMLException {
177
        IProjection proj = null;
178
        if (xml.contains("proj")) {
179
            proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
180
        }
181
        else
182
        {
183
            proj = this.getMapContext().getViewPort().getProjection();
184
        }
185
                this.setName(xml.getName());
186
                this.setProjection(proj);
187
                try {
188
                        this.setDriver(
189
                                (VectorialFileDriver)LayerFactory.getDM().getDriver(
190
                                        xml.getStringProperty("driverName")
191
                                )
192
                        );
193
                } catch (DriverLoadException e) {
194
                        throw new XMLException(e);
195
                } catch (ClassCastException e) {
196
                        throw new XMLException(e);
197
                }
198
                try {
199
                        this.setFileName(xml.getStringProperty("file"));
200
                } catch (FileNotFoundException e) {
201
                        throw new XMLException(e);
202
                }
203

    
204
                super.setXMLEntityNew(xml);
205
        }
206
}