org.fife.ui.CustomizableToolBar Maven / Gradle / Ivy
/*
* 04/13/2004
*
* CustomizableToolBar.java - A toolbar with a popup right-click menu allowing
* the user to toggle docking and (in the future) add/remove buttons and
* separators.
* 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.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;
import javax.swing.plaf.ToolBarUI;
import javax.swing.plaf.basic.BasicToolBarUI;
/**
* An extension of JToolBar
that adds a right-click popup
* menu allowing the user to toggle docking and add or remove buttons
* and separators.
* To use this class, create a subclass of it, and at the end of its
* constructor, call makeCustomizable
.
*
* @author Robert Futrell
* @version 0.1
*/
public class CustomizableToolBar extends JToolBar {
private static final long serialVersionUID = 1L;
/**
* The popup menu for the toolbar.
*/
private JPopupMenu popupMenu;
/**
* The menu for adding/removing toolbar buttons.
*/
private JMenu addRemoveMenu;
/**
* The mouse listener that listens for right-clicks on this toolbar.
*/
private MouseInputAdapter mia;
/**
* Whether text labels should be on the buttons, as well as images.
*/
private boolean showText;
private static final String MSG = "org.fife.ui.CustomizableToolBar";
/**
* Creates a new toolbar.
*/
public CustomizableToolBar() {
}
/**
* Creates a new toolbar with the specified orientation.
*
* @param orientation The initial orientation (either
* {@link SwingConstants#VERTICAL} or
* {@link SwingConstants#HORIZONTAL}).
*/
public CustomizableToolBar(int orientation) {
super(orientation);
}
/**
* Creates a new toolbar.
*
* @param name The name for the tool bar.
*/
public CustomizableToolBar(String name) {
super(name);
}
/**
* Creates a new toolbar.
*
* @param name The name for the tool bar.
* @param orientation The initial orientation (either
* {@link SwingConstants#VERTICAL} or
* {@link SwingConstants#HORIZONTAL}).
*/
public CustomizableToolBar(String name, int orientation) {
super(name, orientation);
}
/**
* Creates a button to add to this tool bar.
*
* @param a The action for the button.
* @return The button.
*/
protected JButton createButton(Action a) {
JButton b = new JButton(a);
b.setToolTipText((String)a.getValue(Action.NAME)); // May be null
b.setHorizontalTextPosition(JButton.CENTER);
b.setVerticalTextPosition(JButton.BOTTOM);
if (showText) {
b.setText((String)a.getValue(Action.NAME));
}
else {
b.setText(null);
}
String desc = (String)a.getValue(Action.SHORT_DESCRIPTION);
if (desc!=null) {
b.getAccessibleContext().setAccessibleDescription(desc);
}
return b;
}
/**
* Creates the popup menu.
*/
private void createPopupMenu() {
ResourceBundle msg = ResourceBundle.getBundle(MSG);
popupMenu = new JPopupMenu();
String temp = msg.getString("PopupMenu.LockToolbar.txt");
AbstractAction lockAction = new LockAction(temp);
JCheckBoxMenuItem lockMenuItem = new JCheckBoxMenuItem(lockAction);
lockMenuItem.setMnemonic(KeyEvent.VK_L);
popupMenu.add(lockMenuItem);
popupMenu.addSeparator();
temp = msg.getString("PopupMenu.AddRemoveButtons.txt");
addRemoveMenu = new JMenu(temp);
addRemoveMenu.setMnemonic(KeyEvent.VK_A);
populateAddRemovePopupMenu(msg);
popupMenu.add(addRemoveMenu);
}
/**
* Returns whether text labels are to be displayed on buttons, along
* with the images.
*
* @return Whether text labels are shown.
* @see #setShowText(boolean)
*/
public boolean getShowText() {
return showText;
}
/**
* This should be called at the end of the constructor of any toolbar
* that overrides this class. This is the method that sets up the
* popup menu for the toolbar. If you don't call this method, then
* a CustomizableToolBar
behaves no differently than a
* JToolBar
.
*/
public void makeCustomizable() {
// Remove an old mouse listener if makeCustomizable() has been
// called before.
if (mia!=null) {
removeMouseListener(mia);
for (int i=0; i