Search in sources :

Example 61 with BeanInfo

use of java.beans.BeanInfo in project gate-core by GateNLP.

the class CreoleAnnotationHandler method processParameters.

/**
 * Process any {@link CreoleParameter} and {@link HiddenCreoleParameter}
 * annotations on set methods of the given class and set up the corresponding
 * PARAMETER elements.
 *
 * @param resourceClass
 *          the resource class to process
 * @param resourceElement
 *          the RESOURCE element to which the PARAMETERs are to be added
 * @param parameterMap
 *          a map from parameter names to the PARAMETER elements that define
 *          them. This is used as we combine information from the original
 *          creole.xml, the parameter annotation on the target method and the
 *          annotations on the same method of its superclasses and interfaces.
 *          Parameter names that have been hidden by a
 *          {@link HiddenCreoleParameter} annotation are explicitly mapped to
 *          <code>null</code> in this map.
 * @param disjunctionMap
 *          a map from disjunction IDs to the OR elements that define them.
 *          Disjunctive parameters are handled by specifying a disjunction ID
 *          on the {@link CreoleParameter} annotations - parameters with the
 *          same disjunction ID are grouped under the same OR element.
 */
private void processParameters(Class<?> resourceClass, Element resourceElement, Map<String, Element> parameterMap, Map<String, Element> disjunctionMap) throws GateException {
    BeanInfo bi;
    try {
        bi = Introspector.getBeanInfo(resourceClass);
    } catch (IntrospectionException e) {
        throw new GateException("Failed to introspect " + resourceClass, e);
    }
    for (Method method : resourceClass.getDeclaredMethods()) {
        processElement(method, bi, resourceElement, parameterMap, disjunctionMap);
    }
    for (Field field : resourceClass.getDeclaredFields()) {
        processElement(field, bi, resourceElement, parameterMap, disjunctionMap);
    }
    // go up the tree
    Class<?> superclass = resourceClass.getSuperclass();
    if (superclass != null) {
        processParameters(superclass, resourceElement, parameterMap, disjunctionMap);
    }
    for (Class<?> intf : resourceClass.getInterfaces()) {
        processParameters(intf, resourceElement, parameterMap, disjunctionMap);
    }
}
Also used : Field(java.lang.reflect.Field) BeanInfo(java.beans.BeanInfo) GateException(gate.util.GateException) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method)

Example 62 with BeanInfo

use of java.beans.BeanInfo in project KNOBS by ESSICS.

the class KnobController method initialize.

@Override
public void initialize(URL location, ResourceBundle resources) {
    propertySheet.setPropertyEditorFactory(new KnobPropertyEditorFactory());
    long beforeTime = System.currentTimeMillis();
    Knob knob = KnobBuilder.create().onAdjusted(e -> LOGGER.info(MessageFormat.format("Current value reached target: {0}", ((Knob) e.getSource()).getCurrentValue()))).onTargetSet(e -> {
        LOGGER.info(MessageFormat.format("Target changed: {0}", ((Knob) e.getSource()).getTargetValue()));
        updateCurrentValue = true;
    }).build();
    long afterTime = System.currentTimeMillis();
    LOGGER.log(Level.INFO, "Construction time: {0,number,#########0}ms", afterTime - beforeTime);
    knobContainer.getChildren().add(knob);
    Map<Class<?>, String> categories = new HashMap<>(5);
    categories.put(Knob.class, "\u200BKnob");
    categories.put(Region.class, "\u200B\u200BRegion");
    categories.put(Parent.class, "\u200B\u200B\u200BParent");
    categories.put(Node.class, "\u200B\u200B\u200B\u200BNode");
    categories.put(Object.class, "\u200B\u200B\u200B\u200B\u200BObject");
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(Knob.class, Object.class);
        for (PropertyDescriptor p : beanInfo.getPropertyDescriptors()) {
            try {
                if (p.getReadMethod() != null && p.getWriteMethod() != null) {
                    p.setValue(BeanProperty.CATEGORY_LABEL_KEY, categories.get(p.getReadMethod().getDeclaringClass()));
                    propertySheet.getItems().add(new BeanProperty(knob, p));
                }
            } catch (Exception iex) {
                LOGGER.log(Level.SEVERE, MessageFormat.format("Unable to handle property \"{0}\" [{1}].", p.getName(), iex.getMessage()));
            }
        }
    } catch (IntrospectionException ex) {
        LOGGER.log(Level.SEVERE, "Unable to initialize the controller.", ex);
    }
    propertySheet.setMode(PropertySheet.Mode.CATEGORY);
    timer.scheduleAtFixedRate(() -> {
        if (updateCurrentValue) {
            double step = (knob.getMaxValue() - knob.getMinValue()) / 234;
            double cValue = knob.getCurrentValue();
            double tValue = knob.getTargetValue();
            if (cValue < tValue) {
                Platform.runLater(() -> {
                    if ((tValue - cValue) > step) {
                        knob.setCurrentValue(cValue + step);
                    } else {
                        knob.setCurrentValue(tValue);
                        updateCurrentValue = false;
                    }
                });
            } else if (cValue > tValue) {
                Platform.runLater(() -> {
                    if ((cValue - tValue) > step) {
                        knob.setCurrentValue(cValue - step);
                    } else {
                        knob.setCurrentValue(tValue);
                        updateCurrentValue = false;
                    }
                });
            }
        }
    }, 2000, 50, TimeUnit.MILLISECONDS);
}
Also used : Initializable(javafx.fxml.Initializable) URL(java.net.URL) PropertySheet(org.controlsfx.control.PropertySheet) Node(javafx.scene.Node) HashMap(java.util.HashMap) Logger(java.util.logging.Logger) Executors(java.util.concurrent.Executors) IntrospectionException(java.beans.IntrospectionException) MessageFormat(java.text.MessageFormat) Level(java.util.logging.Level) TimeUnit(java.util.concurrent.TimeUnit) Platform(javafx.application.Platform) FXML(javafx.fxml.FXML) Introspector(java.beans.Introspector) Parent(javafx.scene.Parent) Region(javafx.scene.layout.Region) ResourceBundle(java.util.ResourceBundle) FlowPane(javafx.scene.layout.FlowPane) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BeanProperty(org.controlsfx.property.BeanProperty) PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) IntrospectionException(java.beans.IntrospectionException) BeanProperty(org.controlsfx.property.BeanProperty)

Example 63 with BeanInfo

use of java.beans.BeanInfo in project gate-core by GateNLP.

the class AbstractResource method setResourceListeners.

/**
 * Adds listeners to a resource.
 * @param listeners The listeners to be registered with the resource. A
 * {@link java.util.Map} that maps from fully qualified class name (as a
 * string) to listener (of the type declared by the key).
 * @param resource the resource that listeners will be registered to.
 */
public static void setResourceListeners(Resource resource, Map<String, ? extends Object> listeners) throws IntrospectionException, InvocationTargetException, IllegalAccessException, GateException {
    // get the beaninfo for the resource bean, excluding data about Object
    BeanInfo resBeanInfo = getBeanInfo(resource.getClass());
    // get all the events the bean can fire
    EventSetDescriptor[] events = resBeanInfo.getEventSetDescriptors();
    // add the listeners
    if (events != null) {
        EventSetDescriptor event;
        for (int i = 0; i < events.length; i++) {
            event = events[i];
            // did we get such a listener?
            Object listener = listeners.get(event.getListenerType().getName());
            if (listener != null) {
                Method addListener = event.getAddListenerMethod();
                // call the set method with the parameter value
                Object[] args = new Object[1];
                args[0] = listener;
                addListener.invoke(resource, args);
            }
        }
    // for each event
    }
// if events != null
}
Also used : BeanInfo(java.beans.BeanInfo) Method(java.lang.reflect.Method) EventSetDescriptor(java.beans.EventSetDescriptor)

Example 64 with BeanInfo

use of java.beans.BeanInfo in project gate-core by GateNLP.

the class AbstractResource method getBeanInfo.

public static BeanInfo getBeanInfo(Class<? extends Resource> c) throws IntrospectionException {
    BeanInfo r = beanInfoCache.get(c);
    if (r == null) {
        r = Introspector.getBeanInfo(c, Object.class);
        beanInfoCache.put(c, r);
    }
    return r;
}
Also used : BeanInfo(java.beans.BeanInfo)

Example 65 with BeanInfo

use of java.beans.BeanInfo in project gate-core by GateNLP.

the class AbstractVisualResource method setParameterValue.

/**
 * Sets the value for a specified parameter.
 *
 * @param paramaterName the name for the parameteer
 * @param parameterValue the value the parameter will receive
 */
@Override
public void setParameterValue(String paramaterName, Object parameterValue) throws ResourceInstantiationException {
    // get the beaninfo for the resource bean, excluding data about Object
    BeanInfo resBeanInf = null;
    try {
        resBeanInf = Introspector.getBeanInfo(this.getClass(), Object.class);
    } catch (Exception e) {
        throw new ResourceInstantiationException("Couldn't get bean info for resource " + this.getClass().getName() + Strings.getNl() + "Introspector exception was: " + e);
    }
    AbstractResource.setParameterValue(this, resBeanInf, paramaterName, parameterValue);
}
Also used : BeanInfo(java.beans.BeanInfo)

Aggregations

BeanInfo (java.beans.BeanInfo)420 PropertyDescriptor (java.beans.PropertyDescriptor)328 Method (java.lang.reflect.Method)217 SimpleBeanInfo (java.beans.SimpleBeanInfo)167 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)161 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)148 IntrospectionException (java.beans.IntrospectionException)115 InvocationTargetException (java.lang.reflect.InvocationTargetException)38 HashMap (java.util.HashMap)33 Test (org.junit.jupiter.api.Test)33 ArrayList (java.util.ArrayList)30 Field (java.lang.reflect.Field)17 Map (java.util.Map)17 Test (org.junit.Test)13 EventSetDescriptor (java.beans.EventSetDescriptor)12 MethodDescriptor (java.beans.MethodDescriptor)12 List (java.util.List)11 BeanDescriptor (java.beans.BeanDescriptor)10 HashSet (java.util.HashSet)8 Introspector (java.beans.Introspector)7