de.fatalix.timeline.web.root.block.TimelineEventLayout.java Source code

Java tutorial

Introduction

Here is the source code for de.fatalix.timeline.web.root.block.TimelineEventLayout.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 de.fatalix.timeline.web.root.block;

import com.vaadin.event.LayoutEvents;
import com.vaadin.shared.ui.datefield.Resolution;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.DateField;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import de.fatalix.timeline.businesslogic.model.TimelineEvent;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Fatalix
 */
public class TimelineEventLayout extends VerticalLayout {
    private TextField headline;
    private TextArea text;
    private DateField startDate;
    private DateField endDate;
    private TimelineEvent data;
    private VerticalLayout deleteButton;

    private final ArrayList<EventDeleteListener> listeners = new ArrayList<>();

    public TimelineEventLayout() {
        headline = new TextField();
        headline.setInputPrompt("Your event headline...");
        headline.setColumns(31);
        headline.setNullRepresentation("");
        text = new TextArea();
        text.setInputPrompt("Your event description...");
        text.setColumns(31);
        text.setRows(10);
        text.setNullRepresentation("");

        startDate = new DateField(null, new Date());
        startDate.setDateFormat("dd-MM-yyyy");
        startDate.setResolution(Resolution.DAY);
        startDate.setDescription("Set the start date of an event.");

        endDate = new DateField();
        endDate.setDateFormat("dd-MM-yyyy");
        endDate.setResolution(Resolution.DAY);
        endDate.setDescription("Set the end date of an event. Optional!");

        deleteButton = new VerticalLayout();
        deleteButton.setWidth(16, Unit.PIXELS);
        deleteButton.setHeight(16, Unit.PIXELS);
        deleteButton.addStyleName("delete");
        deleteButton.setDescription("Click to remove this event");
        deleteButton.addLayoutClickListener(new LayoutEvents.LayoutClickListener() {

            @Override
            public void layoutClick(LayoutEvents.LayoutClickEvent event) {
                for (EventDeleteListener listener : listeners) {
                    listener.deleteEvent(TimelineEventLayout.this);
                }
            }
        });

        HorizontalLayout dateLayout = new HorizontalLayout();
        dateLayout.setWidth(100, Unit.PERCENTAGE);
        dateLayout.addComponents(startDate, endDate, deleteButton);
        dateLayout.setComponentAlignment(deleteButton, Alignment.MIDDLE_RIGHT);
        this.setMargin(true);
        this.setSpacing(true);
        this.addComponent(headline);
        this.addComponent(text);

        this.addComponent(dateLayout);
    }

    public void loadTimelineEventData(TimelineEvent data) {
        this.data = data;
        headline.setValue(data.getHeadline());
        text.setValue(data.getText());
        startDate.setValue(data.getStartDate());
        endDate.setValue(data.getEndDate());
    }

    public TimelineEvent getTimelineEventData() {

        data.setHeadline(headline.getValue());
        data.setText(text.getValue());
        data.setStartDate(startDate.getValue());
        data.setEndDate(endDate.getValue());

        return data;
    }

    public void addListener(EventDeleteListener listener) {
        listeners.add(listener);
    }

    public void removeListener(EventDeleteListener listener) {
        listeners.remove(listener);
    }
}