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.icons.VaadinIcons; import com.vaadin.server.FileDownloader; import com.vaadin.server.FileResource; import com.vaadin.server.VaadinSession; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.Component; import com.vaadin.ui.GridLayout; import com.vaadin.ui.Label; import com.vaadin.ui.TabSheet; import com.vaadin.ui.UI; import dhbw.clippinggorilla.userinterface.windows.PDFWindow; import dhbw.clippinggorilla.utilities.language.Language; import dhbw.clippinggorilla.utilities.language.Word; import dhbw.clippinggorilla.utilities.ressources.FileUtils; import java.io.IOException; import java.nio.file.Path; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; /** * This is the class for the Documents View * * @author frank */ public class DocumentsView extends TabSheet { private static final HashMap<VaadinSession, DocumentsView> SESSIONS = new HashMap<>(); public static DocumentsView getCurrent() { return new DocumentsView(); } public DocumentsView() { for (int i = 1; i < 12; i++) { setWidth("55%"); GridLayout layout = new GridLayout(3, 1); layout.setMargin(true); layout.setSpacing(true); try { List<Path> files = FileUtils.listFiles("docs/week" + i); layout.setRows(files.size() > 0 ? files.size() : 1); files.stream().sorted(Comparator.naturalOrder()).forEach(f -> { generateLine(f, layout); }); } catch (IOException ex) { Logger.getLogger(DocumentsView.class.getName()).log(Level.SEVERE, null, ex); } addTab(layout, Language.get(Word.WEEK) + " " + i); } Language.setCustom(Word.WEEK, t -> t + " 1", v -> getTab(0).setCaption(v)); Language.setCustom(Word.WEEK, t -> t + " 2", v -> getTab(1).setCaption(v)); Language.setCustom(Word.WEEK, t -> t + " 3", v -> getTab(2).setCaption(v)); Language.setCustom(Word.WEEK, t -> t + " 4", v -> getTab(3).setCaption(v)); Language.setCustom(Word.WEEK, t -> t + " 5", v -> getTab(4).setCaption(v)); Language.setCustom(Word.WEEK, t -> t + " 6", v -> getTab(5).setCaption(v)); Language.setCustom(Word.WEEK, t -> t + " 7", v -> getTab(6).setCaption(v)); Language.setCustom(Word.WEEK, t -> t + " 8", v -> getTab(7).setCaption(v)); Language.setCustom(Word.WEEK, t -> t + " 9", v -> getTab(8).setCaption(v)); Language.setCustom(Word.WEEK, t -> t + " 10", v -> getTab(9).setCaption(v)); Language.setCustom(Word.WEEK, t -> t + " 11", v -> getTab(10).setCaption(v)); SESSIONS.put(VaadinSession.getCurrent(), this); } private Component generateLine(Path file, GridLayout layout) { Label fileName = new Label(file.getFileName().toString()); layout.addComponent(fileName); layout.setComponentAlignment(fileName, Alignment.MIDDLE_LEFT); Button downloadButton = new Button(); downloadButton.setIcon(VaadinIcons.DOWNLOAD); FileDownloader downloader = new FileDownloader(new FileResource(file.toFile())); downloader.extend(downloadButton); layout.addComponent(downloadButton); layout.setComponentAlignment(downloadButton, Alignment.MIDDLE_CENTER); if (file.getFileName().toString().endsWith("pdf")) { Button viewButton = new Button(); viewButton.setIcon(VaadinIcons.EYE); viewButton.addClickListener(ce -> UI.getCurrent().addWindow(PDFWindow.get(file))); layout.addComponent(viewButton); layout.setComponentAlignment(viewButton, Alignment.MIDDLE_CENTER); } else { layout.addComponent(new Label(" ")); } return layout; } }