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 @ 47437

History | View | Annotate | Download (5.95 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.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
48

    
49
import org.gvsig.fmap.geom.Geometry;
50
import org.gvsig.fmap.geom.Geometry.TYPES;
51
import org.gvsig.fmap.geom.GeometryLocator;
52
import org.gvsig.fmap.geom.type.GeometryType;
53
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
54
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
55
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
56
import org.gvsig.tools.locator.LocatorException;
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
         */
148
        public static IPlacementConstraints createPlacementConstraints(int geotype)
149
                        throws Exception {
150

    
151
                GeometryType geom_gt = GeometryLocator.getGeometryManager().getGeometryType(
152
                                geotype, Geometry.SUBTYPES.GEOM2D);
153

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

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

    
184

    
185

    
186
}