Source for javax.swing.text.LabelView

   1: /* LabelView.java -- A view to render styled text
   2:    Copyright (C) 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing.text;
  40: 
  41: import java.awt.Color;
  42: import java.awt.Font;
  43: import java.awt.FontMetrics;
  44: import java.awt.Shape;
  45: 
  46: import javax.swing.event.DocumentEvent;
  47: 
  48: /**
  49:  * A {@link GlyphView} that caches the textattributes for most effective
  50:  * rendering.
  51:  *
  52:  * @author Roman Kennke (kennke@aicas.com)
  53:  */
  54: public class LabelView extends GlyphView
  55: {
  56: 
  57:   /**
  58:    * The background color.
  59:    */
  60:   Color background;
  61: 
  62:   /**
  63:    * The foreground color.
  64:    */
  65:   Color foreground;
  66: 
  67:   /**
  68:    * The background color.
  69:    */
  70:   Font font;
  71: 
  72:   /**
  73:    * The strikethrough flag.
  74:    */
  75:   boolean strikeThrough;
  76: 
  77:   /**
  78:    * The underline flag.
  79:    */
  80:   boolean underline;
  81: 
  82:   /**
  83:    * The subscript flag.
  84:    */
  85:   boolean subscript;
  86: 
  87:   /**
  88:    * The superscript flag.
  89:    */
  90:   boolean superscript;
  91: 
  92:   /**
  93:    * Creates a new <code>GlyphView</code> for the given <code>Element</code>.
  94:    *
  95:    * @param element the element that is rendered by this GlyphView
  96:    */
  97:   public LabelView(Element element)
  98:   {
  99:     super(element);
 100:     setPropertiesFromAttributes();
 101:   }
 102: 
 103:   /**
 104:    * Loads the properties of this label view from the element's text
 105:    * attributes. This method is called from the constructor and the
 106:    * {@link #changedUpdate} method
 107:    */
 108:   protected void setPropertiesFromAttributes()
 109:   {
 110:     Element el = getElement();
 111:     AttributeSet atts = el.getAttributes();
 112:     background = StyleConstants.getBackground(atts);
 113:     foreground = StyleConstants.getForeground(atts);
 114:     strikeThrough = StyleConstants.isStrikeThrough(atts);
 115:     subscript = StyleConstants.isSubscript(atts);
 116:     superscript = StyleConstants.isSuperscript(atts);
 117:     underline = StyleConstants.isUnderline(atts);
 118: 
 119:     // Determine the font.
 120:     String family = StyleConstants.getFontFamily(atts);
 121:     int size = StyleConstants.getFontSize(atts);
 122:     int style = Font.PLAIN;
 123:     if (StyleConstants.isBold(atts))
 124:         style |= Font.BOLD;
 125:     if (StyleConstants.isItalic(atts))
 126:       style |= Font.ITALIC;
 127:     font = new Font(family, style, size);
 128:   }
 129: 
 130:   /**
 131:    * Receives notification when text attributes change in the chunk of
 132:    * text that this view is responsible for. This simply calls
 133:    * {@link #setPropertiesFromAttributes()}.
 134:    *
 135:    * @param e the document event
 136:    * @param a the allocation of this view
 137:    * @param vf the view factory to use for creating new views
 138:    */
 139:   public void changedUpdate(DocumentEvent e, Shape a, ViewFactory vf)
 140:   {
 141:     setPropertiesFromAttributes();
 142:   }
 143: 
 144:   /**
 145:    * Returns the background color for the glyphs.
 146:    *
 147:    * @return the background color for the glyphs
 148:    */
 149:   public Color getBackground()
 150:   {
 151:     return background;
 152:   }
 153: 
 154:   /**
 155:    * Sets the background color for the glyphs. A value of <code>null</code>
 156:    * means the background of the parent view should shine through.
 157:    *
 158:    * @param bg the background to set or <code>null</code>
 159:    *
 160:    * @since 1.5
 161:    */
 162:   protected void setBackground(Color bg)
 163:   {
 164:     background = bg;
 165:   }
 166: 
 167:   /**
 168:    * Returns the foreground color for the glyphs.
 169:    *
 170:    * @return the foreground color for the glyphs
 171:    */
 172:   public Color getForeground()
 173:   {
 174:     return foreground;
 175:   }
 176: 
 177:   /**
 178:    * Returns the font for the glyphs.
 179:    *
 180:    * @return the font for the glyphs
 181:    */
 182:   public Font getFont()
 183:   {
 184:     return font;
 185:   }
 186: 
 187:   /**
 188:    * Returns the font metrics of the current font.
 189:    *
 190:    * @return the font metrics of the current font
 191:    *
 192:    * @deprecated this is not used anymore
 193:    */
 194:   protected FontMetrics getFontMetrics()
 195:   {
 196:     return getContainer().getGraphics().getFontMetrics(font);
 197:   }
 198: 
 199:   /**
 200:    * Returns <code>true</code> if the glyphs are rendered underlined,
 201:    * <code>false</code> otherwise.
 202:    *
 203:    * @return <code>true</code> if the glyphs are rendered underlined,
 204:    *         <code>false</code> otherwise
 205:    */
 206:   public boolean isUnderline()
 207:   {
 208:     return underline;
 209:   }
 210: 
 211:   /**
 212:    * Sets the underline flag.
 213:    *
 214:    * @param flag <code>true</code> if the glyphs are rendered underlined,
 215:    *             <code>false</code> otherwise
 216:    */
 217:   protected void setUnderline(boolean flag)
 218:   {
 219:     underline = flag;
 220:   }
 221: 
 222:   /**
 223:    * Returns <code>true</code> if the glyphs are rendered as subscript,
 224:    * <code>false</code> otherwise.
 225:    *
 226:    * @return <code>true</code> if the glyphs are rendered as subscript,
 227:    *         <code>false</code> otherwise
 228:    */
 229:   public boolean isSubscript()
 230:   {
 231:     return subscript;
 232:   }
 233: 
 234:   /**
 235:    * Sets the subscript flag.
 236:    *
 237:    * @param flag <code>true</code> if the glyphs are rendered as subscript,
 238:    *             <code>false</code> otherwise
 239:    */
 240:   protected void setSubscript(boolean flag)
 241:   {
 242:     subscript = flag;
 243:   }
 244: 
 245:   /**
 246:    * Returns <code>true</code> if the glyphs are rendered as superscript,
 247:    * <code>false</code> otherwise.
 248:    *
 249:    * @return <code>true</code> if the glyphs are rendered as superscript,
 250:    *         <code>false</code> otherwise
 251:    */
 252:   public boolean isSuperscript()
 253:   {
 254:     return superscript;
 255:   }
 256: 
 257:   /**
 258:    * Sets the superscript flag.
 259:    *
 260:    * @param flag <code>true</code> if the glyphs are rendered as superscript,
 261:    *             <code>false</code> otherwise
 262:    */
 263:   protected void setSuperscript(boolean flag)
 264:   {
 265:     superscript = flag;
 266:   }
 267: 
 268:   /**
 269:    * Returns <code>true</code> if the glyphs are rendered strike-through,
 270:    * <code>false</code> otherwise.
 271:    *
 272:    * @return <code>true</code> if the glyphs are rendered strike-through,
 273:    *         <code>false</code> otherwise
 274:    */
 275:   public boolean isStrikeThrough()
 276:   {
 277:     return strikeThrough;
 278:   }
 279: 
 280:   /**
 281:    * Sets the strike-through flag.
 282:    *
 283:    * @param flag <code>true</code> if the glyphs are rendered strike-through,
 284:    *             <code>false</code> otherwise
 285:    */
 286:   protected void setStrikeThrough(boolean flag)
 287:   {
 288:     strikeThrough = flag;
 289:   }
 290: }