Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.beans.property.adapter;
027
028import com.sun.javafx.property.adapter.JavaBeanPropertyBuilderHelper;
029import com.sun.javafx.property.adapter.PropertyDescriptor;
030
031import java.lang.reflect.Method;
032
033/**
034 * A {@code JavaBeanObjectPropertyBuilder} can be used to create
035 * {@link JavaBeanObjectProperty JavaBeanObjectProperties}. To create
036 * a {@code JavaBeanObjectProperty} one first has to call {@link #create()}
037 * to generate a builder, set the required properties, and then one can
038 * call {@link #build()} to generate the property.
039 * <p>
040 * Not all properties of a builder have to specified, there are several
041 * combinations possible. As a minimum the {@link #name(java.lang.String)} of
042 * the property and the {@link #bean(java.lang.Object)} have to be specified.
043 * If the names of the getter and setter follow the conventions, this is sufficient.
044 * Otherwise it is possible to specify an alternative name for the getter and setter
045 * ({@link #getter(java.lang.String)} and {@link #setter(java.lang.String)}) or
046 * the getter and setter {@code Methods} directly ({@link #getter(java.lang.reflect.Method)}
047 * and {@link #setter(java.lang.reflect.Method)}).
048 * <p>
049 * All methods to change properties return a reference to this builder, to enable
050 * method chaining.
051 * <p>
052 * If you have to generate adapters for the same property of several instances
053 * of the same class, you can reuse a {@code JavaBeanObjectPropertyBuilder}.
054 * by switching the Java Bean instance (with {@link #bean(java.lang.Object)} and
055 * calling {@link #build()}.
056 * 
057 * @see JavaBeanObjectProperty
058 * 
059 * @param T the type of the wrapped {@code Object}
060 */
061public final class JavaBeanObjectPropertyBuilder<T> {
062
063    private JavaBeanPropertyBuilderHelper helper = new JavaBeanPropertyBuilderHelper();
064
065    /**
066     * Create a new instance of {@code JavaBeanObjectPropertyBuilder}
067     * 
068     * @return the new {@code JavaBeanObjectPropertyBuilder} 
069     */
070    public static JavaBeanObjectPropertyBuilder create() {
071        return new JavaBeanObjectPropertyBuilder();
072    }
073
074    /**
075     * Generate a new {@link JavaBeanObjectProperty} with the current settings.
076     * 
077     * @return the new {@code JavaBeanObjectProperty}
078     * @throws NoSuchMethodException if the settings were not sufficient to find
079     * the getter and the setter of the Java Bean property
080     */
081    public JavaBeanObjectProperty<T> build() throws NoSuchMethodException {
082        final PropertyDescriptor descriptor = helper.getDescriptor();
083        return new JavaBeanObjectProperty<T>(descriptor, helper.getBean());
084    }
085
086    /**
087     * Set the name of the property
088     * 
089     * @param name the name of the property
090     * @return a reference to this builder to enable method chaining
091     */
092    public JavaBeanObjectPropertyBuilder name(String name) {
093        helper.name(name);
094        return this;
095    }
096
097    /**
098     * Set the Java Bean instance the adapter should connect to
099     * 
100     * @param bean the Java Bean instance
101     * @return a reference to this builder to enable method chaining
102     */
103    public JavaBeanObjectPropertyBuilder bean(Object bean) {
104        helper.bean(bean);
105        return this;
106    }
107    
108    /**
109     * Set the Java Bean class in which the getter and setter should be searched.
110     * This can be useful, if the builder should generate adapters for several
111     * Java Beans of different types.
112     * 
113     * @param beanClass the Java Bean class
114     * @return a reference to this builder to enable method chaining
115     */
116    public JavaBeanObjectPropertyBuilder beanClass(Class<?> beanClass) {
117        helper.beanClass(beanClass);
118        return this;
119    }
120
121    /**
122     * Set an alternative name for the getter. This can be omitted, if the 
123     * name of the getter follows Java Bean naming conventions.
124     * 
125     * @param getter the alternative name of the getter
126     * @return a reference to this builder to enable method chaining
127     */
128    public JavaBeanObjectPropertyBuilder getter(String getter) {
129        helper.getterName(getter);
130        return this;
131    }
132
133    /**
134     * Set an alternative name for the setter. This can be omitted, if the 
135     * name of the setter follows Java Bean naming conventions.
136     * 
137     * @param setter the alternative name of the setter
138     * @return a reference to this builder to enable method chaining
139     */
140    public JavaBeanObjectPropertyBuilder setter(String setter) {
141        helper.setterName(setter);
142        return this;
143    }
144
145    /**
146     * Set the getter method directly. This can be omitted, if the 
147     * name of the getter follows Java Bean naming conventions.
148     * 
149     * @param getter the getter
150     * @return a reference to this builder to enable method chaining
151     */
152    public JavaBeanObjectPropertyBuilder getter(Method getter) {
153        helper.getter(getter);
154        return this;
155    }
156
157    /**
158     * Set the setter method directly. This can be omitted, if the 
159     * name of the setter follows Java Bean naming conventions.
160     * 
161     * @param setter the setter
162     * @return a reference to this builder to enable method chaining
163     */
164    public JavaBeanObjectPropertyBuilder setter(Method setter) {
165        helper.setter(setter);
166        return this;
167    }
168}