Statistics
| Revision:

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

History | View | Annotate | Download (6.25 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 10679 2007-03-09 11:25:01Z jaume $
45
* $Log$
46
* Revision 1.3  2007-03-09 11:20:57  jaume
47
* Advanced symbology (start committing)
48
*
49
* Revision 1.2  2007/03/09 08:33:43  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.1.2.4  2007/02/21 07:34:08  jaume
53
* labeling starts working
54
*
55
* Revision 1.1.2.3  2007/02/12 15:15:20  jaume
56
* refactored interval legend and added graduated symbol legend
57
*
58
* Revision 1.1.2.2  2007/02/09 07:47:05  jaume
59
* Isymbol moved
60
*
61
* Revision 1.1.2.1  2007/02/02 16:21:24  jaume
62
* start commiting labeling stuff
63
*
64
* Revision 1.1.2.1  2007/01/30 18:10:45  jaume
65
* start commiting labeling stuff
66
*
67
*
68
*/
69
package com.iver.cit.gvsig.fmap.rendering.styling;
70

    
71
import org.apache.log4j.Logger;
72

    
73
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
74
import com.hardcode.gdbms.engine.data.driver.DriverException;
75
import com.iver.cit.gvsig.fmap.core.FShape;
76
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
77
import com.iver.cit.gvsig.fmap.layers.FLayer;
78
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
79
import com.iver.utiles.IPersistance;
80
import com.iver.utiles.NotExistInXMLEntity;
81
import com.iver.utiles.XMLEntity;
82

    
83
public class LabelingFactory {
84
        private static Logger logger = Logger.getLogger(SymbologyFactory.class.getName());
85

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

    
106

    
107
                Class clazz = null;
108
                Object obj = null;
109
                try {
110
                        clazz = Class.forName(className);
111
                        // TODO remove the patch the day we deprecate FSymbol
112
                        obj = clazz.newInstance();
113
                        ((IPersistance) obj).setXMLEntity(xml);
114

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

    
132
        /**
133
         * Creates a new instance of a ILabelingStrategy from an XMLEntity.
134
         * @param xml
135
         * @return ILabelingStrategy
136
         */
137
        public static ILabelingStrategy createStrategyFromXML(XMLEntity xml) {
138
                return (ILabelingStrategy) createFromXML(xml);
139
        }
140

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

    
150
        /**
151
         * Creates a default labeling strategy from an XMLEntity.
152
         * @param layer
153
         * @return an instance of DefaultStrategy
154
         * @throws DriverException
155
         */
156
        public static ILabelingStrategy createDefaultStrategy(FLayer layer)
157
        throws ReadDriverException {
158
                return new SimpleLabelingStrategy(layer);
159
        }
160

    
161
        /**
162
         * Given a layer, a labeling method, a label placements constraints, and a label
163
         * zoom constraints it will figure out the best ILabelingStrategy that meets all
164
         * the needs.
165
         * @param layer, the target layer
166
         * @param method, the desired methods
167
         * @param placement, the desired placement constraints
168
         * @param zoom, the desired zoom constraints
169
         * @return ILabelingStrategy
170
         * @throws DriverException
171
         */
172
        public static ILabelingStrategy createStrategy(FLayer layer,
173
                        ILabelingMethod method, IPlacementConstraints placement,
174
                        IZoomConstraints zoom)
175
        throws ReadDriverException {
176
                if (method == null && placement == null && zoom == null)
177
                        return createDefaultStrategy(layer);
178

    
179
                return createDefaultStrategy(layer);
180
                /* solament per a proves, ac? falta muntar el sistema expert que
181
                 * decidisca quina estrat?gia ?s la millor a partir dels par?metres.
182
                 */
183
        }
184

    
185
        /**
186
         * Creates a new instance of placement constraints from a vector layer. The
187
         * placement constraints are created according the layer shape type.
188
         * @param layerDest
189
         * @return
190
         * @throws DriverException
191
         */
192
        public static IPlacementConstraints createPlacementConstraints(FLyrVect layerDest)
193
        throws ReadDriverException {
194
                int shapeType = layerDest.getShapeType();
195
                switch (shapeType) {
196
                case FShape.LINE:
197
                        return new LinePlacementConstraints();
198
                case FShape.POLYGON:
199
                        return new PolygonPlacementConstraints();
200
                case FShape.POINT:
201
                        return new PointPlacementConstraints();
202
                }
203
                throw new Error("Shape type not yet supported");
204
        }
205

    
206

    
207
}