Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / legend / styling / LabelingFactory.java @ 24494

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

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

    
89
import org.apache.log4j.Logger;
90
import org.gvsig.fmap.dal.exceptions.ReadException;
91
import org.gvsig.fmap.mapcontext.layers.FLayer;
92
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
93

    
94
import com.iver.utiles.IPersistence;
95
import com.iver.utiles.NotExistInXMLEntity;
96
import com.iver.utiles.XMLEntity;
97
import com.iver.utiles.XMLException;
98

    
99
public class LabelingFactory {
100
        private static Logger logger = Logger.getLogger(SymbologyFactory.class.getName());
101
        private static Class defaultLabelingStrategy = AttrInTableLabelingStrategy.class;
102
        /**
103
         * Given a layer, a labeling method, a label placements constraints, and a label
104
         * zoom constraints it will figure out the best ILabelingStrategy that meets all
105
         * the needs.
106
         * @param layer, the target layer
107
         * @param method, the desired methods
108
         * @param placement, the desired placement constraints
109
         * @param zoom, the desired zoom constraints
110
         * @return ILabelingStrategy
111
         * @throws DriverException
112
         */
113
        public static ILabelingStrategy createStrategy(FLayer layer,
114
                        ILabelingMethod method, IPlacementConstraints placement,
115
                        IZoomConstraints zoom)         {
116
                if (method == null && placement == null && zoom == null)
117
                        return createDefaultStrategy(layer);
118

    
119
                ILabelingStrategy strat = createDefaultStrategy(layer);
120
                if (method != null)
121
                        strat.setLabelingMethod(method);
122
                if (placement != null)
123
                        strat.setPlacementConstraints(placement);
124
                return strat;
125

    
126
        }
127

    
128
        /**
129
         * Instantiates an object from an instance of XMLEntity. The XMLEntity
130
         * must follow the XMLEntity contract in IPersistence. It at least must
131
         * contain a className that allow create objects via instrospection. It
132
         * also should have the set of attributes that the instantiated object's
133
         * setXMLEntity(XMLEntity) method expects.
134
         * @param xml
135
         * @return
136
         */
137
        private static Object createFromXML(XMLEntity xml) {
138
                String className = null;
139
                try {
140
                        className = xml.getStringProperty("className");
141
                } catch (NotExistInXMLEntity e) {
142
                        logger.error("Symbol class name not set.\n" +
143
                                        " Maybe you forgot to add the" +
144
                                        " putProperty(\"className\", yourClassName)" +
145
                                        " call in the getXMLEntity method of your symbol", e);
146
                }
147

    
148

    
149
                Class clazz = null;
150
                Object obj = null;
151
                try {
152
                        clazz = Class.forName(className);
153
                        obj = clazz.newInstance();
154
                        ((IPersistence) obj).setXMLEntity(xml);
155

    
156
                } catch (InstantiationException e) {
157
                        logger.error("Trying to instantiate an interface" +
158
                                        " or abstract class + "+className, e);
159
                } catch (IllegalAccessException e) {
160
                        logger.error(null, e);
161
                } catch (ClassNotFoundException e) {
162
                        logger.error("No class called " + className +
163
                                        " was found.\nCheck the following.\n<br>" +
164
                                        "\t- The fullname of the class you're looking " +
165
                                        "for matches the value in the className " +
166
                                        "property of the XMLEntity ("+className+").\n<br>" +
167
                                        "\t- The jar file containing your symbol class is in" +
168
                                        "the application classpath<br>", e);
169
                } catch (XMLException e) {
170
                        logger.error("Trying to load XML data"+className, e);
171
                }
172
                return obj;
173
        }
174

    
175
        /**
176
         * Creates a new instance of a ILabelingStrategy from an XMLEntity.
177
         * @param xml
178
         * @return ILabelingStrategy
179
         * @throws ReadDriverException
180
         */
181
        public static ILabelingStrategy createStrategyFromXML(XMLEntity xml, FLayer lyr) throws ReadException {
182
                ILabelingStrategy str = (ILabelingStrategy) createFromXML(xml);
183
                str.setLayer(lyr);
184
                return str;
185
        }
186

    
187
        /**
188
         * Creates a new instance of ILabelingMethod from an XMLEntity.
189
         * @param xml
190
         * @return ILabelingMethod
191
         */
192
        public static ILabelingMethod createMethodFromXML(XMLEntity xml) {
193
                return (ILabelingMethod) createFromXML(xml);
194
        }
195

    
196

    
197
        /**
198
         * Creates a default labeling strategy from an XMLEntity.
199
         * @param layer
200
         * @return an instance of DefaultStrategy
201
         * @throws DriverException
202
         */
203
        public static ILabelingStrategy createDefaultStrategy(FLayer layer) {
204
                ILabelingStrategy st;
205
                try {
206
                        st = (ILabelingStrategy) defaultLabelingStrategy.newInstance();
207
                        st.setLayer(layer);
208
                        return st;
209
                } catch (Exception e) {
210
                        Logger.getLogger(LabelingFactory.class).error(e);
211
                        return null;
212
                }
213

    
214
        }
215

    
216
        public static void setDefaultLabelingStrategy(Class defaultLabelingStrategy) {
217
                LabelingFactory.defaultLabelingStrategy = defaultLabelingStrategy;
218
        }
219

    
220
        public static IPlacementConstraints createPlacementConstraintsFromXML(XMLEntity xml) {
221
                return (IPlacementConstraints) createFromXML(xml);
222
        }
223

    
224
        public static IZoomConstraints createZoomConstraintsFromXML(XMLEntity xml) {
225
                return (IZoomConstraints) createFromXML(xml);
226
        }
227

    
228
        public static LabelClass createLabelClassFromXML(XMLEntity xml) {
229
                return (LabelClass) createFromXML(xml);
230
        }
231
}