码迷,mamicode.com
首页 > 编程语言 > 详细

java学习-javabeans-customization

时间:2020-11-30 15:28:01      阅读:7      评论:0      收藏:0      [点我收藏+]

标签:size   stat   hat   package   复用   属性   ack   定义   awt   

java beans specification

9 customization

When a user is composing an application in an application builder we want to allow them to
customize the appearance and behaviour of the various beans they are using.
We allow this customization to occur in two different ways. When a bean exports a set of properties, then an application builder can use these properties to construct a GUI property sheet
that lists the properties and provides a property editor for each property. The user can then use
this property sheet to update the various properties of the bean.
This kind of simple property sheet is likely to work well for simple to medium sized beans.
However for larger and more complex beans, we want to allow more sophisticated kinds of
component customization. For example, we would like to allow component writers to provide
customization “wizards” that guide users through the different steps of customizing a bean,
rather than simply facing them with property sheet choices.
We therefore allow each Java Bean to be accompanied by a customizer class that controls the
customization of the bean. This customizer class should be an AWT component that can be run
to customize a target bean. It can provide whatever GUI behaviour it wishes to control the customization of the target bean

个人理解其通用的含义在于提供通用的可复用的属性操作工具类

 

/*
 * Copyright (c) 2020, guoxing, Co,. Ltd. All Rights Reserved
 */
package com.xingguo.java.beans.customization;

import com.xingguo.java.beans.properties.Person;
import lombok.extern.slf4j.Slf4j;

import java.beans.*;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * PersonCustomizationDemo
 *
 * @author guoxing
 * @date 2020/11/24 9:26 PM
 * @since
 */
@Slf4j
public class PersonCustomizationDemo {

    public static void main(String[] args) throws IntrospectionException {
        // 模拟 <property name="age">18</property>
        Person person = new Person();

        // 通过 内省来获取到所有的property
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class, Object.class);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        if (Objects.nonNull(propertyDescriptors)) {
            // 对于propertyDescriptor 中的name属性实际就等价于baseName字段
            Stream.of(propertyDescriptors)
                    .filter(propertyDescriptor -> "age".equals(propertyDescriptor.getName()))
                    .findFirst()
                    .ifPresent(propertyDescriptor -> {
                        // 对当前属性设置 自定义 propertyEditor
                        propertyDescriptor.setPropertyEditorClass(PersonAgeEditor.class);
                        PropertyEditor propertyEditor = propertyDescriptor.createPropertyEditor(PersonAgeEditor.class);
                        //对于 propertyEditor 需要增加相应的事件监听器来获取到 setValue/setAsText传递的数据
                        // 对于 propertySupport 中当 set操作完成后会执行 java.beans.PropertyEditorSupport.firePropertyChange 发送事件
                        // 因此需要得到相关的事件信息
                        propertyEditor.addPropertyChangeListener(event -> {
                            // 对于当前操作而言, 因为 对于 propertyEditor 实际只是将外部传递的数据做自定义数据处理并存储,并不支持直接将转换后的数据赋值给要操作的Property所属对象;对于propertyEditor 并没有属于某个对象的含义,其存在的含义是工作类(插件)的作用
                            // 因此对于 真正的对象属性赋值仍然需要手动赋值操作; 因此需要通过监听 propertyEditor#value 属性变更事件来获取到传递的数据("18")
                            person.setAge((int) propertyEditor.getValue());
                        });
                        propertyEditor.setAsText("18");
                    });
            log.info("person.age:{}", person.getAge());
        }
    }
}

 

java学习-javabeans-customization

标签:size   stat   hat   package   复用   属性   ack   定义   awt   

原文地址:https://www.cnblogs.com/xingguoblog/p/14033098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!