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);
}
}
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);
}
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
}
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;
}
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);
}
Aggregations