PageRenderTime 59ms CodeModel.GetById 48ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/FontSelector.java

#
Java | 166 lines | 99 code | 18 blank | 49 comment | 5 complexity | 763a3ead6a6c4c17316efb1d1e7d7654 MD5 | raw file
  1/*
  2 * FontSelector.java - Font selector
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000, 2003 Slava Pestov
  7 * Portions copyright (C) 1999 Jason Ginchereau
  8 * Portions copyright (C) 2003 mike dillon
  9 *
 10 * This program is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU General Public License
 12 * as published by the Free Software Foundation; either version 2
 13 * of the License, or any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 23 */
 24
 25package org.gjt.sp.jedit.gui;
 26
 27//{{{ Imports
 28import java.awt.event.*;
 29import java.awt.*;
 30import java.util.ArrayList;
 31import java.util.List;
 32import javax.swing.border.*;
 33import javax.swing.event.*;
 34import javax.swing.*;
 35import org.gjt.sp.jedit.*;
 36import org.gjt.sp.util.Log;
 37//}}}
 38
 39//{{{ FontSelector class
 40/**
 41 * A font chooser widget.
 42 * @author Slava Pestov
 43 * @version $Id: FontSelector.java 17430 2010-03-07 03:16:20Z vanza $
 44 */
 45public class FontSelector extends JButton
 46{
 47	//{{{ FontSelector constructor
 48	/**
 49	 * Creates a new font selector control.
 50	 * @param font The font
 51	 */
 52	public FontSelector(Font font)
 53	{
 54		this(font,false);
 55	} //}}}
 56
 57	//{{{ FontSelector constructor
 58	/**
 59	 * Creates a new font selector control.
 60	 * @param font The font
 61	 * @param antiAlias Is anti-aliasing enabled?
 62	 * @since jEdit 4.2pre7
 63	 */
 64	public FontSelector(Font font, boolean antiAlias)
 65	{
 66		setFont(font);
 67		this.antiAlias = antiAlias;
 68
 69		updateText();
 70
 71		setRequestFocusEnabled(false);
 72
 73		addActionListener(new ActionHandler());
 74	} //}}}
 75
 76	//{{{ paintComponent() method
 77	public void paintComponent(Graphics g)
 78	{
 79		setAntiAliasEnabled(g);
 80		super.paintComponent(g);
 81	} //}}}
 82
 83	//{{{ isAntiAliasEnabled() method
 84	public boolean isAntiAliasEnabled()
 85	{
 86		return antiAlias;
 87	} //}}}
 88
 89	//{{{ setAntiAliasEnabled() method
 90	public void setAntiAliasEnabled(boolean antiAlias)
 91	{
 92		this.antiAlias = antiAlias;
 93	} //}}}
 94
 95	//{{{ updateText() method
 96	private void updateText()
 97	{
 98		Font font = getFont();
 99		String styleString;
100		switch(font.getStyle())
101		{
102		case Font.PLAIN:
103			styleString = jEdit.getProperty("font-selector.plain");
104			break;
105		case Font.BOLD:
106			styleString = jEdit.getProperty("font-selector.bold");
107			break;
108		case Font.ITALIC:
109			styleString = jEdit.getProperty("font-selector.italic");
110			break;
111		case Font.BOLD | Font.ITALIC:
112			styleString = jEdit.getProperty("font-selector.bolditalic");
113			break;
114		default:
115			styleString = "UNKNOWN!!!???";
116			break;
117		}
118
119		setText(font.getName() + ' ' + font.getSize() + ' ' + styleString);
120	} //}}}
121
122	//{{{ setAntiAliasEnabled() method
123	void setAntiAliasEnabled(Graphics g)
124	{
125		if (antiAlias)
126		{
127			Graphics2D g2 = (Graphics2D)g;
128			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
129				RenderingHints.VALUE_ANTIALIAS_ON);
130		}
131	} //}}}
132
133	private boolean antiAlias;
134
135	//{{{ ActionHandler class
136	class ActionHandler implements ActionListener
137	{
138		public void actionPerformed(ActionEvent evt)
139		{
140			Font font;
141
142			JDialog dialog = GUIUtilities.getParentDialog(FontSelector.this);
143			if(dialog == null)
144			{
145				font = new FontSelectorDialog(
146					JOptionPane.getFrameForComponent(
147					FontSelector.this),getFont(),
148					FontSelector.this)
149					.getSelectedFont();
150			}
151			else
152			{
153				font = new FontSelectorDialog(dialog,getFont(),
154					FontSelector.this)
155					.getSelectedFont();
156			}
157
158			if(font != null)
159			{
160				setFont(font);
161				updateText();
162			}
163		}
164	} //}}}
165} //}}}
166