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

org.fife.rtext.optionsdialog.ShortcutOptionPanel 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
/*
 * 08/21/2004
 *
 * ShortcutOptionPanel.java - Option panel letting the user configure
 * shortcuts in RText.
 * Copyright (C) 2004 Robert Futrell
 * robert_futrell at users.sourceforge.net
 * http://rtext.fifesoft.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.optionsdialog;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Comparator;
import java.util.ResourceBundle;
import javax.swing.*;
import javax.swing.table.*;

import org.fife.rtext.GetKeyStrokeDialog;
import org.fife.rtext.RText;
import org.fife.rtext.RTextActionInfo;
import org.fife.rtext.RTextUtilities;
import org.fife.ui.*;
import org.fife.ui.app.AbstractGUIApplication;
import org.fife.ui.modifiabletable.*;


/**
 * Option panel letting the user configure shortcut keys for RText.
 *
 * @author Robert Futrell
 * @version 0.2
 */
class ShortcutOptionPanel extends OptionsDialogPanel
					implements ActionListener, ModifiableTableListener {

	private ModifiableTable shortcutTable;
	private DefaultTableModel model;
	private RText rtext;
	private Action[] masterActionList;

	private static final String SHORTCUT_PROPERTY	= "SOP.shortcut";


	/**
	 * Constructor.
	 *
	 * @param rtext The owner of the options dialog in which this panel
	 *        appears.
	 * @param msg The resource bundle to use.
	 */
	public ShortcutOptionPanel(final RText rtext, final ResourceBundle msg) {

		super(msg.getString("OptSCName"));
		this.rtext = rtext;

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

		setLayout(new BorderLayout());
		setBorder(UIUtil.getEmpty5Border());
		JPanel contentPane = new JPanel(new BorderLayout());
		contentPane.setBorder(
					new OptionPanelBorder(msg.getString("OptSCLabel")));
		add(contentPane);

		model = new DefaultTableModel(new Object[] {
				msg.getString("OptSCCol1"), msg.getString("OptSCCol2")},
				RTextActionInfo.actionNames.length);
		shortcutTable = new ModifiableTable(model, ModifiableTable.BOTTOM,
										ModifiableTable.MODIFY);
		shortcutTable.addModifiableTableListener(this);
		shortcutTable.setRowHandler(new ShortcutTableRowHandler());
		JTable table = shortcutTable.getTable();
		table.getColumn(msg.getString("OptSCCol2")).setCellRenderer(
									new ShortcutCellRenderer());
		table.setPreferredScrollableViewportSize(new Dimension(300,300));
		contentPane.add(shortcutTable);

		RButton defButton = new RButton(msg.getString("RestoreDefaults"));
		defButton.setActionCommand("RestoreDefaults");
		defButton.addActionListener(this);
		JPanel temp = new JPanel(new BorderLayout());
		temp.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
		temp.add(defButton, BorderLayout.LINE_START);
		add(temp, BorderLayout.SOUTH);
		applyComponentOrientation(orientation);

	}


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

		String actionCommand = e.getActionCommand();

		if (actionCommand.equals("RestoreDefaults")) {
			rtext.restoreDefaultAccelerators(); // Does mainView too.
			setActions(rtext);
		}

	}


	/**
	 * Applies the settings entered into this dialog on the specified
	 * application.
	 *
	 * @param owner The application.
	 */
	protected void doApplyImpl(Frame owner) {

		Action[] actions = getActions();
		int actionsLength = actions.length;
		Action[] realActions = ((AbstractGUIApplication)owner).getActions();
		int j;

		for (int k=0; kJComponent at the "top" of this Options
	 * panel.
	 */
	public JComponent getTopJComponent() {
		return shortcutTable.getTable();
	}


	/**
	 * Called whenever the extension/color mapping table is changed.
	 *
	 * @param e An event describing the change.
	 */
	public void modifiableTableChanged(ModifiableTableChangeEvent e) {
		hasUnsavedChanges = true;
		firePropertyChange(SHORTCUT_PROPERTY, null, new Integer(e.getRow()));
	}


	/**
	 * Sets the actions to display as configurable.
	 *
	 * @param actions The actions of the application.
	 */
	private void setActions(AbstractGUIApplication app) {

		masterActionList = (Action[])app.getActions();

		Arrays.sort(masterActionList, new Comparator() {
			public int compare(Object o1, Object o2) {
				String name1 = (String)((Action)o1).getValue(Action.NAME);
				String name2 = (String)((Action)o2).getValue(Action.NAME);
				if (name1==null) {
					if (name2==null)
						return 0;
					return -1;
				}
				if (name2==null) // name1!=null && name2==null.
					return 1;
				return name1.compareTo(name2);
			}
			public boolean equals(Object o2) {
				return o2==this;
			}
		});

		// Action count may change from initial value as plugins might
		// add Actions to application.
		model.setRowCount(masterActionList.length);

		for (int i=0; iRowHandler#updateUI().
		 */
		public void updateUI() {
			if (ksDialog!=null) {
				SwingUtilities.updateComponentTreeUI(ksDialog);
			}
		}

	}


	/**
	 * Renderer for shortcuts in the JTable.
	 */
	static class ShortcutCellRenderer extends DefaultTableCellRenderer {

		public Component getTableCellRendererComponent(JTable table,
								Object value, boolean isSelected,
								boolean hasFocus, int row, int column) {
			super.getTableCellRendererComponent(table, value, isSelected,
										 hasFocus, row, column);
			setText(RTextUtilities.getPrettyStringFor((KeyStroke)value));
			setComponentOrientation(table.getComponentOrientation());
			return this;
		}

	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy