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

📄 propertysheettablemodel.java

📁 JGraph扩展应用。自定义Renderer,自定义视图View实现自定义工作流控件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        case VALUE_COLUMN:
          try {
            result = item.getProperty().getValue();
          } catch (Exception e) {
            e.printStackTrace();
          }
          break;
          
        default:
          // should not happen
      }
    }
    else {
      result = item;
    }
    return result;
  }

  /**
   * Sets the value at the specified row and column.  This will have
   * no effect unless the row is a property and the column is
   * {@link #VALUE_COLUMN}.
   * 
   * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
   */
  public void setValueAt(Object value, int rowIndex, int columnIndex) {
    Item item = getPropertySheetElement(rowIndex);
    if (item.isProperty() ) {
      if (columnIndex == VALUE_COLUMN) {
        try {
          item.getProperty().setValue(value);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
  }

  /**
   * Add a {@link PropertyChangeListener} to the current model.
   */
  public void addPropertyChangeListener(PropertyChangeListener listener) {
    listeners.addPropertyChangeListener(listener);
  }

  public void removePropertyChangeListener(PropertyChangeListener listener) {
    listeners.removePropertyChangeListener(listener);
  }

  public void propertyChange(PropertyChangeEvent evt) {
    // forward the event to registered listeners
    listeners.firePropertyChange(evt);
  }

  protected void visibilityChanged(final boolean restoreOldStates) {
    // Store the old visibility states
    if (restoreOldStates) {
      for (Iterator iter=publishedModel.iterator(); iter.hasNext();) {
        final Item item=(Item)iter.next();
        toggleStates.put(item.getKey(), item.isVisible() ? Boolean.TRUE : Boolean.FALSE);
      }
    }
    publishedModel.clear();
    for (Iterator iter = model.iterator(); iter.hasNext();) {
      Item item = (Item) iter.next();
      Item parent = item.getParent();
      if (restoreOldStates) {
        Boolean oldState=(Boolean)toggleStates.get(item.getKey());
        if (oldState!=null) {
          item.setVisible(oldState.booleanValue());
        }
        if (parent!=null) {
          oldState=(Boolean)toggleStates.get(parent.getKey());
          if (oldState!=null) {
            parent.setVisible(oldState.booleanValue());
          }
        }
      }
      if (parent == null || parent.isVisible())
        publishedModel.add(item);
    }
  }
  
  private void buildModel() {
    model.clear();

    if (properties != null && properties.size() > 0) {
      List sortedProperties = sortProperties(properties);
      
      switch (mode) {
        case PropertySheet.VIEW_AS_FLAT_LIST:
          // just add all the properties without categories
          addPropertiesToModel(sortedProperties, null);
          break;
          
        case PropertySheet.VIEW_AS_CATEGORIES: {
          // add properties by category
          List categories = sortCategories(getPropertyCategories(sortedProperties));
          
          for (Iterator iter = categories.iterator(); iter.hasNext();) {
            String category = (String) iter.next();
            Item categoryItem = new Item(category, null);
            model.add(categoryItem);
            addPropertiesToModel(
                sortProperties(getPropertiesForCategory(properties, category)),
                categoryItem);
          }
          break;
        }
        
        default:
          // should not happen
      }
    }

    visibilityChanged(restoreToggleStates);
    fireTableDataChanged();
  }

  protected List sortProperties(List localProperties) {
    List sortedProperties = new ArrayList(localProperties);
    if (sortingProperties) {
      if (propertySortingComparator == null) {
        // if no comparator was defined by the user, use the default
        propertySortingComparator = new PropertyComparator();
      }
      Collections.sort(sortedProperties, propertySortingComparator);
    }
    return sortedProperties;
  }
  
  protected List sortCategories(List localCategories) {
    List sortedCategories = new ArrayList(localCategories);
    if (sortingCategories) {
      if (categorySortingComparator == null) {
        // if no comparator was defined by the user, use the default
        categorySortingComparator = STRING_COMPARATOR;
      }
      Collections.sort(sortedCategories, categorySortingComparator);
    }
    return sortedCategories;
  }
  
  protected List getPropertyCategories(List localProperties) {
    List categories = new ArrayList();
    for (Iterator iter = localProperties.iterator(); iter.hasNext();) {
      Property property = (Property) iter.next();
      if (!categories.contains(property.getCategory()))
        categories.add(property.getCategory());
    }
    return categories;
  }

  /**
   * Add the specified properties to the model using the specified parent.
   *
   * @param localProperties the properties to add to the end of the model
   * @param parent the {@link Item} parent of these properties, null if none
   */
  private void addPropertiesToModel(List localProperties, Item parent) {
    for (Iterator iter = localProperties.iterator(); iter.hasNext();) {
      Property property = (Property) iter.next();
      Item propertyItem = new Item(property, parent);
      model.add(propertyItem);
      
      // add any sub-properties
      Property[] subProperties = property.getSubProperties();
      if (subProperties != null && subProperties.length > 0)
        addPropertiesToModel(Arrays.asList(subProperties), propertyItem);
    }
  }

  /**
   * Convenience method to get all the properties of one category.
   */
  private List getPropertiesForCategory(List localProperties, String category) {
    List categoryProperties = new ArrayList();
    for (Iterator iter = localProperties.iterator(); iter.hasNext();) {
      Property property = (Property) iter.next();
      if ((category == property.getCategory())
          || (category != null && category.equals(property.getCategory()))) {
        categoryProperties.add(property);
      }
    }
    return categoryProperties;
  }
  
  public class Item {
    private String name;
    private Property property;
    private Item parent;
    private boolean hasToggle = true;
    private boolean visible = true;

    private Item(String name, Item parent) {
      this.name = name;
      this.parent = parent;
      // this is not a property but a category, always has toggle
      this.hasToggle = true;
    }
    
    private Item(Property property, Item parent) {
      this.name = property.getDisplayName();
      this.property = property;
      this.parent = parent;
      this.visible = (property == null);
      
      // properties toggle if there are sub-properties
      Property[] subProperties = property.getSubProperties();
      hasToggle = subProperties != null && subProperties.length > 0;
    }

    public String getName() {
      return name;
    }
    
    public boolean isProperty() {
      return property != null;
    }
    
    public Property getProperty() {
      return property;
    }
    
    public Item getParent() {
      return parent;
    }
    
    public int getDepth() {
      int depth = 0;
      if (parent != null) {
        depth = parent.getDepth();
        if (parent.isProperty())
          ++depth;
      }
      return depth;
    }
    
    public boolean hasToggle() {
      return hasToggle;
    }

    public void toggle() {
      if (hasToggle()) {
        visible = !visible;
        visibilityChanged(false);
        fireTableDataChanged();
      }
    }

    public void setVisible(final boolean visible) {
      this.visible = visible;
    }

    public boolean isVisible() {
      return (parent == null || parent.isVisible()) && (!hasToggle || visible);
    }
    
    public String getKey() {
      StringBuffer key = new StringBuffer(name);
      Item itemParent = parent;
      while (itemParent != null) {
        key.append(":");
        key.append(itemParent.getName());
        itemParent = itemParent.getParent();
      }
      return key.toString();
    }

  }
  
  /**
   * The default comparator for Properties. Used if no other comparator is
   * defined.
   */
  public static class PropertyComparator implements Comparator {
    public int compare(Object o1, Object o2) {
      if (o1 instanceof Property && o2 instanceof Property) {
        Property prop1 = (Property) o1;
        Property prop2 = (Property) o2;
        if (prop1 == null) {
          return prop2==null?0:-1;
        } else {
          return STRING_COMPARATOR.compare(prop1.getDisplayName()==null?null:prop1.getDisplayName().toLowerCase(),
              prop2.getDisplayName() == null ? null : prop2.getDisplayName().toLowerCase());
        }
      } else {
        return 0;
      }
    }
  }

  private static final Comparator STRING_COMPARATOR =
    new NaturalOrderStringComparator();

  public static class NaturalOrderStringComparator implements Comparator {    
    public int compare(Object o1, Object o2) {
      String s1 = (String) o1;
      String s2 = (String) o2;
      if (s1 == null) {
        return s2==null?0:-1;
      } else {
        if (s2 == null) {
          return 1;
        } else {
          return s1.compareTo(s2);
        }
      }
    }
  }
}

⌨️ 快捷键说明

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