📄 propertytablecontainer.java
字号:
/*
* 08/10/2005
*
* PropertyTableContainer.java - Container for tables in 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.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.Icon;
import javax.swing.JPanel;
import org.fife.ui.propertysheet.infos.PropertyInfo;
class PropertyTableContainer extends JPanel {
/**
*
*/
private static final long serialVersionUID = -1069218298771935102L;
private ArrayList infos;
private boolean sortByCategory;
private PropertyTable propertyTable;
private AlphabeticalPropertyTableModel alphaTableModel;
private CategorizedPropertyTableModel catTableModel;
private PropertyTableModel currentTableModel;
/*****************************************************************************/
public PropertyTableContainer(boolean sortByCategory) {
infos = new ArrayList();
setLayout(new BorderLayout());
add(new LeftBorderPanel(), BorderLayout.WEST);
propertyTable = new PropertyTable();
add(propertyTable);
setSortByCategory(sortByCategory);
}
/*****************************************************************************/
/**
* Adds a property to the contained property table.
*
* @param info The property to add.
*/
public void addProperty(PropertyInfo info) {
infos.add(info);
currentTableModel.addProperty(info);
}
/*****************************************************************************/
/**
* Begins the resizing of the columns in this property container.
*
* @see #resizeColumns
* @see #endResizingColumns
*/
public void beginResizingColumns() {
propertyTable.beginResizingColumns();
}
/*****************************************************************************/
/**
* Ends the resizing of the columns in this property panel.
*
* @see #beginResizingColumns
* @see #resizeColumns
*/
public void endResizingColumns() {
propertyTable.endResizingColumns();
}
/*****************************************************************************/
/**
* Returns information on a property by the property's name.
*
* @param name The property's name.
* @return Information on the property. If the property is not found in
* this property container, <code>null</code> is returned.
*/
public PropertyInfo getPropertyInfo(String name) {
return propertyTable.getPropertyInfo(name);
}
/*****************************************************************************/
/**
* Returns the number of properties in this property container.
*
* @return The property count.
*/
public int getPropertyCount() {
return currentTableModel.getPropertyCount();
}
/******************************************************************************/
public boolean getSortByCategory() {
return sortByCategory;
}
/******************************************************************************/
private void initializePropertyTableModel(PropertyTableModel model) {
int propertyCount = infos.size();
for (int i=0; i<propertyCount; i++) {
PropertyInfo info = (PropertyInfo)infos.get(i);
model.addProperty(info);
}
}
/*****************************************************************************/
/**
* Resizes the columns such that the first column is the specified width,
* and the second column is the remaining space in the table.
*
* @param width The new width for the first column.
* @see #beginResizingColumns
* @see #endResizingColumns
*/
public void resizeColumns(int width) {
propertyTable.resizeColumns(width);
}
/*****************************************************************************/
/**
* Sets the values of all properties in the specified JavaBean to be
* the values represented in this property container.<p>
*
* It is assumed that the parent property sheet verified that a JavaBean
* is represented.
*
* @param bean The JavaBean.
*/
public void setBeanProperties(Object bean) {
propertyTable.setBeanProperties(bean);
}
/*****************************************************************************/
public void setSortByCategory(boolean sortByCategory) {
boolean wasSorted = getSortByCategory();
if (wasSorted!=sortByCategory || currentTableModel==null) {
this.sortByCategory = sortByCategory;
// FIXME: Obtain the currently-selected property (if any), and
// select that property again after the new table model has
// been installed.
// Get the new table model.
if (sortByCategory) {
if (catTableModel==null) {
catTableModel = new CategorizedPropertyTableModel();
initializePropertyTableModel(catTableModel);
}
currentTableModel = catTableModel;
}
else { // !sortByCategory
if (alphaTableModel==null) {
alphaTableModel = new AlphabeticalPropertyTableModel();
initializePropertyTableModel(alphaTableModel);
}
currentTableModel = alphaTableModel;
}
// Use the new table model.
propertyTable.setModel(currentTableModel);
}
}
/******************************************************************************/
/************************** INNER CLASSES *************************************/
/******************************************************************************/
class LeftBorderPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 3025784584012023630L;
public LeftBorderPanel() {
super();
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
public Color getBackground() {
return PropertySheetUtilities.getBorderColor();
}
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.width = getWidth();
return d;
}
public int getWidth() {
return PropertySheet.PROPERTY_ROW_HEIGHT;
}
public void paint(Graphics g) {
// FIXME: Optimize me.
super.paint(g);
Rectangle visibleRect = g.getClipBounds();
int rowHeight = propertyTable.getRowHeight();
int row = visibleRect.y / rowHeight;
Rectangle rowBounds = propertyTable.getCellRect(row, 0, false);
while ((row*rowHeight)<(visibleRect.y+visibleRect.height)) {
if (currentTableModel.isExpandableRow(row)) {
PropertySheetManager psm = PropertySheetManager.getInstance();
boolean isCollapsed = !((Boolean)currentTableModel.getValueAt(row, 1)).booleanValue();
Icon icon = psm.getExpandedStateIcon(isCollapsed);
icon.paintIcon(this, g, (rowHeight-icon.getIconWidth())/2, rowBounds.y+(rowBounds.height-icon.getIconHeight())/2);
g.setColor(javax.swing.UIManager.getColor("Panel.background"));
g.drawLine(0,rowBounds.y-2, rowHeight-1,rowBounds.y-2);
}
row++;
rowBounds = propertyTable.getCellRect(row, 0, false);
}
}
protected void processMouseEvent(MouseEvent e) {
switch (e.getID()) {
case MouseEvent.MOUSE_CLICKED:
Point p = e.getPoint();
p.x += getWidth();
int row = propertyTable.rowAtPoint(p);
if (row!=-1) {
if (currentTableModel.isExpandableRow(row)) {
currentTableModel.toggleExpandedState(row);
}
}
break;
}
super.processMouseEvent(e);
}
}
/******************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -