use of org.terasology.reflection.metadata.ClassMetadata in project Terasology by MovingBlocks.
the class WidgetSelectionScreen method initialise.
@Override
public void initialise() {
availableWidgets = find("availableWidgets", UIDropdownScrollable.class);
// Populate the widget list.
ClassLibrary<UIWidget> metadataLibrary = getManager().getWidgetMetadataLibrary();
for (ClassMetadata metadata : metadataLibrary) {
if (!CoreScreenLayer.class.isAssignableFrom(metadata.getType())) {
widgets.put(metadata.toString(), metadata);
}
}
List<String> options = Lists.newArrayList(widgets.keySet());
Collections.sort(options);
availableWidgets.setOptions(options);
// Add the widget as a child of the node.
WidgetUtil.trySubscribe(this, "ok", button -> {
String selection = availableWidgets.getSelection();
JsonTree childNode;
if (node.getValue().getType() == JsonTreeValue.Type.ARRAY) {
ClassMetadata metadata = widgets.get(selection);
// Get the widget tree from a utility method.
childNode = NUIEditorNodeUtils.createNewWidget(selection, "newWidget", false);
// If the widget is an UILayout override, also add a "contents" array node to the tree.
if (UILayout.class.isAssignableFrom(metadata.getType())) {
childNode.addChild(new JsonTreeValue("contents", null, JsonTreeValue.Type.ARRAY));
}
} else {
childNode = new JsonTree(new JsonTreeValue(selection, null, JsonTreeValue.Type.OBJECT));
childNode.setExpanded(true);
}
node.addChild(childNode);
closeListeners.forEach(UpdateListener::onAction);
getManager().closeScreen(ASSET_URI);
});
find("ok", UIButton.class).bindEnabled(new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
return availableWidgets.getSelection() != null;
}
});
WidgetUtil.trySubscribe(this, "cancel", button -> getManager().closeScreen(ASSET_URI));
}
use of org.terasology.reflection.metadata.ClassMetadata in project Terasology by MovingBlocks.
the class TypeSerializationLibrary method getHandlerFor.
// TODO: Refactor
@SuppressWarnings("unchecked")
public TypeHandler<?> getHandlerFor(Type genericType) {
Class<?> typeClass = ReflectionUtil.getClassOfType(genericType);
if (typeClass == null) {
logger.error("Unabled to get class from type {}", genericType);
return null;
}
if (Enum.class.isAssignableFrom(typeClass)) {
return new EnumTypeHandler(typeClass);
} else if (List.class.isAssignableFrom(typeClass)) {
// For lists, createEntityRef the handler for the contained type and wrap in a list type handler
Type parameter = ReflectionUtil.getTypeParameter(genericType, 0);
if (parameter != null) {
TypeHandler<?> innerHandler = getHandlerFor(parameter);
if (innerHandler != null) {
return new ListTypeHandler<>(innerHandler);
}
}
logger.error("List field is not parametrized, or holds unsupported type");
return null;
} else if (Set.class.isAssignableFrom(typeClass)) {
// For sets:
Type parameter = ReflectionUtil.getTypeParameter(genericType, 0);
if (parameter != null) {
TypeHandler<?> innerHandler = getHandlerFor(parameter);
if (innerHandler != null) {
return new SetTypeHandler<>(innerHandler);
}
}
logger.error("Set field is not parametrized, or holds unsupported type");
return null;
} else if (Map.class.isAssignableFrom(typeClass)) {
// For Maps, createEntityRef the handler for the value type (and maybe key too?)
Type keyParameter = ReflectionUtil.getTypeParameter(genericType, 0);
Type contentsParameter = ReflectionUtil.getTypeParameter(genericType, 1);
if (keyParameter != null && contentsParameter != null && String.class == keyParameter) {
TypeHandler<?> valueHandler = getHandlerFor(contentsParameter);
if (valueHandler != null) {
return new StringMapTypeHandler<>(valueHandler);
}
}
logger.error("Map field is not parametrized, does not have a String key, or holds unsupported values");
} else if (typeHandlers.containsKey(typeClass)) {
// For known types, just use the handler
return typeHandlers.get(typeClass);
} else if (typeClass.getAnnotation(MappedContainer.class) != null && !Modifier.isAbstract(typeClass.getModifiers()) && !typeClass.isLocalClass() && !(typeClass.isMemberClass() && !Modifier.isStatic(typeClass.getModifiers()))) {
try {
ClassMetadata<?, ?> metadata = new DefaultClassMetadata<>(new SimpleUri(), typeClass, reflectFactory, copyStrategies);
MappedContainerTypeHandler<?> mappedHandler = new MappedContainerTypeHandler(typeClass, getFieldHandlerMap(metadata));
typeHandlers.put(typeClass, mappedHandler);
return mappedHandler;
} catch (NoSuchMethodException e) {
logger.error("Unable to register field of type {}: no publicly accessible default constructor", typeClass.getSimpleName());
return null;
}
} else {
logger.error("Unable to register field of type {}: not a supported type or MappedContainer", typeClass.getSimpleName());
}
return null;
}
Aggregations