📄 categorizedpropertytablemodel.java
字号:
/*
* 01/27/2005
*
* CategorizedPropertyTableModel.java - Model for a property table whose
* properties are sorted by property category.
* 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.util.*;
import javax.swing.table.*;
import org.fife.ui.propertysheet.infos.PropertyInfo;
import org.fife.ui.propertysheet.renderers.CategoryNameCellRenderer;
/**
* A table model for a table of properties sorted by category.
*
* @author Robert Futrell
* @version 1.0
* @see PropertyTable
*/
class CategorizedPropertyTableModel extends PropertyTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private HashMap categoryProperties;
private static final TableCellRenderer CATEGORY_ROW_RENDERER =
new CategoryNameCellRenderer();
/*****************************************************************************/
/**
* Constructor.
*/
public CategorizedPropertyTableModel() {
categoryProperties = new HashMap();
}
/*****************************************************************************/
/**
* Adds a property to this table model.
*
* @param info The property to add.
*/
public void addProperty(PropertyInfo info) {
String displayName = info.getDisplayName();
String category = info.getCategory();
if (!categoryProperties.containsKey(category)) {
TreeSet set = new TreeSet();
set.add(info);
categoryProperties.put(category, set);
Object[] rowData = new Object[] { category, Boolean.FALSE };
addRow(rowData);
rowData = new Object[] { displayName, info };
addRow(rowData);
}
else {
TreeSet set = (TreeSet)categoryProperties.get(category);
set.add(info);
int categoryRow = findCategoryRow(category);
int rowCount = getRowCount();
Object[] rowData = new Object[] { displayName, info };
boolean inserted = false;
for (int i=categoryRow+1; i<rowCount; i++) {
if (isCategoryRow(i)) {
insertRow(i, rowData);
inserted = true;
break;
}
else {
String rowName = (String)getValueAt(i, 0);
if (displayName.compareTo(rowName)<=0) {
insertRow(i, rowData);
inserted = true;
break;
}
}
}
// If it's lexicographically at the end, add it to the end.
if (!inserted) {
addRow(rowData);
}
}
}
/*****************************************************************************/
public int findCategoryRow(String category) {
int rowCount = getRowCount();
for (int i=0; i<rowCount; i++) {
if (isCategoryRow(i)) {
String text = (String)getValueAt(i, 0);
if (text.equals(category))
return i;
}
}
return -1;
}
/*****************************************************************************/
/**
* Returns the renderer for a given cell.
*
* @param row The row of the cell.
* @param column The column of the cell.
* @return The renderer. If <code>null</code> is returned, the table
* itself will decide on a renderer.
*/
public TableCellRenderer getCellRenderer(int row, int column) {
// If this row is a category header, use the special renderer.
if (isCategoryRow(row)) {
return CATEGORY_ROW_RENDERER;
}
return null;
}
/*****************************************************************************/
/**
* Returns the number of properties in this property container.
*
* @return The property count.
*/
public int getPropertyCount() {
int count = 0;
Set keySet = categoryProperties.keySet();
for (Iterator i=keySet.iterator(); i.hasNext(); ) {
Set propertySet = (Set)categoryProperties.get(i.next());
count += propertySet.size();
}
return count;
}
/*****************************************************************************/
/**
* Returns an iterator over the properties in this property sheet.
*
* @return The iterator.
*/
public Iterator getPropertyIterator() {
return new CategorizedPropertyTableIterator();
}
/*****************************************************************************/
public boolean isCategoryRow(int row) {
Object value = getValueAt(row, 1);
return !(value instanceof PropertyInfo);
}
/*****************************************************************************/
/**
* Returns whether the specified cell is editable.
*
* @param row The row of the cell.
* @param column The column of the cell.
* @return Whether the cell is editable.
*/
public boolean isCellEditable(int row, int column) {
return column==1 && !isCategoryRow(row) &&
((PropertyInfo)getValueAt(row, column)).isModifiable();
}
/*****************************************************************************/
/**
* Returns whether the specified row is expandable (that is, it contains
* "sub-rows" that can optionally be displayed).
*
* @param row The row in question. This is a row index into this table as
* it is currently displayed.
* @return Whether the row is expandable.
*/
public boolean isExpandableRow(int row) {
return isCategoryRow(row);
}
/*****************************************************************************/
/**
* If the specified row is expandable, its expanded state is toggled.
*
* @param row The row in question. This is a row index into this table as
* it is currently displayed.
*/
void toggleExpandedState(int row) {
if (isExpandableRow(row)) {
boolean b = ((Boolean)getValueAt(row, 1)).booleanValue();
boolean newValue = !b;
setValueAt(Boolean.valueOf(newValue), row,1);
if (newValue) {
row++;
while (row<getRowCount() && !isCategoryRow(row)) {
removeRow(row);
}
}
else { // newValue==false;
String category = (String)getValueAt(row, 0);
row++;
Set propSet = (Set)categoryProperties.get(category);
for (Iterator i=propSet.iterator(); i.hasNext(); ) {
PropertyInfo pi = (PropertyInfo)i.next();
String displayName = pi.getDisplayName();
insertRow(row, new Object[] { displayName, pi });
}
}
}
}
/*****************************************************************************/
/******************* PRIVATE INNER CLASSES ***********************************/
/*****************************************************************************/
class CategorizedPropertyTableIterator implements Iterator {
private Iterator keyIterator;
private List propList;
private int index;
public CategorizedPropertyTableIterator() {
Set keySet = categoryProperties.keySet();
keyIterator = keySet.iterator();
if (keyIterator.hasNext()) {
propList = (List)keyIterator.next();
index = 0;
}
}
public boolean hasNext() {
if (propList!=null && index<propList.size())
return true;
if (keyIterator.hasNext()) {
propList = (List)keyIterator.next();
index = 0;
return hasNext();
}
return false;
}
public Object next() {
if (!hasNext())
throw new NoSuchElementException();
return propList.get(index++);
}
public void remove() {
throw new UnsupportedOperationException();
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -