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

📄 propertysheettablemodel.java

📁 一个demo是关于swing
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * $ $ License.
 *
 * Copyright $ 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.swing.ObjectTableModel;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import javax.swing.table.AbstractTableModel;

/**
 * PropertySheetTableModel. <br>
 *  
 */
public class PropertySheetTableModel
  extends AbstractTableModel
  implements PropertyChangeListener, PropertySheet, ObjectTableModel {
  public static final int NAME_COLUMN = 0;
  public static final int VALUE_COLUMN = 1;
  public static final int NUM_COLUMNS = 2;

  private PropertyChangeSupport listeners = new PropertyChangeSupport(this);
  private List model;
  private List publishedModel;
  private List properties;
  private int mode;
  private boolean sortingCategories;
  private boolean sortingProperties;
  private Comparator categorySortingComparator;
  private Comparator propertySortingComparator;

  public PropertySheetTableModel() {
    model = new ArrayList();
    publishedModel = new ArrayList();
    properties = new ArrayList();
    mode = PropertySheet.VIEW_AS_FLAT_LIST;
    sortingCategories = false;
    sortingProperties = false;
  }

  /* (non-Javadoc)
   * @see com.l2fprod.common.propertysheet.PropertySheet#setProperties(com.l2fprod.common.propertysheet.Property[])
   */
  public void setProperties(Property[] newProperties) {
    // unregister the listeners from previous properties
    for (Iterator iter = properties.iterator(); iter.hasNext();) {
      Property prop = (Property) iter.next();
      prop.removePropertyChangeListener(this);
      
      Property[] subProp = prop.getSubProperties();
      if (subProp != null)
        for (int i = 0; i < subProp.length; ++i)
    	  subProp[i].removePropertyChangeListener(this);
    }

    // replace the current properties
    properties.clear();
    properties.addAll(Arrays.asList(newProperties));

    // add listeners
    for (Iterator iter = properties.iterator(); iter.hasNext();) {
      Property prop = (Property) iter.next();
      prop.addPropertyChangeListener(this);
      
      Property[] subProp = prop.getSubProperties();
      if (subProp != null)
        for (int i = 0; i < subProp.length; ++i)
    	  subProp[i].addPropertyChangeListener(this);
    }

    buildModel();
  }

  /* (non-Javadoc)
   * @see com.l2fprod.common.propertysheet.PropertySheet#getProperties()
   */
  public Property[] getProperties() {
    return (Property[]) properties.toArray(new Property[properties.size()]);
  }

  /* (non-Javadoc)
   * @see com.l2fprod.common.propertysheet.PropertySheet#addProperty(com.l2fprod.common.propertysheet.Property)
   */
  public void addProperty(Property property) {
    properties.add(property);
    property.addPropertyChangeListener(this);
    buildModel();
  }

  /* (non-Javadoc)
   * @see com.l2fprod.common.propertysheet.PropertySheet#addProperty(int, com.l2fprod.common.propertysheet.Property)
   */
  public void addProperty(int index, Property property) {
    properties.add(index, property);
    property.addPropertyChangeListener(this);
    buildModel();
  }

  /* (non-Javadoc)
   * @see com.l2fprod.common.propertysheet.PropertySheet#removeProperty(com.l2fprod.common.propertysheet.Property)
   */
  public void removeProperty(Property property) {
    properties.remove(property);
    property.removePropertyChangeListener(this);
    buildModel();
  }

  /* (non-Javadoc)
   * @see com.l2fprod.common.propertysheet.PropertySheet#getPropertyCount()
   */
  public int getPropertyCount() {
    return properties.size();
  }

  /* (non-Javadoc)
   * @see com.l2fprod.common.propertysheet.PropertySheet#propertyIterator()
   */
  public Iterator propertyIterator() {
    return properties.iterator();
  }

  /**
   * Set the current mode, either {@link PropertySheet#VIEW_AS_CATEGORIES}
   * or {@link PropertySheet#VIEW_AS_FLAT_LIST}. 
   */
  public void setMode(int mode) {
    if (this.mode == mode) {
      return;
    }
    this.mode = mode;
    buildModel();
  }

  /**
   * Get the current mode, either {@link PropertySheet#VIEW_AS_CATEGORIES}
   * or {@link PropertySheet#VIEW_AS_FLAT_LIST}.
   */
  public int getMode() {
    return mode;
  }

  /* (non-Javadoc)
   * @see javax.swing.table.TableModel#getColumnClass(int)
   */
  public Class getColumnClass(int columnIndex) {
    return super.getColumnClass(columnIndex);
  }

  /* (non-Javadoc)
   * @see javax.swing.table.TableModel#getColumnCount()
   */
  public int getColumnCount() {
    return NUM_COLUMNS;
  }

  /* (non-Javadoc)
   * @see javax.swing.table.TableModel#getRowCount()
   */
  public int getRowCount() {
    return publishedModel.size();
  }

  /* (non-Javadoc)
   * @see com.l2fprod.common.swing.ObjectTableModel#getObject(int)
   */
  public Object getObject(int rowIndex) {
    return getPropertySheetElement(rowIndex);
  }

  /**
   * Get the current property sheet element, of type {@link Item}, at
   * the specified row.
   */
  public Item getPropertySheetElement(int rowIndex) {
    return (Item) publishedModel.get(rowIndex);
  }

  /**
   * Get whether this model is currently sorting categories.
   */
  public boolean isSortingCategories() {
    return sortingCategories;
  }

  /**
   * Set whether this model is currently sorting categories.
   * If this changes the sorting, the model will be rebuilt.
   */
  public void setSortingCategories(boolean value) {
    boolean old = sortingCategories;
    sortingCategories = value;
    if (sortingCategories != old)
      buildModel();
  }

  /**
   * Get whether this model is currently sorting properties.
   */
  public boolean isSortingProperties() {
    return sortingProperties;
  }

  /**
   * Set whether this model is currently sorting properties.
   * If this changes the sorting, the model will be rebuilt.
   */
  public void setSortingProperties(boolean value) {
    boolean old = sortingProperties;
    sortingProperties = value;
    if (sortingProperties != old)
      buildModel();
  }

  /**
   * Set the comparator used for sorting categories.  If this
   * changes the comparator, the model will be rebuilt.
   */
  public void setCategorySortingComparator(Comparator comp) {
    Comparator old = categorySortingComparator;
    categorySortingComparator = comp;
    if (categorySortingComparator != old)
      buildModel();
  }

  /**
   * Set the comparator used for sorting properties.  If this
   * changes the comparator, the model will be rebuilt.
   */
  public void setPropertySortingComparator(Comparator comp) {
    Comparator old = propertySortingComparator;
    propertySortingComparator = comp;
    if (propertySortingComparator != old)
      buildModel();
  }

  /**
   * Retrieve the value at the specified row and column location.
   * When the row contains a category or the column is
   * {@link #NAME_COLUMN}, an {@link Item} object will be returned.
   * If the row is a property and the column is {@link #VALUE_COLUMN},
   * the value of the property will be returned.
   * 
   * @see javax.swing.table.TableModel#getValueAt(int, int)
   */
  public Object getValueAt(int rowIndex, int columnIndex) {
    Object result = null;
    Item item = getPropertySheetElement(rowIndex);

    if (item.isProperty()) {
      switch (columnIndex) {
        case NAME_COLUMN:
          result = item;
          break;
          
        case VALUE_COLUMN:

⌨️ 快捷键说明

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