Example usage for com.vaadin.server FileResource FileResource

List of usage examples for com.vaadin.server FileResource FileResource

Introduction

In this page you can find the example usage for com.vaadin.server FileResource FileResource.

Prototype

public FileResource(File sourceFile) 

Source Link

Document

Creates a new file resource for providing given file for client terminals.

Usage

From source file:org.azrul.langkuik.framework.customtype.attachment.AttachmentCustomTypeUICreator.java

@Override
public Component createUIForForm(final C currentBean, final Class<? extends CustomType> attachmentClass,
        final String pojoFieldName, final BeanView beanView, final DataAccessObject<C> conatainerClassDao,
        final DataAccessObject<? extends CustomType> customTypeDao,
        final RelationManagerFactory relationManagerFactory, final Configuration config,
        final ComponentState componentState, final Window window) {
    final FormLayout form = new FormLayout();

    final DataAccessObject<AttachmentCustomType> attachmentDao = ((DataAccessObject<AttachmentCustomType>) customTypeDao);
    final Collection<AttachmentCustomType> attachments = attachmentDao.find(currentBean, pojoFieldName, null,
            true, 0, Integer.parseInt(config.get("uploadCountLimit")));

    final WebEntityItemContainer attachmentIC = new WebEntityItemContainer(attachmentClass);
    if (!attachments.isEmpty()) {
        attachmentIC.addAll(attachments);
    }//from   w w w .  j av a 2 s  .co m
    final ListSelect attachmentList = new ListSelect("", attachmentIC);
    createAtachmentList(attachmentList, attachments, config, beanView, form);

    final String relativePath = currentBean.getClass().getCanonicalName() + File.separator
            + conatainerClassDao.getIdentifierValue(currentBean);
    final String fullPath = config.get("attachmentRepository") + File.separator + relativePath;
    MyUploadHandler<C> uploadFinishHandler = new MyUploadHandler<>(currentBean,
            Integer.parseInt(config.get("uploadCountLimit").trim()), pojoFieldName, fullPath, relativePath,
            attachmentList, attachmentIC, customTypeDao, attachmentClass, relationManagerFactory);

    //File upload/download/delete buttons
    HorizontalLayout attachmentButtons = new HorizontalLayout();
    attachmentButtons.setSpacing(true);

    UploadStateWindow uploadStateWindow = new UploadStateWindow();
    MultiFileUpload fileUpload = new MultiFileUpload(uploadFinishHandler, uploadStateWindow);
    uploadFinishHandler.setFileUpload(fileUpload);

    fileUpload.getSmartUpload().setEnabled(true);
    fileUpload.getSmartUpload().setMaxFileSize(Integer.parseInt(config.get("uploadSizeLimit").trim()));
    fileUpload.getSmartUpload().setUploadButtonCaptions("Upload files", "Upload files");
    fileUpload.getSmartUpload().setId("Upload files");
    fileUpload.setId("Upload files2");

    Button deleteAttachmentBtn = new Button("Delete file", new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            Collection<AttachmentCustomType> attachments = (Collection<AttachmentCustomType>) attachmentList
                    .getValue();
            if (!attachments.isEmpty()) {
                customTypeDao.unlinkAndDelete(attachments, currentBean, pojoFieldName,
                        relationManagerFactory.create(currentBean.getClass(), attachmentClass));
                Collection<AttachmentCustomType> a = attachmentDao.find(currentBean, pojoFieldName, null, true,
                        0, Integer.parseInt(config.get("uploadCountLimit")));
                attachmentIC.removeAllItems();
                attachmentIC.addAll(a);
                attachmentIC.refreshItems();
            }
        }
    });
    deleteAttachmentBtn.setId(deleteAttachmentBtn.getCaption());

    Button downloadAttachmentBtn = new Button("Download file", new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {

            Collection<AttachmentCustomType> attachments = (Collection<AttachmentCustomType>) attachmentList
                    .getValue();
            if (!attachments.isEmpty()) {
                AttachmentCustomType attachment = attachments.iterator().next();
                final String fullPath = config.get("attachmentRepository") + File.separator
                        + attachment.getRelativeLocation() + File.separator + attachment.getFileName();
                FileResource res = new FileResource(new File(fullPath));
                beanView.setViewResource("DOWNLOAD", res);
                ResourceReference rr = ResourceReference.create(res, beanView, "DOWNLOAD");
                Page.getCurrent().open(rr.getURL(), null);
            }
        }
    });
    downloadAttachmentBtn.setId(downloadAttachmentBtn.getCaption());

    Button closeWindowBtn = new Button("Close", new Button.ClickListener() {
        @Override
        public void buttonClick(ClickEvent event) {
            window.close();
        }
    });
    closeWindowBtn.setId(closeWindowBtn.getCaption());

    if (componentState.equals(ComponentState.EDITABLE)) {
        attachmentButtons.addComponent(fileUpload);
        attachmentButtons.addComponent(deleteAttachmentBtn);
    }

    if (componentState.equals(ComponentState.EDITABLE) || componentState.equals(ComponentState.READ_ONLY)) {
        attachmentButtons.addComponent(downloadAttachmentBtn);
    }

    attachmentButtons.addComponent(closeWindowBtn);

    form.addComponent(attachmentButtons);
    form.setMargin(true);
    //beanView.addComponent(form);
    return form;
}

From source file:org.azrul.langkuik.framework.customtype.attachment.AttachmentCustomTypeUICreator.java

private void createAtachmentList(final ListSelect attachmentList,
        final Collection<? extends CustomType> attachments, final Configuration config, final BeanView view,
        final FormLayout form) {

    attachmentList.setHeight(attachments.size() + 2, Sizeable.Unit.EM);
    attachmentList.setMultiSelect(true);
    VerticalLayout attachmentListCont = new VerticalLayout();
    attachmentListCont.setCaption("Attachments");
    attachmentListCont.addComponent(attachmentList);
    attachmentListCont.addLayoutClickListener(new LayoutEvents.LayoutClickListener() {

        @Override//from  ww w .j  a  v a 2 s .com
        public void layoutClick(LayoutEvents.LayoutClickEvent event) {
            if (event.getClickedComponent() == null) {
                return;
            }
            if (event.getClickedComponent().equals(attachmentList)) {
                Collection<AttachmentCustomType> attachments = (Collection<AttachmentCustomType>) attachmentList
                        .getValue();
                if (!attachments.isEmpty()) {
                    AttachmentCustomType attachment = attachments.iterator().next();
                    final String fullPath = config.get("attachmentRepository") + File.separator
                            + attachment.getRelativeLocation() + File.separator + attachment.getFileName();
                    FileResource res = new FileResource(new File(fullPath));
                    view.setViewResource("DOWNLOAD", res);
                    ResourceReference rr = ResourceReference.create(res, view, "DOWNLOAD");

                    Page.getCurrent().open(rr.getURL(), null);
                }
            }
        }
    });
    form.addComponent(attachmentListCont);
}

From source file:org.inakirj.imagerulette.screens.DicePlayView.java

License:Open Source License

/**
 * Sets the layout.//from w w w .ja  v  a2 s.  c o  m
 */
private void setLayout() {
    mainLayout = new VerticalLayout();
    mainLayout.setWidth(100, Unit.PERCENTAGE);
    FileResource resource = new FileResource(new File(
            VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "/WEB-INF/image/nope.png"));
    Image none = new Image("", resource);
    randomImgToBeReplaced = none;
    randomImgToBeReplaced.setWidth(78, Unit.PIXELS);
    randomImgToBeReplaced.setHeight(81, Unit.PIXELS);
    randomImgToBeReplaced.addStyleName("random-image");

    imageLayout = new HorizontalLayout();
    imageLayout.addComponent(randomImgToBeReplaced);
    imageLayout.setWidth(100, Unit.PERCENTAGE);
    imageLayout.setComponentAlignment(randomImgToBeReplaced, Alignment.TOP_CENTER);

    Button rollBtn = new Button();
    rollBtn.addClickListener(e -> onPickABallClick());
    rollBtn.setWidth(25, Unit.PERCENTAGE);
    rollBtn.setHeight(50, Unit.PIXELS);
    rollBtn.setIcon(FontAwesome.CUBE);
    rollBtn.addStyleName("dice-button-roll");

    mainLayout.addComponent(imageLayout);
    mainLayout.addComponent(rollBtn);
    mainLayout.setComponentAlignment(rollBtn, Alignment.BOTTOM_CENTER);

    calculateStats();
    addComponent(mainLayout);
}

From source file:org.investovator.ui.main.components.GameDetailsView.java

License:Open Source License

public Image getImage() {
    String iconName = "game_icon.jpg";
    GameModes imageMode = null;/*from w  w  w  . jav  a 2  s  .  com*/

    if (gameInstance != null) {
        imageMode = controller.getGameMode(gameInstance);
    } else if (gameMode != null) {
        imageMode = gameMode;
    }

    if (imageMode != null) {
        switch (imageMode) {
        case AGENT_GAME:
            iconName = "agent2.jpg";
            break;
        case NN_GAME:
            iconName = "prediction_based.jpg";
            break;
        case PAYBACK_ENG:
            iconName = "game_icon.jpg";
            break;
        }
    }

    FileResource resource = new FileResource(new File(ConfigHelper.getImagePath() + iconName));
    Image img = new Image(null, resource);
    img.setHeight("50px");
    img.setWidth("50px");
    return img;
}

From source file:org.lunifera.examples.vaaclipse.demo1.e4.editors.ImageView.java

License:Open Source License

@Inject
public ImageView(VerticalLayout container, MInputPart inputPart) {
    super(inputPart.getInputURI());
    embedded = new Embedded();
    container.addComponent(embedded);/*from w  w  w  . j a v  a  2 s. c om*/
    container.setComponentAlignment(embedded, Alignment.MIDDLE_CENTER);
    container.setSizeFull();

    embedded.setSource(new FileResource(getFile()));
}

From source file:org.lunifera.examples.vaaclipse.demo1.e4.editors.TasksEditor.java

License:Open Source License

private Resource findImage(String name) {
    return new FileResource(Demo1Activator.findImage(name));
}

From source file:org.semanticsoft.vaaclipsedemo.cassandra.app.editors.ImageView.java

License:Open Source License

@Inject
public ImageView(VerticalLayout container, MInputPart inputPart) {
    super(inputPart.getInputURI());
    embedded = new Embedded();
    container.addComponent(embedded);//from   w  w w.  j  av a 2s  . c o m
    container.setComponentAlignment(embedded, Alignment.MIDDLE_CENTER);

    embedded.setSource(new FileResource(getFile()));
}

From source file:org.vaadin.alump.fancylayouts.demo.ImageDemo.java

License:Apache License

private List<Resource> getImageResources() {
    List<Resource> list = new ArrayList<Resource>();
    list.add(new ExternalResource("http://misc.siika.fi/fancy-demo1.jpg"));
    list.add(new ExternalResource("http://misc.siika.fi/fancy-demo2.jpg"));
    list.add(new ExternalResource("http://misc.siika.fi/fancy-demo3.jpg"));

    // Image is only added if present in file system. Will not be there
    // unless manually added!
    File fileResImage = new File("/tmp/test.png");
    if (fileResImage.exists()) {
        list.add(new FileResource(fileResImage));
    }/* w w w .  jav  a  2  s .co  m*/
    return list;
}

From source file:tad.grupo7.ccamistadeslargas.AdminIndexView.java

public AdminIndexView() {
    setMenuCaption("CCAmistadesLargas - ADMIN");
    String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath()
            + "/WEB-INF/wallpaper2.jpg";
    FileResource resource = new FileResource(new File(basepath));
    Image image = new Image(null, resource);
    image.setSizeFull();/*from ww w . j  av  a  2 s  .c o  m*/
    addComponent(image);
    addMenuItem("Listado", FontAwesome.LIST, () -> {
        removeAllComponents();
        addComponent(new ListadoLayout());
    });
    addMenuItem("Grafica", FontAwesome.PIE_CHART, () -> {
        removeAllComponents();
        addComponent(new GraficaLayout());
    });
    addMenuItem("Cerrar sesin", FontAwesome.POWER_OFF, () -> {
        Session.destroy();
        UI.getCurrent().getNavigator().navigateTo("");
    });
}

From source file:tad.grupo7.ccamistadeslargas.IndexView.java

public IndexView() {
    setMenuCaption("CCAmistadesLargas");
    String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath()
            + "/WEB-INF/wallpaper2.jpg";
    FileResource resource = new FileResource(new File(basepath));
    Image image = new Image(null, resource);
    image.setSizeFull();/*from w  w w . j a  va 2  s  .c om*/
    addComponent(image);
    addMenuItem("Perfil", FontAwesome.USER, () -> {
        removeAllComponents();
        addComponent(new PerfilLayout());
    });
    addMenuItem("Eventos", FontAwesome.COMPASS, () -> {
        removeAllComponents();
        addComponent(new EventosLayout());
    });
    addMenuItem("Amigos", FontAwesome.USERS, () -> {
        removeAllComponents();
        addComponent(new AmigosLayout());
    });
    addMenuItem("Cerrar sesin", FontAwesome.POWER_OFF, () -> {
        Session.destroy();
        UI.getCurrent().getNavigator().navigateTo("");
    });
}