use of com.haulmont.cuba.gui.model.ScreenData in project cuba by cuba-platform.
the class WindowLoader method loadScreenData.
protected void loadScreenData(Window window, Element element) {
Element dataEl = element.element("data");
if (dataEl != null) {
ScreenDataXmlLoader screenDataXmlLoader = beanLocator.get(ScreenDataXmlLoader.class);
ScreenData screenData = UiControllerUtils.getScreenData(window.getFrameOwner());
screenDataXmlLoader.load(screenData, dataEl, null);
((ComponentLoaderContext) context).setScreenData(screenData);
}
}
use of com.haulmont.cuba.gui.model.ScreenData in project cuba by cuba-platform.
the class RemoveOperation method removeItems.
protected <E extends Entity> void removeItems(RemoveBuilder<E> builder, List<E> selectedItems) {
FrameOwner origin = builder.getOrigin();
ScreenData screenData = UiControllerUtils.getScreenData(origin);
CollectionContainer<E> container = getCollectionContainer(builder);
commitIfNeeded(selectedItems, container, screenData);
if (selectedItems.size() == 1) {
container.getMutableItems().remove(selectedItems.get(0));
} else {
container.getMutableItems().removeAll(selectedItems);
}
focusListComponent(builder);
}
use of com.haulmont.cuba.gui.model.ScreenData in project cuba by cuba-platform.
the class UiControllerDependencyInjector method initSubscribeListeners.
protected void initSubscribeListeners(FrameOwner frameOwner, ScreenIntrospectionData screenIntrospectionData) {
Class<? extends FrameOwner> clazz = frameOwner.getClass();
List<AnnotatedMethod<Subscribe>> eventListenerMethods = screenIntrospectionData.getSubscribeMethods();
Frame frame = UiControllerUtils.getFrame(frameOwner);
ScreenData screenData = getScreenData(frameOwner);
for (AnnotatedMethod<Subscribe> annotatedMethod : eventListenerMethods) {
Method method = annotatedMethod.getMethod();
Subscribe annotation = annotatedMethod.getAnnotation();
String target = UiDescriptorUtils.getInferredSubscribeId(annotation);
Parameter parameter = method.getParameters()[0];
Class<?> eventType = parameter.getType();
Object eventTarget = null;
if (Strings.isNullOrEmpty(target)) {
switch(annotation.target()) {
// if kept default value
case COMPONENT:
case CONTROLLER:
eventTarget = frameOwner;
break;
case FRAME:
eventTarget = frame;
break;
case PARENT_CONTROLLER:
if (frameOwner instanceof Screen) {
throw new DevelopmentException(String.format("Screen %s cannot use @Subscribe with target = PARENT_CONTROLLER", frame.getId()));
}
eventTarget = ((ScreenFragment) frameOwner).getHostController();
break;
case DATA_CONTEXT:
eventTarget = screenData.getDataContext();
break;
default:
throw new UnsupportedOperationException("Unsupported @Subscribe target " + annotation.target());
}
} else {
switch(annotation.target()) {
case CONTROLLER:
Object componentTarget = findMethodTarget(frame, target);
if (!(componentTarget instanceof Fragment)) {
throw new UnsupportedOperationException("Unsupported @Subscribe target " + annotation.target() + ". It is not a Fragment.");
}
eventTarget = ((Fragment) componentTarget).getFrameOwner();
break;
case COMPONENT:
// component event
eventTarget = findMethodTarget(frame, target);
break;
case DATA_LOADER:
if (screenData.getLoaderIds().contains(target)) {
eventTarget = screenData.getLoader(target);
}
break;
case DATA_CONTAINER:
if (screenData.getContainerIds().contains(target)) {
eventTarget = screenData.getContainer(target);
}
break;
default:
throw new UnsupportedOperationException("Unsupported @Subscribe target " + annotation.target());
}
}
if (eventTarget == null) {
if (annotation.required()) {
throw new DevelopmentException(String.format("Unable to find @Subscribe target %s in %s", target, frame.getId()));
}
log.trace("Skip @Subscribe method {} of {} : it is not required and target not found", annotatedMethod.getMethod().getName(), frameOwner.getClass());
continue;
}
Consumer listener;
MethodHandle consumerMethodFactory = reflectionInspector.getConsumerMethodFactory(clazz, annotatedMethod, eventType);
try {
listener = (Consumer) consumerMethodFactory.invoke(frameOwner);
} catch (Error e) {
throw e;
} catch (Throwable e) {
throw new RuntimeException("Unable to bind consumer handler", e);
}
MethodHandle addListenerMethod = reflectionInspector.getAddListenerMethod(eventTarget.getClass(), eventType);
if (addListenerMethod == null) {
throw new DevelopmentException(String.format("Target %s does not support event type %s", eventTarget.getClass().getName(), eventType));
}
try {
addListenerMethod.invoke(eventTarget, listener);
} catch (Error e) {
throw e;
} catch (Throwable e) {
throw new RuntimeException("Unable to add listener" + method, e);
}
}
}
use of com.haulmont.cuba.gui.model.ScreenData in project cuba by cuba-platform.
the class UiControllerDependencyInjector method getInjectedInstance.
@Nullable
protected Object getInjectedInstance(Class<?> type, String name, Class annotationClass, AnnotatedElement element, InjectionContext injectionContext) {
FrameOwner frameOwner = injectionContext.getFrameOwner();
ScreenOptions options = injectionContext.getScreenOptions();
Frame frame = UiControllerUtils.getFrame(frameOwner);
if (annotationClass == WindowParam.class) {
if (options instanceof MapScreenOptions) {
return ((MapScreenOptions) options).getParams().get(name);
}
// Injecting a parameter
return null;
} else if (ScreenFragment.class.isAssignableFrom(type)) {
// Injecting inner fragment controller
Component fragment = frame.getComponent(name);
if (fragment == null) {
return null;
}
return ((Fragment) fragment).getFrameOwner();
} else if (AbstractWindow.class.isAssignableFrom(type)) {
// Injecting inner legacy screen controller
Component fragment = frame.getComponent(name);
if (fragment == null) {
return null;
}
ScreenFragment frameOwner1 = ((Fragment) fragment).getFrameOwner();
if (frameOwner1 instanceof LegacyFragmentAdapter) {
return ((LegacyFragmentAdapter) frameOwner1).getRealScreen();
}
return frameOwner1;
} else if (Component.class.isAssignableFrom(type)) {
// / if legacy frame - inject controller
Component component = frame.getComponent(name);
if (component instanceof Fragment) {
ScreenFragment frameOwner1 = ((Fragment) component).getFrameOwner();
if (type.isAssignableFrom(frameOwner1.getClass())) {
return frameOwner1;
}
}
// for legacy screens only
if (component == null && frameOwner instanceof LegacyFrame) {
// try to find using slow iteration
component = ComponentsHelper.getComponent(frame, name);
}
// Injecting a UI component
return component;
} else if (InstanceContainer.class.isAssignableFrom(type)) {
// Injecting a container
ScreenData data = getScreenData(frameOwner);
return data.getContainer(name);
} else if (DataLoader.class.isAssignableFrom(type)) {
// Injecting a loader
ScreenData data = getScreenData(frameOwner);
return data.getLoader(name);
} else if (DataContext.class.isAssignableFrom(type)) {
// Injecting the data context
ScreenData data = getScreenData(frameOwner);
return data.getDataContext();
} else if (Datasource.class.isAssignableFrom(type)) {
// Injecting a datasource
return ((LegacyFrame) frameOwner).getDsContext().get(name);
} else if (DsContext.class.isAssignableFrom(type)) {
if (frameOwner instanceof LegacyFrame) {
// Injecting the DsContext
return ((LegacyFrame) frameOwner).getDsContext();
} else {
throw new DevelopmentException("DsContext can be injected only into LegacyFrame inheritors");
}
} else if (DataSupplier.class.isAssignableFrom(type)) {
if (frameOwner instanceof LegacyFrame) {
// Injecting the DataSupplier
return ((LegacyFrame) frameOwner).getDsContext().getDataSupplier();
} else {
throw new DevelopmentException("DataSupplier can be injected only into LegacyFrame inheritors");
}
} else if (FrameContext.class.isAssignableFrom(type)) {
// Injecting the FrameContext
return frame.getContext();
} else if (Action.class.isAssignableFrom(type)) {
// Injecting an action
return ComponentsHelper.findAction(name, frame);
} else if (Facet.class.isAssignableFrom(type)) {
// Injecting non-visual component
String[] elements = ValuePathHelper.parse(name);
if (elements.length == 1) {
return frame.getFacet(name);
}
String prefix = pathPrefix(elements);
Component component = frame.getComponent(prefix);
if (component == null) {
return null;
}
if (!(component instanceof Fragment)) {
throw new UnsupportedOperationException(String.format("Unable to inject facet with id %s and type %s. Component %s is not a fragment", name, type, prefix));
}
String facetId = elements[elements.length - 1];
return ((Fragment) component).getFacet(facetId);
} else if (ExportDisplay.class.isAssignableFrom(type)) {
// Injecting an ExportDisplay
ExportDisplay exportDisplay = beanLocator.get(ExportDisplay.NAME);
exportDisplay.setFrame(frame);
return exportDisplay;
} else if (Config.class.isAssignableFrom(type)) {
ClientConfiguration configuration = beanLocator.get(Configuration.NAME);
// noinspection unchecked
return configuration.getConfigCached((Class<? extends Config>) type);
} else if (Logger.class == type && element instanceof Field) {
// injecting logger
return LoggerFactory.getLogger(((Field) element).getDeclaringClass());
} else if (Screens.class.isAssignableFrom(type)) {
// injecting screens
return getScreenContext(frameOwner).getScreens();
} else if (Dialogs.class.isAssignableFrom(type)) {
// injecting dialogs
return getScreenContext(frameOwner).getDialogs();
} else if (Notifications.class.isAssignableFrom(type)) {
// injecting notifications
return getScreenContext(frameOwner).getNotifications();
} else if (Fragments.class.isAssignableFrom(type)) {
// injecting fragments
return getScreenContext(frameOwner).getFragments();
} else if (UrlRouting.class.isAssignableFrom(type)) {
// injecting urlRouting
return getScreenContext(frameOwner).getUrlRouting();
} else if (MessageBundle.class == type) {
return createMessageBundle(element, frameOwner, frame);
} else if (ThemeConstants.class == type) {
// Injecting a Theme
ThemeConstantsManager themeManager = beanLocator.get(ThemeConstantsManager.NAME);
return themeManager.getConstants();
} else if (WebBrowserTools.class.isAssignableFrom(type)) {
// Injecting WebBrowserTools
return getScreenContext(frameOwner).getWebBrowserTools();
} else {
Object instance;
// Try to find a Spring bean
Map<String, ?> beans = beanLocator.getAll(type);
if (!beans.isEmpty()) {
instance = beans.get(name);
// If a bean with required name found, return it. Otherwise return first found.
if (instance != null) {
return instance;
} else {
return beans.values().iterator().next();
}
}
// There are no Spring beans of required type - the last option is Companion
if (frameOwner instanceof LegacyFrame) {
instance = ((LegacyFrame) frameOwner).getCompanion();
if (instance != null && type.isAssignableFrom(instance.getClass())) {
return instance;
}
}
}
return null;
}
use of com.haulmont.cuba.gui.model.ScreenData in project cuba by cuba-platform.
the class CalendarLoader method loadDataContainer.
@SuppressWarnings("unchecked")
protected void loadDataContainer(Calendar component, Element element) {
String containerId = element.attributeValue("dataContainer");
FrameOwner frameOwner = getComponentContext().getFrame().getFrameOwner();
ScreenData screenData = UiControllerUtils.getScreenData(frameOwner);
InstanceContainer container = screenData.getContainer(containerId);
if (!(container instanceof CollectionContainer)) {
throw new GuiDevelopmentException("Not a CollectionContainer: " + containerId, context);
}
component.setEventProvider(new ContainerCalendarEventProvider<>(((CollectionContainer) container)));
}
Aggregations