I've written logic that adds an event inside a Vaadin FullCalendar but it does not work.
This is my code so far. By clicking "update" I should be able to add an event to my calendar:
InMemoryEntryProvider<ResourceEntry> entryProvider = new InMemoryEntryProvider<>();
calendar.setEntryProvider(entryProvider);
//update or remove a calendar entry
HorizontalLayout entryControlsLayout = new HorizontalLayout();
entryControlsLayout.setSpacing(true);
entryControlsLayout.setAlignItems(Alignment.BASELINE);
Entry entryCustom = new Entry();
Select<String> selectTitle = new Select<>();
DatePicker datePickerStart = new DatePicker("inzio");
DatePicker datePickerEnd = new DatePicker("fine");
TimePicker timePicker = new TimePicker("Select Time");
TimePicker timePickerEnd = new TimePicker("Select Time");
datePickerStart.setValue(LocalDate.now());
datePickerEnd.setValue(LocalDate.now());
timePicker.setValue(LocalTime.now());
timePickerEnd.setValue(LocalTime.now());
selectTitle.setLabel("Ordina Come");
selectTitle.setItems("Primo più recente", "Rating: high to low", "Rating: low to high", "Price: high to low", "Price: low to high");
selectTitle.setValue("Primo più recente");
entryCustom.setTitle(selectTitle.getValue());
entryCustom.setColor("#ff3333");
entryCustom.setStartEditable(true);
entryCustom.setStart(datePickerStart.getValue().atTime(timePicker.getValue()));
entryCustom.setEnd(datePickerEnd.getValue().atTime(timePickerEnd.getValue()));
Button updateButton = new Button("Update", e -> {
entryCustom.setTitle(selectTitle.getValue());
entryCustom.setStart(datePickerStart.getValue().atTime(timePicker.getValue()));
entryCustom.setEnd(datePickerEnd.getValue().atTime(timePickerEnd.getValue()));
calendar.getEntryProvider().asInMemory().addEntries(entryCustom);
});
Button removeButton = new Button("Remove", e -> {
calendar.getEntryProvider().asInMemory().removeEntries(entryCustom);
});
entryControlsLayout.add(selectTitle, datePickerStart, datePickerEnd, timePicker, timePickerEnd, updateButton, removeButton );
and this is the style setting that I have:
calendar.setSizeFull();
calendar.setLocale(Locale.ITALIAN);
calendar.setFirstDay(DayOfWeek.MONDAY);
calendar.setTimezone(Timezone.UTC);
calendar.setNowIndicatorShown(true);
calendar.setNumberClickable(true);
calendar.setWeekNumbersVisible(false);
HorizontalLayout calendarLayout = new HorizontalLayout();
calendarLayout.setSizeFull();
calendarLayout.addClassName("calendar-layout");
calendarLayout.add(calendar);
add(entryControlsLayout, topControlsLayout, calendarLayout);
monthViewButton.addClickListener(event -> calendar.changeView(CalendarViewImpl.DAY_GRID_MONTH));
weekViewButton.addClickListener(event -> calendar.changeView(CalendarViewImpl.TIME_GRID_WEEK));
dayViewButton.addClickListener(event -> calendar.changeView(CalendarViewImpl.TIME_GRID_DAY));
setSizeFull();
setSpacing(true);
I've been trying to follow this but it doesn't make much sense to me.
now i understand better. to create a event by clicking on a date on the calendar:
private void showEditorDialog(Entry entryOnClick, boolean isNew, FullCalendar calendar) {
InMemoryEntryProvider<Entry> entryProvider = calendar.getEntryProvider().asInMemory();
Dialog editorDialog = new Dialog();
VerticalLayout layout = new VerticalLayout();
TextField titleField = new TextField("Titolo Evento");
titleField.setValue(entryOnClick.getTitle());
layout.add(titleField);
// Save button click listener
Button saveButton = new Button("Salva", event -> {
entryOnClick.setTitle(titleField.getValue());
if (isNew) {
entryProvider.addEntry(entryOnClick);
} else {
entryProvider.refreshItem(entryOnClick);
}
editorDialog.close();
entryProvider.refreshAll();
});
// Cancel button click listener
Button cancelButton = new Button("Cancella", event -> {
entryProvider.removeEntry(entryOnClick);
editorDialog.close();
entryProvider.refreshAll();
});
layout.add(saveButton, cancelButton);
editorDialog.add(layout);
editorDialog.open();
}
Then i created a separate class to handle the toolbar:
protected SubMenu initEditItems() {
SubMenu calendarItems = addItem("Crea Evento").getSubMenu();
MenuItem addRecurringItems;
MenuItem addSingleItem;
InMemoryEntryProvider<Entry> entryProviderToolbar = calendar.getEntryProvider().asInMemory();
if (onSamplesCreated != null) {
addSingleItem = calendarItems.addItem("Crea singolo Evento", event -> {
//event.getSource().setEnabled(false);
// Create a dialog to collect user input
Dialog dialog = new Dialog();
VerticalLayout layout = new VerticalLayout();
// Create Vaadin components for user input
TextField titleField = new TextField("Titolo Evento");
titleField.setRequiredIndicatorVisible(true);
DatePicker startDatePicker = new DatePicker("Inizio Evento");
startDatePicker.setRequiredIndicatorVisible(true);
TimePicker startTimePicker = new TimePicker("Orario Inizio Evento");
startTimePicker.setValue(LocalTime.MIN);
DatePicker endDatePicker = new DatePicker("Fine evento");
endDatePicker.setValue(LocalDate.now());
TimePicker endTimePicker = new TimePicker("Orario Fine Evento");
endTimePicker.setValue(LocalTime.MAX);
// Add components to the layout
layout.add(titleField, startDatePicker, startTimePicker, endDatePicker, endTimePicker);
// Create save and cancel buttons
Button saveButton = new Button("Salva", saveEvent -> {
//validate required fields
if (titleField.getValue().isEmpty() || startDatePicker.getValue() == null) {
Notification.show("Pervafore inserisci tutti i campi obbligatori", 3000, Notification.Position.MIDDLE);
return;
}
// Create a new entry with the user's input
Entry entry = new Entry();
entry.setStart(startDatePicker.getValue().atTime(startTimePicker.getValue()));
entry.setEnd(endDatePicker.getValue().atTime(endTimePicker.getValue()));
entry.setTitle(titleField.getValue());
// Add the entry to the calendar and refresh the view
entryProviderToolbar.addEntry(entry);
entryProviderToolbar.refreshAll();
onSamplesCreated.accept(Collections.singletonList(entry));
Notification.show("Added a single entry");
// Close the dialog
dialog.close();
});
saveButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
Button cancelButton = new Button("Cancella", cancelEvent -> {
dialog.close();
});
// Add buttons to the layout
layout.add(saveButton, cancelButton);
// Add layout to the dialog and open it
dialog.add(layout);
dialog.open();
});
this is how you can create an event outside and inside the calendar