Statistics
| Revision:

svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLayerGenericVectorial.java @ 19509

History | View | Annotate | Download (6.26 KB)

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

    
3
import org.cresques.cts.IProjection;
4

    
5
import com.hardcode.driverManager.DriverLoadException;
6
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
7
import com.iver.cit.gvsig.exceptions.layers.DriverLayerException;
8
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException;
9
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
10
import com.iver.cit.gvsig.exceptions.layers.NameLayerException;
11
import com.iver.cit.gvsig.exceptions.layers.ProjectionLayerException;
12
import com.iver.cit.gvsig.exceptions.layers.XMLLayerException;
13
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
14
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
15
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
16
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
17
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
18
import com.iver.cit.gvsig.fmap.rendering.styling.AttrInTableLabeling;
19
import com.iver.cit.gvsig.fmap.rendering.styling.ILabelingStrategy;
20
import com.iver.utiles.IPersistence;
21
import com.iver.utiles.XMLEntity;
22

    
23
public class FLayerGenericVectorial extends FLyrVect {
24
        private boolean loaded = false;
25

    
26
        private VectorialDriver vDriver = null;
27

    
28

    
29
        /* Esto deberia ir en el FLyrDefault */
30
        public void setProjectionByName(String projectionName) throws Exception{
31
                IProjection proj = CRSFactory.getCRS(projectionName);
32
                if (proj == null) {
33
                        throw new Exception("No se ha encontrado la proyeccion: "+ projectionName);
34
                }
35
                this.setProjection(proj);
36

    
37
        }
38

    
39

    
40
        public void setDriver(VectorialDriver vDriver) {
41
                this.vDriver = vDriver;
42
        }
43

    
44
        public VectorialDriver getDriver() {
45
                return this.vDriver ;
46
        }
47

    
48
        public void setDriverByName(String driverName) throws DriverLoadException {
49
                this.setDriver(
50
                  (VectorialDriver)LayerFactory.getDM().getDriver(driverName)
51
                );
52
        }
53

    
54

    
55
        /* Esto deberia ir en FLyrVect */
56
        private void initializeLegendDefault() throws LegendLayerException, ReadDriverException {
57
                if (this.getLegend() == null) {
58
            if (this.getRecordset().getDriver() instanceof WithDefaultLegend) {
59
                WithDefaultLegend aux = (WithDefaultLegend) this.getRecordset().getDriver();
60
                this.setLegend((IVectorLegend) aux.getDefaultLegend());
61

    
62
                ILabelingStrategy labeler = aux.getDefaultLabelingStrategy();
63
                if (labeler instanceof AttrInTableLabeling) {
64
                        ((AttrInTableLabeling) labeler).setLayer(this);
65
                }
66

    
67
                this.setLabelingStrategy(labeler);
68
            } else {
69
                this.setLegend(LegendFactory.createSingleSymbolLegend(
70
                        this.getShapeType()));
71
            }
72
                }
73
        }
74
        /* FIXME: esto tendria que tener declarado un throws de algo*/
75
        public void wakeUp() throws LoadLayerException {
76
                if (!loaded) {
77
                        this.load();
78
                }
79

    
80
        }
81

    
82

    
83
        public void load() throws LoadLayerException {
84
                if (this.getName() == null || this.getName().length() == 0) {
85
                        this.setAvailable(false);
86
                        throw new NameLayerException(getName(),null);
87
                }
88
                if (this.vDriver == null) {
89
                        this.setAvailable(false);
90
                        throw new DriverLayerException(getName(),null);
91
                }
92
                if (this.getProjection() == null) {
93
                        this.setAvailable(false);
94
                        throw new ProjectionLayerException(getName(),null);
95
                }
96

    
97
                VectorialAdapter adapter = null;
98
                adapter = new VectorialDefaultAdapter();
99
                adapter.setDriver(this.vDriver);
100
                this.setSource(adapter);
101

    
102
                try {
103
                        this.putLoadSelection();
104
                        this.putLoadLegend();
105
                        this.initializeLegendDefault();
106

    
107
                } catch (LegendLayerException e) {
108
                        this.setAvailable(false);
109
                        throw new LegendLayerException(getName(),e);
110
                } catch (XMLException e) {
111
                        this.setAvailable(false);
112
                        throw new XMLLayerException(getName(),e);
113
                } catch (ReadDriverException e) {
114
                        this.setAvailable(false);
115
                        throw new LoadLayerException(getName(),e);
116
                }
117
                this.cleanLoadOptions();
118
        }
119

    
120
        public void setXMLEntity(XMLEntity xml) throws XMLException {
121
        IProjection proj = null;
122
        if (xml.contains("proj")) {
123
            proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
124
        }
125
        else
126
        {
127
            proj = this.getMapContext().getViewPort().getProjection();
128
        }
129
                this.setName(xml.getName());
130
                this.setProjection(proj);
131

    
132
        String driverName = xml.getStringProperty("other");
133
        VectorialDriver driver = null;
134
        try {
135
            driver = (VectorialDriver) LayerFactory.getDM().getDriver(driverName);
136
        } catch (DriverLoadException e) {
137
            // Si no existe ese driver, no pasa nada.
138
            // Puede que el desarrollador no quiera que
139
            // aparezca en el cuadro de di?logo y ha metido
140
            // el jar con sus clases en nuestro directorio lib.
141
            // Intentamos cargar esa clase "a pelo".
142
            if (xml.getChild(2).contains("className"))
143
            {
144
                String className2 = xml.getChild(2).getStringProperty("className");
145
                try {
146
                    driver = (VectorialDriver) Class.forName(className2).newInstance();
147
                } catch (Exception e1) {
148
                    throw new XMLException(e1);
149
                }
150
            }
151
        } catch (NullPointerException npe) {
152
            // Si no existe ese driver, no pasa nada.
153
            // Puede que el desarrollador no quiera que
154
            // aparezca en el cuadro de di?logo y ha metido
155
            // el jar con sus clases en nuestro directorio lib.
156
            // Intentamos cargar esa clase "a pelo".
157
            if (xml.getChild(2).contains("className"))
158
            {
159
                String className2 = xml.getChild(2).getStringProperty("className");
160
                try {
161
                    driver = (VectorialDriver) Class.forName(className2).newInstance();
162
                } catch (Exception e1) {
163
                    throw new XMLException(e1);
164
                }
165
            }
166
        }
167
        if (driver == null) {
168
                throw new XMLException(new Exception("Error al cargar el driver"));
169
        }
170
        if (driver instanceof IPersistence)
171
        {
172
                IPersistence persist = (IPersistence) driver;
173
            persist.setXMLEntity(xml.getChild(2));
174
        }
175
        this.setDriver(driver);
176
        super.setXMLEntityNew(xml);
177
        }
178
}