use of io.jmix.ui.component.Component in project jmix-docs by Haulmont.
the class ValuePickerScreen method onInit.
// end::inject-valuePickerClearAction[]
// tag::onInit-start[]
@Subscribe
protected void onInit(InitEvent event) {
// end::onInit-start[]
// tag::base-action-value-picker[]
ValuePicker valueField = uiComponents.create(ValuePicker.NAME);
valueField.addAction(new BaseAction("hello") {
@Override
public String getCaption() {
return null;
}
@Override
public String getDescription() {
return messageBundle.getMessage("helloDescription");
}
@Override
public String getIcon() {
return JmixIcon.HANDSHAKE_O.source();
}
@Override
public void actionPerform(Component component) {
notifications.create().withCaption("Hello!").withType(Notifications.NotificationType.TRAY).show();
}
});
valueField.addAction(new BaseAction("goodbye").withCaption(null).withDescription(messageBundle.getMessage("goodbyeDescription")).withIcon(JmixIcon.HAND_PAPER_O.source()).withHandler(e -> notifications.create().withCaption("Goodbye!").withType(Notifications.NotificationType.TRAY).show()));
vBox.add(valueField);
// end::base-action-value-picker[]
loginValuePicker.addAction(new BaseAction("showValue").withHandler(actionPerformedEvent -> {
String value = loginValuePicker.getValue();
notifications.create().withCaption(value != null ? value : "No value").show();
}).withDescription("Show Value").withIcon(JmixIcon.EYE.source()));
// tag::add-clear-action1[]
loginValuePicker.addAction(actions.create(ValueClearAction.ID));
// end::add-clear-action1[]
// tag::onInit-end[]
}
use of io.jmix.ui.component.Component in project jmix by jmix-framework.
the class DsContextImpl method get.
@Override
public Datasource get(String id) {
checkNotNullArgument(id, "Null datasource ID");
id = aliasesMap.getOrDefault(id, id);
Datasource ds = null;
if (!id.contains(".")) {
ds = datasourceMap.get(id);
if (ds == null && parent != null) {
ds = parent.get(id);
}
} else {
if (windowContext != null) {
String nestedFramePath = id.substring(0, id.indexOf("."));
Component nestedFrame = getFrameContext().getFrame().getComponent(nestedFramePath);
if (nestedFrame instanceof Frame) {
String nestedDsId = id.substring(id.indexOf(".") + 1);
FrameOwner frameOwner = ((Frame) nestedFrame).getFrameOwner();
if (frameOwner instanceof LegacyFrame) {
ds = ((LegacyFrame) frameOwner).getDsContext().get(nestedDsId);
}
}
}
}
return ds;
}
use of io.jmix.ui.component.Component in project jmix by jmix-framework.
the class DeclarativeAction method actionPerform.
@Override
public void actionPerform(Component component) {
if (StringUtils.isEmpty(methodName)) {
return;
}
FrameOwner controller = frame.getFrameOwner();
if (controller instanceof LegacyFragmentAdapter) {
controller = ((LegacyFragmentAdapter) controller).getRealScreen();
}
Method method;
try {
method = controller.getClass().getMethod(methodName, Component.class);
} catch (NoSuchMethodException e) {
try {
method = controller.getClass().getMethod(methodName);
} catch (NoSuchMethodException e1) {
throw new IllegalStateException(String.format("No suitable methods named %s for action %s", methodName, id));
}
}
try {
if (method.getParameterCount() == 1) {
method.invoke(controller, component);
} else {
method.invoke(controller);
}
} catch (Exception e) {
throw new RuntimeException("Exception on action handling", e);
}
}
use of io.jmix.ui.component.Component in project jmix by jmix-framework.
the class FieldGroupTest method getComposition.
protected Object getComposition(FieldGroup.FieldConfig fc) {
Method getCompositionMethod = MethodUtils.getAccessibleMethod(fc.getClass(), "getComponent");
Object composition;
try {
composition = getCompositionMethod.invoke(fc);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Invoke error", e);
}
return ((Component) composition).unwrap(Object.class);
}
use of io.jmix.ui.component.Component in project jmix by jmix-framework.
the class LookupFieldTest method testValueChangeListener.
@Test
public void testValueChangeListener() {
LookupField component = create(LookupField.class);
final AtomicInteger counter = new AtomicInteger(0);
// noinspection unchecked
Datasource<User> testDs = new DsBuilder().setId("testDs").setJavaClass(User.class).setView(viewRepository.getFetchPlan(User.class, FetchPlan.LOCAL)).buildDatasource();
testDs.setItem(new User());
((DatasourceImpl) testDs).valid();
assertNull(component.getValue());
final Group g = new Group();
testDs.getItem().setGroup(g);
// noinspection unchecked
CollectionDatasource<Group, UUID> groupsDs = new DsBuilder().setId("testDs").setJavaClass(Group.class).setView(viewRepository.getFetchPlan(Group.class, FetchPlan.LOCAL)).setRefreshMode(CollectionDatasource.RefreshMode.NEVER).setAllowCommit(false).buildCollectionDatasource();
groupsDs.includeItem(g);
Group g1 = new Group();
groupsDs.includeItem(g1);
Group g2 = new Group();
groupsDs.includeItem(g2);
component.setOptionsDatasource(groupsDs);
Consumer<HasValue.ValueChangeEvent> listener1 = e -> {
assertNull(e.getPrevValue());
assertEquals(g2, e.getValue());
counter.addAndGet(1);
};
Subscription subscription = component.addValueChangeListener(listener1);
component.setValue(g2);
subscription.remove();
assertEquals(1, counter.get());
Consumer<HasValue.ValueChangeEvent> listener2 = e -> {
assertEquals(g2, e.getPrevValue());
assertEquals(g, e.getValue());
counter.addAndGet(1);
};
subscription = component.addValueChangeListener(listener2);
component.setDatasource(testDs, "group");
assertEquals(g, component.getValue());
assertEquals(2, counter.get());
subscription.remove();
component.setValue(g1);
assertEquals(g1, testDs.getItem().getGroup());
assertEquals(2, counter.get());
Consumer<HasValue.ValueChangeEvent> listener3 = e -> {
assertEquals(g1, e.getPrevValue());
assertEquals(g2, e.getValue());
counter.addAndGet(1);
};
component.addValueChangeListener(listener3);
testDs.getItem().setGroup(g2);
assertEquals(g2, component.getValue());
assertEquals(3, counter.get());
}
Aggregations