001/* 002 * $Id: FontHighlighter.java 1164 2009-11-03 04:22:00Z kschaefe $ 003 * 004 * Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 * 021 */ 022package org.jdesktop.swingx.decorator; 023 024import java.awt.Component; 025import java.awt.Font; 026 027/** 028 * A Highlighter which sets the Font of the component.<p> 029 * 030 * @author Karl George Schaefer 031 * 032 */ 033public class FontHighlighter extends AbstractHighlighter { 034 private Font font; 035 036 /** 037 * Instantiates a FontHighlighter with null Font. The Highlighter is 038 * applied always. 039 */ 040 public FontHighlighter() { 041 this((HighlightPredicate) null); 042 } 043 044 /** 045 * Instantiates a FontHighlighter with the given Font. The Highlighter is 046 * applied always. 047 * 048 * @param font the Font to apply 049 */ 050 public FontHighlighter(Font font) { 051 this(null, font); 052 } 053 054 /** 055 * Instantiates a FontHighlighter with the given HighlightPredicate and null Font. 056 * 057 * @param predicate the HighlightPredicate to use, may be null to default to ALWAYS. 058 */ 059 public FontHighlighter(HighlightPredicate predicate) { 060 this(predicate, null); 061 } 062 063 /** 064 * Instantiates a FontHighlighter with the given Font and HighlightPredicate. 065 * 066 * @param predicate the HighlightPredicate to use, may be null to default to ALWAYS. 067 * @param font the Font to apply, may be null 068 */ 069 public FontHighlighter(HighlightPredicate predicate, Font font) { 070 super(predicate); 071 this.font = font; 072 } 073 074 /** 075 * Returns the Font used for decoration. 076 * 077 * @return the Font used for decoration 078 * 079 * @see #setFont(Font) 080 */ 081 public Font getFont() { 082 return font; 083 } 084 085 /** 086 * Sets the Font used for decoration. May be null to not decorate. 087 * 088 * @param font the Font used for decoration, may be null to not decorate. 089 * 090 * @see #getFont() 091 */ 092 public void setFont(Font font) { 093 if (areEqual(font, getFont())) return; 094 this.font = font; 095 fireStateChanged(); 096 } 097 098 /** 099 * {@inheritDoc}<p> 100 * 101 * Implemented to return false if the font property is null. 102 */ 103 @Override 104 protected boolean canHighlight(Component component, ComponentAdapter adapter) { 105 return font != null; 106 } 107 108 /** 109 * {@inheritDoc}<p> 110 * 111 * Implemented to set the component's Font. 112 */ 113 @Override 114 protected Component doHighlight(Component component, ComponentAdapter adapter) { 115 component.setFont(font); 116 return component; 117 } 118}