All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fife.ui.rtextarea.CaretAndSelectionOptionPanel Maven / Gradle / Ivy

Go to download

RText is a powerful, cross-platform programmer's text editor written in Java. It is designed to be easy to use, highly customizable and flexible. Part of RText's design is for the source code to be simple, easy to understand, and well documented, so that other programmers can look into its inner-workings and figure out how RText ticks with ease. A good place to start (besides the source code) is the Javadoc for all classes used in the project.

There is a newer version: 2.0.7
Show newest version
/*
 * 03/20/2006
 *
 * CaretAndSelectionOptionPanel.java - Contains options relating to the caret
 * and selection.
 * Copyright (C) 2006 Robert Futrell
 * http://fifesoft.com/rtext
 * Licensed under a modified BSD license.
 * See the included license file for details.
 */
package org.fife.ui.rtextarea;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Frame;
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.Box;
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.SpinnerNumberModel;
import javax.swing.SpringLayout;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.fife.rtext.AbstractMainView;
import org.fife.rtext.RText;
import org.fife.ui.OptionsDialogPanel;
import org.fife.ui.RButton;
import org.fife.ui.RColorButton;
import org.fife.ui.RColorSwatchesButton;
import org.fife.ui.UIUtil;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rtextarea.ConfigurableCaret;
import org.fife.ui.rtextarea.RTextArea;


/**
 * Option panel for caret and selection options.
 *
 * @author Robert Futrell
 * @version 0.5
 */
public class CaretAndSelectionOptionPanel extends OptionsDialogPanel
		implements ActionListener, ChangeListener, PropertyChangeListener {

	private JComboBox insCaretCombo;
	private JComboBox overCaretCombo;
	private JSpinner blinkRateSpinner;
	private RColorSwatchesButton caretColorButton;
	private RColorSwatchesButton selColorButton;
	private RColorSwatchesButton markAllColorButton;
	private JCheckBox roundedSelCheckBox;
	private JCheckBox enableMOCheckBox;
	private JLabel moColorLabel;
	private RColorSwatchesButton moColorButton;
	private JCheckBox secLangCB;
	private JLabel[] secLangLabels;
	private RColorSwatchesButton[] secLangButtons;

	private static final int SEC_LANG_COUNT		= 3;
	private static final String PROPERTY		= "property";


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

		ComponentOrientation o = ComponentOrientation.
									getOrientation(getLocale());

		ResourceBundle msg = ResourceBundle.getBundle(
					"org.fife.ui.rtextarea.CaretAndSelectionOptionPanel");

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

		setBorder(UIUtil.getEmpty5Border());
		setLayout(new BorderLayout());

		// We'll add everything to this panel, then add this panel so that
		// stuff stays at the "top."
		Box topPanel = Box.createVerticalBox();

		Box caretPanel = Box.createVerticalBox();
		caretPanel.setBorder(new OptionPanelBorder(msg.getString("Carets")));
		JPanel temp = new JPanel(new SpringLayout());
		JLabel insLabel = new JLabel(msg.getString("InsertCaret"));
		insCaretCombo = createCaretComboBox(msg);
		insCaretCombo.setActionCommand("InsertCaretCombo");
		insCaretCombo.addActionListener(this);
		insLabel.setLabelFor(insCaretCombo);
		JLabel overLabel = new JLabel(msg.getString("OverwriteCaret"));
		overCaretCombo = createCaretComboBox(msg);
		overCaretCombo.setActionCommand("OverwriteCaretCombo");
		overCaretCombo.addActionListener(this);
		overLabel.setLabelFor(overCaretCombo);
		JLabel caretDelayLabel = new JLabel(msg.getString("BlinkRate"));
		SpinnerNumberModel spinnerModel = new SpinnerNumberModel(500, 0,10000, 50);
		blinkRateSpinner = new JSpinner(spinnerModel);
		blinkRateSpinner.addChangeListener(this);
		caretDelayLabel.setLabelFor(blinkRateSpinner);
		JLabel caretColorLabel = new JLabel(msg.getString("Color"));
		caretColorButton = new RColorSwatchesButton();
		caretColorButton.addPropertyChangeListener(
					RColorSwatchesButton.COLOR_CHANGED_PROPERTY, this);
		caretColorLabel.setLabelFor(caretColorButton);
		JPanel buttonPanel = new JPanel(new BorderLayout());
		buttonPanel.add(caretColorButton, BorderLayout.LINE_START);
		if (o.isLeftToRight()) {
			temp.add(insLabel);          temp.add(insCaretCombo);
			temp.add(overLabel);         temp.add(overCaretCombo);
			temp.add(caretDelayLabel);   temp.add(blinkRateSpinner);
			temp.add(caretColorLabel);   temp.add(buttonPanel);
		}
		else {
			temp.add(insCaretCombo);     temp.add(insLabel);
			temp.add(overCaretCombo);    temp.add(overLabel);
			temp.add(blinkRateSpinner);  temp.add(caretDelayLabel);
			temp.add(buttonPanel);       temp.add(caretColorLabel);
		}
		UIUtil.makeSpringCompactGrid(temp, 4,2, 0,0, 5,5);
		caretPanel.add(temp);
		caretPanel.add(Box.createVerticalStrut(5));
		topPanel.add(caretPanel);
		topPanel.add(Box.createVerticalStrut(5));

		topPanel.add(createSelectionPanel(msg, o));
		topPanel.add(Box.createVerticalStrut(5));

		topPanel.add(createMarkOccurrencesPanel(msg, o));
		topPanel.add(Box.createVerticalStrut(5));

		topPanel.add(createSecondaryLanguagesPanel(msg, o));
		topPanel.add(Box.createVerticalStrut(10));

		RButton rdButton = new RButton(msg.getString("RestoreDefaults"));
		rdButton.setActionCommand("RestoreDefaults");
		rdButton.addActionListener(this);
		addLeftAligned(topPanel, rdButton);

		add(topPanel, BorderLayout.NORTH);
		applyComponentOrientation(o);

		msg = null;

	}


	/**
	 * Listens for actions in this panel.
	 */
	public void actionPerformed(ActionEvent e) {

		String command = e.getActionCommand();
		Object source = e.getSource();

		if ("RestoreDefaults".equals(command)) {

			Color defaultCaretColor = RTextArea.getDefaultCaretColor();
			Color defaultSelectionColor = RSyntaxTextArea.getDefaultSelectionColor();
			Color defaultMarkAllColor = RTextArea.getDefaultMarkAllHighlightColor();
			Color defaultMarkOccurrencesColor = new Color(224, 224, 224);
			int defaultInsertCaret = ConfigurableCaret.THICK_VERTICAL_LINE_STYLE;
			int defaultOverwriteCaret = ConfigurableCaret.BLOCK_STYLE;
			Integer defaultCaretBlinkRate = new Integer(500);
			Color[] defaultSecLangColor = new Color[SEC_LANG_COUNT];
			defaultSecLangColor[0] = new Color(0xfff0cc);
			defaultSecLangColor[1] = new Color(0xdafeda);
			defaultSecLangColor[2] = new Color(0xffe0f0);

			if ( !getCaretColor().equals(defaultCaretColor) ||
				!getSelectionColor().equals(defaultSelectionColor) ||
				!getMarkAllHighlightColor().equals(defaultMarkAllColor) ||
				getCaretStyle(RTextArea.INSERT_MODE)!=defaultInsertCaret ||
				getCaretStyle(RTextArea.OVERWRITE_MODE)!=defaultOverwriteCaret ||
				!blinkRateSpinner.getValue().equals(defaultCaretBlinkRate) ||
				getRoundedSelection()==true ||
				!enableMOCheckBox.isSelected() ||
				!moColorButton.getColor().equals(defaultMarkOccurrencesColor) ||
				secLangCB.isSelected() ||
				!defaultSecLangColor[0].equals(secLangButtons[0]) ||
				!defaultSecLangColor[1].equals(secLangButtons[1]) ||
				!defaultSecLangColor[2].equals(secLangButtons[2]))
			{
				setCaretColor(defaultCaretColor);
				setSelectionColor(defaultSelectionColor);
				setMarkAllHighlightColor(defaultMarkAllColor);
				setCaretStyle(RTextArea.INSERT_MODE, defaultInsertCaret);
				setCaretStyle(RTextArea.OVERWRITE_MODE, defaultOverwriteCaret);
				blinkRateSpinner.setValue(defaultCaretBlinkRate);
				setRoundedSelection(false);
				enableMOCheckBox.setSelected(true);
				moColorButton.setEnabled(true);
				moColorButton.setColor(defaultMarkOccurrencesColor);
				setHighlightSecondaryLanguages(false);
				for (int i=0; i
	 * These should really be elsewhere, but we're getting short on space in
	 * other text editor-related panels.
	 *
	 * @param msg The resource bundle to use for localization.
	 * @param o The component orientation.
	 * @return The panel.
	 */
	private Box createSecondaryLanguagesPanel(ResourceBundle msg,
											ComponentOrientation o) {

		Box p = Box.createVerticalBox();
		p.setBorder(new OptionPanelBorder(msg.getString("SecondaryLanguages")));

		secLangCB =  new JCheckBox(msg.getString("HighlightSecondaryLanguages"));
		secLangCB.addActionListener(this);
		addLeftAligned(p, secLangCB);

		secLangLabels = new JLabel[SEC_LANG_COUNT];
		secLangButtons = new RColorSwatchesButton[SEC_LANG_COUNT];
		for (int i=0; iRTextArea.INSERT_MODE or
	 *        RTextArea.OVERWRITE_MODE.
	 * @return The style of that caret, such as
	 *        ConfigurableCaret.VERTICAL_LINE_STYLE.
	 * @see org.fife.ui.rtextarea.ConfigurableCaret
	 */
	public int getCaretStyle(int mode) {
		if (mode==RTextArea.INSERT_MODE)
			return insCaretCombo.getSelectedIndex();
		return overCaretCombo.getSelectedIndex(); // OVERWRITE_MODE
	}


	/**
	 * Returns the color selected by the user for "mark all."
	 *
	 * @return The color.
	 */
	public Color getMarkAllHighlightColor() {
		return markAllColorButton.getColor();
	}


	/**
	 * Returns whether the user selected to use rounded edges on selections.
	 *
	 * @return Whether rounded selection edges were selected.
	 */
	public boolean getRoundedSelection() {
		return roundedSelCheckBox.isSelected();
	}


	/**
	 * Returns the color the user chose for selections.
	 *
	 * @return The selection color the user chose.
	 */
	public Color getSelectionColor() {
		return selColorButton.getColor();
	}


	/**
	 * Returns the JComponent at the "top" of this Options
	 * panel.  This is the component that will receive focus if the user
	 * switches to this Options panel in the Options dialog.  As an added
	 * bonus, if this component is a JTextComponent, its
	 * text is selected for easy changing.
	 */
	public JComponent getTopJComponent() {
		return caretColorButton;
	}


	/**
	 * Called when a property changes in an object we're listening to.
	 */
	public void propertyChange(PropertyChangeEvent e) {
		// We need to forward this on to the options dialog, whatever
		// it is, so that the "Apply" button gets updated.
		if (RColorButton.COLOR_CHANGED_PROPERTY.equals(e.getPropertyName())) {
			hasUnsavedChanges = true;
			firePropertyChange(PROPERTY, e.getOldValue(), e.getNewValue());
		}
	}


	/**
	 * Sets the blink rate displayed.
	 *
	 * @param blinkRate The blink rate to display.
	 * @see #getBlinkRate()
	 */
	private void setBlinkRate(int blinkRate) {
		blinkRateSpinner.setValue(new Integer(blinkRate));
	}


	/**
	 * Sets the caret color displayed in this panel.
	 *
	 * @param color The caret color to display.  If null is
	 *        passed in, Color.BLACK is used.
	 * @see #getCaretColor()
	 */
	private void setCaretColor(Color color) {
		if (color==null) {
			color = Color.BLACK;
		}
		caretColorButton.setColor(color);
	}


	/**
	 * Sets the caret style for either the insert or overwrite caret, as
	 * displayed in this option panel.
	 *
	 * @param mode Either RTextArea.INSERT_MODE or
	 *        RTextArea.OVERWRITE_MODE.
	 * @param style The style for the specified caret, such as
	 *        ConfigurableCaret.VERTICAL_LINE_STYLE.
	 * @see #getCaretStyle(int)
	 */
	private void setCaretStyle(int mode, int style) {
		switch (mode) {
			case RTextArea.INSERT_MODE:
				insCaretCombo.setSelectedIndex(style);
				break;
			case RTextArea.OVERWRITE_MODE:
				overCaretCombo.setSelectedIndex(style);
				break;
			default:
				throw new IllegalArgumentException("mode must be " +
					RTextArea.INSERT_MODE + " or " +
					RTextArea.OVERWRITE_MODE);
		}
	}


	private void setHighlightSecondaryLanguages(boolean highlight) {
		secLangCB.setSelected(highlight);
		for (int i=0; inull is passed in, nothing happens.
	 * @see #getSelectionColor()
	 */
	private void setSelectionColor(Color color) {
		if (color!=null)
			selColorButton.setColor(color);
	}


	/**
	 * {@inheritDoc}
	 */
	protected void setValuesImpl(Frame owner) {
		RText rtext = (RText)owner;
		AbstractMainView mainView = rtext.getMainView();
		setCaretColor(mainView.getCaretColor());
		setSelectionColor(mainView.getSelectionColor());
		setMarkAllHighlightColor(mainView.getMarkAllHighlightColor());
		setRoundedSelection(mainView.getRoundedSelectionEdges());
		setCaretStyle(RTextArea.INSERT_MODE, mainView.getCaretStyle(RTextArea.INSERT_MODE));
		setCaretStyle(RTextArea.OVERWRITE_MODE, mainView.getCaretStyle(RTextArea.OVERWRITE_MODE));
		setBlinkRate(mainView.getCaretBlinkRate());
		enableMOCheckBox.setSelected(mainView.getMarkOccurrences());
		moColorLabel.setEnabled(enableMOCheckBox.isSelected());
		moColorButton.setEnabled(enableMOCheckBox.isSelected());
		moColorButton.setColor(mainView.getMarkOccurrencesColor());

		setHighlightSecondaryLanguages(mainView.getHighlightSecondaryLanguages());
		for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy