Statistics
| Revision:

gvsig-3d / 2.1 / branches / org.gvsig.view3d_vector_and_extrusion_2.3 / org.gvsig.view3d / org.gvsig.view3d / org.gvsig.view3d.lib / org.gvsig.view3d.lib.impl / src / main / java / org / gvsig / view3d / lib / impl / DefaultView3DManger.java @ 708

History | View | Annotate | Download (6.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright © 2007-2016 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.view3d.lib.impl;
26

    
27
import java.util.ArrayList;
28
import java.util.List;
29

    
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

    
33
import org.gvsig.fmap.mapcontext.layers.FLayer;
34
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
35
import org.gvsig.view3d.lib.api.View3DManager;
36
import org.gvsig.view3d.lib.api.loadingmodes.NwwLoaderFactory;
37
import org.gvsig.view3d.lib.api.properties.E3DLayerLoadingModes;
38
import org.gvsig.view3d.lib.api.properties.LayerProperties3D;
39

    
40
/**
41
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
42
 * @author Andrea Antonello andrea.antonello@gmail.com
43
 */
44
public class DefaultView3DManger implements View3DManager {
45

    
46
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultView3DManger.class);
47

    
48
    private static final String PROPERTIES3D = "properties3D";
49

    
50
    private List<NwwLoaderFactory> registeredVectorFactories = new ArrayList<>();
51
    private List<NwwLoaderFactory> registeredRasterFactories = new ArrayList<>();
52

    
53
    public LayerProperties3D getLayerProperties(FLayer layer, NwwLoaderFactory loadingfactory) {
54
        Object properties = layer.getProperty(PROPERTIES3D);
55
        if (properties == null) {
56
            LOGGER.debug("Layer properties are created new");
57
            LayerProperties3D layerProperties;
58
            if (loadingfactory != null) {
59
                layerProperties = loadingfactory.createProperties();
60
            } else {
61
                layerProperties = getLayerLoadingFactory(layer).createProperties();
62
            }
63
            setLayerProperties(layer, layerProperties);
64
            return layerProperties;
65
        } else {
66
            /*
67
             * if the properties are available, let's make sure they are of the
68
             * right type. If not, they need to be adapted to the requested
69
             * mode.
70
             */
71
            String propertiesCanonicalName = properties.getClass().getCanonicalName();
72
            LOGGER.debug("Layer properties are available and of type: " + propertiesCanonicalName);
73
            if (loadingfactory != null) {
74
                LayerProperties3D tmpProperties = loadingfactory.createProperties();
75
                if (!tmpProperties.getClass().getCanonicalName().equals(propertiesCanonicalName)) {
76
                    properties = tmpProperties;
77
                    setLayerProperties(layer, tmpProperties);
78
                }
79
            }
80
            LOGGER.info("Layer properties set to the layer after check: " + properties.getClass().getSimpleName());
81
        }
82
        return (LayerProperties3D) properties;
83
    }
84

    
85
    @Override
86
    public NwwLoaderFactory getLayerLoadingFactory(FLayer layer) {
87
        Object properties = layer.getProperty(PROPERTIES3D);
88
        if (properties != null) {
89
            String canonicalName = properties.getClass().getCanonicalName();
90
            // test vector factories
91
            List<NwwLoaderFactory> vectorLoaderFactories = getVectorLoaderFactories();
92
            for (NwwLoaderFactory nwwVectorLoaderFactory : vectorLoaderFactories) {
93
                LayerProperties3D tmpProperties = nwwVectorLoaderFactory.createProperties();
94
                if (tmpProperties.getClass().getCanonicalName().equals(canonicalName)) {
95
                    return nwwVectorLoaderFactory;
96
                }
97
            }
98
            // test raster factories
99
            List<NwwLoaderFactory> rasterLoaderFactories = getRasterLoaderFactories();
100
            for (NwwLoaderFactory nwwRasterLoaderFactory : rasterLoaderFactories) {
101
                LayerProperties3D tmpProperties = nwwRasterLoaderFactory.createProperties();
102
                if (tmpProperties.getClass().getCanonicalName().equals(canonicalName)) {
103
                    return nwwRasterLoaderFactory;
104
                }
105
            }
106
            // couldn't understand the mode, set to null to return the default
107
            properties = null;
108

    
109
        }
110

    
111
        // this means for some reason the properties are null, return default
112
        if (layer instanceof FLyrVect) {
113
            // default for vectors is RASTERIZED_VECTOR
114
            String defaultVectorfactoryName = E3DLayerLoadingModes.RASTERIZED_VECTOR.getTitle();
115
            List<NwwLoaderFactory> vectorLoaderFactories = getVectorLoaderFactories();
116
            for (NwwLoaderFactory nwwLoaderFactory : vectorLoaderFactories) {
117
                if (nwwLoaderFactory.getName().equals(defaultVectorfactoryName)) {
118
                    return nwwLoaderFactory;
119
                }
120
            }
121
            return vectorLoaderFactories.get(0);
122
        } else {
123
            // default for rasters is RASTER_IMAGE
124
            String defaultRasterFactoryName = E3DLayerLoadingModes.RASTER_IMAGE.getTitle();
125
            List<NwwLoaderFactory> rasterLoaderFactories = getRasterLoaderFactories();
126
            for (NwwLoaderFactory nwwLoaderFactory : rasterLoaderFactories) {
127
                if (nwwLoaderFactory.getName().equals(defaultRasterFactoryName)) {
128
                    return nwwLoaderFactory;
129
                }
130
            }
131
            return getRasterLoaderFactories().get(0);
132
        }
133
    }
134

    
135
    public void setLayerProperties(FLayer layer, LayerProperties3D properties) {
136
        layer.setProperty(PROPERTIES3D, properties);
137
        properties.setLayer(layer);
138
    }
139

    
140
    @Override
141
    public void registerVectorLoaderFactory(NwwLoaderFactory loaderFactory) {
142
        if (!registeredVectorFactories.contains(loaderFactory)) {
143
            registeredVectorFactories.add(loaderFactory);
144
        }
145
    }
146

    
147
    @Override
148
    public List<NwwLoaderFactory> getVectorLoaderFactories() {
149
        return registeredVectorFactories;
150
    }
151

    
152
    @Override
153
    public void registerRasterLoaderFactory(NwwLoaderFactory loaderFactory) {
154
        if (!registeredRasterFactories.contains(loaderFactory)) {
155
            registeredRasterFactories.add(loaderFactory);
156
        }
157
    }
158

    
159
    @Override
160
    public List<NwwLoaderFactory> getRasterLoaderFactories() {
161
        return registeredRasterFactories;
162
    }
163
}