    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/graphics/displayelements/LabelDisplayElement.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.displayelements;
    037
    038    import java.awt.Graphics;
    039    import java.awt.Graphics2D;
    040    import java.util.ArrayList;
    041    import java.util.Iterator;
    042
    043    import org.deegree.graphics.sld.ParameterValueType;
    044    import org.deegree.graphics.sld.TextSymbolizer;
    045    import org.deegree.graphics.transformation.GeoTransform;
    046    import org.deegree.model.feature.Feature;
    047    import org.deegree.model.spatialschema.Geometry;
    048
    049    /**
    050     * <tt>DisplayElement</tt> that encapsulates a <tt>GM_Object</tt> (geometry), a <tt>ParameterValueType</tt> (caption)
    051     * and a <tt>TextSymbolizer</tt> (style).
    052     * <p>
    053     * The graphical (say: screen) representations of this <tt>DisplayElement</tt> are <tt>Label</tt>-instances. These are
    054     * generated either when the <tt>paint</tt>-method is called or assigned externally using the <tt>setLabels</tt>- or
    055     * <tt>addLabels</tt>-methods.
    056     * </p>
    057     *
    058     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    059     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider</a>
    060     * @version $Revision: 24811 $ $Date: 2010-06-10 09:44:06 +0200 (Do, 10. Jun 2010) $
    061     */
    062
    063    public class LabelDisplayElement extends GeometryDisplayElement {
    064
    065        /** Use serialVersionUID for interoperability. */
    066        private final static long serialVersionUID = -7870967255670858503L;
    067
    068        private ParameterValueType label = null;
    069
    070        // null means that the labels have to be created inside the paint-method
    071        // (and have not been set externally)
    072        private ArrayList<Label> labels = null;
    073
    074        /**
    075         * Creates a new LabelDisplayElement object.
    076         * <p>
    077         *
    078         * @param feature
    079         *            associated <tt>Feature</tt>
    080         * @param geometry
    081         *            associated <tt>GM_Object</tt>
    082         * @param symbolizer
    083         *            associated <tt>TextSymbolizer</tt>
    084         */
    085        public LabelDisplayElement( Feature feature, Geometry geometry, TextSymbolizer symbolizer ) {
    086            super( feature, geometry, symbolizer );
    087            setLabel( symbolizer.getLabel() );
    088        }
    089
    090        /**
    091         * Sets the caption of the label.
    092         *
    093         * @param label
    094         */
    095        public void setLabel( ParameterValueType label ) {
    096            this.label = label;
    097        }
    098
    099        /**
    100         * Returns the caption of the label as <tt>ParameterValueType<tt>.
    101         *
    102         * @return label
    103         */
    104        public ParameterValueType getLabel() {
    105            return label;
    106        }
    107
    108        /**
    109         * Renders the <tt>DisplayElement</tt> to the submitted graphic context. If the <tt>Label</tt>-represenations have
    110         * been assigned externally, these labels are used, else <tt>Label</tt>-instances are created automatically using
    111         * the <tt>LabelFactory</tt>.
    112         * <p>
    113         *
    114         * @param g
    115         *            <tt>Graphics</tt> context to be used
    116         * @param projection
    117         *            <tt>GeoTransform</tt> to be used
    118         */
    119        public void paint( Graphics g, GeoTransform projection, double scale ) {
    120            synchronized ( symbolizer ) {
    121                ( (ScaledFeature) feature ).setScale( scale );
    122                if ( label == null )
    123                    return;
    124                Graphics2D g2D = (Graphics2D) g;
    125                if ( labels == null ) {
    126                    try {
    127                        setLabels( LabelFactory.createLabels( this, projection, g2D ) );
    128                    } catch ( Exception e ) {
    129                        e.printStackTrace();
    130                    }
    131                }
    132                // paint all labels
    133                if ( labels == null ) {
    134                    return;
    135                }
    136                Iterator<Label> it = labels.iterator();
    137                while ( it.hasNext() ) {
    138                    Label lab = it.next();
    139                    lab.paint( g2D );
    140                }
    141
    142                // mark the labels as unset (for the next paint-call)
    143                labels = null;
    144            }
    145        }
    146
    147        @Override
    148        public boolean doesScaleConstraintApply( double scale ) {
    149            return ( symbolizer.getMinScaleDenominator() <= scale ) && ( symbolizer.getMaxScaleDenominator() > scale );
    150        }
    151
    152        /**
    153         * Removes all <tt>Label<tt> representations for this
    154         * <tt>LabelDisplayElement</tt>.
    155         */
    156        public void clearLabels() {
    157            labels = null;
    158        }
    159
    160        /**
    161         * Adds a <tt>Label</tt> representation that is to be considered when the <tt>LabelDisplayElement</tt> is painted to
    162         * the view.
    163         *
    164         * @param label
    165         */
    166        public void addLabel( Label label ) {
    167            if ( labels == null ) {
    168                labels = new ArrayList<Label>( 100 );
    169            }
    170            labels.add( label );
    171        }
    172
    173        /**
    174         * Adds <tt>Label</tt> representations that are to be considered when the <tt>LabelDisplayElement</tt> is painted to
    175         * the view.
    176         *
    177         * @param labels
    178         */
    179        public void addLabels( Label[] labels ) {
    180            if ( this.labels == null ) {
    181                this.labels = new ArrayList<Label>( 100 );
    182            }
    183            for ( int i = 0; i < labels.length; i++ ) {
    184                this.labels.add( labels[i] );
    185            }
    186        }
    187
    188        /**
    189         * Sets the <tt>Label</tt> representations that are to be considered when the <tt>LabelDisplayElement</tt> is
    190         * painted to the view.
    191         *
    192         * @param labels
    193         */
    194        public void setLabels( Label[] labels ) {
    195            this.labels = new ArrayList<Label>( 100 );
    196            for ( int i = 0; i < labels.length; i++ ) {
    197                this.labels.add( labels[i] );
    198            }
    199        }
    200    }