diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/AddPersonCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/AddPersonCommand.java new file mode 100644 index 0000000..c476d3e --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/AddPersonCommand.java @@ -0,0 +1,47 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.function.Consumer; + +import com.vaadin.server.Resource; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; + +public class AddPersonCommand implements ClubCommand { + + private final Consumer createdPersonConsumer; + + private final GroupDef defaultGroup; + + public AddPersonCommand(Consumer createdPersonConsumer, GroupDef defaultGroup) { + super(); + this.createdPersonConsumer = createdPersonConsumer; + this.defaultGroup = defaultGroup; + } + + @Override + public String getLabel() { + return "Person Hinzufügen"; + } + + @Override + public Resource getIcon() { + return null; + } + + @Override + public void execute() { + Person person = new Person(); + + person.setGroups(new HashSet<>()); + person.setAdresses(new ArrayList<>()); + person.setEvents(new HashSet<>()); + person.setRelatives1(new ArrayList<>()); + person.add(defaultGroup); + + createdPersonConsumer.accept(person); + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedinEditPersonViewMenuitemState.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedinEditPersonViewMenuitemState.java new file mode 100644 index 0000000..1a77c25 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedinEditPersonViewMenuitemState.java @@ -0,0 +1,50 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu; + +import java.time.ZonedDateTime; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Supplier; + +import org.springframework.context.ApplicationContext; + +import com.vaadin.ui.MenuBar.MenuItem; +import com.vaadin.ui.UI; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.GroupDao; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.AddPersonCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.PersonEditView; +import net.sf.jasperreports.engine.JasperPrint; + +/** + * Status für authentifizierte User im {@link PersonEditView} + * @author markus + * + */ +class LoggedinEditPersonViewMenuitemState extends LoggedinMenuitemState { + + private final Consumer newPersonConsumer; + + private GroupDao groupDao; + + public LoggedinEditPersonViewMenuitemState(ApplicationContext context, UI ui, Supplier startProvider, + Supplier endProvider, BiConsumer printConsumer, + Consumer newPersonConsumer) { + super(context, ui, startProvider, endProvider, printConsumer); + this.newPersonConsumer = newPersonConsumer; + groupDao = context.getBean(GroupDao.class); + } + + @Override + public void applyMenuStates(ClubhelperMenuBar menuBar) { + super.applyMenuStates(menuBar); + + MenuItem editMenu = menuBar.getEditMenuItem(); + + GroupDef defaultGroup = groupDao.get(1); + editMenu.addSeparator(); + AddPersonCommand addPersonCommand = new AddPersonCommand(newPersonConsumer, defaultGroup); + editMenu.addItem(addPersonCommand.getLabel(), new CommandWrapper(addPersonCommand)); + } +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedinMenuitemState.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedinMenuitemState.java index 46782ab..5af631d 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedinMenuitemState.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedinMenuitemState.java @@ -30,14 +30,16 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.LogoutCommand; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.SwitchViewCommand; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.ConfirmDialog; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperViews; import net.sf.jasperreports.engine.JasperPrint; +/** + * Status für authentifizierte User mit Menuitems für jedes View + * @author markus + * + */ class LoggedinMenuitemState extends LoggedOffState { - private ClubNavigator navigator2; - private SecurityVerifier securityVerifier; private ClubhelperNavigation navigator; @@ -90,7 +92,7 @@ class LoggedinMenuitemState extends LoggedOffState { @Override protected ClubCommand loginOutCommand() { - return new LogoutCommand(navigator2, securityVerifier); + return new LogoutCommand(navigator.getNavigator(), securityVerifier); } private void prepareViewMenu(ClubhelperMenuBar menuBar) { @@ -123,6 +125,7 @@ class LoggedinMenuitemState extends LoggedOffState { CommandWrapper deleeteEvent = new CommandWrapper(new DeleteEventCommand(this::deleteEvent)); deleteMenuItem = editMenu.addItem(deleeteEvent.getLabel(), deleeteEvent); + } protected void setSelectedMenuItem(ClubhelperViews view) { diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/MenuItemStateFactory.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/MenuItemStateFactory.java index 087c6e3..51b2629 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/MenuItemStateFactory.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/MenuItemStateFactory.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.time.ZonedDateTime; +import java.util.function.Consumer; import java.util.function.Supplier; import org.slf4j.Logger; @@ -21,6 +22,7 @@ import com.vaadin.ui.BrowserFrame; import com.vaadin.ui.UI; import com.vaadin.ui.Window; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityVerifier; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperViews; @@ -47,12 +49,16 @@ public class MenuItemStateFactory implements ApplicationContextAware { private Supplier endDateSupplier; + private Consumer newPersonConsumer; + public MenuItemState currentState() { MenuItemState state; View currentView = clubhelperNavigation.getNavigator().getCurrentView(); ClubhelperViews current = ClubhelperViews.byView(currentView); if (ClubhelperViews.PersonEditView == current) { - state = new LoggedinMenuitemState(context, ui, startDateSupplier, endDateSupplier, this::showPrint); + state = new LoggedinEditPersonViewMenuitemState(context, ui, startDateSupplier, endDateSupplier, + this::showPrint, + newPersonConsumer); } else if (securityGroupVerifier.isLoggedin()) { state = new LoggedinMenuitemState(context, ui, startDateSupplier, endDateSupplier, this::showPrint); @@ -113,4 +119,8 @@ public class MenuItemStateFactory implements ApplicationContextAware { this.ui = ui; } + public void setNewPersonConsumer(Consumer newPersonConsumer) { + this.newPersonConsumer = newPersonConsumer; + } + } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperNavigation.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperNavigation.java index 2a8ce17..f65dfe5 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperNavigation.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperNavigation.java @@ -71,12 +71,19 @@ public class ClubhelperNavigation implements ApplicationContextAware { ViewFactory factory = new ViewFactory(page); mainView = factory.createMain(); + personEdit = factory.createPersonEdit(); + + MenuItemStateFactory menuItemFactory = context.getBean(MenuItemStateFactory.class); + setupMenuItemStateFactory(menuItemFactory); navi = new ClubNavigator().init(mainUI); + ClubhelperMenuBar menuBar = new ClubhelperMenuBar(menuItemFactory.currentState()); + personEdit.setMenuBar(menuBar); + personEdit.setMenuStateFactory(menuItemFactory); + navi.addView("", mainView); navi.addView(ClubhelperViews.MainView.name(), mainView); navi.addView(ClubhelperViews.LoginUI.name(), new LoginUI(personBusiness, securityGroupVerifier)); - personEdit = factory.createPersonEdit(); navi.addView(ClubhelperViews.PersonEditView.name(), personEdit); navi.addView(ClubhelperViews.EventDetails.name(), new EventDetails(context)); @@ -91,6 +98,15 @@ public class ClubhelperNavigation implements ApplicationContextAware { }); } + private void setupMenuItemStateFactory(MenuItemStateFactory menuItemFactory) { + + menuItemFactory.setStartDateSupplier(mainView.startDateSupplier()); + menuItemFactory.setEndDateSupplier(mainView.endDateSupplier()); + + menuItemFactory.setNewPersonConsumer(personEdit::setNewPerson); + + } + public ClubNavigator getNavigator() { return navi; } @@ -114,12 +130,9 @@ public class ClubhelperNavigation implements ApplicationContextAware { } public PersonEditView createPersonEdit() { - MenuItemStateFactory menuItemFactory = context.getBean(MenuItemStateFactory.class); - menuItemFactory.setStartDateSupplier(mainView.startDateSupplier()); - menuItemFactory.setEndDateSupplier(mainView.endDateSupplier()); - ClubhelperMenuBar menuBar = new ClubhelperMenuBar(menuItemFactory.currentState()); - return new PersonEditView(groupDao, personBusiness, - menuBar, menuItemFactory, (page.getBrowserWindowWidth() >= WIDTH_LIMIT_FOR_MOBILE)); + PersonEditView personEditView = new PersonEditView(groupDao, personBusiness, + (page.getBrowserWindowWidth() >= WIDTH_LIMIT_FOR_MOBILE)); + return personEditView; } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/PersonEditView.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/PersonEditView.java index 2b88c7f..802910a 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/PersonEditView.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/PersonEditView.java @@ -1,14 +1,11 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation; -import java.util.ArrayList; -import java.util.HashSet; import java.util.Optional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.vaadin.event.selection.SelectionEvent; -import com.vaadin.navigator.Navigator; import com.vaadin.navigator.View; import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent; import com.vaadin.shared.ui.ContentMode; @@ -38,17 +35,11 @@ public class PersonEditView extends VerticalLayout implements View { private PersonEditDetails personDetails; - private Navigator navigator; - private MenuItemStateFactory menuStateFactory; - public PersonEditView(GroupDao groupDao, PersonBusiness personDao, ClubhelperMenuBar menuBar, - MenuItemStateFactory menuStateFactory, + public PersonEditView(GroupDao groupDao, PersonBusiness personDao, boolean horizontalLayout) { setMargin(true); - this.menuBar = menuBar; - this.menuStateFactory = menuStateFactory; - addComponent(menuBar); personGrid = new PersonGrid(groupDao, personDao); personGrid.setSizeFull(); personGrid.onPersonEdit(); @@ -65,10 +56,6 @@ public class PersonEditView extends VerticalLayout implements View { else { addComponent(createVerticalLayout()); } - Button addPerson = new Button("Hinzufügen"); - addPerson.addClickListener(ev -> addPerson()); - - addComponent(addPerson); } public HorizontalLayout createHorizontalLayout() { @@ -87,13 +74,8 @@ public class PersonEditView extends VerticalLayout implements View { return layout; } - private void addPerson() { - Person person = new Person(); - person.setGroups(new HashSet<>()); - person.setAdresses(new ArrayList<>()); - person.setEvents(new HashSet<>()); - person.setRelatives1(new ArrayList<>()); - personDetails.setBean(person); + public void setNewPerson(Person p) { + personDetails.setBean(p); personGrid.deselectAll(); } @@ -137,9 +119,16 @@ public class PersonEditView extends VerticalLayout implements View { @Override public void enter(ViewChangeEvent event) { - this.navigator = event.getNavigator(); menuBar.applyState(menuStateFactory.currentState()); LOG.debug("opened {}", getClass().getName()); } + public void setMenuBar(ClubhelperMenuBar menuBar) { + this.menuBar = menuBar; + addComponentAsFirst(menuBar); + } + + public void setMenuStateFactory(MenuItemStateFactory menuStateFactory) { + this.menuStateFactory = menuStateFactory; + } }