use of io.jmix.ui.model.ScreenData in project jmix by jmix-framework.
the class UiDataFilterSupport method getFragmentFilterConfigurationModel.
protected FilterConfiguration getFragmentFilterConfigurationModel(Filter.Configuration configuration, ScreenFragment configurationFragment) {
Filter filter = configuration.getOwner();
FilterConfiguration configurationModel;
if (configurationFragment instanceof UiDataFilterConfigurationModelFragment) {
ScreenData screenData = UiControllerUtils.getScreenData(configurationFragment.getHostController());
InstanceContainer<FilterConfiguration> configurationDc = screenData.getContainer(CONFIGURATION_CONTAINER_ID);
configurationModel = configurationDc.getItem();
if (filter.getId() != null && filter.getFrame() != null && ((UiDataFilterConfigurationModelFragment) configurationFragment).getDefaultForMeFieldVisible()) {
ScreenSettingsFacet settingsFacet = UiControllerUtils.getFacet(filter.getFrame(), ScreenSettingsFacet.class);
if (settingsFacet != null) {
saveFilterSettings(settingsFacet, filter.getId(), configurationModel);
}
}
} else {
configurationModel = loadFilterConfigurationModel(filter, configuration.getId());
}
if (configurationModel == null) {
configurationModel = createFilterConfigurationModel(configuration);
}
return configurationModel;
}
use of io.jmix.ui.model.ScreenData in project jmix by jmix-framework.
the class DataLoadCoordinatorImpl method configureAutomatically.
@Override
public void configureAutomatically() {
FrameOwner frameOwner = getFrameOwner();
ScreenData screenData = UiControllerUtils.getScreenData(frameOwner);
getUnconfiguredLoaders(screenData).forEach(loader -> configureAutomatically(loader, frameOwner));
}
use of io.jmix.ui.model.ScreenData in project jmix by jmix-framework.
the class AbstractSingleFilterComponentLoader method loadDataLoader.
protected void loadDataLoader(C resultComponent, Element element) {
String dataLoaderId = element.attributeValue("dataLoader");
if (StringUtils.isNotBlank(dataLoaderId)) {
FrameOwner frameOwner = getComponentContext().getFrame().getFrameOwner();
ScreenData screenData = UiControllerUtils.getScreenData(frameOwner);
DataLoader dataLoader = screenData.getLoader(dataLoaderId);
resultComponent.setDataLoader(dataLoader);
}
}
use of io.jmix.ui.model.ScreenData in project jmix by jmix-framework.
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;
// in Java 17+
if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_16) || getClass().getClassLoader() == frameOwner.getClass().getClassLoader()) {
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);
}
} else {
listener = event -> {
try {
annotatedMethod.getMethodHandle().invoke(frameOwner, event);
} catch (Throwable e) {
throw new RuntimeException("Error subscribe listener method invocation", 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 io.jmix.ui.model.ScreenData in project jmix by jmix-framework.
the class UiControllerDependencyInjector method getInjectedInstance.
@Nullable
protected Object getInjectedInstance(Class<?> type, String name, InjectElement injectElement, FrameOwner frameOwner, ScreenOptions options) {
AnnotatedElement element = injectElement.getElement();
Class annotationClass = injectElement.getAnnotationClass();
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 (Component.class.isAssignableFrom(type)) {
// / if legacy frame - inject controller
Component component = frame.getComponent(name);
if (component instanceof Fragment) {
ScreenFragment fragmentFrameOwner = ((Fragment) component).getFrameOwner();
if (type.isAssignableFrom(fragmentFrameOwner.getClass())) {
return fragmentFrameOwner;
}
}
// 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 (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 (Downloader.class.isAssignableFrom(type)) {
// Injecting a Downloader
return applicationContext.getBean(Downloader.class);
} else if (MessageBundle.class == type) {
return createMessageBundle(element, frameOwner, frame);
} else if (ThemeConstants.class == type) {
// Injecting a Theme
ThemeConstantsManager themeManager = applicationContext.getBean(ThemeConstantsManager.class);
return themeManager.getConstants();
} else if (BeanFactory.class.isAssignableFrom(type)) {
return applicationContext;
} else if (ObjectProvider.class.isAssignableFrom(type)) {
if (!(element instanceof Field && ((Field) element).getGenericType() instanceof ParameterizedType) && !(element instanceof Method && ((Method) element).getGenericParameterTypes().length > 0 && ((Method) element).getGenericParameterTypes()[0] instanceof ParameterizedType)) {
throw new UnsupportedOperationException("Unable to inject ObjectProvider without generic parameter");
}
Type genericType;
if (element instanceof Field) {
genericType = ((ParameterizedType) ((Field) element).getGenericType()).getActualTypeArguments()[0];
} else {
genericType = ((ParameterizedType) ((Method) element).getGenericParameterTypes()[0]).getActualTypeArguments()[0];
}
if (genericType instanceof ParameterizedType) {
genericType = ((ParameterizedType) genericType).getRawType();
}
return applicationContext.getBeanProvider((Class<?>) genericType);
} else if (ActionsAwareDialogFacet.DialogAction.class.isAssignableFrom(type)) {
// facet's action
Facet facet;
String actionId;
String[] path = ValuePathHelper.parse(name);
if (path.length == 2) {
facet = frame.getFacet(path[0]);
actionId = path[1];
} else {
String prefix = ValuePathHelper.pathPrefix(path, 2);
Component component = frame.getComponent(prefix);
if (component == null) {
return null;
}
if (!(component instanceof Fragment)) {
throw new UnsupportedOperationException(String.format("Unable to inject dialog action with id '%s'. Component '%s' is not a fragment", name, prefix));
}
actionId = path[path.length - 1];
facet = ((Fragment) component).getFacet(path[path.length - 2]);
}
if (!(facet instanceof ActionsAwareDialogFacet)) {
return null;
}
// noinspection unchecked
Collection<ActionsAwareDialogFacet.DialogAction<Facet>> actions = ((ActionsAwareDialogFacet<Facet>) facet).getActions();
if (CollectionUtils.isNotEmpty(actions)) {
return actions.stream().filter(action -> action.getId().equals(actionId)).findFirst().orElse(null);
}
} else {
Object instance;
// Try to find a Spring bean
Map<String, ?> beans = applicationContext.getBeansOfType(type);
if (!beans.isEmpty()) {
instance = beans.get(name);
// If a bean with required name found, return it
if (instance != null) {
return instance;
} else {
// Otherwise get a bean from the context again to respect @Primary annotation
return applicationContext.getBean(type);
}
}
}
return null;
}
Aggregations