`
nlslzf
  • 浏览: 1048928 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

PropertyEditor

    博客分类:
  • java
 
阅读更多

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;   
      }   
   
    };   
   
  }   
}  
 
分享到:
评论

相关推荐

    学习Spring必学的Java基础知识(3)—PropertyEditor

    在深入学习Spring框架之前,掌握基础的Java知识是至关重要的,特别是关于`PropertyEditor`的部分。`PropertyEditor`是Java中用于类型转换的关键组件,它允许我们自定义数据类型的转换规则,这对于处理用户输入或者在...

    SpringMVC之DataBinding和Validation--Validator,PropertyEditor,Converter,Formatter

    本文将深入探讨这两个概念,并结合`Validator`、`PropertyEditor`、`Converter`和`Formatter`这四个核心组件,来阐述它们在实际开发中的应用。 首先,Data Binding是Spring MVC中一种自动将HTTP请求参数绑定到Java...

    spring数据绑定.docx

    在`spring-beans`模块中,`PropertyEditorRegistrySupport`类是`PropertyEditor`注册的核心,它负责管理一系列默认的`PropertyEditor`实例,用于处理常见的数据类型转换。 在提供的源码片段中,我们可以看到`...

    25 Spring Core 数据绑定之BeanWrapper实现示例及背后原理探究慕课专栏1

    `TypeConverterDelegate#convertIfNecessary()`方法是整个数据绑定流程中的关键点,它根据需要的类型选择合适的`PropertyEditor`并执行转换。这个过程体现了Spring的灵活性和强大的类型转换能力。 总的来说,Spring...

    Spring Boot项目中定制PropertyEditors方法

    最后,在 BookController 中,我们可以使用 @PathVariable 注解来将 ISBN 号码注入到函数中,并使用自定义的 PropertyEditor 来将 ISBN 号码转换成 Isbn 对象。 在实际应用中,我们可以使用 Httpie 工具来访问我们...

    spring数据格式转换

    在上述内容中,提到了Spring内建的多种`PropertyEditor`,例如`ByteArrayPropertyEditor`用于`String`到`byte[]`的转换,`ClassEditor`用于`String`到`Class`的转换,以及`CustomBooleanEditor`用于处理`String`到`...

    专题资料(2021-2022年)SpringMVC数据类型转换要点.doc

    在Spring 3之前,数据转换、验证和格式化主要依赖于`PropertyEditor`。这个机制的工作流程如下: 1. 类型转换:使用`PropertyEditor`的`setAsText(String)`方法将字符串转换为对象,然后通过`setValue(Object)`设置...

    Property Editor

    "Property Editor"是一款专为Eclipse集成开发环境设计的属性编辑器插件。这款插件极大地提升了用户在处理.properties文件时的效率和便利性。在Eclipse中,.properties文件通常用于存储应用程序的配置信息,如国际化...

    Java反射中java.beans包学习总结.docx

    `PropertyEditor`接口是JavaBeans的核心组件之一,用于处理JavaBean的属性转换。它允许我们将字符串与其他类型的数据之间进行转换。例如,在给定的例子中,`DodeDOEditor`类扩展了`PropertyEditorSupport`,并实现...

    Acegi将资源权限数据存储到数据库.pdf

    为了实现从数据库加载权限配置,我们可以创建一个新的`PropertyEditor`,比如`DatabaseDrivenPermissionEditor`,并在其中实现从数据库读取并解析权限逻辑。 这个自定义的`PropertyEditor`会在遇到特定标记(如...

    spring 自定义属性编辑器

    在Spring框架中,属性编辑器(PropertyEditor)是一种强大的工具,允许我们自定义类型转换过程。当我们需要将字符串形式的数据转换为Java对象时,属性编辑器就发挥了关键作用。例如,从请求参数或配置文件中读取的...

    第十五章 Spring 类型转换(Type Conversion)1

    总结来说,Spring 的类型转换机制是一个强大而灵活的系统,它涵盖了从传统的 `PropertyEditor` 到现代的 `Converter` 和 `GenericConverter` 接口。理解并掌握这一机制,可以帮助开发者更好地处理应用程序中的类型...

    Spring MVC学习(七)-------SpringMVC数据类型转换

    在Spring 3之前的版本中,开发者主要依靠`PropertyEditor`来完成这些任务,但在实际应用中发现这种方式存在诸多局限性。随着Spring 3的发布,Spring MVC引入了一套全新的机制,极大地简化了这些过程,并提高了灵活性...

    Delphi7~Delphi2010 JSON 读写组件(源码)

    JSON,全称JavaScript Object Notation,是一种轻量级的数据交换格式,因其简洁和高效而广泛应用于Web服务和应用程序之间的数据传输。在Delphi编程环境中,处理JSON数据通常需要借助特定的组件或库。...

    26 Spring Core 数据绑定之DataBinder实现示例及背后原理探究慕课专栏(1)1

    在实际应用中,我们可以通过`initBinder`方法来自定义DataBinder的行为,例如注册自定义的PropertyEditor或Validator,以满足特定的业务需求。这在Spring MVC中尤其常见,例如在Controller方法前注册校验规则,确保...

    Saber仿真软件入门教程

    通过以上知识点的学习,用户可以初步掌握如何使用SaberDesigner创建简单的单级晶体管放大器电路设计,并能够熟练运用PartGallery查找元件、使用PropertyEditor修改元件属性、进行电路布线以及使用模板等功能。...

    29 Spring MVC之类型转换Converter慕课专栏1

    在早期版本中,我们依赖于`PropertyEditor`来完成这个任务,它只能处理从`String`到其他类型的转换。然而,随着Spring的发展,从Spring 3开始,引入了`Converter`接口,大大增强了类型转换的能力。 `Converter`接口...

Global site tag (gtag.js) - Google Analytics