⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsyntaxtextareaoptionpanel.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * 02/26/2004
 *
 * RSyntaxTextAreaOptionsPanel.java - An options panel that can be
 * added to org.fife.ui.OptionsDialog for an RSyntaxTextArea.
 * 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.rsyntaxtextarea;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
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.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.fife.RListSelectionModel;
import org.fife.ui.FontDialog;
import org.fife.ui.OptionsDialogPanel;
import org.fife.ui.RButton;
import org.fife.ui.RColorSwatchesButton;
import org.fife.ui.RScrollPane;


/**
 * An options panel that can be added to an
 * <code>org.fife.ui.OptionsDialog</code> to display options appropriate for an
 * <code>RSyntaxTextArea</code>.  This panel allows the user to change the
 * following properties of the text area:
 * <ul>
 *    <li>Syntax highlighting colors</li>
 *    <li>Bracket matching and color</li>
 *    <li>Toggle whitespace (spaces and tabs) visibility</li>
 *    <li>Toggle anti-aliasing and fractional fontmetrics</li>
 * </ul>
 * It also gives the user a button to restore the default color scheme.
 *
 * @author Robert Futrell
 * @version 0.5
 */
public class RSyntaxTextAreaOptionPanel extends OptionsDialogPanel
					implements ActionListener, PropertyChangeListener,
								ListSelectionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2368211128353576829L;

	private JPanel syntaxPanel;

	private JList syntaxList;
	private DefaultListModel syntaxListModel;
	private FontTextField fontField;
	private JButton fontBrowseButton;
	private RColorSwatchesButton foregroundButton;
	private JCheckBox backgroundCheckBox;
	private RColorSwatchesButton backgroundButton;
	private JPanel previewPanel;
	private JLabel stylePreviewLabel;

	private SyntaxHighlightingColorScheme colorScheme;

	private JPanel bracketMatchingPanel;
	private JCheckBox bracketMatchCheckBox;
	private JLabel bmBGColorLabel;
	private RColorSwatchesButton bmBGColorButton;
	private JLabel bmBorderColorLabel;
	private RColorSwatchesButton bmBorderColorButton;

	private JCheckBox visibleWhitespaceCheckBox;

	private JCheckBox autoIndentCheckBox;
	private JCheckBox remWhitespaceLinesCheckBox;

	private JCheckBox smoothTextCheckBox;
	private JCheckBox fractionalMetricsCheckBox;

	private RButton restoreDefaultsButton;

	private boolean isSettingStyle;

	private static final String BRACKET_MATCHING_PROPERTY		= "RSTAOptionPanel.bracketMatchCheckBox";
	private static final String BRACKET_MATCHING_BG_PROPERTY	= "RSTAOptionPanel.bracketMatchBackgroundColor";
	private static final String BM_BORDER_PROPERTY			= "RSTAOptionPanel.bracketMatchBorderColor";
	private static final String DEFAULTS_RESTORED			= "RSTAOptionPanel.defaultsRestored";
	private static final String FRACTIONAL_METRICS_PROPERTY	= "RSTAOptionPanel.fractionalFontMetrics";
	private static final String SMOOTH_TEXT_PROPERTY			= "RSTAOptionPanel.smoothText";
	private static final String SYNTAX_COLOR_PROPERTY			= "RSTAOptionPanel.syntaxColor";
	private static final String SYNTAX_FONT_PROPERTY			= "RSTAOptionPanel.syntaxFont";
	private static final String UNKNOWN_PROPERTY				= "RSTAOptionPanel.unknown";
	private static final String VISIBLE_WHITESPACE_PROPERTY	= "RSTAOptionPanel.visibleWhitespace";

	private static final int[] indexMap = {
									2,
									3,
									4,
									5,
									6,
									8,
									9,
									10,
									11,
									12,
									13,
									14,
									15,
									16,
									17,
									18,
									19,
									20,
									21,
									23,
									24,
									25,
									26
									};


/*****************************************************************************/


	/**
	 * Constructor.
	 */
	public RSyntaxTextAreaOptionPanel() {

		super();

		ResourceBundle msg = ResourceBundle.getBundle(
							"org.fife.ui.rsyntaxtextarea.OptionPanel");

		setName(msg.getString("Title"));

		setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		setLayout(new BorderLayout());

		// Syntax panel contains all of the "syntax highlighting" stuff.
		syntaxPanel = new JPanel(new BorderLayout());
		syntaxPanel.setBorder(new OptionPanelBorder(
								msg.getString("SyntaxHighlighting")));

		// Add the token style selection panel to the right.
		syntaxListModel = new DefaultListModel();
		syntaxList = new JList(syntaxListModel);
		syntaxList.setSelectionModel(new RListSelectionModel());
		syntaxList.addListSelectionListener(this);
		syntaxList.setCellRenderer(new RSTACellRenderer());
		syntaxListModel.addElement(msg.getString("Style.Comment.EndOfLine"));
		syntaxListModel.addElement(msg.getString("Style.Comment.Multiline"));
		syntaxListModel.addElement(msg.getString("Style.Comment.Documentation"));
		syntaxListModel.addElement(msg.getString("Style.ReservedWord"));
		syntaxListModel.addElement(msg.getString("Style.Function"));
		syntaxListModel.addElement(msg.getString("Style.Literal.Boolean"));
		syntaxListModel.addElement(msg.getString("Style.Literal.Integer"));
		syntaxListModel.addElement(msg.getString("Style.Literal.Float"));
		syntaxListModel.addElement(msg.getString("Style.Literal.Hex"));
		syntaxListModel.addElement(msg.getString("Style.Literal.String"));
		syntaxListModel.addElement(msg.getString("Style.Literal.Char"));
		syntaxListModel.addElement(msg.getString("Style.Literal.Backquote"));
		syntaxListModel.addElement(msg.getString("Style.DataType"));
		syntaxListModel.addElement(msg.getString("Style.Variable"));
		syntaxListModel.addElement("*"+msg.getString("Style.Identifier.PlainText"));
		syntaxListModel.addElement(msg.getString("Style.Whitespace"));
		syntaxListModel.addElement(msg.getString("Style.Separator"));
		syntaxListModel.addElement(msg.getString("Style.Operator"));
		syntaxListModel.addElement(msg.getString("Style.Preprocessor"));
		syntaxListModel.addElement(msg.getString("Style.Error.Identifier"));
		syntaxListModel.addElement(msg.getString("Style.Error.Number"));
		syntaxListModel.addElement(msg.getString("Style.Error.String"));
		syntaxListModel.addElement(msg.getString("Style.Error.Char"));
		syntaxPanel.add(new RScrollPane(syntaxList), BorderLayout.WEST);

		// Create a panel for other syntax style properties.
		JPanel propertiesPanel = new JPanel();
		propertiesPanel.setLayout(new BoxLayout(propertiesPanel,
										BoxLayout.Y_AXIS));
		Dimension spacer = new Dimension(5,8);

		// Create a panel for selecting the font.
		JPanel fontPanel = new JPanel();
		fontPanel.setLayout(new BoxLayout(fontPanel, BoxLayout.X_AXIS));
		fontPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		JLabel fontLabel = new JLabel(msg.getString("Font"));
		fontField = new FontTextField();
		fontField.addPropertyChangeListener(this);
		fontLabel.setLabelFor(fontField);
		fontBrowseButton = new RButton(msg.getString("Browse"));
		fontBrowseButton.setActionCommand("Browse");
		fontBrowseButton.addActionListener(this);
		fontPanel.add(fontLabel);
		fontPanel.add(Box.createRigidArea(spacer));
		fontPanel.add(fontField);
		fontPanel.add(Box.createRigidArea(spacer));
		fontPanel.add(fontBrowseButton);
		fontPanel.add(Box.createHorizontalGlue());
		propertiesPanel.add(fontPanel);
		propertiesPanel.add(Box.createRigidArea(spacer));

		// Add the foreground and background buttons to the properties panel.
		JPanel temp = new JPanel();
		temp.setLayout(new BoxLayout(temp, BoxLayout.X_AXIS));
		temp.setAlignmentX(Component.LEFT_ALIGNMENT);
		JLabel fgLabel = new JLabel(msg.getString("Foreground"));
		foregroundButton = new RColorSwatchesButton(Color.BLACK);
		foregroundButton.addPropertyChangeListener(this);
		fgLabel.setLabelFor(foregroundButton);
		temp.add(fgLabel);
		temp.add(foregroundButton);
		temp.add(Box.createHorizontalGlue());
		propertiesPanel.add(temp);
		propertiesPanel.add(Box.createRigidArea(spacer));
		temp = new JPanel();
		temp.setLayout(new BoxLayout(temp, BoxLayout.X_AXIS));
		temp.setAlignmentX(Component.LEFT_ALIGNMENT);
		backgroundCheckBox = new JCheckBox(msg.getString("Background"));
		backgroundCheckBox.setActionCommand("BackgroundCheckBox");
		backgroundCheckBox.addActionListener(this);
		backgroundButton = new RColorSwatchesButton(Color.BLACK);
		backgroundButton.addPropertyChangeListener(this);
		temp.add(backgroundCheckBox);
		temp.add(backgroundButton);
		temp.add(Box.createHorizontalGlue());
		propertiesPanel.add(temp);
		propertiesPanel.add(Box.createRigidArea(spacer));

		// Add the properties panel and a "preview" panel to the main
		// syntax panel.
		JPanel temp2 = new JPanel(new BorderLayout());
		temp2.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
		temp2.add(propertiesPanel, BorderLayout.NORTH);
		previewPanel = new JPanel(new BorderLayout());
		JLabel previewLabel = new JLabel(msg.getString("Preview"));
		previewPanel.add(previewLabel, BorderLayout.NORTH);
		stylePreviewLabel = new JLabel("Fake", JLabel.CENTER);
		stylePreviewLabel.setBorder(
							BorderFactory.createLineBorder(Color.BLACK));
		stylePreviewLabel.setOpaque(true);
		previewPanel.add(stylePreviewLabel);
		temp2.add(previewPanel);
		syntaxPanel.add(temp2);

		// Add the syntax panel to us.
		add(syntaxPanel);

		// Now create a panel containing all checkbox properties in the
		// bottom of this panel.
		temp = new JPanel();
		temp.setBorder(new OptionPanelBorder(msg.getString("Advanced")));
		temp.setLayout(new BoxLayout(temp, BoxLayout.Y_AXIS));
		spacer = new Dimension(5,3); // A little smaller for this stuff.

		bracketMatchingPanel = new JPanel();
		bracketMatchingPanel.setLayout(new BoxLayout(bracketMatchingPanel, BoxLayout.X_AXIS));
		bracketMatchCheckBox = new JCheckBox(msg.getString("HighlightMB"));
		bracketMatchCheckBox.setActionCommand("BracketMatchCheckBox");
		bracketMatchCheckBox.addActionListener(this);
		bmBGColorLabel = new JLabel(msg.getString("BackgroundFill"));
		bmBGColorButton = new RColorSwatchesButton(Color.BLACK, 50,15);
		bmBGColorButton.addPropertyChangeListener(this);
		bmBorderColorLabel = new JLabel(msg.getString("Border"));
		bmBorderColorButton = new RColorSwatchesButton(Color.BLACK, 50,15);
		bmBorderColorButton.addPropertyChangeListener(this);
		bracketMatchingPanel.add(bracketMatchCheckBox);
		bracketMatchingPanel.add(bmBGColorLabel);
		bracketMatchingPanel.add(bmBGColorButton);
		bracketMatchingPanel.add(Box.createHorizontalStrut(5));
		bracketMatchingPanel.add(bmBorderColorLabel);
		bracketMatchingPanel.add(bmBorderColorButton);
		bracketMatchingPanel.add(Box.createHorizontalGlue());
		temp.add(bracketMatchingPanel);

		temp.add(Box.createRigidArea(spacer));

		visibleWhitespaceCheckBox = new JCheckBox(msg.getString("VisibleWhitespace"));
		visibleWhitespaceCheckBox.setActionCommand("VisibleWhitespace");
		visibleWhitespaceCheckBox.addActionListener(this);
		addLeftAlignedComponent(temp, visibleWhitespaceCheckBox);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -