All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fife.ui.RColorSwatchesButton Maven / Gradle / Ivy

/*
 * 09/17/2004
 *
 * RColorSwatchesButton.java - A JButton that lets you pick a color via
 * a popup menu.
 * Copyright (C) 2004 Robert Futrell
 * http://fifesoft.com/rtext
 * Licensed under a modified BSD license.
 * See the included license file for details.
 */
package org.fife.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicMenuItemUI;


/**
 * A color button that, when clicked, displays a popup menu containing several
 * color "swatches" to chooser from.  The popup also contains a menu item that
 * brings up a JColorChooser if the user wants a color that does
 * not have a swatch.
 *
 * @author Robert Futrell
 * @version 0.6
 */
public class RColorSwatchesButton extends RColorButton {

	private static final long serialVersionUID = 1L;

	private static final Dimension SWATCH_MENUITEM_SIZE = new Dimension(24,24);
	private static final int NUM_COLUMNS			  = 6;
	private static final String MSG	  = "org.fife.ui.RColorSwatchesButton";

	private static final Color[] predefinedColors = new Color[] {
		Color.RED,
		new Color(255,128,128),
		new Color(255,153,0),
		new Color(204,102,0),
		Color.ORANGE,
		new Color(255,240,204),
		Color.YELLOW,
		new Color(255,255,164),
		new Color(148,148,0),
		new Color(188,188,40),
		new Color(0,128,0),
		Color.GREEN,
		new Color(48,255,47),
		new Color(218,254,218),
		new Color(0,128,164),
		Color.BLUE,
		Color.CYAN,
		new Color(231,231,255),
		Color.PINK,
		new Color(255,224,240),
		Color.MAGENTA,
		new Color(173,0,128),
		new Color(128,0,0),
		new Color(100,0,200),
		Color.WHITE,
		new Color(224,224,224),
		Color.LIGHT_GRAY,
		Color.GRAY,
		Color.DARK_GRAY,
		Color.BLACK,
	};

	private ColorPopupMenu popup;
	private PopupListener popupListener;


	/**
	 * Creates a new RColorSwatchesButton defaulting to black.
	 */
	public RColorSwatchesButton() {
		this(Color.BLACK);
	}


	/**
	 * Creates a new RColorSwatchesButton.
	 *
	 * @param color The initial color for the button.
	 */
	public RColorSwatchesButton(Color color) {
		super(color);
	}


	/**
	 * Creates a new RColorSwatchesButton.
	 *
	 * @param color The initial color for the button.
	 * @param width The width of the color in the button.
	 * @param height The height of the color in the button.
	 */
	public RColorSwatchesButton(Color color, int width, int height) {
		super(color, width, height);
	}


	/**
	 * Returns the listener listening for this button to be clicked.
	 *
	 * @return The action listener for this button.  For an
	 *         RColorSwatchesButton, the returned listener
	 *         displays a popup menu with color swatches, with an item that
	 *         pops up a JColorChooser.
	 */
	public ActionListener createActionListener() {
		return new SwatchesActionListener();
	}


	/**
	 * Overridden so the popup menu gets its LnF updated too.
	 */
	public void updateUI() {

		super.updateUI();

		// We don't just call SwingUtilites.updateComponentTreeUI() because
		// we don't want to update the custom UI on the swatches.
		if (popup!=null) {
			//SwingUtilities.updateComponentTreeUI(popup);
			popup.updateUI();
			// We only need to update the UI of the last child, as it is the
			// "More colors..." menu item.  All of the others are swatches,
			// and updating them would get rid of our custom UI.
			((JComponent)popup.getComponent(popup.getComponentCount()-1)).
													updateUI();
		}

	}


	/**
	 * Listens for the user to click on the RColorSwatchesButton
	 * so it can display the popup menu.
	 */
	private class SwatchesActionListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			if (popup==null) {
				popup = new ColorPopupMenu();
				popup.applyComponentOrientation(getComponentOrientation());
			}
			popup.show(RColorSwatchesButton.this,
				0, 0+RColorSwatchesButton.this.getHeight());
		}

	}


	/**
	 * The popup menu for this button.  It contains several color swatches,
	 * as well as an item that brings up a JColorChooser so the
	 * user can customize the color chosen.
	 */
	private class ColorPopupMenu extends JPopupMenu {

		private static final long serialVersionUID = 1L;

		public ColorPopupMenu() {

			// Create the listener.
			popupListener = new PopupListener();

			GridBagLayout layout = new GridBagLayout();
			setLayout(layout);
			GridBagConstraints c = new GridBagConstraints();

			int i = 0;
			int length = predefinedColors.length;
			while (i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy