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 ReadOnlyJavaBeanBooleanPropertyBuilder} can be used to create
035 * {@link ReadOnlyJavaBeanBooleanProperty ReadOnlyJavaBeanBooleanProperties}. To create
036 * a {@code ReadOnlyJavaBeanBooleanProperty} 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 ReadOnlyJavaBeanBooleanPropertyBuilder}.
053 * by switching the Java Bean instance (with {@link #bean(java.lang.Object)} and
054 * calling {@link #build()}.
055 * 
056 * @see ReadOnlyJavaBeanBooleanProperty
057 */
058public final class ReadOnlyJavaBeanBooleanPropertyBuilder {
059
060    private final ReadOnlyJavaBeanPropertyBuilderHelper helper = new ReadOnlyJavaBeanPropertyBuilderHelper();
061
062    /**
063     * Create a new instance of {@code ReadOnlyJavaBeanBooleanPropertyBuilder}
064     * 
065     * @return the new {@code ReadOnlyJavaBeanBooleanPropertyBuilder} 
066     */
067    public static ReadOnlyJavaBeanBooleanPropertyBuilder create() {
068        return new ReadOnlyJavaBeanBooleanPropertyBuilder();
069    }
070
071    /**
072     * Generate a new {@link ReadOnlyJavaBeanBooleanProperty} with the current settings.
073     * 
074     * @return the new {@code ReadOnlyJavaBeanBooleanProperty}
075     * @throws NoSuchMethodException if the settings were not sufficient to find
076     * the getter of the Java Bean property
077     * @throws IllegalArgumentException if the Java Bean property is not of type
078     * {@code boolean} or {@code Boolean}
079     */
080    public ReadOnlyJavaBeanBooleanProperty build() throws NoSuchMethodException {
081        final ReadOnlyPropertyDescriptor descriptor = helper.getDescriptor();
082        if (!boolean.class.equals(descriptor.getType()) && !Boolean.class.equals(descriptor.getType())) {
083            throw new IllegalArgumentException("Not a boolean property");
084        }
085        return new ReadOnlyJavaBeanBooleanProperty(descriptor, helper.getBean());
086    }
087
088    /**
089     * Set the name of the property
090     * 
091     * @param name the name of the property
092     * @return a reference to this builder to enable method chaining
093     */
094    public ReadOnlyJavaBeanBooleanPropertyBuilder name(String name) {
095        helper.name(name);
096        return this;
097    }
098
099    /**
100     * Set the Java Bean instance the adapter should connect to
101     * 
102     * @param bean the Java Bean instance
103     * @return a reference to this builder to enable method chaining
104     */
105    public ReadOnlyJavaBeanBooleanPropertyBuilder bean(Object bean) {
106        helper.bean(bean);
107        return this;
108    }
109    
110    /**
111     * Set the Java Bean class in which the getter should be searched.
112     * This can be useful, if the builder should generate adapters for several
113     * Java Beans of different types.
114     * 
115     * @param beanClass the Java Bean class
116     * @return a reference to this builder to enable method chaining
117     */
118    public ReadOnlyJavaBeanBooleanPropertyBuilder beanClass(Class<?> beanClass) {
119        helper.beanClass(beanClass);
120        return this;
121    }
122
123    /**
124     * Set an alternative name for the getter. This can be omitted, if the 
125     * name of the getter follows Java Bean naming conventions.
126     * 
127     * @param getter the alternative name of the getter
128     * @return a reference to this builder to enable method chaining
129     */
130    public ReadOnlyJavaBeanBooleanPropertyBuilder getter(String getter) {
131        helper.getterName(getter);
132        return this;
133    }
134
135    /**
136     * Set the getter method directly. This can be omitted, if the 
137     * name of the getter follows Java Bean naming conventions.
138     * 
139     * @param getter the getter
140     * @return a reference to this builder to enable method chaining
141     */
142    public ReadOnlyJavaBeanBooleanPropertyBuilder getter(Method getter) {
143        helper.getter(getter);
144        return this;
145    }
146}