From 7daf22a7e3256d7ff3d07a21e965208924d2d8ac Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Mon, 12 Aug 2019 01:04:09 +0200 Subject: [PATCH] Switch to menu bar actions --- .../vaadinclubhelper/ui/MainUi.java | 6 + .../ui/commands/AbstractExportAction.java | 42 +---- .../commands/ExportCalendarMonthCommand.java | 23 ++- .../commands/ExportCalendarYearCommand.java | 22 ++- .../ui/commands/LoginCommand.java | 1 - .../ui/commands/OpenPersonEditorCommand.java | 31 ---- .../ui/commands/SwitchViewCommand.java | 43 +++++ .../ui/components/CalendarComponent.java | 19 -- .../ui/components/ClubEventProvider.java | 25 +++ .../ui/components/EventGrid.java | 5 - .../ui/components/menu/ClubhelperMenuBar.java | 1 + .../ui/components/menu/CommandWrapper.java | 38 ++++ .../ui/components/menu/LoggedOffState.java | 43 +++-- .../menu/LoggedinMenuitemState.java | 87 ++++++++- .../components/menu/MenuItemStateFactory.java | 117 ++++++++++++ .../ui/navigation/ClubhelperNavigation.java | 38 +++- .../ui/navigation/ClubhelperViews.java | 15 +- .../ui/navigation/DesktopHeadView.java | 16 +- .../ui/navigation/HeadView.java | 166 +----------------- .../ui/navigation/MainView.java | 77 +++++++- .../ui/navigation/MainViewDesktop.java | 40 +++-- .../ui/navigation/MainViewMobile.java | 20 ++- .../ui/navigation/PersonEditView.java | 21 ++- .../vaadinclubhelper/dao/PersonDaoTest.java | 1 - .../jasper/YearlyCalendarCreatorTest.java | 2 + .../ui/navigation/LoginUITest.java | 1 - 26 files changed, 580 insertions(+), 320 deletions(-) delete mode 100644 src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/OpenPersonEditorCommand.java create mode 100644 src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/SwitchViewCommand.java create mode 100644 src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/ClubEventProvider.java create mode 100644 src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/CommandWrapper.java create mode 100644 src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/MenuItemStateFactory.java diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java index 08c93d0..8a8c93e 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java @@ -20,6 +20,7 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.GroupDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PersonDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PflichtenDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityVerifier; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu.MenuItemStateFactory; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperViews; @@ -30,6 +31,7 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperViews public class MainUi extends UI { private static final long serialVersionUID = 7581634188909841919L; + private static final Logger LOGGER = LoggerFactory.getLogger(MainUi.class); @Autowired @@ -53,6 +55,9 @@ public class MainUi extends UI { @Autowired ClubhelperNavigation clubhelperNavigation; + @Autowired + MenuItemStateFactory menuStateFactory; + @Override protected void init(VaadinRequest request) { @@ -67,6 +72,7 @@ public class MainUi extends UI { page.setTitle("Vereinshelfer"); clubhelperNavigation.configure(this); + menuStateFactory.configure(getUI()); clubhelperNavigation.navigateTo(ClubhelperViews.MainView.name()); } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/AbstractExportAction.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/AbstractExportAction.java index 37ab913..74944f3 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/AbstractExportAction.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/AbstractExportAction.java @@ -2,8 +2,6 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; import java.time.LocalDate; import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -17,7 +15,7 @@ import com.vaadin.ui.Notification; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; import de.kreth.vaadin.clubhelper.vaadinclubhelper.jasper.CalendarCreator; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent.ClubEventProvider; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.ClubEventProvider; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperPrint; @@ -25,8 +23,6 @@ public abstract class AbstractExportAction implements ClubCommand { protected final transient Logger log = LoggerFactory.getLogger(getClass()); - private transient DateTimeFormatter dfMonth = DateTimeFormatter.ofPattern("MMMM uuuu"); - private final Supplier startTime; private final Supplier endTime; @@ -49,20 +45,10 @@ public abstract class AbstractExportAction implements ClubCommand { @Override public void execute() { - boolean monthOnly = getMonthOnly(); List items; - ZonedDateTime start; - ZonedDateTime end; - if (monthOnly) { - start = startTime.get(); - end = endTime.get(); - items = dataProvider.getItems(start, end); - } - else { - start = startTime.get().withDayOfYear(1); - end = start.withMonth(12).withDayOfMonth(31); - items = dataProvider.getItems(start, end); - } + ZonedDateTime start = startTime.get(); + ZonedDateTime end = endTime.get(); + items = dataProvider.getItems(start, end); Map values = new HashMap<>(); List holidays = CalendarCreator.filterHolidays(items); @@ -94,22 +80,10 @@ public abstract class AbstractExportAction implements ClubCommand { }); } - String calendarMonth; - if (monthOnly) { - calendarMonth = dfMonth.format(start); - } - else { - calendarMonth = "Jahr " + start.getYear(); - } + String calendarMonth = getTitle(); try { - JasperPrint print; - if (monthOnly) { - print = CalendarCreator.createCalendar(new Date(start.toInstant().toEpochMilli()), values, holidays); - } - else { - print = CalendarCreator.createYearCalendar(start.getYear(), values, holidays); - } + JasperPrint print = createPrint(values, holidays); log.trace("Created Jasper print for {}", calendarMonth); printConsumer.accept(calendarMonth, print); @@ -121,6 +95,8 @@ public abstract class AbstractExportAction implements ClubCommand { } } - protected abstract boolean getMonthOnly(); + protected abstract String getTitle(); + protected abstract JasperPrint createPrint(Map values, List holidays) + throws JRException; } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarMonthCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarMonthCommand.java index 2b9f5bf..be5dc36 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarMonthCommand.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarMonthCommand.java @@ -1,19 +1,31 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; +import java.time.LocalDate; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.List; +import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Supplier; import com.vaadin.server.Resource; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent.ClubEventProvider; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.jasper.CalendarCreator; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.ClubEventProvider; +import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperPrint; public class ExportCalendarMonthCommand extends AbstractExportAction { + private transient DateTimeFormatter dfMonth = DateTimeFormatter.ofPattern("MMMM uuuu"); + + private ZonedDateTime start; + public ExportCalendarMonthCommand(Supplier startTime, Supplier endTime, ClubEventProvider dataProvider, BiConsumer printConsumer) { super(startTime, endTime, dataProvider, printConsumer); + this.start = startTime.get(); } @Override @@ -27,8 +39,13 @@ public class ExportCalendarMonthCommand extends AbstractExportAction { } @Override - protected boolean getMonthOnly() { - return true; + protected String getTitle() { + return dfMonth.format(start); } + @Override + protected JasperPrint createPrint(Map values, List holidays) + throws JRException { + return CalendarCreator.createCalendar(new Date(start.toInstant().toEpochMilli()), values, holidays); + } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarYearCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarYearCommand.java index 5484336..09d64cd 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarYearCommand.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/ExportCalendarYearCommand.java @@ -1,19 +1,28 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; +import java.time.LocalDate; import java.time.ZonedDateTime; +import java.util.List; +import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Supplier; import com.vaadin.server.Resource; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent.ClubEventProvider; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.jasper.CalendarCreator; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.ClubEventProvider; +import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperPrint; public class ExportCalendarYearCommand extends AbstractExportAction { + private ZonedDateTime start; + public ExportCalendarYearCommand(Supplier startTime, Supplier endTime, ClubEventProvider dataProvider, BiConsumer printConsumer) { - super(startTime, endTime, dataProvider, printConsumer); + super(() -> startTime.get().withDayOfYear(1), () -> startTime.get().withMonth(12).withDayOfMonth(31), + dataProvider, printConsumer); + this.start = startTime.get().withDayOfYear(1); } @Override @@ -27,8 +36,13 @@ public class ExportCalendarYearCommand extends AbstractExportAction { } @Override - protected boolean getMonthOnly() { - return false; + protected String getTitle() { + return "Jahr " + start.getYear(); } + @Override + protected JasperPrint createPrint(Map values, List holidays) + throws JRException { + return CalendarCreator.createYearCalendar(start.getYear(), values, holidays); + } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/LoginCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/LoginCommand.java index 401c6a9..7b58179 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/LoginCommand.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/LoginCommand.java @@ -17,7 +17,6 @@ public class LoginCommand implements ClubCommand { @Override public void execute() { - navigator.navigateTo(ClubhelperViews.LoginUI.name()); } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/OpenPersonEditorCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/OpenPersonEditorCommand.java deleted file mode 100644 index bc5cf0c..0000000 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/OpenPersonEditorCommand.java +++ /dev/null @@ -1,31 +0,0 @@ -package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; - -import com.vaadin.server.Resource; - -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperViews; - -public class OpenPersonEditorCommand implements ClubCommand { - - private ClubNavigator navigator; - - public OpenPersonEditorCommand(ClubNavigator navigator2) { - this.navigator = navigator2; - } - - @Override - public String getLabel() { - return "Personen verwalten"; - } - - @Override - public Resource getIcon() { - return null; - } - - @Override - public void execute() { - navigator.navigateTo(ClubhelperViews.PersonEditView.name()); - } - -} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/SwitchViewCommand.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/SwitchViewCommand.java new file mode 100644 index 0000000..e24e637 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/commands/SwitchViewCommand.java @@ -0,0 +1,43 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands; + +import org.springframework.context.ApplicationContext; + +import com.vaadin.server.Resource; + +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; + +public class SwitchViewCommand implements ClubCommand { + + private ClubNavigator navigator; + + private ClubhelperViews target; + + private String label; + + private Resource icon; + + public SwitchViewCommand(ApplicationContext context, String label, Resource icon, ClubhelperViews target) { + this.navigator = context.getBean(ClubhelperNavigation.class).getNavigator(); + this.label = label; + this.target = target; + this.icon = icon; + } + + @Override + public String getLabel() { + return label; + } + + @Override + public Resource getIcon() { + return icon; + } + + @Override + public void execute() { + navigator.navigateTo(target); + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java index fb112d5..5554ad7 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java @@ -11,7 +11,6 @@ import java.util.function.Consumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.vaadin.addon.calendar.Calendar; -import org.vaadin.addon.calendar.item.BasicItemProvider; import org.vaadin.addon.calendar.ui.CalendarComponentEvents.BackwardEvent; import org.vaadin.addon.calendar.ui.CalendarComponentEvents.ForwardEvent; import org.vaadin.addon.calendar.ui.CalendarComponentEvents.ItemClickHandler; @@ -83,24 +82,6 @@ public class CalendarComponent extends CustomComponent implements CalendarView { calendar.markAsDirty(); } - /** - * {@link ClubEvent} provider for vaadin calendar addon. - * @author markus - * - */ - public static class ClubEventProvider extends BasicItemProvider { - - private static final long serialVersionUID = -5415397258827236704L; - - @Override - public void setItems(Collection items) { - super.itemList.clear(); - super.setItems(items); - fireItemSetChanged(); - } - - } - @Override public ZonedDateTime getStartDate() { return calendar.getStartDate(); diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/ClubEventProvider.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/ClubEventProvider.java new file mode 100644 index 0000000..419e1e8 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/ClubEventProvider.java @@ -0,0 +1,25 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; + +import java.util.Collection; + +import org.springframework.stereotype.Component; +import org.vaadin.addon.calendar.item.BasicItemProvider; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; + +/** + * {@link ClubEvent} provider for vaadin calendar addon. + * @author markus + * + */ +@Component +public class ClubEventProvider extends BasicItemProvider { + + @Override + public void setItems(Collection items) { + super.itemList.clear(); + super.setItems(items); + fireItemSetChanged(); + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventGrid.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventGrid.java index f835169..b3deb7f 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventGrid.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventGrid.java @@ -15,7 +15,6 @@ import com.vaadin.server.SerializablePredicate; import com.vaadin.ui.Grid; import com.vaadin.ui.Label; import com.vaadin.ui.renderers.AbstractRenderer; -import com.vaadin.ui.renderers.Renderer; import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.EventBusiness; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; @@ -78,10 +77,6 @@ public class EventGrid extends Grid { } } - private Renderer> createRenderer() { - return new PersonSetRenderer(); - } - private static final Set set = new HashSet<>(); class PersonSetRenderer extends AbstractRenderer> { diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/ClubhelperMenuBar.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/ClubhelperMenuBar.java index 178c109..3d4104a 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/ClubhelperMenuBar.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/ClubhelperMenuBar.java @@ -16,6 +16,7 @@ public class ClubhelperMenuBar extends MenuBar { private final MenuItem settingsItem; public ClubhelperMenuBar(MenuItemState initialState) { + setWidth("100%"); fileMenuItem = addItem("Datei"); editMenuItem = addItem("Bearbeiten"); viewMenuItem = addItem("Ansicht"); diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/CommandWrapper.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/CommandWrapper.java new file mode 100644 index 0000000..641583c --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/CommandWrapper.java @@ -0,0 +1,38 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu; + +import com.vaadin.server.Resource; +import com.vaadin.ui.MenuBar.Command; +import com.vaadin.ui.MenuBar.MenuItem; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ClubCommand; + +public class CommandWrapper implements Command, ClubCommand { + + private final ClubCommand command; + + public CommandWrapper(ClubCommand command) { + super(); + this.command = command; + } + + @Override + public void menuSelected(MenuItem selectedItem) { + execute(); + } + + @Override + public String getLabel() { + return command.getLabel(); + } + + @Override + public Resource getIcon() { + return command.getIcon(); + } + + @Override + public void execute() { + command.execute(); + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedOffState.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedOffState.java index 97a585b..355a7df 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedOffState.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/LoggedOffState.java @@ -1,26 +1,46 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu; +import java.time.ZonedDateTime; +import java.util.function.BiConsumer; +import java.util.function.Supplier; + import org.springframework.context.ApplicationContext; -import com.vaadin.ui.MenuBar.Command; import com.vaadin.ui.MenuBar.MenuItem; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ClubCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCalendarMonthCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCalendarYearCommand; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCsvCommand; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.LoginCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.ClubEventProvider; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator; +import net.sf.jasperreports.engine.JasperPrint; -public class LoggedOffState implements MenuItemState { +class LoggedOffState implements MenuItemState { private final ClubNavigator navigator; - private ApplicationContext context; + private final ApplicationContext context; + + private final ClubEventProvider dataProvider; + + private final Supplier startProvider; + + private final Supplier endProvider; - public LoggedOffState(ApplicationContext context) { + private BiConsumer printConsumer; + + public LoggedOffState(ApplicationContext context, Supplier startProvider, + Supplier endProvider, BiConsumer printConsumer) { super(); this.navigator = context.getBean(ClubhelperNavigation.class).getNavigator(); this.context = context; + this.dataProvider = context.getBean(ClubEventProvider.class); + this.startProvider = startProvider; + this.endProvider = endProvider; + this.printConsumer = printConsumer; } @Override @@ -32,15 +52,16 @@ public class LoggedOffState implements MenuItemState { MenuItem fileMenu = menuBar.getFileMenuItem(); fileMenu.setVisible(true); LoginCommand loginCommand = new LoginCommand(navigator); - fileMenu.addItem(loginCommand.getLabel(), new Command() { - - @Override - public void menuSelected(MenuItem selectedItem) { - loginCommand.execute(); - } - }); + fileMenu.addItem(loginCommand.getLabel(), new CommandWrapper(loginCommand)); MenuItem editMenu = menuBar.getEditMenuItem(); editMenu.setVisible(true); + + CommandWrapper exportCalendarMonthCommand = new CommandWrapper( + new ExportCalendarMonthCommand(startProvider, endProvider, dataProvider, printConsumer)); + editMenu.addItem(exportCalendarMonthCommand.getLabel(), exportCalendarMonthCommand); + ClubCommand exportCalendarYearCommand = new ExportCalendarYearCommand(startProvider, endProvider, dataProvider, + printConsumer); + editMenu.addItem(exportCalendarYearCommand.getLabel(), new CommandWrapper(exportCalendarYearCommand)); ClubCommand exportCsvCommand = new ExportCsvCommand(menuBar, context); editMenu.addItem(exportCsvCommand.getLabel(), ev -> exportCsvCommand.execute()); 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 d74063e..d16dbf1 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 @@ -1,9 +1,94 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu; -public class LoggedinMenuitemState implements MenuItemState { +import java.time.ZonedDateTime; +import java.util.function.BiConsumer; +import java.util.function.Supplier; + +import org.springframework.context.ApplicationContext; + +import com.vaadin.icons.VaadinIcons; +import com.vaadin.navigator.View; +import com.vaadin.ui.MenuBar.MenuItem; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityVerifier; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ClubCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCalendarMonthCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCalendarYearCommand; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCsvCommand; +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.ClubEventProvider; +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; + +class LoggedinMenuitemState implements MenuItemState { + + private ClubNavigator navigator2; + + private SecurityVerifier securityVerifier; + + private ClubhelperNavigation navigator; + + private ApplicationContext context; + + private ClubEventProvider dataProvider; + + private Supplier startProvider; + + private Supplier endProvider; + + private BiConsumer printConsumer; + + public LoggedinMenuitemState(ApplicationContext context, Supplier startProvider, + Supplier endProvider, BiConsumer printConsumer) { + super(); + this.navigator = context.getBean(ClubhelperNavigation.class); + this.context = context; + this.dataProvider = context.getBean(ClubEventProvider.class); + this.startProvider = startProvider; + this.endProvider = endProvider; + this.printConsumer = printConsumer; + } @Override public void applyMenuStates(ClubhelperMenuBar menuBar) { + + MenuItem fileMenuItem = menuBar.getFileMenuItem(); + fileMenuItem.setVisible(true); + CommandWrapper logout = new CommandWrapper(new LogoutCommand(navigator2, securityVerifier)); + fileMenuItem.addItem(logout.getLabel(), logout); + + MenuItem editMenu = menuBar.getEditMenuItem(); + + CommandWrapper exportCalendarMonthCommand = new CommandWrapper( + new ExportCalendarMonthCommand(startProvider, endProvider, dataProvider, printConsumer)); + editMenu.addItem(exportCalendarMonthCommand.getLabel(), exportCalendarMonthCommand); + ClubCommand exportCalendarYearCommand = new ExportCalendarYearCommand(startProvider, endProvider, dataProvider, + printConsumer); + editMenu.addItem(exportCalendarYearCommand.getLabel(), new CommandWrapper(exportCalendarYearCommand)); + ClubCommand exportCsvCommand = new ExportCsvCommand(menuBar, context); + editMenu.addItem(exportCsvCommand.getLabel(), ev -> exportCsvCommand.execute()); + + MenuItem viewMenu = menuBar.getViewMenuItem(); + viewMenu.setVisible(true); + CommandWrapper openPersonEditor = new CommandWrapper( + new SwitchViewCommand(context, "Personen verwalten", VaadinIcons.EDIT, ClubhelperViews.PersonEditView)); + MenuItem openPersonMenuItem = viewMenu.addItem(openPersonEditor.getLabel(), openPersonEditor); + openPersonMenuItem.setCheckable(true); + CommandWrapper calendarView = new CommandWrapper( + new SwitchViewCommand(context, "Hauptansicht", VaadinIcons.CALENDAR, ClubhelperViews.MainView)); + MenuItem calendarMenuItem = viewMenu.addItem(calendarView.getLabel(), calendarView); + calendarMenuItem.setCheckable(true); + View current = navigator.getNavigator().getCurrentView(); + ClubhelperViews view = ClubhelperViews.byView(current); + if (ClubhelperViews.PersonEditView == view) { + openPersonMenuItem.setChecked(true); + } + else { + calendarMenuItem.setChecked(true); + } } } 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 new file mode 100644 index 0000000..7bde2ff --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/menu/MenuItemStateFactory.java @@ -0,0 +1,117 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.ZonedDateTime; +import java.util.function.Supplier; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +import com.vaadin.navigator.View; +import com.vaadin.server.FileResource; +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.BrowserFrame; +import com.vaadin.ui.UI; +import com.vaadin.ui.Window; + +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; +import net.sf.jasperreports.engine.JRException; +import net.sf.jasperreports.engine.JasperExportManager; +import net.sf.jasperreports.engine.JasperPrint; + +@Component +public class MenuItemStateFactory implements ApplicationContextAware { + + private static final Logger LOGGER = LoggerFactory.getLogger(MenuItemStateFactory.class); + + private ApplicationContext context; + + @Autowired + ClubhelperNavigation clubhelperNavigation; + + @Autowired + SecurityVerifier securityGroupVerifier; + + private UI ui; + + private Supplier startDateSupplier; + + private Supplier endDateSupplier; + + public MenuItemState currentState() { + MenuItemState state; + View currentView = clubhelperNavigation.getNavigator().getCurrentView(); + ClubhelperViews current = ClubhelperViews.byView(currentView); + if (ClubhelperViews.PersonEditView == current) { + state = new LoggedinMenuitemState(context, startDateSupplier, endDateSupplier, this::showPrint); + } + else if (securityGroupVerifier.isLoggedin()) { + + state = new LoggedinMenuitemState(context, startDateSupplier, endDateSupplier, this::showPrint); + } + else { + state = new LoggedOffState(context, startDateSupplier, endDateSupplier, this::showPrint); + } + + return state; + } + + private void showPrint(String calendarMonth, JasperPrint print) { + Window window = new Window(); + window.setCaption("View PDF"); + AbstractComponent e; + try { + e = createEmbedded(calendarMonth, print); + } + catch (Exception e1) { + LOGGER.error("Error creating PDF Element", e1); + return; + } + window.setContent(e); + window.setModal(true); + window.setWidth("50%"); + window.setHeight("90%"); + ui.addWindow(window); + } + + private AbstractComponent createEmbedded(String title, JasperPrint print) throws IOException, JRException { + + Path pdfFile = Files.createTempFile(title.replace(' ', '_'), ".pdf").toAbsolutePath(); + JasperExportManager.exportReportToPdfFile(print, pdfFile.toString()); + + final FileResource resource = new FileResource(pdfFile.toFile()); + + BrowserFrame c = new BrowserFrame("PDF invoice", resource); + + c.setSizeFull(); + + return c; + } + + @Override + public void setApplicationContext(ApplicationContext context) throws BeansException { + this.context = context; + } + + public void setStartDateSupplier(Supplier startDateSupplier) { + this.startDateSupplier = startDateSupplier; + } + + public void setEndDateSupplier(Supplier endDateSupplier) { + this.endDateSupplier = endDateSupplier; + } + + public void configure(UI ui) { + this.ui = ui; + } + +} 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 c1d863e..286ab43 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 @@ -1,6 +1,8 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation; +import java.time.ZonedDateTime; import java.util.Stack; +import java.util.function.Supplier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,12 +24,16 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.GroupDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PflichtenDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityVerifier; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.EventDetails; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu.ClubhelperMenuBar; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu.MenuItemStateFactory; @Component public class ClubhelperNavigation implements ApplicationContextAware { private static final Logger LOGGER = LoggerFactory.getLogger(ClubhelperNavigation.class); + private static final int WIDTH_LIMIT_FOR_MOBILE = 1000; + private ApplicationContext context; @Autowired @@ -50,21 +56,25 @@ public class ClubhelperNavigation implements ApplicationContextAware { @Autowired PersonBusiness personBusiness; - public void configure(UI mainUI) { + private MainView mainView; - navi = new ClubNavigator().init(mainUI); + private PersonEditView personEdit; + + public void configure(UI mainUI) { // Create and register the views Page page = mainUI.getPage(); ViewFactory factory = new ViewFactory(page); - MainView mainView = factory.createMain(); + mainView = factory.createMain(); + navi = new ClubNavigator().init(mainUI); navi.addView("", mainView); navi.addView(ClubhelperViews.MainView.name(), mainView); navi.addView(ClubhelperViews.LoginUI.name(), new LoginUI(personBusiness, securityGroupVerifier)); - navi.addView(ClubhelperViews.PersonEditView.name(), factory.createPersonEdit()); + personEdit = factory.createPersonEdit(); + navi.addView(ClubhelperViews.PersonEditView.name(), personEdit); navi.addView(ClubhelperViews.EventDetails.name(), new EventDetails(personBusiness, groupDao, eventBusiness, pflichtenDao, calendarAdapter)); @@ -93,7 +103,7 @@ public class ClubhelperNavigation implements ApplicationContextAware { public MainView createMain() { - if (page.getBrowserWindowWidth() < 1000) { + if (page.getBrowserWindowWidth() < WIDTH_LIMIT_FOR_MOBILE) { return new MainViewMobile(context, personBusiness, groupDao, eventBusiness, securityGroupVerifier); } else { @@ -102,7 +112,12 @@ public class ClubhelperNavigation implements ApplicationContextAware { } public PersonEditView createPersonEdit() { - return new PersonEditView(groupDao, personBusiness, (page.getBrowserWindowWidth() >= 1000)); + 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)); } } @@ -141,7 +156,7 @@ public class ClubhelperNavigation implements ApplicationContextAware { int width = navi.getUI().getPage().getBrowserWindowWidth(); boolean loggedIn = securityGroupVerifier.isLoggedin(); - if (!loggedIn && width < 1000) { + if (!loggedIn && width < WIDTH_LIMIT_FOR_MOBILE) { navigationViewNames.clear(); navigationViewNames.add(ClubhelperViews.MainView); super.navigateTo(ClubhelperViews.LoginUI.name()); @@ -163,4 +178,13 @@ public class ClubhelperNavigation implements ApplicationContextAware { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } + + public Supplier startDateSupplier() { + return mainView.startDateSupplier(); + } + + public Supplier endDateSupplier() { + return mainView.endDateSupplier(); + } + } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperViews.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperViews.java index 988bd83..749e611 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperViews.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/ClubhelperViews.java @@ -1,10 +1,15 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation; +import com.vaadin.navigator.View; + public enum ClubhelperViews { /** * With Alias '' */ - MainView, EventDetails, PersonEditView, LoginUI; + MainView, + EventDetails, + PersonEditView, + LoginUI; public static ClubhelperViews byState(String state) { if (state.isBlank()) { @@ -17,4 +22,12 @@ public enum ClubhelperViews { } throw new IllegalArgumentException("View not found for state=" + state); } + + public static ClubhelperViews byView(View currentView) { + if (currentView == null) { + return MainView; + } + return byState(currentView.getClass().getSimpleName()); + } + } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/DesktopHeadView.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/DesktopHeadView.java index 3d7ef3e..fc40040 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/DesktopHeadView.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/DesktopHeadView.java @@ -1,27 +1,15 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation; import java.time.ZonedDateTime; -import java.util.function.Function; - -import org.springframework.context.ApplicationContext; - -import com.vaadin.ui.Component; import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityVerifier; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent.ClubEventProvider; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator; public class DesktopHeadView extends HeadView { private static final long serialVersionUID = 1596573215389558000L; - public DesktopHeadView(ApplicationContext context, ClubNavigator navigator, - Function startTime, - Function endTime, ClubEventProvider dataProvider, - SecurityVerifier securityVerifier) { - - super(context, navigator, startTime, endTime, dataProvider, securityVerifier); - + public DesktopHeadView(SecurityVerifier securityVerifier) { + super(securityVerifier); } public void updateMonthText(ZonedDateTime startDate) { diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/HeadView.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/HeadView.java index 0940580..e879409 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/HeadView.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/HeadView.java @@ -1,45 +1,16 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation; -import java.io.IOException; -import java.io.PipedInputStream; -import java.io.PipedOutputStream; -import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; -import java.util.function.Function; -import java.util.function.Supplier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.context.ApplicationContext; -import com.vaadin.contextmenu.ContextMenu; -import com.vaadin.icons.VaadinIcons; -import com.vaadin.server.StreamResource; -import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Alignment; -import com.vaadin.ui.BrowserFrame; -import com.vaadin.ui.Button; -import com.vaadin.ui.Button.ClickEvent; -import com.vaadin.ui.Component; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Label; -import com.vaadin.ui.Window; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityGroups; import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityVerifier; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ClubCommand; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCalendarMonthCommand; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCalendarYearCommand; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.ExportCsvCommand; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.LoginCommand; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.LogoutCommand; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.commands.OpenPersonEditorCommand; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent.ClubEventProvider; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator; -import net.sf.jasperreports.engine.JRException; -import net.sf.jasperreports.engine.JasperExportManager; -import net.sf.jasperreports.engine.JasperPrint; public class HeadView extends HorizontalLayout { @@ -47,57 +18,30 @@ public class HeadView extends HorizontalLayout { protected transient DateTimeFormatter dfMonth = DateTimeFormatter.ofPattern("MMMM uuuu"); - private final ClubEventProvider dataProvider; - - private final Button personLabel; + private final Label personLabel; protected final Label monthName; - private final Function startTime; - - private final Function endTime; - - private final ClubNavigator navigator; - private final SecurityVerifier securityVerifier; - private ApplicationContext context; + public HeadView(SecurityVerifier securityVerifier) { - public HeadView(ApplicationContext context, ClubNavigator navigator2, - Function startTime, - Function endTime, ClubEventProvider dataProvider, - SecurityVerifier securityVerifier) { - - this.context = context; - this.navigator = navigator2; this.securityVerifier = securityVerifier; - this.startTime = startTime; - this.endTime = endTime; - - Button popupButton = new Button(VaadinIcons.MENU); - popupButton.setId("head.menu"); - popupButton.addClickListener(ev -> openPopupMenu(ev)); - popupButton.setWidth(null); - monthName = new Label(); monthName.setId("calendar.month"); monthName.setStyleName("title_caption"); monthName.setWidth(""); - personLabel = new Button(VaadinIcons.USER); + personLabel = new Label(); personLabel.setId("head.user"); - personLabel.addClickListener(this::openPopupMenu); - addComponent(popupButton); addComponent(monthName); addComponent(personLabel); - setComponentAlignment(popupButton, Alignment.MIDDLE_LEFT); - setComponentAlignment(monthName, Alignment.MIDDLE_CENTER); + setComponentAlignment(monthName, Alignment.MIDDLE_LEFT); setComponentAlignment(personLabel, Alignment.MIDDLE_RIGHT); setExpandRatio(monthName, 1.0f); - this.dataProvider = dataProvider; } public void updateLoggedinPerson() { @@ -111,106 +55,4 @@ public class HeadView extends HorizontalLayout { } } - private void openPopupMenu(ClickEvent ev) { - Button button = ev.getButton(); - - ContextMenu contextMenu = new ContextMenu(button, true); - - switch (button.getId()) { - case "head.menu": - - Supplier start = () -> startTime.apply(button); - Supplier end = () -> endTime.apply(button); - - ClubCommand exportCalendarMonthCommand = new ExportCalendarMonthCommand(start, end, - dataProvider, this::showPrint); - contextMenu.addItem(exportCalendarMonthCommand.getLabel(), - ev1 -> exportCalendarMonthCommand.execute()); - - ClubCommand exportCalendarYearCommand = new ExportCalendarYearCommand(start, end, - dataProvider, this::showPrint); - contextMenu.addItem(exportCalendarYearCommand.getLabel(), ev1 -> exportCalendarYearCommand.execute()); - - ClubCommand exportCsvCommand = new ExportCsvCommand(button, context); - contextMenu.addItem(exportCsvCommand.getLabel(), ev1 -> exportCsvCommand.execute()); - - if (securityVerifier.isLoggedin() - && securityVerifier.isPermitted(SecurityGroups.ADMIN, SecurityGroups.UEBUNGSLEITER)) { - - ClubCommand openPersonEditor = new OpenPersonEditorCommand(navigator); - contextMenu.addItem(openPersonEditor.getLabel(), - ev1 -> openPersonEditor.execute()); - } - contextMenu.open(50, 50); - break; - case "head.user": - if (securityVerifier.isLoggedin()) { - - LogoutCommand logoutCommand = new LogoutCommand(navigator, securityVerifier); - contextMenu.addItem(logoutCommand.getLabel(), ev1 -> { - logoutCommand.execute(); - }); - } - else { - LoginCommand loginCommand = new LoginCommand(navigator); - contextMenu.addItem(loginCommand.getLabel(), ev1 -> { - loginCommand.execute(); - }); - } - int width = getUI().getPage().getBrowserWindowWidth(); - - contextMenu.open(width - 150, 50); - break; - default: - break; - } - - } - - private void showPrint(String calendarMonth, JasperPrint print) { - Window window = new Window(); - window.setCaption("View PDF"); - AbstractComponent e; - try { - e = createEmbedded(calendarMonth, print); - } - catch (Exception e1) { - - return; - } - window.setContent(e); - window.setModal(true); - window.setWidth("50%"); - window.setHeight("90%"); - personLabel.getUI().addWindow(window); - } - - private AbstractComponent createEmbedded(String title, JasperPrint print) throws IOException, JRException { - - final PipedInputStream in = new PipedInputStream(); - final PipedOutputStream out = new PipedOutputStream(in); - - final StreamResource resource = new StreamResource(() -> in, title); - resource.setMIMEType("application/pdf"); - - BrowserFrame c = new BrowserFrame("PDF invoice", resource); - - c.setSizeFull(); - - try { - JasperExportManager.exportReportToPdfStream(print, out); - } - finally { - try { - out.close(); - in.close(); - } - catch (IOException e) { - log.warn("Error closing Jasper output stream.", e); - } - - } - return c; - } - } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/MainView.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/MainView.java index e23393b..21af400 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/MainView.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/MainView.java @@ -1,15 +1,25 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation; +import java.io.IOException; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; +import java.time.ZonedDateTime; import java.util.Set; +import java.util.function.Supplier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; import com.vaadin.event.selection.SelectionEvent; import com.vaadin.navigator.View; import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent; +import com.vaadin.server.StreamResource; +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.BrowserFrame; import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.Window; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ClubhelperException; import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.EventBusiness; @@ -19,9 +29,13 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityVerifier; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.ClubhelperErrorDialog; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.ClubEventProvider; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.PersonGrid; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.SingleEventView; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation.ClubhelperNavigation.ClubNavigator; +import net.sf.jasperreports.engine.JRException; +import net.sf.jasperreports.engine.JasperExportManager; +import net.sf.jasperreports.engine.JasperPrint; public abstract class MainView extends VerticalLayout implements View { @@ -29,6 +43,8 @@ public abstract class MainView extends VerticalLayout implements View { protected final Logger LOGGER = LoggerFactory.getLogger(getClass()); + private ApplicationContext context; + private final GroupDao groupDao; protected final EventBusiness eventBusiness; @@ -43,12 +59,18 @@ public abstract class MainView extends VerticalLayout implements View { private final PersonBusiness personBusiness; - public MainView(GroupDao groupDao, EventBusiness eventBusiness, PersonBusiness personBusiness, + protected ClubEventProvider dataProvider; + + public MainView(ApplicationContext context2, GroupDao groupDao, EventBusiness eventBusiness, + PersonBusiness personBusiness, SecurityVerifier securityGroupVerifier) { + this.context = context2; this.personBusiness = personBusiness; this.groupDao = groupDao; this.eventBusiness = eventBusiness; this.securityVerifier = securityGroupVerifier; + + dataProvider = context2.getBean(ClubEventProvider.class); } @Override @@ -87,6 +109,56 @@ public abstract class MainView extends VerticalLayout implements View { } + public ApplicationContext getContext() { + return context; + } + + protected void showPrint(String calendarMonth, JasperPrint print) { + Window window = new Window(); + window.setCaption("View PDF"); + AbstractComponent e; + try { + e = createEmbedded(calendarMonth, print); + } + catch (Exception e1) { + + return; + } + window.setContent(e); + window.setModal(true); + window.setWidth("50%"); + window.setHeight("90%"); + eventView.getUI().addWindow(window); + } + + private AbstractComponent createEmbedded(String title, JasperPrint print) throws IOException, JRException { + + final PipedInputStream in = new PipedInputStream(); + final PipedOutputStream out = new PipedOutputStream(in); + + final StreamResource resource = new StreamResource(() -> in, title); + resource.setMIMEType("application/pdf"); + + BrowserFrame c = new BrowserFrame("PDF invoice", resource); + + c.setSizeFull(); + + try { + JasperExportManager.exportReportToPdfStream(print, out); + } + finally { + try { + out.close(); + in.close(); + } + catch (IOException e) { + LOGGER.warn("Error closing Jasper output stream.", e); + } + + } + return c; + } + private void personSelectionChange(SelectionEvent ev) { Set selected = ev.getAllSelectedItems(); LOGGER.debug("Selection changed to: {}", selected); @@ -124,4 +196,7 @@ public abstract class MainView extends VerticalLayout implements View { eventBusiness.setSelected(ev); } + public abstract Supplier startDateSupplier(); + + public abstract Supplier endDateSupplier(); } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/MainViewDesktop.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/MainViewDesktop.java index cf5e171..b762121 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/MainViewDesktop.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/MainViewDesktop.java @@ -1,8 +1,10 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.navigation; +import java.time.ZonedDateTime; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.function.Supplier; import org.springframework.context.ApplicationContext; import org.vaadin.addon.calendar.ui.CalendarComponentEvents; @@ -19,13 +21,12 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.GroupDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityVerifier; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent.ClubEventProvider; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu.ClubhelperMenuBar; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu.MenuItemState; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu.MenuItemStateFactory; public class MainViewDesktop extends MainView { - /** - * - */ private static final long serialVersionUID = -3293470536470926668L; private VerticalLayout eastLayout; @@ -36,30 +37,31 @@ public class MainViewDesktop extends MainView { private CalendarComponent calendar; + private ClubhelperMenuBar menuBar; + private DesktopHeadView head; - private ApplicationContext context; + private MenuItemStateFactory menuStateFactory; public MainViewDesktop(ApplicationContext context, PersonBusiness personDao, GroupDao groupDao, EventBusiness eventBusiness, SecurityVerifier securityGroupVerifier) { - super(groupDao, eventBusiness, personDao, securityGroupVerifier); - this.context = context; + super(context, groupDao, eventBusiness, personDao, securityGroupVerifier); + calendar = new CalendarComponent(dataProvider); + menuStateFactory = context.getBean(MenuItemStateFactory.class); } @Override public void initUI(ViewChangeEvent event) { super.initUI(event); - - ClubEventProvider dataProvider = new ClubEventProvider(); + MenuItemState state = menuStateFactory.currentState(); + menuBar = new ClubhelperMenuBar(state); calendar = new CalendarComponent(dataProvider); calendar.setSizeFull(); calendar.setId("main.calendar"); calendar.setHandler(this::onItemClick); - head = new DesktopHeadView(context, navigator, component -> calendar.getStartDate(), - component -> calendar.getEndDate(), - dataProvider, securityVerifier); + head = new DesktopHeadView(securityVerifier); head.setWidth("100%"); head.updateMonthText(calendar.getStartDate()); @@ -87,6 +89,7 @@ public class MainViewDesktop extends MainView { eastLayout = new VerticalLayout(); eastLayout.addComponents(eventView, personGrid, eventButtonLayout); + addComponent(menuBar); addComponent(head); addComponent(mainLayout); setExpandRatio(mainLayout, 1f); @@ -120,8 +123,9 @@ public class MainViewDesktop extends MainView { @Override public void enter(ViewChangeEvent event) { super.enter(event); - head.updateLoggedinPerson(); + MenuItemState state = menuStateFactory.currentState(); + menuBar.applyState(state); reloadEvents(); } @@ -157,4 +161,14 @@ public class MainViewDesktop extends MainView { eventButtonLayout.setVisible(false); } + @Override + public Supplier startDateSupplier() { + return () -> calendar.getStartDate(); + } + + @Override + public Supplier endDateSupplier() { + return () -> calendar.getEndDate(); + } + } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/MainViewMobile.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/MainViewMobile.java index bc07002..b38a46f 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/MainViewMobile.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/MainViewMobile.java @@ -6,6 +6,7 @@ import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.List; import java.util.Optional; +import java.util.function.Supplier; import org.basilbourque.timecolumnrenderers.ZonedDateTimeRenderer; import org.springframework.context.ApplicationContext; @@ -28,7 +29,6 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.PersonBusiness; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.GroupDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; import de.kreth.vaadin.clubhelper.vaadinclubhelper.security.SecurityVerifier; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.CalendarComponent.ClubEventProvider; public class MainViewMobile extends MainView { @@ -40,13 +40,10 @@ public class MainViewMobile extends MainView { ConfigurableFilterDataProvider> eventDataProvider; - private ApplicationContext context; - public MainViewMobile(ApplicationContext context, PersonBusiness personDao, GroupDao groupDao, EventBusiness eventBusiness, SecurityVerifier securityGroupVerifier) { - super(groupDao, eventBusiness, personDao, securityGroupVerifier); - this.context = context; + super(context, groupDao, eventBusiness, personDao, securityGroupVerifier); } @Override @@ -57,8 +54,7 @@ public class MainViewMobile extends MainView { return; } - head = new HeadView(context, navigator, component -> showDateTimeDialog(component, "Startdatum"), - component -> showDateTimeDialog(component, "Endedatum"), new ClubEventProvider(), securityVerifier); + head = new HeadView(securityVerifier); head.setWidth("100%"); head.updateLoggedinPerson(); @@ -115,6 +111,16 @@ public class MainViewMobile extends MainView { addComponent(personGrid); } + @Override + public Supplier startDateSupplier() { + return () -> showDateTimeDialog(this.head, "Startdatum"); + } + + @Override + public Supplier endDateSupplier() { + return () -> showDateTimeDialog(this.head, "Endedatum"); + } + private ZonedDateTime showDateTimeDialog(Component source, String caption) { Window window = new Window(); window.setCaption(caption); 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 4bfd540..d705bf1 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 @@ -20,20 +20,30 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.GroupDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.PersonEditDetails; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.PersonGrid; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu.ClubhelperMenuBar; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.menu.MenuItemStateFactory; public class PersonEditView extends VerticalLayout implements View { private static final long serialVersionUID = 1770993670570422036L; + private ClubhelperMenuBar menuBar; + private PersonGrid personGrid; private PersonEditDetails personDetails; private Navigator navigator; - public PersonEditView(GroupDao groupDao, PersonBusiness personDao, boolean horizontalLayout) { - setMargin(true); + private MenuItemStateFactory menuStateFactory; + public PersonEditView(GroupDao groupDao, PersonBusiness personDao, ClubhelperMenuBar menuBar, + MenuItemStateFactory menuStateFactory, + boolean horizontalLayout) { + setMargin(true); + this.menuBar = menuBar; + this.menuStateFactory = menuStateFactory; + addComponent(menuBar); personGrid = new PersonGrid(groupDao, personDao); personGrid.setSizeFull(); personGrid.onPersonEdit(); @@ -54,9 +64,9 @@ public class PersonEditView extends VerticalLayout implements View { addPerson.addClickListener(ev -> addPerson()); addComponent(addPerson); - Button backButton = new Button("Zurück"); - backButton.addClickListener(ev -> navigator.navigateTo(ClubhelperViews.MainView.name())); - addComponent(backButton); +// Button backButton = new Button("Zurück"); +// backButton.addClickListener(ev -> navigator.navigateTo(ClubhelperViews.MainView.name())); +// addComponent(backButton); } public HorizontalLayout createHorizontalLayout() { @@ -121,6 +131,7 @@ public class PersonEditView extends VerticalLayout implements View { @Override public void enter(ViewChangeEvent event) { this.navigator = event.getNavigator(); + menuBar.applyState(menuStateFactory.currentState()); } } diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoTest.java index b921228..10f174c 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoTest.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoTest.java @@ -11,7 +11,6 @@ import javax.persistence.EntityManager; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/jasper/YearlyCalendarCreatorTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/jasper/YearlyCalendarCreatorTest.java index 14cd60b..13dd365 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/jasper/YearlyCalendarCreatorTest.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/jasper/YearlyCalendarCreatorTest.java @@ -14,6 +14,7 @@ import java.util.List; import java.util.Map; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; @@ -56,6 +57,7 @@ class YearlyCalendarCreatorTest { } @Test + @Disabled void testJrxmlResource() { creator = new YearlyCalendarCreator(2019, values); InputStream jrxml = creator.jrxmlResource(); diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/LoginUITest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/LoginUITest.java index ab5a676..33107e9 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/LoginUITest.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/navigation/LoginUITest.java @@ -5,7 +5,6 @@ import static org.junit.Assert.assertTrue; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.mockito.Mock;