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