📄 propertysheetmanager.java
字号:
/*
* 08/10/2005
*
* PropertySheetManager.java - Manages information common to all property
* sheets.
* 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.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.ResourceBundle;
import javax.swing.Icon;
import javax.swing.UIManager;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import org.fife.ui.propertysheet.editors.*;
import org.fife.ui.propertysheet.infos.*;
import org.fife.ui.propertysheet.renderers.*;
/**
* Manages all properties common to all property sheets.
*
* @author Robert Futrell
* @version 1.0
* @see PropertySheet
*/
public class PropertySheetManager {
private HashMap infoClasses;
private HashMap editors;
private HashMap renderers;
private TableCellRenderer defaultRenderer;
private ResourceBundle errorBundle;
private static PropertySheetManager instance = new PropertySheetManager();
private static final String ERROR_BUNDLE = "PropertySheetErrors";
/*****************************************************************************/
/**
* Constructor.
*/
private PropertySheetManager() {
infoClasses = new HashMap();
editors = new HashMap();
renderers = new HashMap();
defaultRenderer = new DefaultCellRenderer();
// Set up default editors and renderers.
// FIXME: Have us lazily created.
BooleanCellEditor bce = new BooleanCellEditor();
BooleanCellRenderer bcr = new BooleanCellRenderer();
register(boolean.class, BooleanPropertyInfo.class, bcr, bce);
ColorCellEditor cce = new ColorCellEditor();
ColorCellRenderer ccr = new ColorCellRenderer();
register(Color.class, ColorPropertyInfo.class, ccr, cce);
StringCellEditor sce = new StringCellEditor();
StringCellRenderer scr = new StringCellRenderer();
register(String.class, StringPropertyInfo.class, scr, sce);
StringChoiceCellEditor scce = new StringChoiceCellEditor();
register(String[].class, StringChoicePropertyInfo.class, scr, scce);
IntegerCellEditor ice = new IntegerCellEditor();
NumberCellRenderer ncr = new NumberCellRenderer();
register(int.class, IntegerPropertyInfo.class, ncr, ice);
ShortCellEditor sce2 = new ShortCellEditor();
register(short.class, ShortPropertyInfo.class, ncr, sce2);
LongCellEditor lce = new LongCellEditor();
register(long.class, LongPropertyInfo.class, ncr, lce);
FloatCellEditor fce = new FloatCellEditor();
register(float.class, FloatPropertyInfo.class, ncr, fce);
DoubleCellEditor dce = new DoubleCellEditor();
register(double.class, DoublePropertyInfo.class, ncr, dce);
DimensionCellEditor dce2 = new DimensionCellEditor();
DimensionCellRenderer dcr = new DimensionCellRenderer();
register(Dimension.class, DimensionPropertyInfo.class, dcr, dce2);
RectangleCellEditor rce = new RectangleCellEditor();
RectangleCellRenderer rcr = new RectangleCellRenderer();
register(Rectangle.class, RectanglePropertyInfo.class, rcr, rce);
InsetsCellEditor ice2 = new InsetsCellEditor();
InsetsCellRenderer icr2 = new InsetsCellRenderer();
register(Insets.class, InsetsPropertyInfo.class, icr2, ice2);
FontCellEditor fce2 = new FontCellEditor();
FontCellRenderer fcr2 = new FontCellRenderer();
register(Font.class, FontPropertyInfo.class, fcr2, fce2);
IconCellEditor ice3 = new IconCellEditor();
IconCellRenderer icr3 = new IconCellRenderer();
register(Icon.class, IconPropertyInfo.class, icr3, ice3);
}
/*****************************************************************************/
/**
* Returns the default cell renderer used when no renderer is registered
* for a class type.
*
* @return The default renderer.
* @see #setDefaultRenderer
*/
protected TableCellRenderer getDefaultRenderer() {
return defaultRenderer;
}
/*****************************************************************************/
/**
* Returns the editor to use for the given class (object type).
*
* @param clazz The class.
* @return The editor, or <code>null</code> if no editor has been
* registered for the specified class.
* @see #getRenderer
*/
public TableCellEditor getEditor(Class clazz) {
return (TableCellEditor)editors.get(clazz);
}
/*****************************************************************************/
public synchronized ResourceBundle getErrorBundle() {
if (errorBundle==null)
errorBundle = ResourceBundle.getBundle(ERROR_BUNDLE);
return errorBundle;
}
/*****************************************************************************/
/**
* Returns the proper icon (expanded or collapsed) depending on the state
* of the category group.
*
* @param boolean collapsed Whether the category is collapsed.
* @return The icon.
*/
public Icon getExpandedStateIcon(boolean collapsed) {
Icon icon = UIManager.getIcon(collapsed ?
"Tree.collapsedIcon" : "Tree.expandedIcon");
if (icon==null)
icon = PlusMinusIcon.getInstance();
return icon;
}
/*****************************************************************************/
/**
* Returns the singleton instance of the property sheet manager.
*
* @return The property sheet manager.
*/
public static PropertySheetManager getInstance() {
if (instance==null)
instance = new PropertySheetManager();
return instance;
}
/*****************************************************************************/
/**
* Returns the info wrapper class used for the given class (object type).
*
* @param clazz The class.
* @return The info wrapper class.
*/
public Class getPropertyInfoClass(Class clazz) {
Class c = (Class)infoClasses.get(clazz);
if (c==null)
c = DefaultPropertyInfo.class;
return c;
}
/*****************************************************************************/
/**
* Returns the renderer to use for the given class (object type).
*
* @param clazz The class.
* @return The renderer. If no renderer has been registered for this
* class, a default renderer is returned.
* @see #getEditor
*/
public TableCellRenderer getRenderer(Class clazz) {
TableCellRenderer renderer = (TableCellRenderer)renderers.get(clazz);
if (renderer==null)
renderer = getDefaultRenderer();
return renderer;
}
/*****************************************************************************/
/**
* Returns whether the specified class has an editor/renderer pair
* registered for it.
*
* @param clazz The class.
* @return Whether the class is supported.
*/
public boolean isSupportedClass(Class clazz) {
return editors.containsKey(clazz); // Can use editors or renderers.
}
/*****************************************************************************/
/**
* Registers an editor/renderer pair with the property sheet manager.
*
* @param clazz The class type for the editor/renderer pair.
* @param propertyInfoClass Wrapper class containing information on this
* data type.
* @param renderer The renderer.
* @param editor The editor.
*/
public void register(Class clazz, Class propertyInfoClass,
TableCellRenderer renderer,
TableCellEditor editor) {
infoClasses.put(clazz, propertyInfoClass);
editors.put(clazz, editor);
renderers.put(clazz, renderer);
}
/*****************************************************************************/
/**
* Sets the cell renderer used when none has been registered for a class.
*
* @param renderer The new default renderer. This renderer should be
* generic enough to not care what type the cell value is.
* @see #getDefaultRenderer
*/
public void setDefaultRenderer(TableCellRenderer renderer) {
defaultRenderer = renderer;
}
/*****************************************************************************/
/*********************** INNER CLASSES ***************************************/
/*****************************************************************************/
/**
* The icon to expand/collapse property groups, if one is not found for
* the current UI.
*/
private static class PlusMinusIcon implements Icon {
public static final int WIDTH = 10;
public static final int HEIGHT = 10;
private static PlusMinusIcon instance;
private PlusMinusIcon() {
}
public int getIconHeight() {
return HEIGHT;
}
public int getIconWidth() {
return WIDTH;
}
public static Icon getInstance() {
if (instance==null)
instance = new PlusMinusIcon();
return instance;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.WHITE);
g.fillRect(x,y, WIDTH,HEIGHT);
g.setColor(Color.BLACK);
g.drawRect(x,y, WIDTH,HEIGHT);
y += HEIGHT/2;
g.drawLine(x+3,y, x+WIDTH-3,y);
//if (isCollapsed) {
y -= HEIGHT/2;
x += WIDTH/2;
g.drawLine(x,y+3, x,y+HEIGHT-3);
//}
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -