📄 toolbar.java
字号:
/* * ToolBar.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */package org.underworldlabs.swing.toolbar;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Cursor;import java.awt.Dimension;import java.awt.Graphics;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Shape;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.util.ArrayList;import javax.swing.BorderFactory;import javax.swing.ButtonGroup;import javax.swing.JComboBox;import javax.swing.JComponent;import javax.swing.JPanel;import javax.swing.JToggleButton;import javax.swing.UIManager;import javax.swing.border.Border;import org.underworldlabs.swing.GUIUtils;import org.underworldlabs.swing.RolloverButton;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * * @author Takis Diakoumis * @version $Revision: 1.8 $ * @date $Date: 2006/08/11 12:32:07 $ */public class ToolBar extends AbstractToolBarPanel implements MouseListener, MouseMotionListener { /** The tool bar separator width */ public static final int SEPARATOR_WIDTH = 4; // --------------------------------------------- // --- Bound properties for tool bar changes --- // --------------------------------------------- /** Selected property */ public static final String TOOL_BAR_BEGIN_MOVING = "toolBarBeginMoving"; /** Moving property */ public static final String TOOL_BAR_MOVING = "toolBarMoving"; /** Moved property */ public static final String TOOL_BAR_MOVED = "toolBarMoved"; /** Selected property */ public static final String TOOL_BAR_SELECTED = "toolBarSelected"; /** Deselected property */ public static final String TOOL_BAR_DESELECTED = "toolBarDeselected"; /** Resize property */ public static final String TOOL_BAR_RESIZING = "toolBarResizing"; /** The tool bar's border */ protected static Border toolBarBorder; /** The top parent container */ protected ToolBarBase parent; /** The button panel */ protected JPanel buttonPanel; /** This tool bar's name */ protected String name; /** Button group for toggle buttons */ protected ButtonGroup buttonGroup; /** Component constraints */ protected GridBagConstraints gbc; /** The tool buttons added */ protected ArrayList toolButtons; /** Dummy object for separator */ private static final Object SEPARATOR = new Object(); // -------------------------------- // --- Tracking mouse movements --- // -------------------------------- /** The initial X position */ private int m_XDifference; /** The initial Y position */ private int m_YDifference; /** Whether the tool bar is being dragged */ private boolean dragging; /** The tool bar width + the x position */ private int rightX; /** Whether the tool bar is being resized */ private boolean resizing; /** The mouse region where resizing will begin */ private static final int resizeRegionX = 2; public ToolBar(ToolBarBase parent) { super(new BorderLayout()); this.parent = parent; toolButtons = new ArrayList(); ToolBarSelectionWidget moveWidget = new ToolBarSelectionWidget(); moveWidget.addMouseListener(this); moveWidget.addMouseMotionListener(this); buttonPanel = new JPanel(new GridBagLayout()); buttonPanel.setOpaque(false); add(moveWidget, BorderLayout.WEST); add(buttonPanel, BorderLayout.CENTER); if (toolBarBorder == null) { toolBarBorder = BorderFactory.createEtchedBorder(); } gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.WEST; gbc.insets.left = 2; gbc.insets.top = 1; gbc.insets.bottom = 1; setBorder(toolBarBorder); } public ToolBar(ToolBarBase parent, String name) { this(parent); this.name = name; } public void enableButtonsSelection(boolean enable) { for (int i = 0, k = toolButtons.size(); i < k; i++) { Object obj = toolButtons.get(i); if (obj instanceof RolloverButton) { ((RolloverButton)obj).enableSelectionRollover(enable); } } } public void addSeparator() { toolButtons.add(SEPARATOR); } public void addToggleButton(JToggleButton button) { if (buttonGroup == null) { buttonGroup = new ButtonGroup(); } buttonGroup.add(button); gbc.gridx++; buttonPanel.add(button, gbc); button.setSelected(true); } public void removeToggleButton(JToggleButton button) { buttonGroup.remove(button); buttonPanel.remove(button); repaint(); } public void addButton(JComponent button) { toolButtons.add(button); } public void buildToolBar() { Object obj = null; int buttonCount = toolButtons.size(); gbc.weightx = 0; for (int i = 0; i < buttonCount; i++) { gbc.gridx++; if (i + 1 == buttonCount - 1) { obj = toolButtons.get(i + 1); if (obj == SEPARATOR) { gbc.weightx = 1.0; } } obj = toolButtons.get(i); if (obj == SEPARATOR) { gbc.insets.left = SEPARATOR_WIDTH; continue; } else if (obj instanceof JComboBox) { gbc.insets.top = 1; } if (i == buttonCount - 1) { gbc.weightx = 1.0; } buttonPanel.add((JComponent)obj, gbc); gbc.insets.left = 0; gbc.insets.top = 1; } } public void removeAllButtons() { int size = toolButtons.size(); for (int i = 0; i < size; i++) { Object obj = toolButtons.get(i); if (obj instanceof Component) { buttonPanel.remove((Component)obj); } } toolButtons.clear(); } public void setName(String name) { this.name = name; } public String getName() { return name; } public void mouseDragged(MouseEvent e) { int ex = e.getX();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -