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.ReadOnlyJavaBeanPropertyBuilderHelper;
029import com.sun.javafx.property.adapter.ReadOnlyPropertyDescriptor;
030
031import java.lang.reflect.Method;
032
033/**
034 * A {@code ReadOnlyJavaBeanObjectPropertyBuilder} can be used to create
035 * {@link ReadOnlyJavaBeanObjectProperty ReadOnlyJavaBeanObjectProperties}. To create
036 * a {@code ReadOnlyJavaBeanObjectProperty} 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 name of the getter follows the conventions, this is sufficient.
044 * Otherwise it is possible to specify an alternative name for the getter 
045 * ({@link #getter(java.lang.String)}) or
046 * the getter {@code Methods} directly ({@link #getter(java.lang.reflect.Method)}).
047 * <p>
048 * All methods to change properties return a reference to this builder, to enable
049 * method chaining.
050 * <p>
051 * If you have to generate adapters for the same property of several instances
052 * of the same class, you can reuse a {@code ReadOnlyJavaBeanObjectPropertyBuilder}.
053 * by switching the Java Bean instance (with {@link #bean(java.lang.Object)} and
054 * calling {@link #build()}.
055 * 
056 * @see ReadOnlyJavaBeanObjectProperty
057 * 
058 * @param T the type of the wrapped {@code Object}
059 */
060public final class ReadOnlyJavaBeanObjectPropertyBuilder<T> {
061
062    private final ReadOnlyJavaBeanPropertyBuilderHelper helper = new ReadOnlyJavaBeanPropertyBuilderHelper();
063
064    /**
065     * Create a new instance of {@code ReadOnlyJavaBeanObjectPropertyBuilder}
066     * 
067     * @return the new {@code ReadOnlyJavaBeanObjectPropertyBuilder} 
068     */
069    public static <T> ReadOnlyJavaBeanObjectPropertyBuilder<T> create() {
070        return new ReadOnlyJavaBeanObjectPropertyBuilder<T>();
071    }
072
073    /**
074     * Generate a new {@link ReadOnlyJavaBeanObjectProperty} with the current settings.
075     * 
076     * @return the new {@code ReadOnlyJavaBeanObjectProperty}
077     * @throws NoSuchMethodException if the settings were not sufficient to find
078     * the getter of the Java Bean property
079     */
080    public ReadOnlyJavaBeanObjectProperty<T> build() throws NoSuchMethodException {
081        final ReadOnlyPropertyDescriptor descriptor = helper.getDescriptor();
082        return new ReadOnlyJavaBeanObjectProperty<T>(descriptor, helper.getBean());
083    }
084
085    /**
086     * Set the name of the property
087     * 
088     * @param name the name of the property
089     * @return a reference to this builder to enable method chaining
090     */
091    public ReadOnlyJavaBeanObjectPropertyBuilder<T> name(String name) {
092        helper.name(name);
093        return this;
094    }
095
096    /**
097     * Set the Java Bean instance the adapter should connect to
098     * 
099     * @param bean the Java Bean instance
100     * @return a reference to this builder to enable method chaining
101     */
102    public ReadOnlyJavaBeanObjectPropertyBuilder<T> bean(Object bean) {
103        helper.bean(bean);
104        return this;
105    }
106    
107    /**
108     * Set the Java Bean class in which the getter should be searched.
109     * This can be useful, if the builder should generate adapters for several
110     * Java Beans of different types.
111     * 
112     * @param beanClass the Java Bean class
113     * @return a reference to this builder to enable method chaining
114     */
115    public ReadOnlyJavaBeanObjectPropertyBuilder<T> beanClass(Class<?> beanClass) {
116        helper.beanClass(beanClass);
117        return this;
118    }
119
120    /**
121     * Set an alternative name for the getter. This can be omitted, if the 
122     * name of the getter follows Java Bean naming conventions.
123     * 
124     * @param getter the alternative name of the getter
125     * @return a reference to this builder to enable method chaining
126     */
127    public ReadOnlyJavaBeanObjectPropertyBuilder<T> getter(String getter) {
128        helper.getterName(getter);
129        return this;
130    }
131
132    /**
133     * Set the getter method directly. This can be omitted, if the 
134     * name of the getter follows Java Bean naming conventions.
135     * 
136     * @param getter the getter
137     * @return a reference to this builder to enable method chaining
138     */
139    public ReadOnlyJavaBeanObjectPropertyBuilder<T> getter(Method getter) {
140        helper.getter(getter);
141        return this;
142    }
143}