Java tutorial
/** * Copyright 2015 Alessandro Daniele, Fabio Cesarato, Andrea Giraldin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.salsaw.msalsa; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import com.salsaw.msalsa.algorithm.exceptions.SALSAException; import com.salsaw.msalsa.cli.SalsaParameters; import com.salsaw.msalsa.config.ConfigurationManager; import com.vaadin.annotations.AutoGenerated; import com.vaadin.data.util.BeanItem; import com.vaadin.navigator.View; import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent; import com.vaadin.server.Page; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.CustomComponent; import com.vaadin.ui.GridLayout; import com.vaadin.ui.Label; import com.vaadin.ui.Notification; import com.vaadin.ui.Upload; import com.vaadin.ui.Upload.Receiver; import com.vaadin.ui.Upload.SucceededEvent; import com.vaadin.ui.Upload.SucceededListener; public class HomePageView extends CustomComponent implements IHomePageView, View { /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */ /** * */ private static final long serialVersionUID = 1L; @AutoGenerated private GridLayout mainLayout; @AutoGenerated private Upload uploadInput; @AutoGenerated private Label title; /** * The constructor should first build the main layout, set the * composition root and then do any custom initialization. * * The constructor will not be automatically regenerated by the * visual editor. * @param navigator */ public HomePageView(SalsaParameters salsaParameters) { if (salsaParameters == null) { throw new IllegalArgumentException("salsaParameters"); } buildMainLayout(); // Create form with salsa parameters BeanItem<SalsaParameters> salsaParametersBeanItem = new BeanItem<SalsaParameters>(salsaParameters); SalsaParametersForm salsaParametersForm = new SalsaParametersForm(salsaParametersBeanItem); Button toogleSalsaParametersButton = new Button("Show/Hide M-SALSA Parameters"); toogleSalsaParametersButton.addClickListener(new Button.ClickListener() { /** * */ private static final long serialVersionUID = 1L; public void buttonClick(ClickEvent event) { salsaParametersForm.setVisible(!salsaParametersForm.isVisible()); } }); toogleSalsaParametersButton.setWidth("-1px"); toogleSalsaParametersButton.setHeight("-1px"); mainLayout.addComponent(toogleSalsaParametersButton); mainLayout.setComponentAlignment(toogleSalsaParametersButton, new Alignment(48)); salsaParametersForm.setWidth("-1px"); salsaParametersForm.setHeight("-1px"); mainLayout.addComponent(salsaParametersForm); mainLayout.setComponentAlignment(salsaParametersForm, new Alignment(48)); setCompositionRoot(mainLayout); // TODO add user code here // Implement both receiver that saves upload in a file and // listener for successful upload class AligmentUploader implements Receiver, SucceededListener { /** * */ private static final long serialVersionUID = 1L; public File file; public OutputStream receiveUpload(String filename, String mimeType) { // Create upload stream FileOutputStream fos = null; // Stream to write to try { // Load server configuration String tmpFolder = ConfigurationManager.getInstance().getServerConfiguration() .getTemporaryFilePath(); // Open the file for writing. file = Paths.get(tmpFolder, filename).toFile(); fos = new FileOutputStream(file); } catch (final java.io.FileNotFoundException e) { new Notification("Could not open file<br/>", e.getMessage(), Notification.Type.ERROR_MESSAGE) .show(Page.getCurrent()); return null; } return fos; // Return the output stream to write to } public void uploadSucceeded(SucceededEvent event) { try { for (IHomePageListener listener : listeners) { listener.buttonClick(file); } } catch (SALSAException | IOException | InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); new Notification("ERROR: ", e.getMessage(), Notification.Type.ERROR_MESSAGE) .show(Page.getCurrent()); } } } ; AligmentUploader receiver = new AligmentUploader(); uploadInput.setReceiver(receiver); uploadInput.addSucceededListener(receiver); } /* Only the presenter registers one listener... */ List<IHomePageListener> listeners = new ArrayList<IHomePageListener>(); @Override public void addListener(IHomePageListener listener) { listeners.add(listener); } @AutoGenerated private GridLayout buildMainLayout() { // common part: create layout mainLayout = new GridLayout(); mainLayout.setImmediate(false); mainLayout.setWidth("100%"); mainLayout.setHeight("100%"); mainLayout.setMargin(false); mainLayout.setRows(4); // top-level component properties setWidth("100.0%"); setHeight("100.0%"); // title title = new Label(); title.setImmediate(false); title.setWidth("-1px"); title.setHeight("-1px"); title.setValue("M-SALSA"); mainLayout.addComponent(title, 0, 0); mainLayout.setComponentAlignment(title, new Alignment(48)); // uploadInput uploadInput = new Upload(); uploadInput.setCaption("Upload and process align input"); uploadInput.setImmediate(false); uploadInput.setWidth("-1px"); uploadInput.setHeight("-1px"); mainLayout.addComponent(uploadInput, 0, 1); mainLayout.setComponentAlignment(uploadInput, new Alignment(48)); return mainLayout; } @Override public void enter(ViewChangeEvent event) { // TODO Auto-generated method stub } }