Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / placements / PlacementManager.java @ 40911

History | View | Annotate | Download (6.05 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.labeling.placements;
42

    
43
import java.util.ArrayList;
44
import java.util.Iterator;
45

    
46
import org.gvsig.fmap.geom.Geometry;
47
import org.gvsig.fmap.geom.Geometry.TYPES;
48
import org.gvsig.fmap.geom.GeometryLocator;
49
import org.gvsig.fmap.geom.type.GeometryType;
50
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
51
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
52
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
53
import org.gvsig.tools.exception.DriverException;
54
import org.gvsig.tools.locator.LocatorException;
55
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
57

    
58

    
59
/**
60
 *
61
 * PlacementManager.java
62
 *
63
 *
64
 * @author jaume dominguez faus - jaume.dominguez@iver.es Jan 3, 2008
65
 *
66
 */
67
public class PlacementManager {
68
        
69
        private static Logger logger = LoggerFactory.getLogger(PlacementManager.class);
70
        
71
        private static ArrayList<Class<?>> installedLabelPlacements = new ArrayList<Class<?>>();
72
        private static ArrayList<ILabelPlacement> availableLabelPlacements;
73
        private static final CannotPlaceLabel cantPlaceLabel = new CannotPlaceLabel();
74
        private PlacementManager() {}
75

    
76
        public static ILabelPlacement getPlacement(
77
                        IPlacementConstraints placementConstraints, int shapeType) {
78

    
79
                ArrayList<ILabelPlacement> suitablePlacements = new ArrayList<ILabelPlacement>();
80
                if (placementConstraints.getClass().equals(MultiShapePlacementConstraints.class)) {
81
                        MultiShapePlacementConstraints msp = (MultiShapePlacementConstraints) placementConstraints;
82

    
83
                        return new MultiShapePlacement(
84
                                        getPlacement(msp.getPointConstraints(),   Geometry.TYPES.POINT),
85
                                        getPlacement(msp.getLineConstraints(),    Geometry.TYPES.CURVE),
86
                                        getPlacement(msp.getPolygonConstraints(), Geometry.TYPES.SURFACE)
87
                                        );
88
                } else {
89
                        for (Iterator<ILabelPlacement> iterator = getAvailablePlacements().iterator(); iterator.hasNext();) {
90
                                ILabelPlacement placement = iterator.next();
91
                                if (placement.isSuitableFor(placementConstraints, shapeType)) {
92
                                        suitablePlacements.add(placement);
93
                                }
94
                        }
95

    
96
                        if (suitablePlacements.size() == 0)
97
                                return cantPlaceLabel;
98
                        else if (suitablePlacements.size() == 1)
99
                                return suitablePlacements.get(0);
100
                        else
101
                                return new CompoundLabelPlacement(
102
                                                (ILabelPlacement[]) suitablePlacements.
103
                                                toArray(new ILabelPlacement[suitablePlacements.size()]));
104

    
105
                }
106

    
107
        }
108

    
109
        public static void addLabelPlacement(Class<?> labelPlacementClass) {
110
//                if (!labelPlacementClass.isInstance(ILabelPlacement.class))
111
//                throw new IllegalArgumentException(
112
//                        labelPlacementClass.getName()+" is not an valid label placement algorithm.");
113
                installedLabelPlacements.add(labelPlacementClass);
114

    
115
                // invalidate current list of available placements
116
                if (availableLabelPlacements != null)
117
                        availableLabelPlacements.clear();
118
                availableLabelPlacements = null;
119
        }
120

    
121
        private static ArrayList<ILabelPlacement> getAvailablePlacements() {
122
                if (availableLabelPlacements == null) {
123
                        availableLabelPlacements = new ArrayList<ILabelPlacement>(installedLabelPlacements.size());
124
                        for (Iterator<Class<?>> iterator = installedLabelPlacements.iterator(); iterator.hasNext();) {
125
                                Class<?> clazz = null;
126
                                try {
127
                                        clazz = iterator.next();
128
                                        availableLabelPlacements.add((ILabelPlacement) clazz.newInstance());
129
                                } catch (Exception e) {
130
                                        logger.error("Couldn't install label placement: "+clazz.getName(), e);
131
                                }
132
                        }
133
                }
134

    
135
                return availableLabelPlacements;
136
        }
137

    
138

    
139
        /**
140
         * Creates a new instance of placement constraints from a vector layer. The
141
         * placement constraints are created according the layer shape type.
142
         * @param layerDest
143
         * @return
144
         * @throws LocatorException 
145
         * @throws GeometryTypeNotValidException 
146
         * @throws GeometryTypeNotSupportedException 
147
         * @throws DriverException
148
         */
149
        public static IPlacementConstraints createPlacementConstraints(int geotype)
150
                        throws Exception {
151
                
152
                GeometryType geom_gt = GeometryLocator.getGeometryManager().getGeometryType(
153
                                geotype, Geometry.SUBTYPES.GEOM2D);
154

    
155
                if (geom_gt.getType() == TYPES.POINT ||
156
                                geom_gt.getType() == TYPES.MULTIPOINT) {
157
                        return new PointPlacementConstraints();
158
                } else {
159
                        if (geom_gt.isTypeOf(TYPES.CURVE) ||
160
                                        geom_gt.getType() == TYPES.MULTICURVE) {
161
                                return new LinePlacementConstraints();
162
                        } else {
163
                                if (geom_gt.isTypeOf(TYPES.SURFACE) ||
164
                                                geom_gt.getType() == TYPES.MULTISURFACE) {
165
                                        return new PolygonPlacementConstraints();
166
                                } else {
167
                                        if (geom_gt.getType() == TYPES.GEOMETRY ||
168
                                        geom_gt.getType() == TYPES.AGGREGATE) {
169

    
170
                                                return new MultiShapePlacementConstraints(
171
                                                                (PointPlacementConstraints)
172
                                                                createPlacementConstraints(TYPES.POINT),
173
                                                                (LinePlacementConstraints)
174
                                                                createPlacementConstraints(TYPES.CURVE),
175
                                                                (PolygonPlacementConstraints)
176
                                                                createPlacementConstraints(TYPES.SURFACE));
177
                                        }
178
                                }
179
                        }
180
                }                
181
                logger.error("Unknown geo type in createPlacementConstraints: " + geotype);
182
                return null;
183
        }
184
        
185

    
186

    
187
}