Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.impl / src / main / java / org / gvsig / fmap / mapcontext / layers / order / impl / RasterPolLinePointOrderManager.java @ 43511

History | View | Annotate | Download (5.03 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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 3
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
package org.gvsig.fmap.mapcontext.layers.order.impl;
25

    
26
import java.util.Map;
27

    
28
import org.gvsig.fmap.dal.exception.ReadException;
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.Geometry.TYPES;
31
import org.gvsig.fmap.geom.type.GeometryType;
32
import org.gvsig.fmap.mapcontext.layers.FLayer;
33
import org.gvsig.fmap.mapcontext.layers.FLayers;
34
import org.gvsig.fmap.mapcontext.layers.order.LayerOrderManager;
35
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
36
import org.gvsig.i18n.Messages;
37
import org.gvsig.tools.ToolsLocator;
38
import org.gvsig.tools.dynobject.DynStruct;
39
import org.gvsig.tools.persistence.PersistenceManager;
40
import org.gvsig.tools.persistence.PersistentState;
41
import org.gvsig.tools.persistence.exception.PersistenceException;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

    
45

    
46
public class RasterPolLinePointOrderManager implements LayerOrderManager {
47

    
48
        public static final String RASTER_POLYGON_LINE_POINT_LAYER_ORDER_MANAGER_PERSISTENCE_NAME =
49
                        "RASTER_POLYGON_LINE_POINT_LAYER_ORDER_MANAGER_PERSISTENCE_NAME";
50
        
51
        private static Logger logger =
52
                LoggerFactory.getLogger(RasterPolLinePointOrderManager.class);
53
        
54
        public RasterPolLinePointOrderManager() {
55
                
56
        }
57
        
58
        public int getPosition(FLayers target, FLayer newLayer) {
59
                
60
                int new_weight = 3;
61
                new_weight = getLayerWeight(newLayer);
62
                
63
                int len = target.getLayersCount();
64
                int item_w = 0;
65
                // from top to bottom,
66
                // look for a layer at
67
                // least as "heavy" as this one
68
                for (int i=(len-1); i>=0; i--) {
69
                        item_w = getLayerWeight(target.getLayer(i));
70
                        if (item_w >= new_weight) {
71
                                return (i+1);
72
                        }
73
                }
74
                // layer "falls" to bottom
75
                return 0;
76
        }
77

    
78
        public String getDescription() {
79
                return Messages.getText("_Raster_at_bottom_then_polygons_lines_points");
80
        }
81

    
82
        public String getName() {
83
                return Messages.getText("_Raster_Polygon_Line_Point_order_manager");
84
        }
85

    
86
        
87
        public String getCode() {
88
                return this.getClass().getName();
89
        }
90

    
91
        /**
92
         * 
93
         * @param lyr
94
         * @return weight: point=0, line=1, polygon=2, other=3, raster=4
95
         */
96
        private int getLayerWeight(FLayer lyr) {
97
                
98
                if (lyr.getClass().getName().toLowerCase().indexOf("raster") != -1) {
99
                        return 4;
100
                } else {
101
                        if (lyr instanceof FLyrVect) {
102
                                FLyrVect lyrv = (FLyrVect) lyr;
103
                                int type2d = Geometry.TYPES.SURFACE;
104
                                try {
105
                                        type2d = simplifyType(lyrv.getGeometryType());
106
                                } catch (ReadException e) {
107
                                        logger.error("While getting geo type.", e);
108
                                }
109
                                switch (type2d) {
110
                                case TYPES.SURFACE:
111
                                        return 2;
112
                                case TYPES.CURVE:
113
                                        return 1;
114
                                case TYPES.POINT:
115
                                        return 0;
116
                                default:
117
                                        // should not reach this
118
                                        return 3;
119
                                }
120
                        } else {
121
                                // other:
122
                                return 3;
123
                        }
124
                }
125
                
126
        }
127

    
128
        /**
129
         * 
130
         */
131
        private int simplifyType(GeometryType gt) {
132
        if( gt.isTypeOf(TYPES.POINT) || gt.isTypeOf(TYPES.MULTIPOINT) ) {
133
            return TYPES.POINT;
134

    
135
        } else if( gt.isTypeOf(TYPES.CURVE) || gt.isTypeOf(TYPES.MULTICURVE) ) {
136
            return TYPES.CURVE;
137

    
138
        } else if( gt.isTypeOf(TYPES.SURFACE) || gt.isTypeOf(TYPES.MULTISURFACE) ) {
139
            return TYPES.SURFACE;
140

    
141
        }
142
        return TYPES.SURFACE;
143
        }
144

    
145
        public void loadFromState(PersistentState state) throws PersistenceException {
146
        }
147

    
148
        public void saveToState(PersistentState state) throws PersistenceException {
149
                state.set("name", this.getName());
150
        }
151
        
152
        public static void registerPersistent() {
153
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
154
                if (manager.getDefinition(RASTER_POLYGON_LINE_POINT_LAYER_ORDER_MANAGER_PERSISTENCE_NAME) == null) {
155
                        DynStruct def = manager.addDefinition(
156
                                        RasterPolLinePointOrderManager.class,
157
                                        RASTER_POLYGON_LINE_POINT_LAYER_ORDER_MANAGER_PERSISTENCE_NAME,
158
                                        RASTER_POLYGON_LINE_POINT_LAYER_ORDER_MANAGER_PERSISTENCE_NAME +" Persistence definition",
159
                                        null, 
160
                                        null);
161
                        def.addDynFieldString("name").setMandatory(true);
162
                }                        
163
        }
164
        
165
        public Object clone() throws CloneNotSupportedException {
166
                return new RasterPolLinePointOrderManager();
167
        }
168
        
169

    
170

    
171
}