PageRenderTime 281ms CodeModel.GetById 275ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 127 lines | 77 code | 13 blank | 37 comment | 7 complexity | d7eea6c087fd7b42cabe54571f172ea4 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.GUIUtilities;
 31import org.gjt.sp.jedit.OperatingSystem;
 32//}}}
 33
 34/**
 35 * A button that, when clicked, shows a color chooser.
 36 *
 37 * You can get and set the currently selected color using
 38 * {@link #getSelectedColor()} and {@link #setSelectedColor(Color)}.
 39 * @author Slava Pestov
 40 * @version $Id: ColorWellButton.java 18910 2010-10-31 14:04:47Z daleanson $
 41 */
 42public class ColorWellButton extends JButton
 43{
 44	//{{{ ColorWellButton constructor
 45	public ColorWellButton(Color color)
 46	{
 47		setIcon(new ColorWell(color));
 48		setMargin(new Insets(2,2,2,2));
 49		addActionListener(new ActionHandler());
 50
 51		// according to krisk this looks better on OS X...
 52		if(OperatingSystem.isMacOSLF())
 53			putClientProperty("JButton.buttonType","toolbar");
 54	} //}}}
 55
 56	//{{{ getSelectedColor() method
 57	public Color getSelectedColor()
 58	{
 59		return ((ColorWell)getIcon()).color;
 60	} //}}}
 61
 62	//{{{ setSelectedColor() method
 63	public void setSelectedColor(Color color)
 64	{
 65		((ColorWell)getIcon()).color = color;
 66		repaint();
 67		fireStateChanged();
 68	} //}}}
 69
 70	//{{{ ColorWell class
 71	static class ColorWell implements Icon
 72	{
 73		Color color;
 74
 75		ColorWell(Color color)
 76		{
 77			this.color = color;
 78		}
 79
 80		public int getIconWidth()
 81		{
 82			return 35;
 83		}
 84
 85		public int getIconHeight()
 86		{
 87			return 10;
 88		}
 89
 90		public void paintIcon(Component c, Graphics g, int x, int y)
 91		{
 92			if(color == null)
 93				return;
 94
 95			g.setColor(color);
 96			g.fillRect(x,y,getIconWidth(),getIconHeight());
 97			g.setColor(color.darker());
 98			g.drawRect(x,y,getIconWidth()-1,getIconHeight()-1);
 99		}
100	} //}}}
101
102	//{{{ ActionHandler class
103	class ActionHandler implements ActionListener
104	{
105		public void actionPerformed(ActionEvent evt)
106		{
107			JDialog parent = GUIUtilities.getParentDialog(ColorWellButton.this);
108			Color c = null;
109			if (parent != null)
110			{
111				c = JColorChooser.showDialog(parent,
112					jEdit.getProperty("colorChooser.title"),
113					ColorWellButton.this.getSelectedColor());
114			}
115			else
116			{
117				c = JColorChooser.showDialog(
118					SwingUtilities.getRoot(ColorWellButton.this),
119					jEdit.getProperty("colorChooser.title"),
120					ColorWellButton.this.getSelectedColor());
121			}
122			if (c != null) {
123				setSelectedColor(c);	
124			}
125		}
126	} //}}}
127}