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

📄 propertysheetutilities.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 03/30/2005
 *
 * PropertySheetUtilities.java - Utilities used by the Property Sheet classes.
 * Copyright (C) 2005 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.propertysheet;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.table.TableCellEditor;


/**
 * Utility methods for use by property sheet classes.
 *
 * @author Robert Futrell
 * @version 0.1
 */
public class PropertySheetUtilities {

	private static Color cachedPanelBackground;
	private static Color borderColor;

	private static final Color PROPERTY_COLOR		= Color.WHITE;
	private static final String PROPERTY_PREFIX		= "Property.";

	private static final FocusAdapter textFieldFocusListener =
									new TextFieldFocusListener();


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


	/**
	 * Creates a checkbox for use by a property sheet.
	 *
	 * @return A checkbox.
	 */
	public static JCheckBox createCheckBox() {
		JCheckBox checkBox = new JCheckBox();
		checkBox.setBackground(PropertySheetUtilities.getPropertyColor());
		//checkBox.setBorder(BorderFactory.createEmptyBorder());
		checkBox.setBorderPaintedFlat(true);
		return checkBox;
	}


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


	/**
	 * Creates a combo box for use by a property sheet.
	 *
	 * @return A combo box.
	 */
	/*
	 * FIXME: We've hardcoded STRING_CHOICE_PROPERTY, but we may want to pass
	 * it in here so if other property types use this editor it'll be done
	 * for them too.
	 */
	public static JComboBox createComboBox() {
		final JComboBox combo = new JComboBox() {
			protected void processFocusEvent(FocusEvent e) {
				if (e.getID()==FocusEvent.FOCUS_LOST) {
					TableCellEditor editor =
						PropertySheetManager.getInstance().getEditor(
											String[].class);
					editor.cancelCellEditing();
				}
				super.processFocusEvent(e);
			}
		};
		// The label painted for the value for uneditable combo boxes takes
		// its background color from the combo box's background color.
		PropertySheetUtilities.setBorderAndBackground(combo);
		return combo;
	}


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


	/**
	 * Creates a text field for use by a property sheet.
	 *
	 * @return The text field.
	 */
	public static JTextField createTextField() {
		JTextField field = new JTextField();
		field.addFocusListener(textFieldFocusListener);
		PropertySheetUtilities.setBorderAndBackground(field);
		return field;
	}


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


	/**
	 * Returns the color to use for the borders of the property sheet.
	 *
	 * @return The border color.
	 */
	public static Color getBorderColor() {
		// Check in case user changes L&F, otherwise use cached value.
		Color c = UIManager.getColor("Panel.background");
		if (!c.equals(cachedPanelBackground)) {
			cachedPanelBackground = c;
			borderColor = new Color(c.getRed()-20,
								c.getGreen()-20, c.getBlue()-20);
		}
		return borderColor;
	}


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


	/**
	 * Returns the color to use for the edges of borders of the property sheet.
	 *
	 * @return The border edge color.
	 */
	public static Color getBorderEdgeColor() {
		return getBorderColor().darker();
	}


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


	/**
	 * Returns a "default" value for a given type (NOTE:  Is there a better
	 * way to do this?
	 *
	 * @param type The type.
	 * @return The default value.  Primitives are wrapped in their respective
	 *         wrapper classes.
	 */
	public static Object getDefaultValueForType(Class type) {
		if (type.equals(int.class)) {
			return new Integer(0);
		}
		else if (type.equals(boolean.class)) {
			return Boolean.TRUE;
		}
		else if (type.equals(float.class)) {
			return new Float(3.4f);
		}
		else if (type.equals(double.class)) {
			return new Double(69.7);
		}
		else if (type.equals(long.class)) {
			return new Long(300l);
		}
		else if (type.equals(short.class)) {
			return new Short((short)3);
		}
		else if (type.equals(String.class)) {
			return "Hello world";
		}
		else if (type.equals(Color.class)) {
			return Color.MAGENTA;
		}
		return null;
	}


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


	/**
	 * Returns a string representing a <code>Dimension</code>.
	 *
	 * @param dim The dimension.
	 * @return The string.
	 */
	public static String getDimensionString(Dimension dim) {
		return "(" + dim.width + ", " + dim.height + ")";
	}


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


	/**
	 * Returns a string representing an <code>Insets</code>.
	 *
	 * @param insets The insets.
	 * @return The string.
	 */
	public static String getInsetsString(final Insets insets) {
		return "[" + insets.top + ", " + insets.left+ ", " +
				insets.bottom + ", " + insets.right+ "]";
	}


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


	/**
	 * Returns the property sheet that owns the specified component.
	 *
	 * @param comp The component.
	 * @return The property sheet.
	 */
	public static PropertySheet getParentPropertySheet(Component comp) {
		Container parent = comp.getParent();
		while (parent!=null && !(parent instanceof PropertySheet))
			parent = parent.getParent();
		// Could be null non-PropertySheet (but shouldn't be).
		if (parent instanceof PropertySheet)
			return (PropertySheet)parent;
		return null;
	}


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


	/**
	 * Returns the color to use as the background for property names and
	 * editors.
	 *
	 * @return The background color.
	 */
	public static final Color getPropertyColor() {
		return PROPERTY_COLOR;
	}


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


	/**
	 * Returns a string that is the prefix for any property modified in this
	 * property sheet.  A property change listener can then distinguish
	 * between properties being modified that are editable in this property
	 * sheet, and properties of the property sheet itself (and its components)
	 * changing.
	 */
	public static final String getPropertyPrefix() {
		return PROPERTY_PREFIX;
	}


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


	/**
	 * Returns a string representing a <code>Rectangle</code>.
	 *
	 * @param r The rectangle.
	 * @return The string.
	 */
	public static String getRectangleString(final Rectangle r) {
		if (r==null)
			return "<null>";
		return "[" + r.x + ", " + r.y + ", " +
				r.width + ", " + r.height + "]";
	}


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


	private static final void setBorderAndBackground(JComponent comp) {
		comp.setBorder(BorderFactory.createEmptyBorder());
		comp.setBackground(PropertySheetUtilities.getPropertyColor());
	}


/*****************************************************************************/
/********************* INNER CLASSES *****************************************/
/*****************************************************************************/


	/**
	 * Listens for a text field receiving focus and selects its text.
	 */
	static class TextFieldFocusListener extends FocusAdapter {

		public void focusGained(FocusEvent e) {
			JTextField tf = (JTextField)e.getSource();
			tf.setCaretPosition(0);
			tf.moveCaretPosition(tf.getDocument().getLength());
		}

	}


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

}

⌨️ 快捷键说明

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