dhbw.clippinggorilla.userinterface.windows.ManageSourcesWindow.java Source code

Java tutorial

Introduction

Here is the source code for dhbw.clippinggorilla.userinterface.windows.ManageSourcesWindow.java

Source

/*
 * 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.windows;

import com.vaadin.event.ShortcutAction;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.server.ExternalResource;
import com.vaadin.server.VaadinSession;
import com.vaadin.shared.ui.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.Window;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;
import dhbw.clippinggorilla.objects.source.SourceUtils;
import dhbw.clippinggorilla.utilities.language.Language;
import dhbw.clippinggorilla.utilities.language.Word;
import dhbw.clippinggorilla.utilities.log.Log;
import java.net.URL;
import java.util.HashMap;

/**
 * This is the Window to manage the sources
 * @author frank
 */
public class ManageSourcesWindow extends Window {

    private static final HashMap<VaadinSession, ManageSourcesWindow> SESSIONS = new HashMap<>();

    public static ManageSourcesWindow create() {
        return new ManageSourcesWindow();
    }

    public static ManageSourcesWindow getCurrent() {
        return SESSIONS.get(VaadinSession.getCurrent());
    }

    public ManageSourcesWindow() {
        SESSIONS.put(VaadinSession.getCurrent(), this);
        setModal(true);
        setSizeUndefined();
        setDraggable(true);
        setWidth("950px");
        setHeight("850px");
        center();
        setClosable(false);
        setResizable(false);
        addCloseShortcut(ShortcutAction.KeyCode.ENTER, null);
        setContent(getSourcesList());
    }

    private VerticalLayout sourcesLayout;
    private final HashMap<String, GridLayout> sourceLayouts = new HashMap<>();

    public Component getSourcesList() {
        VerticalLayout windowLayout = new VerticalLayout();
        windowLayout.setSizeFull();
        sourcesLayout = new VerticalLayout();
        sourcesLayout.setWidth("100%");
        Panel sourcesPanel = new Panel(sourcesLayout);

        refreshSources();

        sourcesPanel.setContent(sourcesLayout);
        sourcesPanel.setHeight("100%");
        windowLayout.addComponent(sourcesPanel);
        windowLayout.setExpandRatio(sourcesPanel, 1);
        windowLayout.setSpacing(false);
        windowLayout.setMargin(false);

        TextField textFieldSearch = new TextField();
        textFieldSearch.setPlaceholder(Language.get(Word.SEARCH));
        textFieldSearch.addValueChangeListener(searchValue -> {
            sourceLayouts.forEach((sourceName, sourceLayout) -> {
                if (!sourceName.contains(searchValue.getValue().toLowerCase().replaceAll(" ", "")
                        .replaceAll("-", "").replaceAll("_", ""))) {
                    sourcesLayout.removeComponent(sourceLayout);
                } else {
                    sourcesLayout.addComponent(sourceLayout);
                }
            });
        });

        Button buttonAddSource = new Button(Language.get(Word.ADD_SOURCE), VaadinIcons.PLUS);
        buttonAddSource.addClickListener(ce -> UI.getCurrent().addWindow(NewSourceWindow.create()));

        Button buttonSave = new Button();
        Language.set(Word.SAVE, buttonSave);
        buttonSave.setIcon(VaadinIcons.CHECK);
        buttonSave.addStyleName(ValoTheme.BUTTON_PRIMARY);
        buttonSave.addClickListener(ce -> {
            close();
        });
        buttonSave.setClickShortcut(ShortcutAction.KeyCode.ENTER, null);

        Label placeholder = new Label();

        GridLayout footer = new GridLayout(4, 1);
        footer.setSpacing(true);
        footer.setSizeUndefined();
        footer.addStyleName(ValoTheme.WINDOW_BOTTOM_TOOLBAR);
        footer.addStyleName("menubar");
        footer.setWidth(100.0f, Unit.PERCENTAGE);
        footer.addComponents(textFieldSearch, placeholder, buttonAddSource, buttonSave);
        footer.setComponentAlignment(textFieldSearch, Alignment.MIDDLE_CENTER);
        footer.setComponentAlignment(buttonAddSource, Alignment.MIDDLE_CENTER);
        footer.setComponentAlignment(buttonSave, Alignment.MIDDLE_CENTER);
        footer.setColumnExpandRatio(1, 5);

        windowLayout.addComponent(footer);
        return windowLayout;
    }

    public void refreshSources() {
        sourcesLayout.removeAllComponents();
        SourceUtils.getSources().stream().sorted((s1, s2) -> s1.getName().compareToIgnoreCase(s2.getName()))
                .forEach(source -> {
                    try {
                        GridLayout sourceLayout = new GridLayout(4, 1);
                        sourceLayout.setSizeFull();

                        Image sourceLogo = new Image();
                        URL url = source.getLogo();
                        if (url != null) {
                            sourceLogo.setSource(new ExternalResource(url));
                        }
                        sourceLogo.setWidth("150px");

                        VerticalLayout sourceText = new VerticalLayout();
                        sourceText.setSizeFull();

                        Label labelHeadLine = new Label(
                                source.getCategory().getIcon().getHtml() + "  " + source.getName(),
                                ContentMode.HTML);
                        labelHeadLine.addStyleName(ValoTheme.LABEL_H2);

                        Label labelDescription = new Label(source.getDescription(), ContentMode.HTML);
                        labelDescription.addStyleName(ValoTheme.LABEL_SMALL);
                        labelDescription.setWidth("100%");

                        sourceText.setMargin(false);
                        sourceText.addComponents(labelHeadLine, labelDescription);

                        Button buttonEdit = new Button(VaadinIcons.EDIT);
                        buttonEdit.addStyleName(ValoTheme.BUTTON_PRIMARY);
                        buttonEdit.addClickListener(
                                ce -> UI.getCurrent().addWindow(NewSourceWindow.createEditMode(source)));

                        Button buttonDelete = new Button(VaadinIcons.TRASH);
                        buttonDelete.addStyleName(ValoTheme.BUTTON_DANGER);
                        buttonDelete.addClickListener(ce -> {
                            sourcesLayout.removeComponent(sourceLayout);
                            sourceLayouts.remove(source.getName().toLowerCase().replaceAll(" ", "")
                                    .replaceAll("-", "").replaceAll("_", ""));
                            SourceUtils.removeSource(source);
                        });

                        sourceLayout.addComponents(sourceLogo, sourceText, buttonEdit, buttonDelete);
                        sourceLayout.setComponentAlignment(sourceLogo, Alignment.MIDDLE_CENTER);
                        sourceLayout.setComponentAlignment(buttonEdit, Alignment.MIDDLE_CENTER);
                        sourceLayout.setComponentAlignment(buttonDelete, Alignment.MIDDLE_CENTER);
                        sourceLayout.setColumnExpandRatio(1, 5);
                        sourceLayout.setSpacing(true);

                        sourceLayouts.put(source.getName().toLowerCase().replaceAll(" ", "").replaceAll("-", "")
                                .replaceAll("_", ""), sourceLayout);
                        sourcesLayout.addComponent(sourceLayout);
                    } catch (Exception e) {
                        Log.error("Skipping Source! ", e);
                    }
                });
    }

}