001 /* JAPI - (Yet another (hopefully) useful) Java API 002 * 003 * Copyright (C) 2004-2006 Christian Hujer 004 * 005 * This program is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU General Public License as 007 * published by the Free Software Foundation; either version 2 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, but 011 * WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 * 02111-1307, USA. 019 */ 020 021 package net.sf.japi.swing.font; 022 023 import java.awt.Component; 024 import java.awt.Font; 025 import static java.awt.Font.BOLD; 026 import static java.awt.Font.ITALIC; 027 import static java.awt.Font.PLAIN; 028 import javax.swing.DefaultListCellRenderer; 029 import javax.swing.JList; 030 import net.sf.japi.swing.ActionFactory; 031 032 /** ListCellRenderer for font styles. 033 * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a> 034 * @todo improve performance 035 */ 036 public class FontStyleListCellRenderer extends DefaultListCellRenderer { 037 038 /** Serial Version. */ 039 private static final long serialVersionUID = 1L; 040 041 /** Action Factory. */ 042 private static final ActionFactory ACTION_FACTORY = ActionFactory.getFactory("net.sf.japi.swing.font"); 043 044 /** {@inheritDoc} */ 045 @Override public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) { 046 final Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 047 final int style = (Integer) value; 048 setText(getTextFor(style)); 049 setFont(getFontFor(style)); 050 return c; 051 } 052 053 /** Get the text representation of a style. 054 * @param style style to get text representation for 055 * @return text representation of style 056 * @todo store values 057 */ 058 private static String getTextFor(final int style) { 059 switch (style) { 060 case PLAIN: return ACTION_FACTORY.getString("font.style.plain"); 061 case BOLD: return ACTION_FACTORY.getString("font.style.bold"); 062 case ITALIC: return ACTION_FACTORY.getString("font.style.italic"); 063 case BOLD|ITALIC: return ACTION_FACTORY.getString("font.style.bolditalic"); 064 default: return ACTION_FACTORY.getString("font.style.unknown"); 065 } 066 } 067 068 /** Get the font representation of a style. 069 * @param style style to get font representation for 070 * @return font representation of style 071 */ 072 private Font getFontFor(final int style) { 073 switch (style) { 074 case PLAIN: return getFont().deriveFont(PLAIN); 075 case BOLD: return getFont().deriveFont(BOLD); 076 case ITALIC: return getFont().deriveFont(ITALIC); 077 case BOLD|ITALIC: return getFont().deriveFont(BOLD|ITALIC); 078 default: return getFont().deriveFont(PLAIN); 079 } 080 } 081 082 } // class FontStyleListCellRenderer