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 @ 40673
History | View | Annotate | Download (7.12 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.apache.log4j.Logger; |
47 |
import org.gvsig.symbology.fmap.labeling.placements.CannotPlaceLabel; |
48 |
import org.gvsig.symbology.fmap.labeling.placements.CompoundLabelPlacement; |
49 |
import org.gvsig.symbology.fmap.labeling.placements.ILabelPlacement; |
50 |
import org.gvsig.symbology.fmap.labeling.placements.LinePlacementConstraints; |
51 |
import org.gvsig.symbology.fmap.labeling.placements.MultiShapePlacement; |
52 |
import org.gvsig.symbology.fmap.labeling.placements.MultiShapePlacementConstraints; |
53 |
import org.gvsig.symbology.fmap.labeling.placements.PointPlacementConstraints; |
54 |
import org.gvsig.symbology.fmap.labeling.placements.PolygonPlacementConstraints; |
55 |
|
56 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
57 |
import com.iver.cit.gvsig.fmap.core.FShape; |
58 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.IPlacementConstraints; |
59 |
import com.iver.utiles.IPersistence; |
60 |
import com.iver.utiles.NotExistInXMLEntity; |
61 |
import com.iver.utiles.XMLEntity; |
62 |
|
63 |
/**
|
64 |
*
|
65 |
* PlacementManager.java
|
66 |
*
|
67 |
*
|
68 |
* @author jaume dominguez faus - jaume.dominguez@iver.es Jan 3, 2008
|
69 |
*
|
70 |
*/
|
71 |
public class PlacementManager { |
72 |
private static Logger logger = Logger.getLogger(PlacementManager.class.getName()); |
73 |
private static ArrayList<Class<?>> installedLabelPlacements = new ArrayList<Class<?>>(); |
74 |
private static ArrayList<ILabelPlacement> availableLabelPlacements; |
75 |
private static final CannotPlaceLabel cantPlaceLabel = new CannotPlaceLabel(); |
76 |
private PlacementManager() {}
|
77 |
|
78 |
public static ILabelPlacement getPlacement(IPlacementConstraints placementConstraints, int shapeType) { |
79 |
|
80 |
ArrayList<ILabelPlacement> suitablePlacements = new ArrayList<ILabelPlacement>(); |
81 |
if (placementConstraints.getClass().equals(MultiShapePlacementConstraints.class)) {
|
82 |
MultiShapePlacementConstraints msp = (MultiShapePlacementConstraints) placementConstraints; |
83 |
|
84 |
return new MultiShapePlacement( |
85 |
getPlacement(msp.getPointConstraints(), FShape.POINT), |
86 |
getPlacement(msp.getLineConstraints(), FShape.LINE), |
87 |
getPlacement(msp.getPolygonConstraints(), FShape.POLYGON) |
88 |
); |
89 |
} else {
|
90 |
for (Iterator<ILabelPlacement> iterator = getAvailablePlacements().iterator(); iterator.hasNext();) { |
91 |
ILabelPlacement placement = iterator.next(); |
92 |
if (placement.isSuitableFor(placementConstraints, shapeType)) {
|
93 |
suitablePlacements.add(placement); |
94 |
} |
95 |
} |
96 |
|
97 |
if (suitablePlacements.size() == 0) |
98 |
return cantPlaceLabel;
|
99 |
else if (suitablePlacements.size() == 1) |
100 |
return suitablePlacements.get(0); |
101 |
else
|
102 |
return new CompoundLabelPlacement( |
103 |
(ILabelPlacement[]) suitablePlacements.
|
104 |
toArray(new ILabelPlacement[suitablePlacements.size()]));
|
105 |
|
106 |
} |
107 |
|
108 |
} |
109 |
|
110 |
public static void addLabelPlacement(Class<?> labelPlacementClass) { |
111 |
// if (!labelPlacementClass.isInstance(ILabelPlacement.class))
|
112 |
// throw new IllegalArgumentException(
|
113 |
// labelPlacementClass.getName()+" is not an valid label placement algorithm.");
|
114 |
installedLabelPlacements.add(labelPlacementClass); |
115 |
|
116 |
// invalidate current list of available placements
|
117 |
if (availableLabelPlacements != null) |
118 |
availableLabelPlacements.clear(); |
119 |
availableLabelPlacements = null;
|
120 |
} |
121 |
|
122 |
private static ArrayList<ILabelPlacement> getAvailablePlacements() { |
123 |
if (availableLabelPlacements == null) { |
124 |
availableLabelPlacements = new ArrayList<ILabelPlacement>(installedLabelPlacements.size()); |
125 |
for (Iterator<Class<?>> iterator = installedLabelPlacements.iterator(); iterator.hasNext();) { |
126 |
Class<?> clazz = null; |
127 |
try {
|
128 |
clazz = iterator.next(); |
129 |
availableLabelPlacements.add((ILabelPlacement) clazz.newInstance()); |
130 |
} catch (Exception e) { |
131 |
Logger.getLogger(PlacementManager.class).error("couldn't install label placement '"+clazz.getName(), e); |
132 |
} |
133 |
} |
134 |
} |
135 |
|
136 |
return availableLabelPlacements;
|
137 |
} |
138 |
|
139 |
|
140 |
/**
|
141 |
* Creates a new instance of placement constraints from a vector layer. The
|
142 |
* placement constraints are created according the layer shape type.
|
143 |
* @param layerDest
|
144 |
* @return
|
145 |
* @throws DriverException
|
146 |
*/
|
147 |
public static IPlacementConstraints createPlacementConstraints(int shapeType) { |
148 |
switch (shapeType % FShape.Z) {
|
149 |
case FShape.LINE:
|
150 |
return new LinePlacementConstraints(); |
151 |
case FShape.POLYGON:
|
152 |
return new PolygonPlacementConstraints(); |
153 |
case FShape.POINT:
|
154 |
case FShape.MULTIPOINT: // TODO (09/01/08) is this correct??? if not fix it also in PlacementProperties (twice), , MarkerPlacementAroundPoint |
155 |
return new PointPlacementConstraints(); |
156 |
case FShape.MULTI:
|
157 |
return new MultiShapePlacementConstraints( |
158 |
createPlacementConstraints(FShape.POINT), |
159 |
createPlacementConstraints(FShape.LINE), |
160 |
createPlacementConstraints(FShape.POLYGON)); |
161 |
} |
162 |
throw new Error("Shape type not yet supported"); |
163 |
// return null;
|
164 |
} |
165 |
|
166 |
public static IPlacementConstraints createPlacementConstraints(XMLEntity entity) { |
167 |
String className = null; |
168 |
try {
|
169 |
className = entity.getStringProperty("className");
|
170 |
} catch (NotExistInXMLEntity e) {
|
171 |
logger.error("Symbol class name not set.\n" +
|
172 |
" Maybe you forgot to add the" +
|
173 |
" putProperty(\"className\", yourClassName)" +
|
174 |
" call in the getXMLEntity method of your symbol", e);
|
175 |
} |
176 |
|
177 |
|
178 |
Class clazz = null; |
179 |
Object obj = null; |
180 |
try {
|
181 |
clazz = Class.forName(className);
|
182 |
obj = clazz.newInstance(); |
183 |
((IPersistence) obj).setXMLEntity(entity); |
184 |
|
185 |
} catch (InstantiationException e) { |
186 |
logger.error("Trying to instantiate an interface" +
|
187 |
" or abstract class + "+className+"\n"+e.getMessage(), e); |
188 |
} catch (IllegalAccessException e) { |
189 |
logger.error(null, e);
|
190 |
} catch (ClassNotFoundException e) { |
191 |
logger.error("No class called " + className +
|
192 |
" was found.\nCheck the following.\n<br>" +
|
193 |
"\t- The fullname of the class you're looking " +
|
194 |
"for matches the value in the className " +
|
195 |
"property of the XMLEntity ("+className+").\n<br>" + |
196 |
"\t- The jar file containing your symbol class is in" +
|
197 |
"the application classpath<br>", e);
|
198 |
} |
199 |
return (IPlacementConstraints) obj;
|
200 |
} |
201 |
|
202 |
} |