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

📄 rutilities.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 08/12/2004
 *
 * RUtilities.java - General-purpose utility methods.
 * 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;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.ResourceBundle;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButton;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.KeyStroke;

import org.fife.ui.RButton;


/**
 * General-use static methods that can be used by any program that wants them.
 * Some of these methods were ripped off from the Sun Java Tutorial pages.
 *
 * @author Robert Futrell
 * @version 0.1
 */
public class RUtilities {


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


	/**
	 * Returns an <code>JCheckBox</code> with the specified text and mnemonic.
	 *
	 * @param bundle The resource bundle in which to get the int.
	 * @param textKey The key into the bundle containing the string text value.
	 * @param mnemonicKey The key into the bundle containing either an
	 *                    <code>Integer</code> or <code>String</code> mnemonic.
	 * @return The <code>JCheckBox</code>.
	 */
	public static final JCheckBox createCheckBox(ResourceBundle bundle,
								String textKey, String mnemonicKey) {
		JCheckBox checkBox = new JCheckBox(bundle.getString(textKey));
		RUtilities.setMnemonic(checkBox, bundle.getObject(mnemonicKey));
		return checkBox;
	}


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


	/**
	 * Returns an <code>JLabel</code> with the specified text and mnemonic.
	 *
	 * @param bundle The resource bundle in which to get the int.
	 * @param textKey The key into the bundle containing the string text value.
	 * @param mnemonicKey The key into the bundle containing either an
	 *                    <code>Integer</code> or <code>String</code> mnemonic.
	 * @return The <code>JLabel</code>.
	 */
	public static final JLabel createLabel(ResourceBundle bundle,
								String textKey, String mnemonicKey) {
		JLabel label = new JLabel(bundle.getString(textKey));
		Object mnemonic = bundle.getObject(mnemonicKey);
		if (mnemonic instanceof Integer)
			label.setDisplayedMnemonic(((Integer)mnemonic).intValue());
		else if (mnemonic instanceof String)
			label.setDisplayedMnemonic((int)((String)mnemonic).charAt(0));
		return label;
	}


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


	/**
	 * Returns an <code>JMenu</code> with the specified text and mnemonic.
	 *
	 * @param bundle The resource bundle in which to get the int.
	 * @param textKey The key into the bundle containing the string text value.
	 * @param mnemonicKey The key into the bundle containing either an
	 *                    <code>Integer</code> or <code>String</code> mnemonic.
	 * @return The <code>JMenu</code>.
	 */
	public static final JMenu createMenu(ResourceBundle bundle,
								String textKey, String mnemonicKey) {
		JMenu menu = new JMenu(bundle.getString(textKey));
		RUtilities.setMnemonic(menu, bundle.getObject(mnemonicKey));
		return menu;
	}


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


	/**
	 * Returns an <code>JMenuItem</code> with the specified text and
	 * mnemonic that performs the specified action when clicked.
	 *
	 * @param a The action that occurs when this item is clicked.
	 * @param bundle The resource bundle in which to get the int.
	 * @param textKey The key into the bundle containing the string text value.
	 * @param mnemonicKey The key into the bundle containing either an
	 *                    <code>Integer</code> or <code>String</code> mnemonic.
	 * @return The <code>JMenuItem</code>.
	 */
	public static final JMenuItem createMenuItem(Action a,
				ResourceBundle bundle, String textKey, String mnemonicKey) {
		return createMenuItem(a, bundle, textKey, mnemonicKey, null);
	}


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


	/**
	 * Returns an <code>JMenuItem</code> with the specified text and
	 * mnemonic that performs the specified action when clicked.
	 *
	 * @param a The action that occurs when this item is clicked.
	 * @param bundle The resource bundle in which to get the int.
	 * @param textKey The key into the bundle containing the string text value.
	 * @param mnemonicKey The key into the bundle containing either an
	 *                    <code>Integer</code> or <code>String</code> mnemonic.
	 * @param accelerator The accelerator for the menu item.
	 * @return The <code>JMenuItem</code>.
	 */
	public static final JMenuItem createMenuItem(Action a,
				ResourceBundle bundle, String textKey, String mnemonicKey,
				KeyStroke accelerator) {
		JMenuItem item = new JMenuItem(a);
		item.setText(bundle.getString(textKey));
		RUtilities.setMnemonic(item, bundle.getObject(mnemonicKey));
		item.setAccelerator(accelerator);
		return item;
	}


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


	/**
	 * Returns an <code>JMenuItem</code> with the specified text and
	 * mnemonic.
	 *
	 * @param bundle The resource bundle in which to get the int.
	 * @param textKey The key into the bundle containing the string text value.
	 * @param mnemonicKey The key into the bundle containing either an
	 *                    <code>Integer</code> or <code>String</code> mnemonic.
	 * @return The <code>JMenuItem</code>.
	 */
	public static final JMenuItem createMenuItem(ResourceBundle bundle,
								String textKey, String mnemonicKey) {
		JMenuItem item = new JMenuItem(bundle.getString(textKey));
		RUtilities.setMnemonic(item, bundle.getObject(mnemonicKey));
		return item;
	}


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


	/**
	 * Returns an <code>JRadioButton</code> with the specified text and
	 * mnemonic.
	 *
	 * @param bundle The resource bundle in which to get the int.
	 * @param textKey The key into the bundle containing the string text value.
	 * @param mnemonicKey The key into the bundle containing either an
	 *                    <code>Integer</code> or <code>String</code> mnemonic.
	 * @return The <code>JRadioButton</code>.
	 */
	public static final JRadioButton createRadioButton(ResourceBundle bundle,
								String textKey, String mnemonicKey) {
		JRadioButton radio = new JRadioButton(bundle.getString(textKey));
		RUtilities.setMnemonic(radio, bundle.getObject(mnemonicKey));
		return radio;
	}


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


	/**
	 * Returns an <code>org.fife.ui.RButton</code> with the specified text and
	 * mnemonic.
	 *
	 * @param bundle The resource bundle in which to get the int.
	 * @param textKey The key into the bundle containing the string text value.
	 * @param mnemonicKey The key into the bundle containing either an
	 *                    <code>Integer</code> or <code>String</code> mnemonic.
	 * @return The <code>RButton</code>.
	 */
	public static final RButton createRButton(ResourceBundle bundle,
								String textKey, String mnemonicKey) {
		RButton b = new RButton(bundle.getString(textKey));
		RUtilities.setMnemonic(b, bundle.getObject(mnemonicKey));
		return b;
	}


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


	/**
	 * Returns an <code>org.fife.ui.RButton</code> created from the specified
	 * action and with the specified mnemonic.
	 *
	 * @param action The action for the button.
	 * @param bundle The resource bundle in which to get the int.
	 * @param mnemonicKey The key into the bundle containing either an
	 *                    <code>Integer</code> or <code>String</code> mnemonic.
	 * @return The <code>RButton</code>.
	 */
	public static final RButton createRButton(Action action,
							ResourceBundle bundle, String mnemonicKey) {
		RButton b = new RButton(action);
		RUtilities.setMnemonic(b, bundle.getObject(mnemonicKey));
		return b;
	}


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


	/**
	 * Derives a color from another color by linearly shifting its blue, green,
	 * and blue values.
	 *
	 * @param orig The original color.
	 * @param darker The amount by which to decrease its r, g, and b values.
	 *        Note that you can use negative values for making a color
	 *        component "brighter."  If this makes any of the three values
	 *        less than zero, zero is used for that component value; similarly,
	 *        if it makes any value greater than 255, 255 is used for that
	 *        component's value.
	 */
	public static final Color deriveColor(Color orig, int darker) {

		int red = orig.getRed()-darker;
		int green = orig.getGreen()-darker;
		int blue = orig.getBlue()-darker;

		if (red<0) red=0; if (red>255) red=255;
		if (green<0) green=0; if (green>255) green=255;
		if (blue<0) blue=0; if (blue>255) blue=255;

		return new Color(red, green, blue);

	}


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


	/**
	 * Returns a color from the specified packed integer (as obtained from
	 * {@link #getPackedColor}).
	 *
	 * @param packed The 24-bit packed color to decode.
	 * @return The represented color.
	 * @see #getPackedColor
	 */
	public static Color getColorFromPacked(int packed) {
		int r = (packed>>16) & 0xFF;
		int g = (packed>>8) & 0xFF;
		int b = packed & 0xFF;
		return new Color(r,g,b);
	}


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


	/**
	 * Used by makeSpringCompactGrid.  This is ripped off directly from
	 * <code>SpringUtilities.java</code> in the Sun Java Tutorial.
	 *
	 * @param parent The container whose layout must be an instance of
	 *                     <code>SpringLayout</code>.
	 * @return The spring constraints for the specified component contained in
	 *                    <code>parent</code>.
	 */
	private static final SpringLayout.Constraints getConstraintsForCell(
										int row, int col,
										Container parent, int cols) {
		SpringLayout layout = (SpringLayout) parent.getLayout();

⌨️ 快捷键说明

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