001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/graphics/DefaultScaleBar.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.graphics;
037    
038    import java.awt.Color;
039    import java.awt.Font;
040    import java.awt.Graphics;
041    import java.awt.Rectangle;
042    import java.text.DecimalFormat;
043    import java.text.NumberFormat;
044    
045    /**
046     *
047     *
048     *
049     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
050     * @author last edited by: $Author: mschneider $
051     *
052     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
053     */
054    public class DefaultScaleBar implements ScaleBar {
055        private Color barColor;
056    
057        private Color labelColor;
058    
059        private Color bgColor;
060    
061        private Font barFont;
062    
063        private String barStyle;
064    
065        private String unit;
066    
067        private double scale;
068    
069        private int bottomLabel;
070    
071        private double scaleDenominator;
072    
073        private int topLabel;
074    
075        private NumberFormat numberFormat;
076    
077        /**
078         * Constructor with all Parameters
079         *
080         * @param topLabel
081         *            type of the label on top of the scale bar. Chose L_NONE or no label, L_SCALE for
082         *            scale on top and L_SCALEDENOMIATOR for scaledenominator on top.
083         * @param bottomLabel
084         *            the same as above but below the scalebar.
085         * @param scale
086         *            the scale to be displayed. For a value of e.g. 1000 there will be 1:1000 displayed
087         * @param scaleDenominator
088         *            the scaledenominator to be displayed
089         * @param units
090         *            the units the scaledenominator is in. Possible values are inch, Meter, Mile,
091         *            Kilometer...
092         * @param labelColor
093         *            the Color the label has to be in (and of course the text below and above)
094         * @param barColor
095         * @param allgColor
096         *            not used so far
097         * @param barStyle
098         *            the style the bar appears in. Currently just "default" is supported.
099         * @param barFont
100         *            the font the text above and below the scale bar appears in.
101         * @param numberFormat
102         *
103         */
104        public DefaultScaleBar( int topLabel, int bottomLabel, double scale, double scaleDenominator, String units,
105                                Color labelColor, Color barColor, Color allgColor, String barStyle, Font barFont,
106                                NumberFormat numberFormat ) {
107            setTopLabel( topLabel );
108            setBottomLabel( bottomLabel );
109            setScale( scale );
110            setScaleDenominator( scaleDenominator );
111            setColor( allgColor );
112            setBarColor( barColor );
113            setLabelColor( labelColor );
114            setStyle( barStyle );
115            setFont( barFont );
116            setUnits( units );
117            setNumberFormat( numberFormat );
118        }
119    
120        /**
121         *
122         * Constructor just using defaults
123         *
124         */
125        public DefaultScaleBar() {
126            this( L_SCALEDENOMINATOR, L_SCALE, 40000, 100, "Meter", Color.GREEN, Color.BLUE, Color.BLACK, "default",
127                  new Font( "default", Font.PLAIN, 12 ), new DecimalFormat( "###,###,###" ) );
128        }
129    
130        /**
131         * will paint the scale bar to the passed graphic context
132         *
133         * @param g
134         *            graphic context
135         */
136        public void paint( Graphics g ) { // throws Exception {
137    
138            Rectangle rect = g.getClipBounds();
139            if ( rect != null && rect.getWidth() > 0 && rect.getHeight() > 0 ) {
140                g.setColor( bgColor );
141                g.fillRect( 0, 0, (int) rect.getWidth(), (int) rect.getHeight() );
142            }
143    
144            // if ( toplabel + bottomlabel < 1 ) throw Exception
145            g.setColor( barColor );
146    
147            int width = g.getClipBounds().width;
148            int height = g.getClipBounds().height;
149            g.setFont( barFont );
150    
151            int laenge;
152    
153            if ( barStyle.equals( "default" ) ) {
154                g.drawLine( 0, ( height / 2 ) + 1, width - 1, ( height / 2 ) + 1 );
155                g.drawLine( 0, height / 2, width - 1, height / 2 );
156                g.drawLine( 0, ( height / 2 ) - 1, width - 1, ( height / 2 ) - 1 );
157                g.drawLine( 0, ( height / 2 ) + 10, 0, ( height / 2 ) - 10 );
158                g.drawLine( width - 1, ( height / 2 ) + 10, width - 1, ( height / 2 ) - 10 );
159            }
160    
161            String strScale = numberFormat.format( scale );
162            String strScaleDen = numberFormat.format( scaleDenominator );
163    
164            g.setColor( labelColor );
165            switch ( topLabel ) {
166            case -1:
167                break;
168            case 0:
169                laenge = g.getFontMetrics().stringWidth( strScale + " " + unit );
170                g.drawString( strScale + " " + unit, ( width - laenge ) / 2, ( height / 2 ) - 6 );
171                break;
172            case 1:
173                laenge = g.getFontMetrics().stringWidth( "1 : " + strScaleDen );
174                g.drawString( "1 : " + strScaleDen, ( width - laenge ) / 2, ( height / 2 ) - 6 );
175                break;
176            }
177    
178            switch ( bottomLabel ) {
179            case -1:
180                break;
181            case 0:
182                laenge = g.getFontMetrics().stringWidth( strScale + " " + unit );
183                g.drawString( strScale + " " + unit, ( width - laenge ) / 2, ( height / 2 ) + 1 + barFont.getSize() );
184                break;
185            case 1:
186                laenge = g.getFontMetrics().stringWidth( "1 : " + strScaleDen );
187                g.drawString( "1 : " + strScaleDen, ( width - laenge ) / 2, ( height / 2 ) + 1 + barFont.getSize() );
188                break;
189            }
190        }
191    
192        /**
193         * sets the type of the label above the scale bar
194         *
195         * @param labelType
196         *            lable type
197         *
198         */
199        public void setTopLabel( int labelType ) {
200            switch ( labelType ) {
201            case -1:
202                topLabel = -1;
203                break;
204            case 0:
205                topLabel = 0;
206                break;
207            case 1:
208                topLabel = 1;
209                break;
210            }
211        }
212    
213        /**
214         * sets the type of the label below the scale bar
215         *
216         * @param labelType
217         *            lable type
218         *
219         */
220        public void setBottomLabel( int labelType ) {
221            switch ( labelType ) {
222            case -1:
223                bottomLabel = -1;
224                break;
225            case 0:
226                bottomLabel = 0;
227                break;
228            case 1:
229                bottomLabel = 1;
230                break;
231            }
232        }
233    
234        /**
235         * sets the scale as defined in the OGC WMS 1.1.1 specification. Scale is defined as the
236         * diagonal size of a pixel in the center of a map measured in meter. The setting of the scale
237         * will affect the value of the scale denominator
238         *
239         * @param scale
240         *            map scale
241         *
242         */
243        public void setScale( double scale ) {
244            this.scale = scale;
245        }
246    
247        /**
248         * sets the scale denominator for the scale bar. The scale denominator is the scale expression
249         * as we know it for printed maps (e.g. 1:10000 1:5000). The passed value is expressed in
250         * meters. The setting of the scale denominator will affect the value of the scale
251         *
252         * @param scaleDen
253         *            scale denominator value
254         *
255         */
256        public void setScaleDenominator( double scaleDen ) {
257            scaleDenominator = scaleDen;
258        }
259    
260        /**
261         * sets the units the scale and the scale denominater will be expressed at. Settings other than
262         * meter will cause that the passed values for scale and scale denominater will be recalculated
263         * for painting. it depends on the implementation what units are supported.
264         *
265         * @param units
266         *            name units (meter, miles, feet etc.)
267         */
268        public void setUnits( String units ) {
269            unit = units;
270        }
271    
272        /**
273         * sets the front color of the scale bar
274         *
275         * @param color
276         */
277        public void setColor( Color color ) {
278            bgColor = color;
279        }
280    
281        /**
282         * sets the label color of the scale bar
283         *
284         * @param color
285         *
286         */
287        public void setLabelColor( Color color ) {
288            labelColor = color;
289        }
290    
291        /**
292         * sets the bar color of the scale bar
293         *
294         * @param color
295         *
296         */
297        public void setBarColor( Color color ) {
298            barColor = color;
299        }
300    
301        /**
302         * sets the style of the scale bar. default style is |--------| the list of known styles depends
303         * on the implementation
304         *
305         * @param style
306         *            style name
307         */
308        public void setStyle( String style ) {
309            barStyle = style;
310        }
311    
312        /**
313         * sets the font for label rendering
314         *
315         * @param font
316         *            awt font object
317         */
318        public void setFont( Font font ) {
319            barFont = font;
320        }
321    
322        /*
323         * (non-Javadoc)
324         *
325         * @see org.deegree.graphics.ScaleBar#setBackgroundColor(java.awt.Color)
326         */
327        public void setBackgroundColor( Color color ) {
328            this.bgColor = color;
329    
330        }
331    
332        /**
333         * sets the number format for a scale bar
334         *
335         * @param numberFormat
336         */
337        public void setNumberFormat( NumberFormat numberFormat ) {
338            this.numberFormat = numberFormat;
339        }
340    }