Hi
I am learning vaadin CDI and java EE in netbeans following the webinar of Matti Tahvonen.
At this stage I am trying to add a login option in my test project. I have taken the following steps:
Problem: CountryTableView is not shown even I am logged in. Something is wrong in calling it …? Is this the right way to call the view originally called before login →
getUI().getNavigator().navigateTo(“country”);
- Main UI
@CDIUI
//@Theme("valo")
public class MainUI extends UI {
@Inject
private CDIViewProvider viewProvider;
@Inject
ViewMenu menu;
@Inject
User user;
VerticalLayout mainarea;
Navigator navigator;
HorizontalSplitPanel splitH;
HorizontalLayout topPanel;
VerticalSplitPanel splitV;
@Override
public void init(VaadinRequest request) {
loadInterface();
setContent(splitV);
}
private void loadInterface(){
mainarea = new VerticalLayout();
navigator = new Navigator(this, mainarea);
navigator.addProvider(viewProvider);
navigator.setErrorView(ErrorView.class);
Logger.getLogger(CDIViewProvider.class.getCanonicalName()).setLevel(Level.SEVERE);
//setContent(new VerticalLayout(menu.getBasicMenu(), mainarea));
splitH= new HorizontalSplitPanel();
splitH.setSplitPosition(15);
splitH.setFirstComponent(menu.getBasicMenu());
splitH.setSecondComponent(mainarea);
splitV= new VerticalSplitPanel();
topPanel=new HorizontalLayout();
topPanel.addComponent(new Header("Inteacc Softech Solutions"));
topPanel.addStyleName("inteaccstyle");
splitV.setFirstComponent(topPanel);
splitV.setSecondComponent(splitH);
splitV.setSplitPosition(5);
}
}
- Login view
@CDIView
public class LoginView extends VerticalLayout implements View {
................
- CountryTableView
@CDIView("country")
public class CountryTableView extends MHorizontalLayout implements View {
@Inject
CountryFacade cf;
@Inject
CountryMaddonForm form;
@Inject
User user;
MTable<Country> table = new MTable<>(Country.class);
@PostConstruct
public void initComponent() {
form.setResetHandler(this::reset);
form.setSavedHandler(this::save);
table.addMValueChangeListener(e -> {
form.setEntity(e.getValue());
});
listEntities();
addComponents(
new Header("Country listing"),
table,
form
);
}
private void listEntities() {
table.setBeans(cf.findAll());
}
public void save(Country entity) {
if (entity.getId() == null) {
cf.create(entity);
} else {
cf.edit(entity);
}
listEntities();
Notification.show("Saved!");
}
public void reset(Country entity) {
// just hide the form
form.setEntity(null);
listEntities();
}
@Override
public void enter(ViewChangeListener.ViewChangeEvent viewChangeEvent) {
if (user.getEmail() == null) getUI().getNavigator().navigateTo("");
[color=#FF0000]
getUI().getNavigator().navigateTo("country");
[/color]
}
}
- ViewMenu (the same file from example)
@SessionScoped
public class ViewMenu implements Serializable {
@Inject
BeanManager beanManager;
public Set<Bean<?>> getAvailableViews() {
Set<Bean<?>> all = beanManager.getBeans(View.class,
new AnnotationLiteral<Any>() {
});
// TODO check if accessible for current user
return all;
}
public Component getBasicMenu() {
//return new MHorizontalLayout(getAsLinkButtons(getAvailableViews())).
return new MVerticalLayout(getAsLinkButtons(getAvailableViews())).
withMargin(true);
}
private Component[] getAsLinkButtons(
Set<Bean<?>> availableViews) {
ArrayList<Button> buttons = new ArrayList();
for (Bean<?> viewBean : availableViews) {
Class<?> beanClass = viewBean.getBeanClass();
if (beanClass.getAnnotation(CDIView.class) != null) {
buttons.add(getButtonFor(beanClass));
}
}
return buttons.toArray(new Button[0]
);
}
private MButton getButtonFor(Class<?> beanClass) {
final MButton button = new MButton(getNameFor(beanClass)).withStyleName(
"link");
button.addClickListener(e -> {
CDIView cdiview = beanClass.getAnnotation(CDIView.class);
UI.getCurrent().getNavigator().navigateTo(cdiview.value());
});
return button;
}
protected String getNameFor(Class<?> viewType) {
return viewType.getSimpleName();
}
}