Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / styling / LabelingFactory.java @ 11192

History | View | Annotate | Download (6.74 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

    
42
/* CVS MESSAGES:
43
*
44
* $Id: LabelingFactory.java 11192 2007-04-13 11:59:30Z jaume $
45
* $Log$
46
* Revision 1.6  2007-04-13 11:59:30  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.5  2007/03/28 16:48:01  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.4  2007/03/20 16:16:20  jaume
53
* refactored to use ISymbol instead of FSymbol
54
*
55
* Revision 1.3  2007/03/09 11:20:57  jaume
56
* Advanced symbology (start committing)
57
*
58
* Revision 1.2  2007/03/09 08:33:43  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.1.2.4  2007/02/21 07:34:08  jaume
62
* labeling starts working
63
*
64
* Revision 1.1.2.3  2007/02/12 15:15:20  jaume
65
* refactored interval legend and added graduated symbol legend
66
*
67
* Revision 1.1.2.2  2007/02/09 07:47:05  jaume
68
* Isymbol moved
69
*
70
* Revision 1.1.2.1  2007/02/02 16:21:24  jaume
71
* start commiting labeling stuff
72
*
73
* Revision 1.1.2.1  2007/01/30 18:10:45  jaume
74
* start commiting labeling stuff
75
*
76
*
77
*/
78
package com.iver.cit.gvsig.fmap.rendering.styling;
79

    
80
import org.apache.log4j.Logger;
81

    
82
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
83
import com.hardcode.gdbms.engine.data.driver.DriverException;
84
import com.iver.cit.gvsig.fmap.core.FShape;
85
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
86
import com.iver.cit.gvsig.fmap.layers.FLayer;
87
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
88
import com.iver.utiles.IPersistance;
89
import com.iver.utiles.NotExistInXMLEntity;
90
import com.iver.utiles.XMLEntity;
91

    
92
public class LabelingFactory {
93
        private static Logger logger = Logger.getLogger(SymbologyFactory.class.getName());
94

    
95
        /**
96
         * Instantiates an object from an instance of XMLEntity. The XMLEntity
97
         * must follow the XMLEntity contract in IPersistence. It at least must
98
         * contain a className that allow create objects via instrospection. It
99
         * also should have the set of attributes that the instantiated object's
100
         * setXMLEntity(XMLEntity) method expects.
101
         * @param xml
102
         * @return
103
         */
104
        private static Object createFromXML(XMLEntity xml) {
105
                String className = null;
106
                try {
107
                        className = xml.getStringProperty("className");
108
                } catch (NotExistInXMLEntity e) {
109
                        logger.error("Symbol class name not set.\n" +
110
                                        " Maybe you forgot to add the" +
111
                                        " putProperty(\"className\", yourClassName)" +
112
                                        " call in the getXMLEntity method of your symbol", e);
113
                }
114

    
115

    
116
                Class clazz = null;
117
                Object obj = null;
118
                try {
119
                        clazz = Class.forName(className);
120
                        obj = clazz.newInstance();
121
                        ((IPersistance) obj).setXMLEntity(xml);
122

    
123
                } catch (InstantiationException e) {
124
                        logger.error("Trying to instantiate an interface" +
125
                                        " or abstract class + "+className, e);
126
                } catch (IllegalAccessException e) {
127
                        logger.error(null, e);
128
                } catch (ClassNotFoundException e) {
129
                        logger.error("No class called " + className +
130
                                        " was found.\nCheck the following.\n<br>" +
131
                                        "\t- The fullname of the class you're looking " +
132
                                        "for matches the value in the className " +
133
                                        "property of the XMLEntity ("+className+").\n<br>" +
134
                                        "\t- The jar file containing your symbol class is in" +
135
                                        "the application classpath<br>", e);
136
                }
137
                return obj;
138
        }
139

    
140
        /**
141
         * Creates a new instance of a ILabelingStrategy from an XMLEntity.
142
         * @param xml
143
         * @return ILabelingStrategy
144
         */
145
        public static ILabelingStrategy createStrategyFromXML(XMLEntity xml) {
146
                return (ILabelingStrategy) createFromXML(xml);
147
        }
148

    
149
        /**
150
         * Creates a new instance of ILabelingMethod from an XMLEntity.
151
         * @param xml
152
         * @return ILabelingMethod
153
         */
154
        public static ILabelingMethod createMethodFromXML(XMLEntity xml) {
155
                return (ILabelingMethod) createFromXML(xml);
156
        }
157

    
158
        /**
159
         * Creates a default labeling strategy from an XMLEntity.
160
         * @param layer
161
         * @return an instance of DefaultStrategy
162
         * @throws DriverException
163
         */
164
        public static ILabelingStrategy createDefaultStrategy(FLayer layer)
165
        throws ReadDriverException {
166
                return new SimpleLabeling(layer);
167
        }
168

    
169
        /**
170
         * Given a layer, a labeling method, a label placements constraints, and a label
171
         * zoom constraints it will figure out the best ILabelingStrategy that meets all
172
         * the needs.
173
         * @param layer, the target layer
174
         * @param method, the desired methods
175
         * @param placement, the desired placement constraints
176
         * @param zoom, the desired zoom constraints
177
         * @return ILabelingStrategy
178
         * @throws DriverException
179
         */
180
        public static ILabelingStrategy createStrategy(FLayer layer,
181
                        ILabelingMethod method, IPlacementConstraints placement,
182
                        IZoomConstraints zoom)
183
        throws ReadDriverException {
184
                if (method == null && placement == null && zoom == null)
185
                        return createDefaultStrategy(layer);
186

    
187
                ILabelingStrategy strat = createDefaultStrategy(layer);
188
                if (method != null)
189
                        strat.setLabelingMethod(method);
190
                if (placement != null)
191
                        strat.setPlacementConstraints(placement);
192
                return strat;
193
                /* solament per a proves, ac? falta muntar el sistema expert que
194
                 * decidisca quina estrat?gia ?s la millor a partir dels par?metres.
195
                 */
196
        }
197

    
198
        /**
199
         * Creates a new instance of placement constraints from a vector layer. The
200
         * placement constraints are created according the layer shape type.
201
         * @param layerDest
202
         * @return
203
         * @throws DriverException
204
         */
205
        public static IPlacementConstraints createPlacementConstraints(FLyrVect layerDest)
206
        throws ReadDriverException {
207
                int shapeType = layerDest.getShapeType();
208
                switch (shapeType) {
209
                case FShape.LINE:
210
                        return new LinePlacementConstraints();
211
                case FShape.POLYGON:
212
                        return new PolygonPlacementConstraints();
213
                case FShape.POINT:
214
                        return new PointPlacementConstraints();
215
                }
216
                throw new Error("Shape type not yet supported");
217
//                return null;
218
        }
219

    
220
        public static IPlacementConstraints createPlacementConstraints(XMLEntity entity) {
221
                return (IPlacementConstraints) createFromXML(entity);
222
        }
223

    
224

    
225
}