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 de.fatalix.timeline.web.root.block; import com.vaadin.server.ExternalResource; import com.vaadin.shared.ui.datefield.Resolution; import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.ui.Button; import com.vaadin.ui.DateField; import com.vaadin.ui.GridLayout; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Label; import com.vaadin.ui.Layout; import com.vaadin.ui.Link; import com.vaadin.ui.TextArea; import com.vaadin.ui.TextField; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.Window; import de.fatalix.timeline.businesslogic.model.Timeline; import de.fatalix.timeline.businesslogic.model.TimelineEvent; import de.fatalix.timeline.web.root.RootViewPresenter; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.List; import javax.annotation.PostConstruct; import javax.enterprise.util.AnnotationLiteral; import javax.inject.Inject; import org.vaadin.jouni.animator.AnimatorProxy; import org.vaadin.jouni.animator.shared.AnimType; /** * * @author Fatalix */ public class TimelineConfigLayout extends VerticalLayout implements EventDeleteListener { @Inject private javax.enterprise.event.Event<TimelineCallback.SaveTimeline> saveEvent; @Inject private javax.enterprise.event.Event<TimelineCallback.LoadTimeline> loadEvent; @Inject private javax.enterprise.event.Event<TimelineCallback.RemoveTimelineEvent> removeEvent; @Inject private javax.enterprise.event.Event<TimelineCallback.ExportTimeline> exportEvent; private Button saveTimeline; private Button exportTimeline; private Label status; private Link timelineURL; private TextField headline; private TextArea text; private DateField startDate; private Timeline data; private List<TimelineEventLayout> eventLayouts; private GridLayout eventGridContainer; private AnimatorProxy proxy; @PostConstruct private void postInit() { HorizontalLayout topLayout = new HorizontalLayout(); topLayout.addComponents(createLeftSide(), createRightSide()); eventGridContainer = new GridLayout(3, 1); eventGridContainer.setSizeFull(); proxy = new AnimatorProxy(); this.addStyleName("timeline-config-layout"); this.setMargin(true); this.setSpacing(true); this.addComponents(proxy, topLayout, eventGridContainer); } private Layout createLeftSide() { headline = new TextField(); headline.setInputPrompt("Your timeline headline..."); headline.setColumns(15); headline.setNullSettingAllowed(true); headline.addStyleName("timeline"); text = new TextArea(); text.setInputPrompt("Your timeline description..."); text.setColumns(30); text.setRows(10); text.setNullSettingAllowed(true); startDate = new DateField(null, new Date()); startDate.setDateFormat("dd-MM-yyyy"); startDate.setResolution(Resolution.DAY); Button createEventBtn = new Button("Add Event", new Button.ClickListener() { @Override public void buttonClick(Button.ClickEvent event) { TimelineEvent newEvent = new TimelineEvent(); newEvent.setTimeline(data); newEvent.setStartDate(new Date()); data.getEvents().add(newEvent); addTimelineEvent(newEvent); } }); return new VerticalLayout(headline, text, startDate, createEventBtn); } private Layout createRightSide() { status = new Label("STATUS"); timelineURL = new Link("Timeline Link", new ExternalResource("http://")); timelineURL.setTargetName("_blank"); saveTimeline = new Button("Save Timeline", new Button.ClickListener() { @Override public void buttonClick(Button.ClickEvent event) { saveEvent.select(new AnnotationLiteral<RootViewPresenter.Save>() { }).fire(new TimelineCallback.SaveTimeline() { @Override public void showResult(String message) { status.setValue(message); } @Override public Timeline getTimelineData() { data.setHeadline(headline.getValue()); data.setText(text.getValue()); data.setStartDate(startDate.getValue()); Collection<TimelineEvent> events = new ArrayList<>(); for (TimelineEventLayout eventLayout : eventLayouts) { events.add(eventLayout.getTimelineEventData()); } data.setEvents(events); return data; } }); loadEvent.select(new AnnotationLiteral<RootViewPresenter.Load>() { }).fire(new TimelineCallback.LoadTimeline() { @Override public void loadTimeline(Timeline data) { loadTimelineData(data); } @Override public int getTimelineId() { return data.getId(); } }); } }); exportTimeline = new Button("export", new Button.ClickListener() { @Override public void buttonClick(Button.ClickEvent event) { exportEvent.select(new AnnotationLiteral<RootViewPresenter.Export>() { }).fire(new TimelineCallback.ExportTimeline() { @Override public int getTimelineId() { return data.getId(); } @Override public void showJsonResult(String json) { Window dialog = new Window("JSON Format", new Label(json, ContentMode.PREFORMATTED)); dialog.setModal(true); UI.getCurrent().addWindow(dialog); } }); } }); return new VerticalLayout(saveTimeline, status, timelineURL, exportTimeline); } public void loadTimelineData(Timeline data) { this.data = data; headline.setValue(data.getHeadline()); text.setValue(data.getText()); startDate.setValue(data.getStartDate()); timelineURL.setResource(new ExternalResource("#!timeline/" + data.getId())); if (eventLayouts == null) { eventLayouts = new ArrayList<>(); } eventLayouts.clear(); eventGridContainer.removeAllComponents(); for (TimelineEvent eventData : data.getEvents()) { addTimelineEvent(eventData); } } public Timeline getTimelineData() { if (data == null) { data = new Timeline(); } data.setHeadline(headline.getValue()); data.setText(text.getValue()); data.setStartDate(startDate.getValue()); Collection<TimelineEvent> events = new ArrayList<>(); for (TimelineEventLayout eventLayout : eventLayouts) { events.add(eventLayout.getTimelineEventData()); } data.setEvents(events); return data; } private void addTimelineEvent(TimelineEvent eventData) { TimelineEventLayout eventLayout = new TimelineEventLayout(); eventLayout.addListener(this); eventLayout.loadTimelineEventData(eventData); eventLayouts.add(eventLayout); if (eventLayouts.size() % 2 == 0) { eventLayout.addStyleName("even"); } else { eventLayout.addStyleName("uneven"); } eventGridContainer.addComponent(eventLayout); proxy.animate(eventLayout, AnimType.FADE_IN).setDuration(500).setDelay(100); } @Override public void deleteEvent(TimelineEventLayout eventLayout) { eventLayouts.remove(eventLayout); eventGridContainer.removeAllComponents(); final TimelineEvent eventData = eventLayout.getTimelineEventData(); if (eventData.getId() != 0) { removeEvent.fire(new TimelineCallback.RemoveTimelineEvent() { @Override public TimelineEvent getEvent() { return eventData; } }); } for (int i = 0; i < eventLayouts.size(); i++) { TimelineEventLayout eLayout = eventLayouts.get(i); if ((i + 1) % 2 == 0) { eLayout.removeStyleName("uneven"); eLayout.addStyleName("even"); } else { eLayout.removeStyleName("even"); eLayout.addStyleName("uneven"); } eventGridContainer.addComponent(eLayout); } } @Override public void changedEvent(TimelineEventLayout eventLayout) { } }