⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binding.java

📁 java属性邦定的(JSR-295)的一个实现
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

        throw new AssertionError();
    }

    private final TV convertForward(SV value) {
        if (converter == null) {
            Class<?> targetType = noPrimitiveType(targetProperty.getWriteType(targetObject));
            return (TV)targetType.cast(Converter.defaultConvert(value, targetType));
        }

        return converter.convertForward(value);
    }

    private final SV convertReverse(TV value) {
        if (converter == null) {
            Class<?> sourceType = noPrimitiveType(sourceProperty.getWriteType(sourceObject));
            return (SV)sourceType.cast(Converter.defaultConvert(value, sourceType));
        }

        return converter.convertReverse(value);
    }

    /**
     * Throws an UnsupportedOperationException if the {@code Binding} is managed.
     * Useful for calling at the beginning of method implementations that
     * shouldn't be called on managed {@code Bindings}
     *
     * @throws UnsupportedOperationException if the {@code Binding} is managed
     * @see #isManaged()
     */
    protected final void throwIfManaged() {
        if (isManaged()) {
            throw new UnsupportedOperationException("Can not call this method on a managed binding");
        }
    }
    
    /**
     * Throws an IllegalStateException if the {@code Binding} is bound.
     * Useful for calling at the beginning of method implementations that
     * shouldn't be called when the {@code Binding} is bound.
     *
     * @throws IllegalStateException if the {@code Binding} is bound.
     */
    protected final void throwIfBound() {
        if (isBound()) {
            throw new IllegalStateException("Can not call this method on a bound binding");
        }
    }

    /**
     * Throws an IllegalStateException if the {@code Binding} is unbound.
     * Useful for calling at the beginning of method implementations that should
     * only be called when the {@code Binding} is bound.
     *
     * @throws IllegalStateException if the {@code Binding} is unbound.
     */
    protected final void throwIfUnbound() {
        if (!isBound()) {
            throw new IllegalStateException("Can not call this method on an unbound binding");
        }
    }

    /**
     * Returns a string representation of the {@code Binding}. This
     * method is intended to be used for debugging purposes only, and
     * the content and format of the returned string may vary between
     * implementations. The returned string may be empty but may not
     * be {@code null}.
     *
     * @return a string representation of this {@code Binding}
     */
    public String toString() {
        return getClass().getName() + " [" + paramString() + "]";
    }

    /**
     * Returns a string representing the internal state of the {@code Binding}.
     * This method is intended to be used for debugging purposes only,
     * and the content and format of the returned string may vary between
     * implementations. The returned string may be empty but may not
     * be {@code null}.
     *
     * @return a string representing the state of the {@code Binding}.
     */
    protected String paramString() {
        return "name=" + getName() +
               ", sourceObject=" + sourceObject +
               ", sourceProperty=" + sourceProperty +
               ", targetObject=" + targetObject +
               ", targetProperty=" + targetProperty +
               ", validator=" + validator +
               ", converter=" + converter +
               ", sourceNullValue=" + sourceNullValue +
               ", targetNullValue=" + targetNullValue +
               ", sourceUnreadableValueSet=" + sourceUnreadableValueSet +
               ", sourceUnreadableValue=" + sourceUnreadableValue +
               ", bound=" + isBound;
    }
    
    private void sourceChanged(PropertyStateEvent pse) {
        if (listeners != null) {
            for (BindingListener listener : listeners) {
                listener.sourceChanged(this, pse);
            }
        }

        sourceChangedImpl(pse);
    }

    /**
     * Called to indicate that the source property has fired a
     * {@code PropertyStateEvent} to indicate that its state has changed for
     * the source object. Called after the {@code Binding} has notified
     * any property change listeners and {@code BindingListeners} that
     * the source value has been edited (only if the {@code PropertyStateEvent}
     * represents a value change). This method is useful for subclasses
     * to detect source changes and perform syncing as appropriate.
     */
    protected void sourceChangedImpl(PropertyStateEvent pse) {
    }

    private void targetChanged(PropertyStateEvent pse) {
        if (listeners != null) {
            for (BindingListener listener : listeners) {
                listener.targetChanged(this, pse);
            }
        }

        targetChangedImpl(pse);
    }

    /**
     * Called to indicate that the target property has fired a
     * {@code PropertyStateEvent} to indicate that its state has changed for
     * the target object. Called after the {@code Binding} has notified
     * any property change listeners and {@code BindingListeners} that
     * the target value has been edited (only if the {@code PropertyStateEvent}
     * represents a value change). This method is useful for subclasses
     * to detect target changes and perform syncing as appropriate.
     */
    protected void targetChangedImpl(PropertyStateEvent pse) {
    }

    /**
     * Adds a {@code PropertyChangeListener} to be notified when any property of
     * this {@code Binding} changes. Does nothing if the listener is
     * {@code null}. If a listener is added more than once, notifications are
     * sent to that listener once for every time that it has been added.
     * The ordering of listener notification is unspecified.
     * <p>
     * {@code Binding} fires property change notification for the following
     * properties:
     * <p>
     * <ul>
     *    <li>{@code sourceProperty}
     *    <li>{@code targetProperty}
     *    <li>{@code sourceObject}
     *    <li>{@code targetObject}
     *    <li>{@code validator}
     *    <li>{@code converter}
     *    <li>{@code sourceNullValue}
     *    <li>{@code targetNullValue}
     *    <li>{@code sourceUnreadableValueSet}
     *    <li>{@code sourceUnreadableValue}
     *    <li>{@code bound}
     * </ul>
     * <p>
     * For other types of {@code Binding} notifications register a
     * {@code BindingListener}.
     *
     * @param listener the listener to add
     * @see #addBindingListener
     */
    public final void addPropertyChangeListener(PropertyChangeListener listener) {
        if (changeSupport == null) {
            changeSupport = new PropertyChangeSupport(this);
        }

        changeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Adds a {@code PropertyChangeListener} to be notified when the property identified
     * by the {@code propertyName} argument changes on this {@code Binding}.
     * Does nothing if the property name or listener is {@code null}.
     * If a listener is added more than once, notifications are
     * sent to that listener once for every time that it has been added.
     * The ordering of listener notification is unspecified.
     * <p>
     * {@code Binding} fires property change notification for the following
     * properties:
     * <p>
     * <ul>
     *    <li>{@code sourceProperty}
     *    <li>{@code targetProperty}
     *    <li>{@code sourceObject}
     *    <li>{@code targetObject}
     *    <li>{@code validator}
     *    <li>{@code converter}
     *    <li>{@code sourceNullValue}
     *    <li>{@code targetNullValue}
     *    <li>{@code sourceUnreadableValueSet}
     *    <li>{@code sourceUnreadableValue}
     *    <li>{@code bound}
     * </ul>
     * <p>
     * For other types of {@code Binding} notifications register a
     * {@code BindingListener}.
     *
     * @param propertyName the name of the property to listen for changes on
     * @param listener the listener to add
     */
    public final void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        if (changeSupport == null) {
            changeSupport = new PropertyChangeSupport(this);
        }

        changeSupport.addPropertyChangeListener(propertyName, listener);
    }

    /**
     * Removes a {@code PropertyChangeListener} from the {@code Binding}. Does
     * nothing if the listener is {@code null} or is not one of those registered.
     * If the listener being removed was registered more than once, only one
     * occurrence of the listener is removed from the list of listeners.
     * The ordering of listener notification is unspecified.
     *
     * @param listener the listener to remove
     * @see #addPropertyChangeListener
     */
    public final void removePropertyChangeListener(PropertyChangeListener listener) {
        if (changeSupport == null) {
            return;
        }

        changeSupport.removePropertyChangeListener(listener);
    }

    /**
     * Removes a {@code PropertyChangeListener} from the {@code Binding} for the given
     * property name. Does nothing if the property name or listener is
     * {@code null} or the listener is not one of those registered.
     * If the listener being removed was registered more than once, only one
     * occurrence of the listener is removed from the list of listeners.
     * The ordering of listener notification is unspecified.
     * 
     * @param propertyName the name of the property to remove the listener for
     * @param listener the listener to remove
     * @see #addPropertyChangeListener(String, PropertyChangeListener)
     */
    public final void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        if (changeSupport == null) {
            return;
        }

        changeSupport.removePropertyChangeListener(propertyName, listener);
    }

    /**
     * Returns the list of {@code PropertyChangeListeners} registered on this
     * {@code Binding}. Order is undefined. Returns an empty array if there are
     * no listeners.
     *
     * @return the list of {@code PropertyChangeListeners} registered on this {@code Binding}
     * @see #addPropertyChangeListener
     */
    public final PropertyChangeListener[] getPropertyChangeListeners() {
        if (changeSupport == null) {
            return new PropertyChangeListener[0];
        }
        
        return changeSupport.getPropertyChangeListeners();
    }

    /**
     * Returns the list of {@code PropertyChangeListeners} registered on this
     * {@code Binding} for the given property name. Order is undefined. Returns an empty array
     * if there are no listeners registered for the property name.
     *
     * @param propertyName the property name to retrieve the listeners for
     * @return the list of {@code PropertyChangeListeners} registered on this {@code Binding}
     *         for the given property name
     * @see #addPropertyChangeListener(String, PropertyChangeListener)
     */
    public final PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
        if (changeSupport == null) {
            return new PropertyChangeListener[0];
        }
        
        return changeSupport.getPropertyChangeListeners(propertyName);
    }

    /**
     * Sends a {@code PropertyChangeEvent} to the {@code PropertyChangeListeners}
     * registered on the {@code Binding}.
     *
     * @param propertyName the name of the property that's changed
     * @param oldValue the old value of the property
     * @param newValue the new value of the property
     */
    protected final void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
        if (changeSupport != null) {
            changeSupport.firePropertyChange(propertyName, oldValue, newValue);
        }
    }

    private class PSL implements PropertyStateListener {
        public void propertyStateChanged(PropertyStateEvent pse) {
            if (ignoreChange) {
                return;
            }

            if (pse.getSourceProperty() == sourceProperty && pse.getSourceObject() == sourceObject) {
                sourceChanged(pse);
            } else {
                targetChanged(pse);
            }
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -