📄 statusbar.java
字号:
/*
* 07/26/2004
*
* StatusBar.java - A generic status bar containing a status message and an
* optional size grip, similar to those found in Microsoft
* Windows applications.
* Copyright (C) 2004 Robert Futrell
* email@address.com
* www.website.com
*
* 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.fife.ui;
import java.awt.AWTEvent;
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.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.BevelBorder;
/**
* A generic status bar containing a status message and a size grip,
* similar to those found in Microsoft Windows applications.<p>
* This component can be used as-is, or can be subclassed and have additional
* components added to it. If the latter, the user MUST add instances of
* {@link StatusBarPanel} and they MUST be added via the
* <code>addStatusBarComponent</code> methods; if you instead use one of
* <code>Container</code>'s <code>add</code> methods, there is no guarantee as
* to the flow/layout of the status bar.
*
* @author Robert Futrell
* @version 0.7
*/
public class StatusBar extends StatusBarPanel implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1410068134864915906L;
public static final String STYLE_PROPERTY = "StatusBar.style";
public static final int WINDOWS_98_STYLE = 0;
public static final int WINDOWS_XP_STYLE = 1;
//public static final int OFFICE_2003_STYLE = 2;
private static final int MIN_STYLE_CONSTANT = 0;
private static final int MAX_STYLE_CONSTANT = 1;//2;
public static final String DEFAULT_STATUS_MESSAGE = "Ready";
private JLabel statusMessage;
private String defaultMessage;
private GridBagLayout gridBag;
private SizeGrip sizeGrip;
private int style = -1;
private static Robot robot;
// Workaround for if X-Server doesn't support XTEST 2.2 -
// see Sun Bug ID 6329226.
private static boolean robotCreationAttempted;
/*****************************************************************************/
/**
* Creates the status bar with a default status message and a size grip.
*/
public StatusBar() {
this(DEFAULT_STATUS_MESSAGE);
}
/*****************************************************************************/
/**
* Creates the status bar with the specified default message and a size
* grip.
*
* @param defaultStatusMessage The default message to display.
*/
public StatusBar(String defaultStatusMessage) {
this(defaultStatusMessage, WINDOWS_XP_STYLE);
}
/*****************************************************************************/
/**
* Creates the status bar.
*
* @param defaultStatusMessage The default message to display.
* @param style The style with which to paint the status bar.
*/
public StatusBar(String defaultStatusMessage, int style) {
// Call JPanel's constructor.
super();
// Initialize private variables.
defaultMessage = defaultStatusMessage;
statusMessage = new JLabel(defaultStatusMessage);
// Try to create a Robot, if necessary.
try {
if (!robotCreationAttempted) { // Sun Bug ID 6329226
robotCreationAttempted = true;
robot = new Robot();
}
// Could be an AWTException or a SecurityException...
// either way, this system does not support Robot.
} catch(Exception e) {
e.printStackTrace();
}
// Make the layout such that different items can be different sizes.
gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridBag);
c.fill = GridBagConstraints.BOTH;
// Create and add a panel containing the "status message."
// The message panel uses GridLayout to keep the message left-aligned.
c.weightx = 1.0;
JPanel status = new StatusBarPanel(new GridLayout(1,1,8,8),
statusMessage);
gridBag.setConstraints(status, c);
add(status);
// Add the size grip.
this.sizeGrip = new SizeGrip();
c.weightx = 0.0;
gridBag.setConstraints(this.sizeGrip, c);
add(this.sizeGrip);
setStyle(style);
setBorder(getStatusBarBorder());
}
/*****************************************************************************/
/**
* Adds a component to this status bar.
*
* @param panel The panel to add.
* @param index The index at which to place this component. A value of
* <code>0</code> means "right after the status message."
* @param constraints The constraints with which to add the panel.
*/
public void addStatusBarComponent(StatusBarPanel panel,
int index,
GridBagConstraints constraints) {
gridBag.setConstraints(panel, constraints);
panel.setBorder(getStatusBarComponentBorder());
index++; // As we're 0-based, but 0==status message panel.
if (index<1)
index = 1;
else if (index>getComponentCount()-fromRight())
index = getComponentCount() - fromRight();
add(panel, index);
}
/*****************************************************************************/
/**
* Adds a component to the end of this status bar (but before the size
* grip). This is the preferred method of adding components.
*
* @param panel The panel to add.
* @param constraints The constraints with which to add the panel.
*/
public void addStatusBarComponent(StatusBarPanel panel,
GridBagConstraints constraints) {
// Put in front of the size grip.
int index = getComponentCount() - fromRight();
addStatusBarComponent(panel, index, constraints);
}
/*****************************************************************************/
/**
* Determines how many panels "over" from the right to add a new panel.
* This method is here because the XP-style uses a "spacer" filler panel
* to have space between the size grip and the line/column indicator, so
* for different styles, panels must be added/removed in different places.
*
* @return The number of panels on the right of the status bar that must
* stay on the right.
*/
private final int fromRight() {
if (getStyle()==WINDOWS_XP_STYLE)
return 2;
return 1;
}
/*****************************************************************************/
/**
* Returns the <code>Robot</code> instance shared among all status
* bars.
*
* @return The <code>Robot</code>, or <code>null</code> if the OS does
* not support it.
*/
private static Robot getRobot() {
return robot;
}
/*****************************************************************************/
/**
* Returns the border for the size grip, depending on the current style.
*
* @return The border.
*/
private Border getSizeGripBorder() {
switch (style) {
default:
return null;
}
}
/*****************************************************************************/
/**
* Returns a border for the status bar.
*
* @return The border.
*/
private Border getStatusBarBorder() {
switch (style) {
case WINDOWS_98_STYLE:
return BorderFactory.createEmptyBorder(0,5,0,0);
case WINDOWS_XP_STYLE:
return BorderFactory.createEmptyBorder(0,5,0,0);
//return new StatusBarTopShadowBorder();
default:
return BorderFactory.createEmptyBorder(0,5,0,0);
}
}
/*****************************************************************************/
/**
* Returns a border suitable for most status bar components.
*
* @return The border.
*/
private Border getStatusBarComponentBorder() {
switch (style) {
case WINDOWS_98_STYLE:
return BorderFactory.createCompoundBorder(
BorderFactory.createBevelBorder(BevelBorder.LOWERED),
BorderFactory.createEmptyBorder(0,3,0,3));
case WINDOWS_XP_STYLE:
return BorderFactory.createCompoundBorder(
new BevelDividerBorder(BevelDividerBorder.LEFT, 2),
BorderFactory.createEmptyBorder(0,3,0,3));
default:
return null;
}
}
/*****************************************************************************/
/**
* Returns the message in the status bar.
*
* @return The message in the status bar.
* @see #setStatusMessage
*/
public String getStatusMessage() {
return statusMessage.getText();
}
/*****************************************************************************/
/**
* Returns the "style" used to paint this status bar.
*
* @return The style of this status bar, such as
* <code>WINDOWS_XP_STYLE</code>.
* @see #setStyle
*/
public int getStyle() {
return style;
}
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -