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

📄 propertytable.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 08/10/2005
 *
 * PropertyTable.java - The actual table in a property sheet.
 * 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.Color;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Iterator;
import javax.swing.ListSelectionModel;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import org.fife.ui.propertysheet.infos.PropertyInfo;
import org.fife.ui.propertysheet.renderers.PropertyNameCellRenderer;


class PropertyTable extends JTable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Cursor tempCursor;

	protected static final PropertySheetManager manager	=
									PropertySheetManager.getInstance();
	private static final TableCellRenderer PROPERTY_NAME_RENDERER =
									new PropertyNameCellRenderer();
	private static final Cursor DRAG_CURSOR = Cursor.
							getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
	private static final int RESIZE_TOL = 3;


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


	/**
	 * Constructor.
	 */
	public PropertyTable() {
		enableEvents(AWTEvent.MOUSE_EVENT_MASK|
					AWTEvent.MOUSE_MOTION_EVENT_MASK);
		getSelectionModel().setSelectionMode(
							ListSelectionModel.SINGLE_SELECTION);
		Listener listener = new Listener();
		getSelectionModel().addListSelectionListener(listener);
		setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
	}


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


	/**
	 * Begins the resizing of the columns in this property container.
	 *
	 * @see #resizeColumns
	 * @see #endResizingColumns
	 */
	public void beginResizingColumns() {
		JTableHeader header = getTableHeader();
		//header.setDraggedDistance(0);
		header.setResizingColumn(getColumnModel().getColumn(0));
	}


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


	/**
	 * Ends the resizing of the columns in this property panel.
	 *
	 * @see #beginResizingColumns
	 * @see #resizeColumns
	 */
	public void endResizingColumns() {
		JTableHeader header = getTableHeader();
		header.setResizingColumn(null);
	}


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


	protected void fireSelectedPropertyChanged(SelectedPropertyEvent e) {
		PropertySheet ps = PropertySheetUtilities.
								getParentPropertySheet(this);
		if (ps!=null)
			ps.selectedPropertyChanged(e);
	}


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


	public TableCellEditor getCellEditor(int row, int column) {
		TableCellEditor editor = null;
		TableModel model = getModel();
		if (model.isCellEditable(row, column)) {
			PropertyInfo info = (PropertyInfo)model.getValueAt(row, column);
			editor = manager.getEditor(info.getType());
		}
		return editor;
	}


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


	/**
	 * Returns the renderer for a given cell.
	 *
	 * @param row The row of the cell.
	 * @param column The column of the cell.
	 * @return The renderer.
	 */
	public TableCellRenderer getCellRenderer(int row, int column) {

		// Give the model a chance to supply a specialized renderer.
		PropertyTableModel ptm = (PropertyTableModel)getModel();
		TableCellRenderer renderer = ptm.getCellRenderer(row, column);
		if (renderer!=null)
			return renderer;

		// If row==0, we're rendering a property name.  This should be
		// bold if the property is not its default value.
		if (column==0) {
			return PROPERTY_NAME_RENDERER;
		}

		// An editable cell means column==1, editable property.
//		else if (tableModel.isCellEditable(row, column)) {
else {
			PropertyInfo info = (PropertyInfo)getValueAt(row, column);
			renderer = manager.getRenderer(info.getType());
if (renderer==null)
	renderer = super.getCellRenderer(row, column);
		}

//		// Otherwise, we'll get the renderer for an uneditable property.
//		// FIXME:  We should return a special renderer for uneditable-but-
//		// known-about-property-types when we support them.
//		else {
//			renderer = super.getCellRenderer(row, column);
//		}

		return renderer;

	}


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


	public Color getGridColor() {
		return PropertySheetUtilities.getBorderColor();
	}


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


	/**
	 * 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) {
		Iterator i = ((PropertyTableModel)getModel()).getPropertyIterator();
		while (i.hasNext()) {
			PropertyInfo info = (PropertyInfo)i.next();
			if (info.getName().equals(name)) {
				i = null; // May help GC.
				return info;
			}
		}
		return null;
	}


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


	public boolean getRowSelectionAllowed() {
		//return false;
		return true;
	}


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


	protected void processMouseEvent(MouseEvent e) {
		switch (e.getID()) {
case MouseEvent.MOUSE_CLICKED:
	System.out.println("yay");
	int row = rowAtPoint(e.getPoint());
	if (row!=-1 && (e.getClickCount()%2==0)) {
		PropertyTableModel model = (PropertyTableModel)getModel();
		if (model.isExpandableRow(row)) {
			model.toggleExpandedState(row);
		}
		else {
			editCellAt(row, 1);
		}
	}
	break;
			case MouseEvent.MOUSE_RELEASED:
				PropertySheet ps = PropertySheetUtilities.
										getParentPropertySheet(this);
				if (ps!=null && ps.isResizingColumns()) {
					ps.endResizingColumns();
					setCursor(tempCursor);
					tempCursor = null;
					e.consume();
					return;
				}
				break;
			case MouseEvent.MOUSE_PRESSED:
				if (getCursor().equals(DRAG_CURSOR)) {
					ps = PropertySheetUtilities.
									getParentPropertySheet(this);
					ps.beginResizingColumns();
					e.consume();
					return;
				}
				break;
		}
		super.processMouseEvent(e);
	}


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


	protected void processMouseMotionEvent(MouseEvent e) {
		switch (e.getID()) {
			case MouseEvent.MOUSE_MOVED:
				TableColumn column = getColumnModel().getColumn(0);
				int colWidth = column.getWidth();
				int x = e.getX();
				if ((x>=colWidth-RESIZE_TOL) && (x<=colWidth+RESIZE_TOL)) {
					// Only change tempCursor once else it'll be set to
					// the resize cursor on the second time.
					if (tempCursor==null)
						tempCursor = getCursor();
					setCursor(DRAG_CURSOR);
				}
				else {
					setCursor(tempCursor);
				}
				break;
			case MouseEvent.MOUSE_DRAGGED:
				PropertySheet ps = PropertySheetUtilities.
										getParentPropertySheet(this);
				if (ps.isResizingColumns()) {
					ps.resizeColumns(e.getX());
				}
				break;
		}
		super.processMouseMotionEvent(e);
	}


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


	/**
	 * 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) {
		TableColumn column = getColumnModel().getColumn(0);
		//JTableHeader header = getTableHeader();
		//header.setDraggedDistance(e.getX());
		//column.setPreferredWidth(e.getX());
		column.setWidth(width);
	}


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


	/**
	 * Sets the values of all properties in the specified JavaBean to be
	 * the values represented in this property container.
	 *
	 * @param bean The JavaBean.
	 */
	public void setBeanProperties(Object bean) {
		Iterator i = ((PropertyTableModel)getModel()).getPropertyIterator();
		while (i.hasNext()) {
			PropertyInfo info = (PropertyInfo)i.next();
			PropertyDescriptor pd = info.getPropertyDescriptor();
			if (pd!=null) {
				Method writeMethod = pd.getWriteMethod();
				if (writeMethod!=null) {
					try {
						writeMethod.invoke(bean,
								new Object[] { info.getValue() });
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}


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


	public void tableChanged(TableModelEvent e) {
		PropertySheet ps = PropertySheetUtilities.
								getParentPropertySheet(this);
		if (ps!=null) {
			PropertyTableModel model = (PropertyTableModel)e.getSource();
			int r = e.getFirstRow();
			int c = e.getColumn();
			if (r!=-1 && c!=-1 && !model.isExpandableRow(r)) {
				// FIXME:  Can we isolate the bounds of the row (actually,
				// just the cell (row, 0)) to repaint only???
				repaint(); // Force displayed property name to repaint.
				PropertyInfo info = (PropertyInfo)model.getValueAt(r, c);
				ps.displayedPropertyChanged(info);
			}
		}
		super.tableChanged(e);
	}


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


	/**
	 * Listens for events in this table.
	 */
	class Listener implements ListSelectionListener {

		// Called when the user clicks a cell in the table.
		public void valueChanged(ListSelectionEvent e) {
			//if (!e.getValueIsAdjusting()) {
				int row = PropertyTable.this.getSelectedRow();
				SelectedPropertyEvent event = null;
				TableModel model = PropertyTable.this.getModel();
				// First time through it may not be...
				if (model instanceof PropertyTableModel) {
					PropertyTableModel model2 = (PropertyTableModel)model;
					if (row!=-1 && !model2.isExpandableRow(row)) {
						PropertyInfo pi = (PropertyInfo)getValueAt(row, 1);
						event = new SelectedPropertyEvent(
							PropertyTable.this, pi.getDisplayName(),
							pi.getDescription(), pi.getType());
					}
					else {
						event = new SelectedPropertyEvent(
							PropertyTable.this, null, null, null);
					}
					fireSelectedPropertyChanged(event);
				}
			//}
		}

	}


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

}

⌨️ 快捷键说明

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