001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_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: 18195 $ $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $ 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 Iterator<Label> it = labels.iterator(); 134 while ( it.hasNext() ) { 135 Label lab = it.next(); 136 lab.paint( g2D ); 137 } 138 139 // mark the labels as unset (for the next paint-call) 140 labels = null; 141 } 142 } 143 144 @Override 145 public boolean doesScaleConstraintApply( double scale ) { 146 return ( symbolizer.getMinScaleDenominator() <= scale ) && ( symbolizer.getMaxScaleDenominator() > scale ); 147 } 148 149 /** 150 * Removes all <tt>Label<tt> representations for this 151 * <tt>LabelDisplayElement</tt>. 152 */ 153 public void clearLabels() { 154 labels = null; 155 } 156 157 /** 158 * Adds a <tt>Label</tt> representation that is to be considered when the <tt>LabelDisplayElement</tt> is painted to 159 * the view. 160 * 161 * @param label 162 */ 163 public void addLabel( Label label ) { 164 if ( labels == null ) { 165 labels = new ArrayList<Label>( 100 ); 166 } 167 labels.add( label ); 168 } 169 170 /** 171 * Adds <tt>Label</tt> representations that are to be considered when the <tt>LabelDisplayElement</tt> is painted to 172 * the view. 173 * 174 * @param labels 175 */ 176 public void addLabels( Label[] labels ) { 177 if ( this.labels == null ) { 178 this.labels = new ArrayList<Label>( 100 ); 179 } 180 for ( int i = 0; i < labels.length; i++ ) { 181 this.labels.add( labels[i] ); 182 } 183 } 184 185 /** 186 * Sets the <tt>Label</tt> representations that are to be considered when the <tt>LabelDisplayElement</tt> is 187 * painted to the view. 188 * 189 * @param labels 190 */ 191 public void setLabels( Label[] labels ) { 192 this.labels = new ArrayList<Label>( 100 ); 193 for ( int i = 0; i < labels.length; i++ ) { 194 this.labels.add( labels[i] ); 195 } 196 } 197 }