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

📄 propertysheettable.java

📁 JGraph扩展应用。自定义Renderer,自定义视图View实现自定义工作流控件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * @PROJECT.FULLNAME@ @VERSION@ License.
 *
 * Copyright @YEAR@ L2FProd.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.l2fprod.common.propertysheet;

import com.l2fprod.common.propertysheet.PropertySheetTableModel.Item;
import com.l2fprod.common.swing.HeaderlessColumnResizer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyEditor;

import javax.swing.AbstractAction;
import javax.swing.CellEditor;
import javax.swing.Icon;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

/**
 * A table which allows the editing of Properties through
 * PropertyEditors. The PropertyEditors can be changed by using the
 * PropertyEditorRegistry.
 */
public class PropertySheetTable extends JTable {
  
  private static final int HOTSPOT_SIZE = 18;
  
  private static final String TREE_EXPANDED_ICON_KEY = "Tree.expandedIcon";
  private static final String TREE_COLLAPSED_ICON_KEY = "Tree.collapsedIcon";
  private static final String TABLE_BACKGROUND_COLOR_KEY = "Table.background";
  private static final String TABLE_FOREGROUND_COLOR_KEY = "Table.foreground";
  private static final String TABLE_SELECTED_BACKGROUND_COLOR_KEY = "Table.selectionBackground";
  private static final String TABLE_SELECTED_FOREGROUND_COLOR_KEY = "Table.selectionForeground";
  private static final String PANEL_BACKGROUND_COLOR_KEY = "Panel.background";

  private PropertyEditorFactory editorFactory;
  private PropertyRendererFactory rendererFactory;

  private TableCellRenderer nameRenderer;  
  
  private boolean wantsExtraIndent = false;
  
  /**
   * Cancel editing when editing row is changed
   */
  private TableModelListener cancelEditing;

  // Colors used by renderers
  private Color categoryBackground;
  private Color categoryForeground;
  private Color propertyBackground;
  private Color propertyForeground;
  private Color selectedPropertyBackground;
  private Color selectedPropertyForeground;
  private Color selectedCategoryBackground;
  private Color selectedCategoryForeground;
  
  public PropertySheetTable() {
    this(new PropertySheetTableModel());
  }

  public PropertySheetTable(PropertySheetTableModel dm) {
    super(dm);
    initDefaultColors();

    // select only one property at a time
    getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    // hide the table header, we do not need it
    Dimension nullSize = new Dimension(0, 0);
    getTableHeader().setPreferredSize(nullSize);
    getTableHeader().setMinimumSize(nullSize);
    getTableHeader().setMaximumSize(nullSize);
    getTableHeader().setVisible(false);

    // table header not being visible, make sure we can still resize the columns
    new HeaderlessColumnResizer(this);

    // default renderers and editors
    setRendererFactory(new PropertyRendererRegistry());
    setEditorFactory(new PropertyEditorRegistry());
    
    nameRenderer = new NameRenderer();
    
    // force the JTable to commit the edit when it losts focus
    putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
    
    // only full rows can be selected
    setColumnSelectionAllowed(false);
    setRowSelectionAllowed(true);

    // replace the edit action to always trigger the editing of the value column
    getActionMap().put("startEditing", new StartEditingAction());
    
    // ensure navigating with "TAB" moves to the next row
    getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0),
      "selectNextRowCell");
    getInputMap().put(
      KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_DOWN_MASK),
      "selectPreviousRowCell");
    
    // allow category toggle with SPACE and mouse
    getActionMap().put("toggle", new ToggleAction());
    getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0),
      "toggle");    
    addMouseListener(new ToggleMouseHandler());
  }

  /**
   * Initializes the default set of colors used by the PropertySheetTable.
   * 
   * @see #categoryBackground
   * @see #categoryForeground
   * @see #selectedCategoryBackground
   * @see #selectedCategoryForeground
   * @see #propertyBackground
   * @see #propertyForeground
   * @see #selectedPropertyBackground
   * @see #selectedPropertyForeground
   */
  private void initDefaultColors() {    
    this.categoryBackground = UIManager.getColor(PANEL_BACKGROUND_COLOR_KEY);
    this.categoryForeground = UIManager.getColor(TABLE_FOREGROUND_COLOR_KEY).darker().darker().darker();
    
    this.selectedCategoryBackground = categoryBackground.darker();
    this.selectedCategoryForeground = categoryForeground;
    
    this.propertyBackground = UIManager.getColor(TABLE_BACKGROUND_COLOR_KEY);
    this.propertyForeground = UIManager.getColor(TABLE_FOREGROUND_COLOR_KEY);
    
    this.selectedPropertyBackground = UIManager
      .getColor(TABLE_SELECTED_BACKGROUND_COLOR_KEY);
    this.selectedPropertyForeground = UIManager
      .getColor(TABLE_SELECTED_FOREGROUND_COLOR_KEY);
    
    setGridColor(categoryBackground);
  }
    
  
  public Color getCategoryBackground() {
    return categoryBackground;
  }

  /**
   * Sets the color used to paint a Category background.
   * 
   * @param categoryBackground
   */
  public void setCategoryBackground(Color categoryBackground) {
    this.categoryBackground = categoryBackground;
    repaint();
  }

  public Color getCategoryForeground() {
    return categoryForeground;
  }

  /**
   * Sets the color used to paint a Category foreground.
   * 
   * @param categoryForeground
   */
  public void setCategoryForeground(Color categoryForeground) {
    this.categoryForeground = categoryForeground;
    repaint();
  }

  public Color getSelectedCategoryBackground() {
    return selectedCategoryBackground;
  }

  /**
   * Sets the color used to paint a selected/focused Category background.
   * 
   * @param selectedCategoryBackground
   */
  public void setSelectedCategoryBackground(Color selectedCategoryBackground) {
    this.selectedCategoryBackground = selectedCategoryBackground;
    repaint();
  }

  public Color getSelectedCategoryForeground() {
    return selectedCategoryForeground;
  }

  /**
   * Sets the color used to paint a selected/focused Category foreground.
   * 
   * @param selectedCategoryForeground
   */
  public void setSelectedCategoryForeground(Color selectedCategoryForeground) {
    this.selectedCategoryForeground = selectedCategoryForeground;
    repaint();
  }

  public Color getPropertyBackground() {
    return propertyBackground;
  }

  /**
   * Sets the color used to paint a Property background.
   * 
   * @param propertyBackground
   */
  public void setPropertyBackground(Color propertyBackground) {
    this.propertyBackground = propertyBackground;
    repaint();
  }

  public Color getPropertyForeground() {
    return propertyForeground;
  }

  /**
   * Sets the color used to paint a Property foreground.
   * 
   * @param propertyForeground
   */
  public void setPropertyForeground(Color propertyForeground) {
    this.propertyForeground = propertyForeground;
    repaint();
  }

  public Color getSelectedPropertyBackground() {
    return selectedPropertyBackground;
  }

  /**
   * Sets the color used to paint a selected/focused Property background.
   * 
   * @param selectedPropertyBackground
   */
  public void setSelectedPropertyBackground(Color selectedPropertyBackground) {
    this.selectedPropertyBackground = selectedPropertyBackground;
    repaint();
  }

  public Color getSelectedPropertyForeground() {
    return selectedPropertyForeground;
  }

  /**
   * Sets the color used to paint a selected/focused Property foreground.
   * 
   * @param selectedPropertyForeground
   */
  public void setSelectedPropertyForeground(Color selectedPropertyForeground) {
    this.selectedPropertyForeground = selectedPropertyForeground;
    repaint();
  }

  public void setEditorFactory(PropertyEditorFactory factory) {
    editorFactory = factory;
  }

  public final PropertyEditorFactory getEditorFactory() {
    return editorFactory;
  }

  /**
   * @param registry
   * @deprecated use {@link #setEditorFactory(PropertyEditorFactory)}
   */
  public void setEditorRegistry(PropertyEditorRegistry registry) {
    setEditorFactory(registry);
  }

  /**
   * @deprecated use {@link #getEditorFactory()}
   * @throws ClassCastException if the current editor factory is not a
   *           PropertyEditorRegistry
   */
  public PropertyEditorRegistry getEditorRegistry() {
    return (PropertyEditorRegistry) editorFactory;
  }

  public void setRendererFactory(PropertyRendererFactory factory) {
    rendererFactory = factory;
  }

  public PropertyRendererFactory getRendererFactory() {
    return rendererFactory;
  }

  /**
   * @deprecated use {@link #setRendererFactory(PropertyRendererFactory)}
   * @param registry
   */
  public void setRendererRegistry(PropertyRendererRegistry registry) {
    setRendererFactory(registry);
  }

  /**
   * @deprecated use {@link #getRendererFactory()}
   * @throws ClassCastException if the current renderer factory is not a
   *           PropertyRendererRegistry
   */
  public PropertyRendererRegistry getRendererRegistry() {
    return (PropertyRendererRegistry) getRendererFactory();
  }

  /* (non-Javadoc)
   * @see javax.swing.JTable#isCellEditable(int, int)
   */
  public boolean isCellEditable(int row, int column) {
    // names are not editable
    if (column == 0) { return false; }

    PropertySheetTableModel.Item item = getSheetModel().getPropertySheetElement(row);
    return item.isProperty() && item.getProperty().isEditable();
  }

  /**
   * Gets the CellEditor for the given row and column. It uses the
   * editor registry to find a suitable editor for the property.
   * @see javax.swing.JTable#getCellEditor(int, int)
   */
  public TableCellEditor getCellEditor(int row, int column) {
    if (column == 0) { return null; }

    Item item = getSheetModel().getPropertySheetElement(row);
    if (!item.isProperty())
      return null;
    
    TableCellEditor result = null;
    Property propery = item.getProperty();
    PropertyEditor editor = getEditorFactory().createPropertyEditor(propery);
    if (editor != null)
      result = new CellEditorAdapter(editor);

    return result;
  }

  /* (non-Javadoc)
   * @see javax.swing.JTable#getCellRenderer(int, int)
   */
  public TableCellRenderer getCellRenderer(int row, int column) {
    PropertySheetTableModel.Item item = getSheetModel()
      .getPropertySheetElement(row);

    switch (column) {
      case PropertySheetTableModel.NAME_COLUMN:
        // name column gets a custom renderer
        return nameRenderer;

      case PropertySheetTableModel.VALUE_COLUMN: {
        if (!item.isProperty())
          return nameRenderer;

        // property value column gets the renderer from the factory
        Property property = item.getProperty();
        TableCellRenderer renderer = getRendererFactory().createTableCellRenderer(property);
        if (renderer == null)
          renderer = getCellRenderer(property.getType());
        return renderer;
      }
      default:
        // when will this happen, given the above?
        return super.getCellRenderer(row, column);
    }

⌨️ 快捷键说明

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