📄 statusbar.java~1~
字号:
/*
* 11/14/2003
*
* StatusBar.java - The status bar used by RText.
* Copyright (C) 2003 Robert Futrell
* email@address.com
* www.website.com
*
* This file is a part of RText.
*
* RText 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.
*
* RText 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.rtext;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ResourceBundle;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.fife.ui.StatusBarPanel;
import org.fife.ui.rtextarea.IconGroup;
/**
* The status bar used by rtext. Contains fields for:
* <ul>
* <li>An informational/status message.
* <li>A line/column number.
* <li>An overwrite (insert) mode indicator.
* <li>A Caps Lock indicator.
* <li>A file "Read Only" mode indicator.
* <li>A "Rec" panel indicating whether a macro is recording.
* </ul>
*
* @author Robert Futrell
* @version 1.0
*/
class StatusBar extends org.fife.ui.StatusBar
implements PropertyChangeListener {
private JLabel rowAndColumnIndicator;
private JLabel overwriteModeIndicator;
private JLabel capsLockIndicator;
private JLabel readOnlyIndicator;
private JLabel recIndicator;
private int row, column;
private boolean rowColumnIndicatorVisible;
private StatusBarPanel overwritePanel;
private StatusBarPanel capsLockPanel;
private StatusBarPanel readOnlyPanel;
private StatusBarPanel recPanel;
private String fileSaveSuccessfulText;
private String openedFileText;
private String rowColumnText;
private RText rtext;
private static final String BUNDLE_NAME = "org.fife.rtext.StatusBar";
/*****************************************************************************/
/**
* Creates the status bar.
*
* @param defaultMessage The default status message for this status bar.
* @param showRowColumn If true, the row/column of the caret are displayed.
* @param newRow The initial value of the row that is displayed.
* @param newColumn The initial value fo the column that is displayed.
* @param overwriteModeEnabled If <code>true</code>, overwrite mode
* indicator ("OVR") is enabled.
*/
public StatusBar(RText rtext, String defaultMessage, boolean showRowColumn,
int newRow, int newColumn, boolean overwriteModeEnabled) {
super(defaultMessage);
// Get the private variables.
this.rtext = rtext;
ResourceBundle msg = ResourceBundle.getBundle(BUNDLE_NAME);
// Initialize private variables.
fileSaveSuccessfulText = msg.getString("FileSaveSuccessful");
openedFileText = msg.getString("OpenedFile");
rowColumnText = msg.getString("RowColumnIndicator");
row = newRow;
column = newColumn; // DON'T call setRowAndColumn() yet!
rowAndColumnIndicator = new JLabel();
rowColumnIndicatorVisible = !showRowColumn; // So next line works.
setRowColumnIndicatorVisible(showRowColumn);
readOnlyIndicator = createLabel(msg, "ReadOnlyIndicator");
capsLockIndicator = createLabel(msg, "CapsLockIndicator");
overwriteModeIndicator = createLabel(msg, "OverwriteModeIndicator");
overwriteModeIndicator = createLabel(msg, "OverwriteModeIndicator");
recIndicator = createLabel(msg, "RecordingMacroIndicator");
recIndicator.setForeground(Color.RED);
// Make the layout such that different items can be different sizes.
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
// Create a Read Only indicator.
c.weightx = 0.0;
readOnlyPanel = new StatusBarPanel(new BorderLayout(),
readOnlyIndicator);
readOnlyIndicator.setEnabled(false);
addStatusBarComponent(readOnlyPanel, c);
// Create a Caps lock indicator.
c.weightx = 0.0;
capsLockPanel = new StatusBarPanel(new BorderLayout(),
capsLockIndicator);
// On Mac OS X at least, the OS doesn't support getLockingKeyState().
try {
capsLockIndicator.setEnabled(Toolkit.getDefaultToolkit().
getLockingKeyState(KeyEvent.VK_CAPS_LOCK));
} catch (UnsupportedOperationException e) {
capsLockIndicator.setText("-");
setCapsLockIndicatorEnabled(false);
}
addStatusBarComponent(capsLockPanel, c);
// Create and add a panel containing the overwrite/insert message.
c.weightx = 0.0;
overwritePanel = new StatusBarPanel(new BorderLayout(),
overwriteModeIndicator);
setOverwriteModeIndicatorEnabled(overwriteModeEnabled);
addStatusBarComponent(overwritePanel, c);
// Create a "Rec" panel for macros.
c.weightx = 0.0;
recPanel = new StatusBarPanel(new BorderLayout(),
recIndicator);
setRecIndicatorEnabled(false);
addStatusBarComponent(recPanel, c);
// Create and add a panel containing the row and column.
c.weightx = 0.0;
StatusBarPanel temp1 = new StatusBarPanel(new BorderLayout()) {
public Dimension getMinimumSize() {
return new Dimension(50, super.getMinimumSize().height);
}
public Dimension getPreferredSize() {
Dimension preferredSize = super.getPreferredSize();
return new Dimension(Math.max(50,preferredSize.width),
preferredSize.height);
}
};
temp1.add(rowAndColumnIndicator);
addStatusBarComponent(temp1, c);
}
/*****************************************************************************/
private static JLabel createLabel(ResourceBundle bundle, String key) {
JLabel label = new JLabel(bundle.getString(key));
label.setHorizontalAlignment(JLabel.CENTER);
return label;
}
/*****************************************************************************/
/**
* Decrements the value of column in row/column indicator.
*/
public void decColumn() {
setRowAndColumn(row, Math.max(column-1, 0));
}
/*****************************************************************************/
/**
* Decrements the value of row in row/column indicator.
*/
public void decRow() {
setRowAndColumn(Math.max(row-1, 0), column);
}
/*****************************************************************************/
/**
* Increments the value of column in row/column indicator.
*/
public void incColumn() {
setRowAndColumn(row, column+1);
}
/*****************************************************************************/
/**
* Increments the value of row in row/column indicator.
*/
public void incRow() {
setRowAndColumn(row+1, column);
}
/*****************************************************************************/
/**
* Returns <code>true</code> if the caps lock indicator is enabled.
*/
public boolean isCapsLockIndicatorEnabled() {
return capsLockIndicator.isEnabled();
}
/*****************************************************************************/
/**
* Returns <code>true</code> if overwrite mode indicator is enabled.
*/
public boolean isOverwriteModeIndicatorEnabled() {
return overwriteModeIndicator.isEnabled();
}
/*****************************************************************************/
/**
* Returns <code>true</code> if the Read Only indicator is enabled.
*/
public boolean isReadOnlyIndicatorEnabled() {
return readOnlyIndicator.isEnabled();
}
/*****************************************************************************/
/**
* Returns whether or not the "Rec" (macro recording) indicator is enabled.
*
* @return Whether or not the "Rec" indicator is enabled.
* @see #setRecIndicatorEnabled
*/
public boolean isRecIndicatorEnabled() {
return recIndicator.isEnabled();
}
/*****************************************************************************/
/**
* Returns whether or not the row/column indicator is visible.
*/
public boolean isRowColumnIndicatorVisible() {
return rowColumnIndicatorVisible;
}
/*****************************************************************************/
/**
* Called whenever a property changes on a component we're listening to.
*/
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
// If a file was just saved...
if (propertyName.equals(RTextEditorPane.MODIFIED_PROPERTY)) {
boolean wasDirty = ((Boolean)e.getOldValue()).booleanValue();
boolean isDirty = ((Boolean)e.getNewValue()).booleanValue();
if (wasDirty && !isDirty)
setStatusMessage(fileSaveSuccessfulText);
}
// If they opened a file...
else if (propertyName.equals(AbstractMainView.NEW_FILE_ADDED_PROPERTY) ||
propertyName.equals(AbstractMainView.OLD_FILE_ADDED_PROPERTY)) {
// Need to "escape" double-quotes as replaceFirst() below treats
// fileName as a regex, eliminating backslashes.
String fileName = ((String)e.getNewValue()).
replaceAll("\\\\", "\\\\\\\\");
setStatusMessage(openedFileText.replaceFirst(
"%fileName%", fileName));
}
// If they saved a read-only file with a different filename (hence it
// is now not read-only)...
// NOTE: We don't have the "else" below because we'll get both this
// property change notification and a MODIFIED_PROPERTY one.
if (propertyName.equals(RTextEditorPane.READ_ONLY_PROPERTY)) {
setReadOnlyIndicatorEnabled(
((Boolean)e.getNewValue()).booleanValue());
}
}
/*****************************************************************************/
/**
* Changes whether the caps lock indicator is enabled or disabled. This
* should be called whenever the user presses CAPS LOCK, perhaps through a
* <code>KeyListener</code>.
*
* @param enabled If <code>true</code>, the caps indicator ("OVR") is
* enabled; if <code>false</code>, it is disabled.
*/
public void setCapsLockIndicatorEnabled(boolean enabled) {
capsLockIndicator.setEnabled(enabled);
}
/*****************************************************************************/
/**
* Setter function for the column in row/column indicator.
*
* @param newColumn The column value to display for the caret.
*/
public void setColumn(int newColumn) {
setRowAndColumn(row, newColumn);
}
/*****************************************************************************/
/**
* Changes whether the overwrite indicator is enabled or disabled.
*
* @param enabled If <code>true</code>, the overwrite indicator ("OVR") is
* enabled; if <code>false</code>, it is disabled.
*/
public void setOverwriteModeIndicatorEnabled(boolean enabled) {
overwriteModeIndicator.setEnabled(enabled);
}
/*****************************************************************************/
/**
* Changes whether the Read Only indicator is enabled or disabled.
*
* @param enabled If <code>true</code>, the read-only indicator is enabled;
* if <code>false</code>, it is disabled.
*/
public void setReadOnlyIndicatorEnabled(boolean enabled) {
readOnlyIndicator.setEnabled(enabled);
}
/*****************************************************************************/
/**
* Sets whether or not the "Rec" (macro recording) indicator is enabled.
*
* @param enabled Whether or not the "Rec" indicator should be enabled.
* @see #isRecIndicatorEnabled
*/
public void setRecIndicatorEnabled(boolean enabled) {
recIndicator.setEnabled(enabled);
}
/*****************************************************************************/
/**
* Setter function for the row in row/column indicator.
*
* @param newRow The row value to display for the caret.
*/
public void setRow(int newRow) {
setRowAndColumn(newRow, column);
}
/*****************************************************************************/
/**
* Setter function for row/column part of status bar.
*
* @param newRow The row value to display for the caret.
* @param newColumn The column value to display for the caret.
*/
public void setRowAndColumn(int newRow, int newColumn) {
row = newRow;
column = newColumn;
updateRowColumnDisplay();
}
/*****************************************************************************/
/**
* Enables or disables the row/column indicator.
*
* @param isVisible Whether or not the row/column indicator should be
* visible.
*/
public void setRowColumnIndicatorVisible(boolean isVisible) {
if (isVisible != rowColumnIndicatorVisible) {
rowColumnIndicatorVisible = isVisible;
if (isVisible)
updateRowColumnDisplay();
else
rowAndColumnIndicator.setText("-");
}
}
/*****************************************************************************/
/**
* Updates the row/column indicator to reflect the current caret
* location, if it is enabled.
*/
private void updateRowColumnDisplay() {
if (rowColumnIndicatorVisible) {
String text = rowColumnText.replaceFirst("%line%", ""+row).
replaceFirst("%column%", ""+column);
rowAndColumnIndicator.setText(text);
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -