Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package dhbw.clippinggorilla.userinterface.views; import com.vaadin.event.ShortcutAction; import com.vaadin.icons.VaadinIcons; import com.vaadin.shared.ui.ContentMode; import com.vaadin.shared.ui.MarginInfo; import com.vaadin.ui.Accordion; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.CheckBox; import com.vaadin.ui.Component; import com.vaadin.ui.CssLayout; import com.vaadin.ui.GridLayout; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Label; import com.vaadin.ui.Layout; import com.vaadin.ui.Panel; import com.vaadin.ui.TabSheet; import com.vaadin.ui.TextField; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.themes.ValoTheme; import dhbw.clippinggorilla.external.database.exceptions.UserNotFoundException; import dhbw.clippinggorilla.objects.group.Group; import dhbw.clippinggorilla.objects.group.GroupInterestProfileUtils; import dhbw.clippinggorilla.objects.group.GroupUtils; import dhbw.clippinggorilla.objects.user.User; import dhbw.clippinggorilla.objects.user.UserUtils; import dhbw.clippinggorilla.userinterface.windows.ConfirmationDialog; import dhbw.clippinggorilla.userinterface.windows.GroupProfileWindow; import dhbw.clippinggorilla.utilities.language.Language; import dhbw.clippinggorilla.utilities.language.Word; import dhbw.clippinggorilla.utilities.ui.VaadinUtils; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * * @author josua.frank */ public class GroupView extends VerticalLayout { //private static final HashMap<User, GroupView> SESSIONS = new HashMap<>(); public static GroupView getCurrent() { return new GroupView(); } private final Accordion accordion = new Accordion(); public GroupView() { User user = UserUtils.getCurrent(); Set<Group> groups = UserUtils.getAllGroups(user); CssLayout newGroupGroup = new CssLayout(); newGroupGroup.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP); TextField textFieldNewGroupName = new TextField(); Language.setCustom(Word.GROUP_NAME, s -> textFieldNewGroupName.setPlaceholder(s)); textFieldNewGroupName.setWidth("260px"); textFieldNewGroupName.setMaxLength(255); newGroupGroup.addComponent(textFieldNewGroupName); Button buttonNewGroup = new Button(); buttonNewGroup.setIcon(VaadinIcons.PLUS); buttonNewGroup.addStyleName(ValoTheme.BUTTON_PRIMARY); buttonNewGroup.addClickListener(e -> { TabSheet.Tab newTab = accordion.addTab(createTab(createEmptyGroup(textFieldNewGroupName.getValue())), textFieldNewGroupName.getValue()); accordion.setSelectedTab(newTab); accordion.setWidth("100%"); textFieldNewGroupName.clear(); }); newGroupGroup.addComponent(buttonNewGroup); textFieldNewGroupName .addFocusListener(f -> buttonNewGroup.setClickShortcut(ShortcutAction.KeyCode.ENTER, null)); textFieldNewGroupName.addBlurListener(f -> buttonNewGroup.removeClickShortcut()); groups.forEach(g -> { accordion.addTab(createTab(g), g.getName()); }); addComponents(newGroupGroup, accordion); //SESSIONS.put(user, this); } //Footer Button buttonLeave; Button buttonDelete; Map<Group, VerticalLayout> groupSettingsLayouts = new HashMap<>(); public VerticalLayout createTab(Group g) { User u = UserUtils.getCurrent(); VerticalLayout groupSettingsLayout = new VerticalLayout(); groupSettingsLayouts.put(g, groupSettingsLayout); GridLayout tabContent; TextField textFieldName = new TextField(); if (GroupUtils.isAdmin(g, u)) { tabContent = new GridLayout(3, 2); Language.set(Word.NAME, textFieldName); textFieldName.setWidth("100%"); textFieldName.setValue(g.getName()); textFieldName.setMaxLength(255); tabContent.addComponent(textFieldName); tabContent.setComponentAlignment(textFieldName, Alignment.MIDDLE_LEFT); } else { tabContent = new GridLayout(3, 1); } buttonLeave = new Button(); Language.set(Word.LEAVE, buttonLeave); buttonLeave.setIcon(VaadinIcons.MINUS); buttonLeave.addStyleName(ValoTheme.BUTTON_DANGER); buttonLeave.addClickListener(ce -> { ConfirmationDialog.show(Language.get(Word.REALLY_LEAVE_GROUP).replace("[GROUP]", g.getName()), () -> { long amountAdmins = g.getUsers().entrySet().stream().filter(e -> e.getValue()).count(); if (amountAdmins > 1 || !GroupUtils.isAdmin(g, u)) { GroupUtils.removeUser(g, u); refreshAll(g); } else { VaadinUtils.errorNotification(Language.get(Word.NOT_ENOUGH_ADMINS_IN_GROUP)); } }); }); buttonDelete = new Button(); Language.set(Word.DELETE, buttonDelete); buttonDelete.setIcon(VaadinIcons.TRASH); buttonDelete.addStyleName(ValoTheme.BUTTON_DANGER); buttonDelete.addClickListener(ce -> { ConfirmationDialog.show(Language.get(Word.REALLY_DELETE_GROUP).replace("[GROUP]", g.getName()), () -> { GroupUtils.removeGroup(g); refreshAll(g); }); }); Button buttonSave = new Button(); Language.set(Word.SAVE, buttonSave); buttonSave.setIcon(VaadinIcons.CHECK); buttonSave.addStyleName(ValoTheme.BUTTON_PRIMARY); buttonSave.addClickListener(ce -> { if (GroupUtils.isAdmin(g, u)) { GroupUtils.changeName(g, textFieldName.getValue()); accordion.getTab(groupSettingsLayout).setCaption(textFieldName.getValue()); } VaadinUtils.infoNotification(Language.get(Word.SUCCESSFULLY_SAVED)); }); buttonSave.setClickShortcut(ShortcutAction.KeyCode.ENTER, null); Label placeholder = new Label(); Label placeholder2 = new Label(); placeholder2.setWidth("100%"); Label placeholder3 = new Label(); GridLayout footer = new GridLayout(4, 1); footer.setSpacing(true); footer.setMargin(new MarginInfo(false, true)); footer.setSizeUndefined(); footer.addStyleName(ValoTheme.WINDOW_BOTTOM_TOOLBAR); footer.setWidth("100%"); footer.addStyleName("menubar"); if (GroupUtils.isAdmin(g, u)) { footer.addComponents(placeholder, buttonDelete, buttonLeave, buttonSave); footer.setComponentAlignment(buttonDelete, Alignment.MIDDLE_CENTER); } else { footer.addComponents(placeholder, placeholder3, buttonLeave, buttonSave); } footer.setColumnExpandRatio(0, 5); footer.setComponentAlignment(buttonLeave, Alignment.MIDDLE_CENTER); footer.setComponentAlignment(buttonSave, Alignment.MIDDLE_CENTER); if (GroupUtils.isAdmin(g, u)) { tabContent.addComponent(getProfiles(g), 0, 1, 1, 1); tabContent.addComponent(getMembers(g), 2, 1); } else { tabContent.addComponent(getProfiles(g), 0, 0, 1, 0); tabContent.addComponent(getMembers(g), 2, 0); } tabContent.setWidth("100%"); tabContent.setSpacing(true); tabContent.setMargin(true); tabContent.addStyleName("profiles"); groupSettingsLayout.addComponents(tabContent, footer); groupSettingsLayout.setMargin(false); groupSettingsLayout.setSpacing(false); groupSettingsLayout.setWidth("100%"); return groupSettingsLayout; } private static Group createEmptyGroup(String name) { return GroupUtils.createNewGroup(UserUtils.getCurrent(), name); } Map<Group, VerticalLayout> mapLayoutMembers = new HashMap<>(); Map<Group, VerticalLayout> mapLayoutProfiles = new HashMap<>(); private Component getProfiles(Group g) { VerticalLayout layoutRootProfiles = new VerticalLayout(); layoutRootProfiles.setMargin(false); layoutRootProfiles.setWidth("100%"); Panel panelProfiles = new Panel(Language.get(Word.PROFILES)); Language.setCustom(Word.PROFILES, s -> panelProfiles.setCaption(s)); panelProfiles.setWidth("100%"); panelProfiles.setHeight("200px"); VerticalLayout layoutProfiles = new VerticalLayout(); mapLayoutProfiles.put(g, layoutProfiles); layoutProfiles.setWidth("100%"); refreshProfiles(g, layoutProfiles); panelProfiles.setContent(layoutProfiles); layoutRootProfiles.addComponent(panelProfiles); if (GroupUtils.isAdmin(g, UserUtils.getCurrent())) { CssLayout addProfileGroup = new CssLayout(); addProfileGroup.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP); addProfileGroup.setWidth("100%"); TextField textFieldAddProfile = new TextField(); Language.setCustom(Word.PROFILE_NAME, s -> textFieldAddProfile.setPlaceholder(s)); textFieldAddProfile.setMaxLength(255); textFieldAddProfile.setWidth("35%"); addProfileGroup.addComponent(textFieldAddProfile); Button buttonAddProfile = new Button(); buttonAddProfile.setIcon(VaadinIcons.PLUS); buttonAddProfile.addStyleName(ValoTheme.BUTTON_PRIMARY); buttonAddProfile.setWidth("15%"); buttonAddProfile.addClickListener(e -> { try { String name = textFieldAddProfile.getValue(); textFieldAddProfile.clear(); Runnable onClose = () -> refreshAll(g); UI.getCurrent().addWindow(GroupProfileWindow.create(g, name, true, onClose)); } catch (UserNotFoundException ex) { VaadinUtils.errorNotification(Language.get(Word.USER_NOT_FOUND)); } }); addProfileGroup.addComponent(buttonAddProfile); layoutRootProfiles.addComponent(addProfileGroup); } return layoutRootProfiles; } private void refreshProfiles(Group g, Layout layoutProfiles) { layoutProfiles.removeAllComponents(); GroupUtils.getAllInterestProfiles(g, UserUtils.getCurrent()).forEach((p, enabled) -> { HorizontalLayout layoutProfile = new HorizontalLayout(); layoutProfile.setWidth("100%"); CheckBox checkBoxEnabled = new CheckBox(); checkBoxEnabled.setValue(enabled); checkBoxEnabled.addValueChangeListener(v -> { GroupInterestProfileUtils.changeEnabled(p, UserUtils.getCurrent(), v.getValue()); refreshAll(g); }); long subscribers = p.getEnabledUsers().values().stream().filter(v -> v).count(); Label labelProfileInfo = new Label(); Language.setCustom(Word.SUBSCRIBERS, s -> { String info = p.getName() + " (" + subscribers + " "; info += subscribers == 1 ? Language.get(Word.SUBSCRIBER) + ")" : Language.get(Word.SUBSCRIBERS) + ")"; labelProfileInfo.setValue(info); }); boolean isAdmin = g.getUsers().getOrDefault(UserUtils.getCurrent(), false); Button buttonOpen = new Button(VaadinIcons.EXTERNAL_LINK); buttonOpen.addClickListener(ce -> { UI.getCurrent().addWindow(GroupProfileWindow.show(p, isAdmin, () -> { })); }); if (isAdmin) { Button buttonRemove = new Button(VaadinIcons.MINUS); buttonRemove.addStyleName(ValoTheme.BUTTON_DANGER); buttonRemove.addClickListener(ce -> { ConfirmationDialog.show( Language.get(Word.REALLY_DELETE_PROFILE).replace("[PROFILE]", p.getName()), () -> { GroupInterestProfileUtils.delete(p); refreshAll(g); }); }); layoutProfile.addComponents(checkBoxEnabled, labelProfileInfo, buttonOpen, buttonRemove); } else { layoutProfile.addComponents(checkBoxEnabled, labelProfileInfo, buttonOpen); } layoutProfile.setExpandRatio(labelProfileInfo, 5); layoutProfile.setComponentAlignment(checkBoxEnabled, Alignment.MIDDLE_CENTER); layoutProfile.setComponentAlignment(labelProfileInfo, Alignment.MIDDLE_LEFT); layoutProfiles.addComponent(layoutProfile); }); } private Component getMembers(Group g) { VerticalLayout layoutRootMembers = new VerticalLayout(); layoutRootMembers.setMargin(false); layoutRootMembers.setWidth("100%"); Panel panelMembers = new Panel(Language.get(Word.MEMBERS)); Language.setCustom(Word.MEMBERS, s -> panelMembers.setCaption(s)); panelMembers.setWidth("100%"); panelMembers.setHeight("200px"); VerticalLayout layoutMembers = new VerticalLayout(); mapLayoutMembers.put(g, layoutMembers); layoutMembers.setWidth("100%"); refreshMembers(g, layoutMembers); panelMembers.setContent(layoutMembers); layoutRootMembers.addComponent(panelMembers); if (GroupUtils.isAdmin(g, UserUtils.getCurrent())) { CssLayout addUserGroup = new CssLayout(); addUserGroup.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP); addUserGroup.setWidth("100%"); TextField textFieldaddUser = new TextField(); Language.setCustom(Word.USERNAME, s -> textFieldaddUser.setPlaceholder(s)); textFieldaddUser.setMaxLength(255); textFieldaddUser.setWidth("70%"); addUserGroup.addComponent(textFieldaddUser); Button buttonAddUser = new Button(); buttonAddUser.setIcon(VaadinIcons.PLUS); buttonAddUser.addStyleName(ValoTheme.BUTTON_PRIMARY); buttonAddUser.setWidth("30%"); buttonAddUser.addClickListener(e -> { try { User u = UserUtils.getUser(textFieldaddUser.getValue()); textFieldaddUser.clear(); GroupUtils.addUser(g, u); refreshAll(g); } catch (UserNotFoundException ex) { VaadinUtils.errorNotification(Language.get(Word.USER_NOT_FOUND)); } }); addUserGroup.addComponent(buttonAddUser); layoutRootMembers.addComponent(addUserGroup); } return layoutRootMembers; } public void refreshMembers(Group g, Layout layoutMembers) { layoutMembers.removeAllComponents(); Map<User, Boolean> allMembers = GroupUtils.getAllMembers(g); boolean isAdmin = GroupUtils.isAdmin(g, UserUtils.getCurrent()); if (!isAdmin) { if (allMembers.size() < 2) { buttonLeave.setVisible(false); } else { buttonLeave.setVisible(true); } } else { if (allMembers.entrySet().stream().map(e -> e.getValue()).filter(b -> b == true).count() > 1) { buttonLeave.setVisible(true); } else { buttonLeave.setVisible(false); } } allMembers.forEach((u, admin) -> { HorizontalLayout layoutMember = new HorizontalLayout(); layoutMember.setWidth("100%"); VaadinIcons iconRole = admin ? VaadinIcons.KEY : VaadinIcons.USER; Label labelMemberName = new Label(iconRole.getHtml() + " " + u.getUsername(), ContentMode.HTML); //labelMemberName.setWidth("50%"); layoutMember.addComponents(labelMemberName); layoutMember.setComponentAlignment(labelMemberName, Alignment.MIDDLE_LEFT); layoutMember.setExpandRatio(labelMemberName, 5); if (isAdmin) { if (g.getUsers().size() > 1) { Button buttonChangeRole = new Button(admin ? VaadinIcons.ARROW_DOWN : VaadinIcons.ARROW_UP); buttonChangeRole.addClickListener(ce -> { if (g.getUsers().get(u)) {//Downgrade long amountAdmins = g.getUsers().entrySet().stream().filter(e -> e.getValue()).count(); if (amountAdmins > 1) { if (UserUtils.getCurrent() == u) { ConfirmationDialog.show(Language.get(Word.REALLY_DEADMIN_YOURSELF), () -> { GroupUtils.makeNormalMember(g, u); refreshAll(g); }); } else { GroupUtils.makeNormalMember(g, u); refreshAll(g); } } else { VaadinUtils.errorNotification(Language.get(Word.NOT_ENOUGH_ADMINS_IN_GROUP)); buttonLeave.setVisible(false); } } else {//Upgrade GroupUtils.makeAdmin(g, u); refreshAll(g); } }); layoutMember.addComponent(buttonChangeRole); if (g.getUsers().values().stream().filter(v -> v).count() > 1 || !GroupUtils.isAdmin(g, u)) { Button buttonRemoveUser = new Button(VaadinIcons.MINUS); buttonRemoveUser.addStyleName(ValoTheme.BUTTON_DANGER); buttonRemoveUser.addClickListener(ce -> { ConfirmationDialog.show( Language.get(Word.REALLY_REMOVE_USER_FROM_GROUP) .replace("[USER]", u.getUsername()).replace("[GROUP]", g.getName()), () -> { GroupUtils.removeUser(g, u); refreshAll(g); }); }); layoutMember.addComponents(buttonRemoveUser); } } } layoutMembers.addComponent(layoutMember); }); } VerticalLayout currentTabLayout = null; private void refreshAll(Group currentTab) { User user = UserUtils.getCurrent(); Set<Group> groups = UserUtils.getAllGroups(user); accordion.removeAllComponents(); currentTabLayout = null; groups.forEach(g -> { VerticalLayout tabContent = createTab(g); accordion.addTab(tabContent, g.getName()); if (g.equals(currentTab)) { currentTabLayout = tabContent; } }); if (currentTabLayout != null) { accordion.setSelectedTab(accordion.getTab(currentTabLayout)); } } }