PropertyEditor

http://read.pudn.com/downloads37/sourcecode/windows/control/117440/PropertyProj/src/propertyproj/PropertyEditor.java__.htm

 

package propertyproj;   
   
import javax.swing.*;   
import javax.swing.table.*;   
import java.util.Hashtable;   
import java.awt.Component;   
import java.awt.*;   
import javax.swing.event.*;   
import java.awt.event.*;   
import java.util.*;   
import java.beans.*;   
   
public class PropertyEditor   
    extends JTable {   
  protected DefaultCellEditor dlgCellEditor;   
  protected DefaultCellEditor booleanCellEditor;   
  protected TableCellRenderer booleanRender;   
  protected DefaultCellEditor longEditor;   
  protected DefaultTableCellRenderer longRender;   
  protected Hashtable propertyEditors = new Hashtable(10);   
  protected Hashtable propertyRender = new Hashtable(10);   
  protected Hashtable propertyEditable = new Hashtable(10);   
  public PropertyEditorModel ptm = new PropertyEditorModel();   
   
  public PropertyEditor(PropertyEditorModel ptm) {   
    super(ptm);   
    this.ptm = ptm;   
  }   
   
  public PropertyEditor() {   
   
  }   
   
  public class PropertyEditorModel   
      extends DefaultTableModel {   
    PropertyEditorModel() {   
      super(0, 2);   
    }   
   
    /**  
     * Returns the name of the column at <code>columnIndex</code>.  
     *  
     * @param columnIndex the index of the column  
     * @return the name of the column  
     * @todo Implement this javax.swing.table.TableModel method  
     */   
    public String getColumnName(int columnIndex) {   
      if (columnIndex == 0) {   
        return "属性";   
      }   
      else {   
        return "值";   
      }   
    }   
   
    /**  
     * Returns true if the cell at <code>rowIndex</code> and  
     * <code>columnIndex</code> is editable.  
     *  
     * @param rowIndex the row whose value to be queried  
     * @param columnIndex the column whose value to be queried  
     * @return true if the cell is editable  
     * @todo Implement this javax.swing.table.TableModel method  
     */   
    public boolean isCellEditable(int rowIndex, int columnIndex) {   
      if (columnIndex == 0) {   
        return false;   
      }   
      else {   
        return true;   
   
      }   
    }   
   
  }   
   
  /**  
   * Returns an appropriate editor for the cell specified by <code>row</code> and  
   * <code>column</code>.  
   *  
   * @param row the row of the cell to edit, where 0 is the first row  
   * @param column the column of the cell to edit, where 0 is the first column  
   * @return the editor for this cell; if <code>null</code> return the default  
   *   editor for this type of cell  
   * @todo Implement this javax.swing.JTable method  
   */   
  public TableCellEditor getCellEditor(int row, int column) {   
    TableCellEditor editor = null;   
    if (column == 1) {   
      editor = (TableCellEditor) propertyEditors.get(this.getValueAt(row, 0));   
    }   
    if (editor == null) {   
      editor = super.getCellEditor(row, column);   
    }   
   
    return editor;   
   
  }   
   
  /**  
   * Returns an appropriate renderer for the cell specified by this row and  
   * column.  
   *  
   * @param row the row of the cell to render, where 0 is the first row  
   * @param column the column of the cell to render, where 0 is the first column  
   * @return the assigned renderer; if <code>null</code> returns the default  
   *   renderer for this type of object  
   * @todo Implement this javax.swing.JTable method  
   */   
  public TableCellRenderer getCellRenderer(int row, int column) {   
    TableCellRenderer render = null;   
    if (column == 1) {   
      render = (TableCellRenderer) propertyRender.get(this.getValueAt(row, 0));   
    }   
    if (render == null) {   
      render = super.getCellRenderer(row, column);   
    }   
    return render;   
  }   
   
  public void addProperty(String propertyName, Long longNumObj) {   
    if (propertyName == null) {   
      throw new RuntimeException("属性名称不能为空");   
    }   
    Object[] row = new Object[2];   
    row[0] = propertyName;   
    row[1] = longNumObj;   
    this.appenRow(row);   
    propertyEditors.put(propertyName, longEditor);   
    this.propertyRender.put(propertyName, longRender);   
    this.propertyEditable.put(propertyName, new Boolean(true));   
   
  }   
   
  public void addProperty(String propertyName, Boolean booleanObj) {   
    if (propertyName == null) {   
      throw new RuntimeException("属性名称不能为空");   
    }   
    Object[] row = new Object[2];   
    row[0] = propertyName;   
    row[1] = booleanObj;   
    this.appenRow(row);   
    propertyEditors.put(propertyName, this.booleanCellEditor);   
    this.propertyRender.put(propertyName, this.booleanRender);   
    this.propertyEditable.put(propertyName, new Boolean(true));   
   
  }   
   
  public Object getPropertyValue(String propertyName) {   
    Object retValue = null;   
    for (int i = 0; i  ptm.getRowCount(); i++) {   
      if (ptm.getValueAt(i, 0).equals(propertyName)) {   
        retValue = ptm.getValueAt(i, 1);   
        break;   
      }   
    }   
    return retValue;   
  }   
   
  public void setPropertyValue(String propertyName, Object newValue) {   
    for (int i = 0; i  ptm.getRowCount(); i++) {   
      if (ptm.getValueAt(i, 0).equals(propertyName)) {   
        ptm.setValueAt(newValue, i, 1);   
        break;   
      }   
    }   
   
  }   
   
  public void appenRow(Object[] row) {   
    ptm.addRow(row);   
   
  }   
   
  public void createLongEditorRenderer() {   
    final PanelInputText longTextField = new PanelInputText();   
    longEditor = new DefaultCellEditor(new JTextField()) {   
      Component editor;   
      private Object previsousValue = null;   
      public Object getCellEditorValue() {   
        if (editor instanceof PanelInputText) {   
   
          if ( ( (PanelInputText) editor).getText().equals("")) {   
            return (Long)null;   
          }   
          else {   
            System.out.println("getCellEditorValue   " +   
                               ( (PanelInputText) editor).getText());   
   
            return new Long( ( (PanelInputText) editor).getText());   
   
          }   
        }   
        else {   
          return (Long)null;   
        }   
      }   
   
      public Component getTableCellEditorComponent(JTable table, Object value,   
          boolean isSelected, int row, int column) {   
        editor = null;   
   
        if (value != null) {   
          longTextField.setText(value.toString());   
   
        }   
        else {   
          longTextField.setText("");   
        }   
        previsousValue = value;   
        System.out.println("getTableCellEditorCompontOldValue:   " + value);   
        editor = longTextField;   
        System.out.println("getTableCellEditorCompont");   
        return editor;   
      }   
   
      public boolean stopCellEditing() {   
        Long lv = null;   
        if (! ( (PanelInputText) editor).getText().equals("")) {   
          try {   
            System.out.println("stopCellEditing");   
            lv = new Long( ( (PanelInputText) editor).getText());   
          }   
          catch (Exception e) {   
            this.cancelCellEditing();   
            return true;   
          }   
   
        }   
        if ( (previsousValue == null) ? (previsousValue == lv) :   
            this.previsousValue.equals(lv)) {   
          System.out.println("stopCellEditing");   
          this.cancelCellEditing();   
          return true;   
        }   
        System.out.println("DefaultstopEditing");   
        return super.stopCellEditing();   
   
      }   
   
    };   
    longTextField.addFocusListener(new FocusAdapter() {   
      public void focusLost(FocusEvent e) {   
        System.out.println("FocusLost");   
        longEditor.stopCellEditing();   
   
      }   
    }   
    );   
   
    longEditor.setClickCountToStart(2);   
    this.longRender = new DefaultTableCellRenderer() {   
      public Component getTableCellRendererComponent(JTable table,   
          Object value,   
          boolean isSelected,   
          boolean hasFocus,   
          int row,   
          int column) {   
        JLabel jLabel;   
        jLabel = (JLabel)super.getTableCellRendererComponent(table, value,   
            isSelected,   
            hasFocus, row, column);   
        if (value == null) {   
          jLabel.setText("");   
        }   
        else   
        if ( ( (Long) value).toString().equals("11")) {   
          jLabel.setText("测试代码名称转换");   
        }   
        else {   
          jLabel.setText( ( (Long) value).toString());   
   
        }   
   
        if (! (value == null)) {   
          System.out.println("getRender" + value.getClass().getName());   
        }   
        return jLabel;   
   
      }   
   
    }   
   
    ;   
    longRender.setHorizontalAlignment(JLabel.LEFT);   
    longRender.setBackground(Color.pink);   
  }   
   
  public void createBooleanEditorRender() {   
    JCheckBox jcb = new JCheckBox("");   
   
    this.booleanCellEditor = new DefaultCellEditor(jcb);   
   
    booleanCellEditor.setClickCountToStart(1);   
   
    this.booleanRender = new TableCellRenderer() {   
   
      public Component getTableCellRendererComponent(JTable table, Object value,   
          boolean isSelected, boolean hasFocus,   
          int row, int column) {   
   
        BooleanRender booleanRender = new BooleanRender();   
        if (value != null) {   
          if ( ( (Boolean) value).booleanValue()) {   
            booleanRender.selJCheckBox(new Boolean(true));   
          }   
          else {   
            booleanRender.selJCheckBox(new Boolean(false));   
   
          }   
        }   
        else {   
          booleanRender.selJCheckBox(new Boolean(false));   
        }   
        return booleanRender;   
      }   
   
    };   
   
  }   
}  
 

你可能感兴趣的:(PropertyEditor)