001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/graphics/DefaultScaleBar.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 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: bezema $
059     * 
060     * @version $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 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,
113                                String units, Color labelColor, Color barColor, Color allgColor,
114                                String barStyle, Font barFont, 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,
135                  Color.BLACK, "default", new Font( "default", Font.PLAIN, 12 ),
136                  new DecimalFormat( "###,###,###" ) );
137        }
138    
139        /**
140         * will paint the scale bar to the passed graphic context
141         * 
142         * @param g
143         *            graphic context
144         */
145        public void paint( Graphics g ) { // throws Exception {
146    
147            Rectangle rect = g.getClipBounds();
148            if ( rect != null && rect.getWidth() > 0 && rect.getHeight() > 0 ) {
149                g.setColor( bgColor );
150                g.fillRect( 0, 0, (int) rect.getWidth(), (int) rect.getHeight() );
151            }
152    
153            // if ( toplabel + bottomlabel < 1 ) throw Exception
154            g.setColor( barColor );
155    
156            int width = g.getClipBounds().width;
157            int height = g.getClipBounds().height;
158            g.setFont( barFont );
159    
160            int laenge;
161    
162            if ( barStyle.equals( "default" ) ) {
163                g.drawLine( 0, ( height / 2 ) + 1, width - 1, ( height / 2 ) + 1 );
164                g.drawLine( 0, height / 2, width - 1, height / 2 );
165                g.drawLine( 0, ( height / 2 ) - 1, width - 1, ( height / 2 ) - 1 );
166                g.drawLine( 0, ( height / 2 ) + 10, 0, ( height / 2 ) - 10 );
167                g.drawLine( width - 1, ( height / 2 ) + 10, width - 1, ( height / 2 ) - 10 );
168            }
169    
170            String strScale = numberFormat.format( scale );
171            String strScaleDen = numberFormat.format( scaleDenominator );
172    
173            g.setColor( labelColor );
174            switch ( topLabel ) {
175            case -1:
176                break;
177            case 0:
178                laenge = g.getFontMetrics().stringWidth( strScale + " " + unit );
179                g.drawString( strScale + " " + unit, ( width - laenge ) / 2, ( height / 2 ) - 6 );
180                break;
181            case 1:
182                laenge = g.getFontMetrics().stringWidth( "1 : " + strScaleDen );
183                g.drawString( "1 : " + strScaleDen, ( width - laenge ) / 2, ( height / 2 ) - 6 );
184                break;
185            }
186    
187            switch ( bottomLabel ) {
188            case -1:
189                break;
190            case 0:
191                laenge = g.getFontMetrics().stringWidth( strScale + " " + unit );
192                g.drawString( strScale + " " + unit, ( width - laenge ) / 2, ( height / 2 ) + 1
193                                                                             + barFont.getSize() );
194                break;
195            case 1:
196                laenge = g.getFontMetrics().stringWidth( "1 : " + strScaleDen );
197                g.drawString( "1 : " + strScaleDen, ( width - laenge ) / 2, ( height / 2 ) + 1
198                                                                            + barFont.getSize() );
199                break;
200            }
201        }
202    
203        /**
204         * sets the type of the label above the scale bar
205         * 
206         * @param labelType
207         *            lable type
208         * 
209         */
210        public void setTopLabel( int labelType ) {
211            switch ( labelType ) {
212            case -1:
213                topLabel = -1;
214                break;
215            case 0:
216                topLabel = 0;
217                break;
218            case 1:
219                topLabel = 1;
220                break;
221            }
222        }
223    
224        /**
225         * sets the type of the label below the scale bar
226         * 
227         * @param labelType
228         *            lable type
229         * 
230         */
231        public void setBottomLabel( int labelType ) {
232            switch ( labelType ) {
233            case -1:
234                bottomLabel = -1;
235                break;
236            case 0:
237                bottomLabel = 0;
238                break;
239            case 1:
240                bottomLabel = 1;
241                break;
242            }
243        }
244    
245        /**
246         * sets the scale as defined in the OGC WMS 1.1.1 specification. Scale is defined as the
247         * diagonal size of a pixel in the center of a map measured in meter. The setting of the scale
248         * will affect the value of the scale denominator
249         * 
250         * @param scale
251         *            map scale
252         * 
253         */
254        public void setScale( double scale ) {
255            this.scale = scale;
256        }
257    
258        /**
259         * sets the scale denominator for the scale bar. The scale denominator is the scale expression
260         * as we know it for printed maps (e.g. 1:10000 1:5000). The passed value is expressed in
261         * meters. The setting of the scale denominator will affect the value of the scale
262         * 
263         * @param scaleDen
264         *            scale denominator value
265         * 
266         */
267        public void setScaleDenominator( double scaleDen ) {
268            scaleDenominator = scaleDen;
269        }
270    
271        /**
272         * sets the units the scale and the scale denominater will be expressed at. Settings other than
273         * meter will cause that the passed values for scale and scale denominater will be recalculated
274         * for painting. it depends on the implementation what units are supported.
275         * 
276         * @param units
277         *            name units (meter, miles, feet etc.)
278         */
279        public void setUnits( String units ) {
280            unit = units;
281        }
282    
283        /**
284         * sets the front color of the scale bar
285         * 
286         * @param color
287         */
288        public void setColor( Color color ) {
289            bgColor = color;
290        }
291    
292        /**
293         * sets the label color of the scale bar
294         * 
295         * @param color
296         * 
297         */
298        public void setLabelColor( Color color ) {
299            labelColor = color;
300        }
301    
302        /**
303         * sets the bar color of the scale bar
304         * 
305         * @param color
306         * 
307         */
308        public void setBarColor( Color color ) {
309            barColor = color;
310        }
311    
312        /**
313         * sets the style of the scale bar. default style is |--------| the list of known styles depends
314         * on the implementation
315         * 
316         * @param style
317         *            style name
318         */
319        public void setStyle( String style ) {
320            barStyle = style;
321        }
322    
323        /**
324         * sets the font for label rendering
325         * 
326         * @param font
327         *            awt font object
328         */
329        public void setFont( Font font ) {
330            barFont = font;
331        }
332    
333        /*
334         * (non-Javadoc)
335         * 
336         * @see org.deegree.graphics.ScaleBar#setBackgroundColor(java.awt.Color)
337         */
338        public void setBackgroundColor( Color color ) {
339            this.bgColor = color;
340    
341        }
342    
343        /**
344         * sets the number format for a scale bar
345         * 
346         * @param numberFormat
347         */
348        public void setNumberFormat( NumberFormat numberFormat ) {
349            this.numberFormat = numberFormat;
350        }
351    }
352    /***************************************************************************************************
353     * <code>
354     Changes to this class. What the people have been up to:
355     
356     $Log$
357     Revision 1.9  2007/03/06 11:19:04  wanhoff
358     Fixed Javadoc (@param)
359    
360     Revision 1.8  2006/08/11 09:59:56  poth
361     number formating added / code formated
362    
363     Revision 1.7  2006/08/06 21:03:18  poth
364     bug fix - recognition for background color added
365    
366     Revision 1.6  2006/07/12 14:46:18  poth
367     comment footer added
368    
369     </code>
370     **************************************************************************************************/