📄 rtextareaoptionpanel.java
字号:
/*
* 10/29/2004
*
* RTextAreaOptionPanel.java - An options panel that can be added to
* org.fife.ui.OptionsDialog for an RTextArea.
* 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.rtextarea;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.text.Document;
import org.fife.ColorCellEditor;
import org.fife.ColorCellRenderer;
import org.fife.RUtilities;
import org.fife.ui.OptionsDialogPanel;
import org.fife.ui.RButton;
import org.fife.ui.RColorSwatchesButton;
import org.fife.ui.RScrollPane;
import org.fife.ui.UIUtilities;
/**
* An options panel that can be added to an
* <code>org.fife.ui.OptionsDialog</code> to display options appropriate for an
* <code>RTextArea</code>. This panel allows the user to change the
* following properties of the text area:
* <ul>
* <li>Caret color</li>
* <li>Selection color</li>
* <li>Mark All color</li>
* <li>Caret style for both insert and overwrite</li>
* <li>Caret blink rate</li>
* <li>Tab size and emulation</li>
* <li>Background style (color or image)</li>
* <li>Word wrap</li>
* <li>Current line highlighting and color</li>
* <li>Margin line and color</li>
* <li>Rounded selection edges</li>
* </ul>
* It also gives the user a button to restore the default values.
*
* @author Robert Futrell
* @version 0.5
*/
public class RTextAreaOptionPanel extends OptionsDialogPanel
implements ActionListener, ChangeListener, DocumentListener,
PropertyChangeListener {
/**
*
*/
private static final long serialVersionUID = 5290764043960111327L;
private JPanel generalPanel;
private JTable generalTable;
private GeneralTableModel generalTableModel;
private JComboBox insCaretCombo;
private JComboBox overCaretCombo;
private JSpinner blinkRateSpinner;
private JTextField backgroundField;
private RButton backgroundButton;
private BackgroundDialog backgroundDialog;
private Object background;
private String bgImageFileName; // null if background is a color.
private JCheckBox wordWrapCheckBox;
private JCheckBox highlightCurrentLineCheckBox;
private RColorSwatchesButton hclColorButton;
private JLabel tabSizeLabel;
private JTextField tabSizeField;
private int tabSize;
private JCheckBox emulateTabsCheckBox;
private JCheckBox marginLineCheckBox;
private JTextField marginLinePositionField;
private JLabel marginLineColorLabel;
private RColorSwatchesButton marginLineColorButton;
private int marginLinePosition;
private JCheckBox roundedSelectionCheckBox;
private RButton restoreDefaultsButton;
private static final String BACKGROUND_PROPERTY = "background";
private static final String CARET_BLINK_RATE_PROPERTY = "caretBlinkRate";
private static final String DEFAULTS_RESTORED = "defaultsRestored";
private static final String EMULATE_TABS_PROPERTY = "emulateTabsCheckBox";
private static final String GENERAL_COLOR_PROPERTY = "generalColor";
private static final String WORD_WRAP_PROPERTY = "wordWrap";
private static final String HCL_PROPERTY = "highlightCurrentLineCheckBox";
private static final String HCLC_PROPERTY = "highlightCurrentLineColor";
private static final String INSERT_CARET_PROPERTY = "insertCaret";
private static final String MARGIN_LINE_PROPERTY = "marginLineCheckBox";
private static final String MARGIN_LINE_POS_PROPERTY = "marginLinePosition";
private static final String OVERWRITE_CARET_PROPERTY = "overwriteCaret";
private static final String ROUNDED_SELECTION_PROPERTY = "roundedSelection";
private static final String TAB_SIZE_PROPERTY = "tabSize";
private static final String UNKNOWN_PROPERTY = "unknown";
/*****************************************************************************/
/**
* Constructor.
*/
public RTextAreaOptionPanel() {
super();
Dimension spacer = new Dimension(5,5);
ResourceBundle resources = ResourceBundle.getBundle(
"org.fife.ui.rtextarea.OptionPanel");
setName(resources.getString("Title"));
setBorder(UIUtilities.getEmpty5Border());
setLayout(new BorderLayout());
// We'll add everything to this panel, then add this panel so that
// stuff stays at the "top."
JPanel topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
generalPanel = new JPanel(new GridLayout(1,1));
setGeneralPanelBorderTitle(resources.getString("General"));
generalTableModel = new GeneralTableModel(
resources.getString("Property"),
resources.getString("Color"),
resources.getString("Caret"),
resources.getString("Selection"),
resources.getString("MarkAll"));
generalTable = new JTable(generalTableModel) {
public Dimension getPreferredScrollableViewportSize() {
Dimension s = super.getPreferredScrollableViewportSize();
s.height = getRowHeight()*2;
return s;
}
};
generalTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
generalTable.getTableHeader().setReorderingAllowed(false);
generalTable.setDefaultRenderer(Color.class, new ColorCellRenderer());
generalTable.setDefaultEditor(Color.class, new ColorCellEditor());
RScrollPane generalScrollPane = new RScrollPane(
300, generalTable.getRowHeight()*5,
generalTable);
generalPanel.add(generalScrollPane);
topPanel.add(generalPanel);
JPanel caretPanel = new JPanel();
caretPanel.setLayout(new BoxLayout(caretPanel, BoxLayout.Y_AXIS));
caretPanel.setBorder(new OptionPanelBorder(resources.getString("Carets")));
JPanel temp = new JPanel(new SpringLayout());
JLabel insLabel = new JLabel(resources.getString("InsertCaret"));
insCaretCombo = createCaretComboBox(resources);
insCaretCombo.setActionCommand("InsertCaretCombo");
insCaretCombo.addActionListener(this);
insLabel.setLabelFor(insCaretCombo);
temp.add(insLabel);
temp.add(insCaretCombo);
JLabel overLabel = new JLabel(resources.getString("OverwriteCaret"));
overCaretCombo = createCaretComboBox(resources);
overCaretCombo.setActionCommand("OverwriteCaretCombo");
overCaretCombo.addActionListener(this);
overLabel.setLabelFor(overCaretCombo);
temp.add(overLabel);
temp.add(overCaretCombo);
JLabel caretDelayLabel = new JLabel(resources.getString("BlinkRate"));
SpinnerNumberModel spinnerModel = new SpinnerNumberModel(500, 0,10000, 50);
blinkRateSpinner = new JSpinner(spinnerModel);
blinkRateSpinner.addChangeListener(this);
caretDelayLabel.setLabelFor(blinkRateSpinner);
temp.add(caretDelayLabel);
temp.add(blinkRateSpinner);
RUtilities.makeSpringCompactGrid(temp, 3,2, 0,0, 5,5);
caretPanel.add(temp);
topPanel.add(caretPanel);
JPanel tabPanel = new JPanel();
tabPanel.setLayout(new BoxLayout(tabPanel, BoxLayout.Y_AXIS));
tabPanel.setBorder(new OptionPanelBorder(resources.getString("Tabs")));
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));
tabSizeLabel = new JLabel(resources.getString("TabSize"));
inputPanel.add(tabSizeLabel);
tabSizeField = new JTextField();
tabSizeField.getDocument().addDocumentListener(this);
Dimension size = new Dimension(40,tabSizeField.getPreferredSize().height);
tabSizeField.setMaximumSize(size);
tabSizeField.setPreferredSize(size);
inputPanel.add(tabSizeField);
inputPanel.add(Box.createHorizontalGlue());
tabPanel.add(inputPanel);
JPanel etPanel = new JPanel();
etPanel.setLayout(new BoxLayout(etPanel, BoxLayout.X_AXIS));
emulateTabsCheckBox = new JCheckBox(resources.getString("EmulateTabs"));
emulateTabsCheckBox.setActionCommand("EmulateTabsCheckBox");
emulateTabsCheckBox.addActionListener(this);
etPanel.add(emulateTabsCheckBox);
etPanel.add(Box.createHorizontalGlue());
tabPanel.add(etPanel);
tabPanel.add(Box.createVerticalGlue());
topPanel.add(tabPanel);
topPanel.add(Box.createRigidArea(spacer));
JPanel bigOtherPanel = new JPanel();
bigOtherPanel.setLayout(new BoxLayout(bigOtherPanel, BoxLayout.Y_AXIS));
bigOtherPanel.setBorder(new OptionPanelBorder(resources.getString("Other")));
JPanel otherPanel = new JPanel();
otherPanel.setLayout(new BoxLayout(otherPanel, BoxLayout.X_AXIS));
JLabel bgLabel = new JLabel(resources.getString("Background"));
backgroundField = new JTextField(20);
backgroundField.setEditable(false);
backgroundButton = new RButton(resources.getString("Change"));
backgroundButton.setActionCommand("BackgroundButton");
backgroundButton.addActionListener(this);
bgLabel.setLabelFor(backgroundButton);
otherPanel.add(bgLabel);
otherPanel.add(Box.createHorizontalStrut(5));
otherPanel.add(backgroundField);
otherPanel.add(Box.createHorizontalStrut(5));
otherPanel.add(backgroundButton);
otherPanel.add(Box.createHorizontalGlue());
bigOtherPanel.add(otherPanel);
otherPanel = new JPanel();
otherPanel.setLayout(new BoxLayout(otherPanel, BoxLayout.X_AXIS));
wordWrapCheckBox = new JCheckBox(resources.getString("WordWrap"));
wordWrapCheckBox.setActionCommand("WordWrapCheckBox");
wordWrapCheckBox.addActionListener(this);
otherPanel.add(wordWrapCheckBox);
otherPanel.add(Box.createHorizontalGlue());
bigOtherPanel.add(otherPanel);
otherPanel = new JPanel();
otherPanel.setLayout(new BoxLayout(otherPanel, BoxLayout.X_AXIS));
highlightCurrentLineCheckBox = new JCheckBox(resources.getString("HighlightCL"));
highlightCurrentLineCheckBox.setActionCommand("HighlightCurrentLineCheckBox");
highlightCurrentLineCheckBox.addActionListener(this);
hclColorButton = new RColorSwatchesButton(Color.BLACK, 50,15);
hclColorButton.addPropertyChangeListener(this);
otherPanel.add(highlightCurrentLineCheckBox);
otherPanel.add(hclColorButton);
otherPanel.add(Box.createHorizontalGlue());
bigOtherPanel.add(otherPanel);
otherPanel = new JPanel();
otherPanel.setLayout(new BoxLayout(otherPanel, BoxLayout.X_AXIS));
marginLineCheckBox = new JCheckBox(resources.getString("DrawML"));
marginLineCheckBox.setActionCommand("MarginLineCheckBox");
marginLineCheckBox.addActionListener(this);
marginLinePositionField = new JTextField();
marginLinePositionField.getDocument().addDocumentListener(this);
size = new Dimension(40,marginLinePositionField.getPreferredSize().height);
marginLinePositionField.setMaximumSize(size);
marginLinePositionField.setPreferredSize(size);
marginLineColorLabel = new JLabel(resources.getString("WithThisColor"));
marginLineColorButton = new RColorSwatchesButton(Color.BLACK, 50,15);
marginLineColorButton.addPropertyChangeListener(this);
marginLineColorLabel.setLabelFor(marginLineColorButton);
otherPanel.add(marginLineCheckBox);
otherPanel.add(marginLinePositionField);
otherPanel.add(Box.createHorizontalStrut(5));
otherPanel.add(marginLineColorLabel);
otherPanel.add(marginLineColorButton);
otherPanel.add(Box.createHorizontalGlue());
bigOtherPanel.add(otherPanel);
otherPanel = new JPanel();
otherPanel.setLayout(new BoxLayout(otherPanel, BoxLayout.X_AXIS));
roundedSelectionCheckBox = new JCheckBox(resources.getString("RoundSel"));
roundedSelectionCheckBox.setActionCommand("RoundedSelectionCheckBox");
roundedSelectionCheckBox.addActionListener(this);
otherPanel.add(roundedSelectionCheckBox);
otherPanel.add(Box.createHorizontalGlue());
bigOtherPanel.add(otherPanel);
bigOtherPanel.add(Box.createRigidArea(spacer));
topPanel.add(bigOtherPanel);
JPanel rdPanel = new JPanel();
rdPanel.setLayout(new BoxLayout(rdPanel, BoxLayout.X_AXIS));
restoreDefaultsButton = new RButton(resources.getString("RestoreDefaults"));
restoreDefaultsButton.setActionCommand("RestoreDefaults");
restoreDefaultsButton.addActionListener(this);
rdPanel.add(restoreDefaultsButton);
rdPanel.add(Box.createHorizontalGlue());
topPanel.add(rdPanel);
add(topPanel, BorderLayout.NORTH);
resources = null;
}
/*****************************************************************************/
/**
* Listens for actions in this panel.
*/
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
// If the user clicked the "restore defaults" button.
if (actionCommand.equals("RestoreDefaults")) {
Color defaultCaretColor = RTextArea.getDefaultCaretColor();
Color defaultSelectionColor = RTextArea.getDefaultSelectionColor();
Color defaultMarkAllColor = RTextArea.getDefaultMarkAllHighlightColor();
Color defaultCurrentLineHighlightColor = RTextArea.getDefaultCurrentLineHighlightColor();
int defaultTabSize = RTextArea.getDefaultTabSize();
int defaultMarginLinePosition = RTextArea.getDefaultMarginLinePosition();
Color defaultMarginLineColor = RTextArea.getDefaultMarginLineColor();
int defaultInsertCaret = ConfigurableCaret.VERTICAL_LINE_STYLE;
int defaultOverwriteCaret = ConfigurableCaret.BLOCK_STYLE;
Integer defaultCaretBlinkRate = new Integer(500);
if ( !getCaretColor().equals(defaultCaretColor) ||
!getSelectionColor().equals(defaultSelectionColor) ||
!getMarkAllHighlightColor().equals(defaultMarkAllColor) ||
getCaretStyle(RTextArea.INSERT_MODE)!=defaultInsertCaret ||
getCaretStyle(RTextArea.OVERWRITE_MODE)!=defaultOverwriteCaret ||
!blinkRateSpinner.getValue().equals(defaultCaretBlinkRate) ||
!Color.WHITE.equals(background) ||
wordWrapCheckBox.isSelected() ||
!highlightCurrentLineCheckBox.isSelected() ||
!getCurrentLineHighlightColor().equals(defaultCurrentLineHighlightColor) ||
getTabSize()!=defaultTabSize ||
getEmulateTabs()==true ||
!marginLineCheckBox.isSelected() ||
getMarginLinePosition()!=defaultMarginLinePosition ||
!getMarginLineColor().equals(defaultMarginLineColor) ||
getRoundedSelection()==true)
{
setCaretColor(defaultCaretColor);
setSelectionColor(defaultSelectionColor);
setMarkAllHighlightColor(defaultMarkAllColor);
setCaretStyle(RTextArea.INSERT_MODE, defaultInsertCaret);
setCaretStyle(RTextArea.OVERWRITE_MODE, defaultOverwriteCaret);
blinkRateSpinner.setValue(defaultCaretBlinkRate);
setBackgroundObject(Color.WHITE);
wordWrapCheckBox.setSelected(false);
highlightCurrentLineCheckBox.setSelected(true);
hclColorButton.setEnabled(true);
setCurrentLineHighlightColor(defaultCurrentLineHighlightColor);
setTabSize(defaultTabSize);
setEmulateTabs(false);
setMarginLineEnabled(true);
setMarginLinePosition(defaultMarginLinePosition);
setMarginLineColor(defaultMarginLineColor);
setRoundedSelection(false);
hasUnsavedChanges = true;
firePropertyChange(DEFAULTS_RESTORED, null, null);
}
}
// Change the insert caret.
else if (actionCommand.equals("InsertCaretCombo")) {
int style = insCaretCombo.getSelectedIndex();
hasUnsavedChanges = true;
firePropertyChange(INSERT_CARET_PROPERTY, -1, style);
}
// Change the overwrite caret.
else if (actionCommand.equals("OverwriteCaretCombo")) {
int style = overCaretCombo.getSelectedIndex();
hasUnsavedChanges = true;
firePropertyChange(OVERWRITE_CARET_PROPERTY, -1, style);
}
// Set the background.
else if (actionCommand.equals("BackgroundButton")) {
if (backgroundDialog==null) {
backgroundDialog = new BackgroundDialog(getOptionsDialog());
}
backgroundDialog.initializeData(background, bgImageFileName);
backgroundDialog.setVisible(true);
Object newBG = backgroundDialog.getChosenBackground();
// Non-null newBG means user hit OK, not Cancel.
if (newBG!=null && !newBG.equals(background)) {
Object oldBG = background;
setBackgroundObject(newBG);
setBackgroundImageFileName(backgroundDialog.
getCurrentImageFileName());
hasUnsavedChanges = true;
firePropertyChange(BACKGROUND_PROPERTY, oldBG, newBG);
}
}
// Toggle word wrap.
else if (actionCommand.equals("WordWrapCheckBox")) {
boolean ww = wordWrapCheckBox.isSelected();
hasUnsavedChanges = true;
firePropertyChange(WORD_WRAP_PROPERTY, !ww, ww);
}
// Toggle whether or not the color button is enabled when they check the checkbox.
else if (actionCommand.equals("HighlightCurrentLineCheckBox")) {
boolean selected = highlightCurrentLineCheckBox.isSelected();
hclColorButton.setEnabled(selected);
hasUnsavedChanges = true;
firePropertyChange(HCL_PROPERTY, !selected, selected);
}
// Toggle whether or not to emulate tabs.
else if (actionCommand.equals("EmulateTabsCheckBox")) {
boolean selected = emulateTabsCheckBox.isSelected();
hasUnsavedChanges = true;
firePropertyChange(EMULATE_TABS_PROPERTY, !selected, selected);
}
// Toggle whether or not to paint the margin line.
else if (actionCommand.equals("MarginLineCheckBox")) {
boolean selected = marginLineCheckBox.isSelected();
marginLinePositionField.setEnabled(selected);
marginLineColorButton.setEnabled(selected);
hasUnsavedChanges = true;
firePropertyChange(MARGIN_LINE_PROPERTY, !selected, selected);
}
// Toggle whether to use rounded edges for selections.
else if (actionCommand.equals("RoundedSelectionCheckBox")) {
boolean selected = roundedSelectionCheckBox.isSelected();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -