PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/ColorWellButton.java

#
Java | 113 lines | 63 code | 13 blank | 37 comment | 5 complexity | 87a749f7dfb2a86f93c4f961654c9f60 MD5 | raw file
  1/*
  2 * ColorWellButton.java - Shows color chooser when clicked
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2002 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import javax.swing.*;
 27import java.awt.event.*;
 28import java.awt.*;
 29import org.gjt.sp.jedit.jEdit;
 30import org.gjt.sp.jedit.OperatingSystem;
 31//}}}
 32
 33/**
 34 * A button that, when clicked, shows a color chooser.
 35 *
 36 * You can get and set the currently selected color using
 37 * <code>getSelectedColor()</code> and <code>setSelectedColor().
 38 * @author Slava Pestov
 39 * @version $Id: ColorWellButton.java 4301 2002-08-12 20:08:01Z spestov $
 40 */
 41public class ColorWellButton extends JButton
 42{
 43	//{{{ ColorWellButton constructor
 44	public ColorWellButton(Color color)
 45	{
 46		setIcon(new ColorWell(color));
 47		setMargin(new Insets(2,2,2,2));
 48		addActionListener(new ActionHandler());
 49
 50		// according to krisk this looks better on OS X...
 51		if(OperatingSystem.isMacOSLF())
 52			putClientProperty("JButton.buttonType","toolbar");
 53	} //}}}
 54
 55	//{{{ getSelectedColor() method
 56	public Color getSelectedColor()
 57	{
 58		return ((ColorWell)getIcon()).color;
 59	} //}}}
 60
 61	//{{{ setSelectedColor() method
 62	public void setSelectedColor(Color color)
 63	{
 64		((ColorWell)getIcon()).color = color;
 65		repaint();
 66	} //}}}
 67
 68	//{{{ ColorWell class
 69	static class ColorWell implements Icon
 70	{
 71		Color color;
 72
 73		ColorWell(Color color)
 74		{
 75			this.color = color;
 76		}
 77
 78		public int getIconWidth()
 79		{
 80			return 35;
 81		}
 82
 83		public int getIconHeight()
 84		{
 85			return 10;
 86		}
 87
 88		public void paintIcon(Component c, Graphics g, int x, int y)
 89		{
 90			if(color == null)
 91				return;
 92
 93			g.setColor(color);
 94			g.fillRect(x,y,getIconWidth(),getIconHeight());
 95			g.setColor(color.darker());
 96			g.drawRect(x,y,getIconWidth()-1,getIconHeight()-1);
 97		}
 98	} //}}}
 99
100	//{{{ ActionHandler class
101	class ActionHandler implements ActionListener
102	{
103		public void actionPerformed(ActionEvent evt)
104		{
105			Color color = JColorChooser.showDialog(
106				ColorWellButton.this,
107				jEdit.getProperty("colorChooser.title"),
108				getSelectedColor());
109			if(color != null)
110				setSelectedColor(color);
111		}
112	} //}}}
113}