Statistics
| Revision:

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

History | View | Annotate | Download (5.85 KB)

1 6763 jmvivo
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
8
import com.hardcode.driverManager.DriverLoadException;
9 10627 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
10
import com.iver.cit.gvsig.exceptions.layers.DriverLayerException;
11
import com.iver.cit.gvsig.exceptions.layers.FileLayerException;
12
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException;
13
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
14
import com.iver.cit.gvsig.exceptions.layers.NameLayerException;
15
import com.iver.cit.gvsig.exceptions.layers.ProjectionLayerException;
16
import com.iver.cit.gvsig.exceptions.layers.XMLLayerException;
17 6763 jmvivo
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
18
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
19
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
20
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
21
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
22 10813 jaume
import com.iver.cit.gvsig.fmap.rendering.styling.AttrInTableLabeling;
23
import com.iver.cit.gvsig.fmap.rendering.styling.ILabelingStrategy;
24 6763 jmvivo
import com.iver.utiles.XMLEntity;
25
26
public class FLayerFileVectorial extends FLyrVect{
27
        private boolean loaded = false;
28
        private File dataFile = null;
29
        private VectorialFileDriver fileDriver = null;
30 10627 caballero
31 6763 jmvivo
        public FLayerFileVectorial() {
32
                super();
33
        }
34 10627 caballero
35 6763 jmvivo
        public FLayerFileVectorial(String name, String fileName,String driverName,String projectionName) throws Exception {
36
                super();
37 10627 caballero
38 6763 jmvivo
                this.setName(name);
39 10627 caballero
40 6763 jmvivo
                this.setFileName(fileName);
41 10627 caballero
42 6763 jmvivo
                this.setDriverByName(driverName);
43 10627 caballero
44 6763 jmvivo
                this.setProjectionByName(projectionName);
45
        }
46 10627 caballero
47 6763 jmvivo
        /* Esto deberia ir en el FLyrDefault */
48 10627 caballero
        public void setProjectionByName(String projectionName) throws Exception{
49 6763 jmvivo
                IProjection proj = CRSFactory.getCRS(projectionName);
50
                if (proj == null) {
51
                        throw new Exception("No se ha encontrado la proyeccion: "+ projectionName);
52
                }
53
                this.setProjection(proj);
54 10627 caballero
55 6763 jmvivo
        }
56 10627 caballero
57 6763 jmvivo
        public void setFileName(String filePath) throws FileNotFoundException{
58
                if (dataFile != null) {
59
                        //TODO: que excepcion lanzar???
60
                        return;
61
                }
62
                File file = new File(filePath);
63
                if (!file.exists()) {
64
                        throw new FileNotFoundException(filePath);
65
                }
66
                this.dataFile = file;
67
        }
68 10627 caballero
69 6763 jmvivo
        public void setFile(File file) throws FileNotFoundException {
70
                if (dataFile != null) {
71
                        //TODO: que excepcion lanzar???
72
                        return;
73 10627 caballero
                }
74 6763 jmvivo
                if (!file.exists()) {
75
                        throw new FileNotFoundException(file.getAbsolutePath());
76 10627 caballero
                }
77 6763 jmvivo
                this.dataFile = new File(file.getAbsolutePath());
78
        }
79 10627 caballero
80 6763 jmvivo
        public String getFileName() {
81
                if (this.dataFile == null) {
82
                        return null;
83
                }
84
                return this.dataFile.getAbsolutePath();
85
        }
86 10627 caballero
87
88 6763 jmvivo
        public void setDriver(VectorialFileDriver driver) {
89
                this.fileDriver = driver;
90
        }
91 10627 caballero
92 10665 caballero
        public void setDriverByName(String driverName) throws DriverLoadException {
93 6763 jmvivo
                this.setDriver(
94 10627 caballero
                  (VectorialFileDriver)LayerFactory.getDM().getDriver(driverName)
95 6763 jmvivo
                );
96
        }
97 10627 caballero
98 9307 jjdelcerro
        public VectorialFileDriver getDriver() {
99
                return this.fileDriver;
100
        }
101 10627 caballero
102 6763 jmvivo
        /* FIXME: esto tendria que tener declarado un throws de algo*/
103 10627 caballero
        public void wakeUp() throws LoadLayerException {
104 6763 jmvivo
                if (!loaded) {
105 10627 caballero
                        this.load();
106 6763 jmvivo
                }
107 10627 caballero
108 6763 jmvivo
        }
109
110 10627 caballero
111
        public void load() throws LoadLayerException {
112 6763 jmvivo
                if (this.dataFile == null) {
113
                        this.setAvailable(false);
114 10627 caballero
                        throw new FileLayerException(getName(),null);
115 6763 jmvivo
                }
116
                if (this.getName() == null || this.getName().length() == 0) {
117
                        this.setAvailable(false);
118 10627 caballero
                        throw new NameLayerException(getName(),null);
119 6763 jmvivo
                }
120
                if (this.fileDriver == null) {
121
                        this.setAvailable(false);
122 10627 caballero
                        throw new DriverLayerException(getName(),null);
123 6763 jmvivo
                }
124
                if (this.getProjection() == null) {
125
                        this.setAvailable(false);
126 10627 caballero
                        throw new ProjectionLayerException(getName(),null);
127 6763 jmvivo
                }
128
129
                        VectorialFileAdapter adapter = new VectorialFileAdapter(this.dataFile);
130 10627 caballero
                        adapter.setDriver(this.fileDriver);
131
132 6763 jmvivo
                        this.setSource(adapter);
133 10627 caballero
134
                        try {
135
                                this.putLoadSelection();
136
                                this.putLoadLegend();
137
                                this.initializeLegendDefault();
138
                        } catch (LegendLayerException e) {
139
                                this.setAvailable(false);
140
                                throw new LegendLayerException(getName(),e);
141
                        } catch (XMLException e) {
142
                                this.setAvailable(false);
143
                                throw new XMLLayerException(getName(),e);
144
                        } catch (ReadDriverException e) {
145
                                this.setAvailable(false);
146
                                throw new LoadLayerException(getName(),e);
147
                        }
148 6763 jmvivo
                this.cleanLoadOptions();
149
                this.loaded = true;
150
        }
151 10627 caballero
152 6763 jmvivo
        /* Esto deberia ir en FLyrVect */
153 10627 caballero
        private void initializeLegendDefault() throws LegendLayerException, ReadDriverException {
154 6763 jmvivo
                if (this.getLegend() == null) {
155
            if (this.getRecordset().getDriver() instanceof WithDefaultLegend) {
156
                WithDefaultLegend aux = (WithDefaultLegend) this.getRecordset().getDriver();
157 10627 caballero
                this.setLegend((VectorialLegend) aux.getDefaultLegend());
158 10813 jaume
159
                ILabelingStrategy labeler = aux.getDefaultLabelingStrategy();
160
                if (labeler instanceof AttrInTableLabeling) {
161
                        ((AttrInTableLabeling) labeler).setLayer(this);
162
                }
163
164
                this.setLabelingStrategy(labeler);
165 6763 jmvivo
            } else {
166
                this.setLegend(LegendFactory.createSingleSymbolLegend(
167
                        this.getShapeType()));
168
            }
169 10627 caballero
                }
170 6763 jmvivo
        }
171 10627 caballero
172 6763 jmvivo
        public void setXMLEntity(XMLEntity xml) throws XMLException {
173
        IProjection proj = null;
174
        if (xml.contains("proj")) {
175
            proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
176
        }
177
        else
178
        {
179 6881 cesar
            proj = this.getMapContext().getViewPort().getProjection();
180 6763 jmvivo
        }
181
                this.setName(xml.getName());
182
                this.setProjection(proj);
183
                try {
184
                        this.setDriver(
185
                                (VectorialFileDriver)LayerFactory.getDM().getDriver(
186
                                        xml.getStringProperty("driverName")
187
                                )
188
                        );
189
                } catch (DriverLoadException e) {
190
                        throw new XMLException(e);
191
                } catch (ClassCastException e) {
192
                        throw new XMLException(e);
193
                }
194
                try {
195
                        this.setFileName(xml.getStringProperty("file"));
196
                } catch (FileNotFoundException e) {
197
                        throw new XMLException(e);
198
                }
199
200 6769 jmvivo
                super.setXMLEntityNew(xml);
201 6763 jmvivo
        }
202
}