de.lessvoid.nifty.NiftyEventAnnotationProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nifty Show documentation
Show all versions of nifty Show documentation
Nifty GUI is a Java Library that supports the building of interactive user interfaces for games or similar applications. It utilizes OpenGL for rendering and it can be easily integrated into many rendering systems. The configuration of the GUI is stored in xml files with little supporting Java code. In short Nifty helps you to layout stuff, display it in a cool way and interact with it :)
package de.lessvoid.nifty;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bushe.swing.event.EventService;
import org.bushe.swing.event.EventServiceLocator;
import org.bushe.swing.event.EventTopicSubscriber;
public class NiftyEventAnnotationProcessor {
private static final Logger log = Logger.getLogger(NiftyEventAnnotationProcessor.class.getName());
public static void process(final Object obj) {
processOrUnprocess(obj, true);
}
public static void unprocess(final Object obj) {
processOrUnprocess(obj, false);
}
private static void processOrUnprocess(@Nullable final Object obj, final boolean add) {
if (obj == null) {
return;
}
Class> cl = obj.getClass();
Method[] methods = cl.getMethods();
if (log.isLoggable(Level.FINE)) {
log.fine("Looking for EventBus annotations for class " + cl + ", methods:" + Arrays.toString(methods));
}
for (Method method : methods) {
NiftyEventSubscriber niftyEventSubscriber = method.getAnnotation(NiftyEventSubscriber.class);
if (niftyEventSubscriber != null) {
if (log.isLoggable(Level.FINE)) {
log.fine("Found NiftyEventSubscriber:" + niftyEventSubscriber + " on method:" + method);
}
process(niftyEventSubscriber, obj, method, add);
}
}
}
private static void process(@Nonnull final NiftyEventSubscriber annotation, final Object obj, @Nonnull final Method method, final boolean add) {
String id = annotation.id();
String pattern = annotation.pattern();
ensureNotNull(id, pattern);
ensureMethodParamCount(method.getParameterTypes());
EventService eventService = getEventService();
Class> eventClass = method.getParameterTypes()[1];
if (isSet(id)) {
idProcess(obj, method, add, id, eventClass, eventService);
} else {
patternProcess(obj, method, add, pattern, eventClass, eventService);
}
}
private static boolean isSet(@Nullable final String value) {
return value != null && value.length() > 0;
}
private static void ensureNotNull(final String id, final String pattern) {
if (!isSet(id) && !isSet(pattern)) {
throw new IllegalArgumentException("id or pattern must have a value for NiftyEventSubscriber annotation");
}
}
private static void ensureMethodParamCount(@Nullable final Class>[] params) {
if (params == null || params.length != 2 || !String.class.equals(params[0]) || params[1].isPrimitive()) {
throw new IllegalArgumentException("The subscriptionMethod must have the two parameters, the first one must be a String and the second a non-primitive (Object or derivative).");
}
}
private static void patternProcess(final Object obj, final Method method, final boolean add, @Nonnull final String topicPattern, final Class> eventClass, @Nonnull final EventService eventService) {
Pattern pattern = Pattern.compile(topicPattern);
Subscriber subscriber = new Subscriber(obj, method, eventClass);
StringBuilder sb = new StringBuilder(" [{0}] -> [{1}]");
if (add) {
sb.insert(0, "-> subscribe");
if(!eventService.subscribeStrongly(pattern, subscriber)){
sb.insert(2, " failed to");
}
log.log(Level.FINE, sb.toString(), new Object[]{pattern, subscriber});
} else {
sb.insert(0, "<- unsubscribe");
if(!eventService.unsubscribe(pattern, subscriber)){
sb.insert(2, " failed to");
}
log.log(Level.FINE, sb.toString(), new Object[]{pattern, subscriber});
}
}
private static void idProcess(final Object obj, final Method method, final boolean add, final String id, final Class> eventClass, @Nonnull final EventService eventService) {
Subscriber subscriber = new Subscriber(obj, method, eventClass);
StringBuilder sb = new StringBuilder(" [{0}] -> [{1}]");
if (add) {
sb.insert(0, "-> subscribe");
if(!eventService.subscribeStrongly(id, subscriber)){
sb.insert(2, " failed to");
}
log.log(Level.FINE, sb.toString(), new Object[]{id, subscriber});
} else {
sb.insert(0, "<- unsubscribe");
if(!eventService.unsubscribe(id, subscriber)){
sb.insert(2, " failed to");
}
log.log(Level.FINE, sb.toString(), new Object[]{id, subscriber});
}
}
private static EventService getEventService() {
return EventServiceLocator.getEventService("NiftyEventBus");
}
private static class Subscriber implements EventTopicSubscriber
© 2015 - 2025 Weber Informatics LLC | Privacy Policy