/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package standardDsls

def bindableMethods = {
    method name: "addPropertyChangeListener",
           type: "void",
           params: [propertyName: "java.lang.String",
           listener: "java.beans.PropertyChangeListener"],
           doc: """
           Add a PropertyChangeListener to the listener list. The listener is registered for all properties.
           The same listener object may be added more than once, and will be called as many times as it is added.
           If listener is null, no exception is thrown and no action is taken.
           @param listener The PropertyChangeListener to be added 
           """

    method name: "addPropertyChangeListener",
           type: "void",
           params: [listener: "java.beans.PropertyChangeListener"],
           doc: """
           Add a PropertyChangeListener for a specific property. The listener will be invoked only when a call on
           firePropertyChange names that specific property. The same listener object may be added more than once.
           For each property, the listener will be invoked the number of times it was added for that property.
           If propertyName or listener is null, no exception is thrown and no action is taken.
           @param propertyName The name of the property to listen on.
           @param listener The PropertyChangeListener to be added 
           """

    method name: "removePropertyChangeListener",
           type: "void",
           params: [propertyName: "java.lang.String",
           listener: "java.beans.PropertyChangeListener"],
           doc: """
           Remove a PropertyChangeListener for a specific property. If listener was added more than once to the same
           event source for the specified property, it will be notified one less time after being removed. If
           propertyName is null, no exception is thrown and no action is taken. If listener is null, or was never
           added for the specified property, no exception is thrown and no action is taken.
           @param propertyName The name of the property that was listened on.
           @param listener The PropertyChangeListener to be removed 
           """

    method name: "removePropertyChangeListener",
           type: "void",
           params: [listener: "java.beans.PropertyChangeListener"],
           doc: """
           Remove a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was
           registered for all properties. If listener was added more than once to the same event source, it will
           be notified one less time after being removed. If listener is null, or was never added, no exception is
           thrown and no action is taken.
           @param listener The PropertyChangeListener to be removed 
           """

    method name: "firePropertyChange",
           type: "void",
           params: [propertyName: "java.lang.String", oldValue: "java.lang.Object", newValue: "java.lang.Object"],
           doc: """
           Report a bound property update to any registered listeners.
           This is merely a convenience wrapper around the more general firePropertyChange method that takes PropertyChangeEvent value.
           @param propertyName The programmatic name of the property that was changed.
           @param oldValue The old value of the property.
           @param newValue The new value of the property.
           """

    method name: "getPropertyChangeListeners",
           type: "java.beans.PropertyChangeListener[]",
           doc: """
           Returns an array of all the listeners that were added to the PropertyChangeSupport object with addPropertyChangeListener().
           @return all of the PropertyChangeListeners added or an empty array if no listeners have been added
           """

    method name: "getPropertyChangeListeners",
           type: "java.beans.PropertyChangeListener[]",
           params: [propertyName: "java.lang.String"],
           doc: """
           Returns an array of all the listeners which have been associated with the named property.
           @param propertyName The name of the property being listened to 
           @return all of the PropertyChangeListeners associated with the named property. If no such 
           listeners have been added, or if propertyName is null, an empty array is returned.
           """
}

contributor(ctype:hasField(hasAnnotation("groovy.beans.Bindable"))) {
  if (psiClass?.fields?.find {it.hasAnnotation("groovy.beans.Bindable")}) {
    Closure c = bindableMethods.clone()
    c.delegate = delegate
    c.resolveStrategy = Closure.DELEGATE_FIRST
    c()
  }
}