Frames | No Frames |
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: // We cannot use StyleConstants.getBackground() here, because that returns 113: // BLACK as default (when background == null). What we need is the 114: // background setting of the text component instead, which is what we get 115: // when background == null anyway. 116: background = (Color) atts.getAttribute(StyleConstants.Background); 117: foreground = StyleConstants.getForeground(atts); 118: strikeThrough = StyleConstants.isStrikeThrough(atts); 119: subscript = StyleConstants.isSubscript(atts); 120: superscript = StyleConstants.isSuperscript(atts); 121: underline = StyleConstants.isUnderline(atts); 122: 123: // Determine the font. 124: String family = StyleConstants.getFontFamily(atts); 125: int size = StyleConstants.getFontSize(atts); 126: int style = Font.PLAIN; 127: if (StyleConstants.isBold(atts)) 128: style |= Font.BOLD; 129: if (StyleConstants.isItalic(atts)) 130: style |= Font.ITALIC; 131: font = new Font(family, style, size); 132: } 133: 134: /** 135: * Receives notification when text attributes change in the chunk of 136: * text that this view is responsible for. This simply calls 137: * {@link #setPropertiesFromAttributes()}. 138: * 139: * @param e the document event 140: * @param a the allocation of this view 141: * @param vf the view factory to use for creating new views 142: */ 143: public void changedUpdate(DocumentEvent e, Shape a, ViewFactory vf) 144: { 145: setPropertiesFromAttributes(); 146: } 147: 148: /** 149: * Returns the background color for the glyphs. 150: * 151: * @return the background color for the glyphs 152: */ 153: public Color getBackground() 154: { 155: return background; 156: } 157: 158: /** 159: * Sets the background color for the glyphs. A value of <code>null</code> 160: * means the background of the parent view should shine through. 161: * 162: * @param bg the background to set or <code>null</code> 163: * 164: * @since 1.5 165: */ 166: protected void setBackground(Color bg) 167: { 168: background = bg; 169: } 170: 171: /** 172: * Returns the foreground color for the glyphs. 173: * 174: * @return the foreground color for the glyphs 175: */ 176: public Color getForeground() 177: { 178: return foreground; 179: } 180: 181: /** 182: * Returns the font for the glyphs. 183: * 184: * @return the font for the glyphs 185: */ 186: public Font getFont() 187: { 188: return font; 189: } 190: 191: /** 192: * Returns the font metrics of the current font. 193: * 194: * @return the font metrics of the current font 195: * 196: * @deprecated this is not used anymore 197: */ 198: protected FontMetrics getFontMetrics() 199: { 200: return getContainer().getGraphics().getFontMetrics(font); 201: } 202: 203: /** 204: * Returns <code>true</code> if the glyphs are rendered underlined, 205: * <code>false</code> otherwise. 206: * 207: * @return <code>true</code> if the glyphs are rendered underlined, 208: * <code>false</code> otherwise 209: */ 210: public boolean isUnderline() 211: { 212: return underline; 213: } 214: 215: /** 216: * Sets the underline flag. 217: * 218: * @param flag <code>true</code> if the glyphs are rendered underlined, 219: * <code>false</code> otherwise 220: */ 221: protected void setUnderline(boolean flag) 222: { 223: underline = flag; 224: } 225: 226: /** 227: * Returns <code>true</code> if the glyphs are rendered as subscript, 228: * <code>false</code> otherwise. 229: * 230: * @return <code>true</code> if the glyphs are rendered as subscript, 231: * <code>false</code> otherwise 232: */ 233: public boolean isSubscript() 234: { 235: return subscript; 236: } 237: 238: /** 239: * Sets the subscript flag. 240: * 241: * @param flag <code>true</code> if the glyphs are rendered as subscript, 242: * <code>false</code> otherwise 243: */ 244: protected void setSubscript(boolean flag) 245: { 246: subscript = flag; 247: } 248: 249: /** 250: * Returns <code>true</code> if the glyphs are rendered as superscript, 251: * <code>false</code> otherwise. 252: * 253: * @return <code>true</code> if the glyphs are rendered as superscript, 254: * <code>false</code> otherwise 255: */ 256: public boolean isSuperscript() 257: { 258: return superscript; 259: } 260: 261: /** 262: * Sets the superscript flag. 263: * 264: * @param flag <code>true</code> if the glyphs are rendered as superscript, 265: * <code>false</code> otherwise 266: */ 267: protected void setSuperscript(boolean flag) 268: { 269: superscript = flag; 270: } 271: 272: /** 273: * Returns <code>true</code> if the glyphs are rendered strike-through, 274: * <code>false</code> otherwise. 275: * 276: * @return <code>true</code> if the glyphs are rendered strike-through, 277: * <code>false</code> otherwise 278: */ 279: public boolean isStrikeThrough() 280: { 281: return strikeThrough; 282: } 283: 284: /** 285: * Sets the strike-through flag. 286: * 287: * @param flag <code>true</code> if the glyphs are rendered strike-through, 288: * <code>false</code> otherwise 289: */ 290: protected void setStrikeThrough(boolean flag) 291: { 292: strikeThrough = flag; 293: } 294: }