Java tutorial
/* * Copyright (c) 2015 Berner Fachhochschule, Switzerland. * * Project Smart Reservation System. * * Distributable under GPL license. See terms of license at gnu.org. */ package ch.bfh.srs.gui.javafx; import ch.bfh.srs.srv.entity.Reservation; import ch.bfh.srs.srv.entity.Resource; import ch.bfh.srs.srv.service.ReservationService; import ch.bfh.srs.srv.service.ResourceService; import ch.bfh.srs.srv.service.ServiceFactory; import com.calendarfx.model.Calendar; import com.calendarfx.model.CalendarSource; import com.calendarfx.model.Entry; import com.calendarfx.util.CalendarFX; import impl.com.calendarfx.view.CalendarViewSkin; import javafx.application.Platform; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.ArrayList; import java.util.List; public class CalendarView extends com.calendarfx.view.CalendarView { private static Logger log = LoggerFactory.getLogger(CalendarView.class); public static final String LICENSEKEY_LOC = ".calendarfx/licensekey"; private static final Session session = Session.getInstance(); private static final ReservationService resSrv = ServiceFactory.instantiateService(ReservationService.class); public CalendarView() throws IOException { loadLicense(); List<Calendar> calendars = loadResourceCalendars(); CalendarSource myCalendarSource = new CalendarSource("My Calendars"); myCalendarSource.getCalendars().addAll(calendars); this.getCalendarSources().addAll(myCalendarSource); this.setRequestedTime(LocalTime.now()); this.setEntryFactory(param -> new MyEntryFactory()); Thread updateTimeThread = new Thread("Calendar: Update Time Thread") { @Override public void run() { Platform.runLater(() -> { Button btnSettings = new Button("settings"); btnSettings.setOnAction(event -> { SettingsView settingsView = new SettingsView(event); Stage stage = new Stage(); try { settingsView.start(stage); } catch (Exception e) { e.printStackTrace(); } }); // This is necessary to put a new button beside of the other controls CalendarViewSkin skin = (CalendarViewSkin) CalendarView.this.getSkin(); BorderPane node = (BorderPane) skin.getChildren().get(0); GridPane node1 = (GridPane) node.getChildren().get(0); HBox node2 = (HBox) node1.getChildren().get(0); node2.getChildren().add(btnSettings); }); while (true) { Platform.runLater(() -> { CalendarView.this.setToday(LocalDate.now()); CalendarView.this.setTime(LocalTime.now()); }); try { // update every 10 seconds sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } } }; updateTimeThread.setPriority(Thread.MIN_PRIORITY); updateTimeThread.setDaemon(true); updateTimeThread.start(); } private List<Calendar> loadResourceCalendars() { ResourceService resourceService = ServiceFactory.instantiateService(ResourceService.class); List<Resource> resources = resourceService.getResources(); List<Calendar> calendars = new ArrayList<>(); Calendar.Style[] styles = Calendar.Style.values(); int counter = 0; for (Resource resource : resources) { Calendar resourceCalendar = new Calendar(resource.getName()); List<Entry> entries = fetchReservations(resource.getIdResource()); entries.forEach(resourceCalendar::addEntry); calendars.add(resourceCalendar); resourceCalendar.setStyle(styles[counter]); if (styles.length <= counter) counter = 0; else counter++; } return calendars; } private List<Entry> fetchReservations(int reservationId) { ReservationService reservationService = ServiceFactory.instantiateService(ReservationService.class); List<Reservation> reservations = reservationService.getReservationsByResourceId(reservationId); List<Entry> result = new ArrayList<>(); for (Reservation reservation : reservations) { String reservationName = String.format("Reservation from %s %s", reservation.getUser().getLastname(), reservation.getUser().getSurname()); Entry e = new Entry(reservationName); LocalDateTime from = reservation.getFrom().toLocalDateTime(); LocalDateTime to = reservation.getTo().toLocalDateTime(); if (reservation.getFullDay()) { e.setFullDay(true); e.setStartDate(from.toLocalDate()); e.setEndDate(to.toLocalDate()); } else { e.setStartDate(from.toLocalDate()); e.setStartTime(from.toLocalTime()); e.setEndDate(to.toLocalDate()); e.setEndTime(to.toLocalTime()); } result.add(e); } return result; } private void loadLicense() throws IOException { String userHome = System.getProperty("user.home"); String pathToLicense = String.format("%s/%s", userHome, LICENSEKEY_LOC); Path path = Paths.get(pathToLicense); if (Files.exists(path)) { String license = new String(Files.readAllBytes(path)); CalendarFX.setLicenseKey(license); } else { log.warn("No license key detected, shutting down application..."); System.exit(0); } } private class MyEntryFactory extends Entry<Reservation> { public MyEntryFactory() { super(); DateTime startTime = getDateTime(this.getStartDate(), this.getStartTime()); DateTime endTime = getDateTime(this.getEndDate(), this.getEndTime()); resSrv.addReservation(session.getLoggedInUser().getIdUser(), startTime, endTime, 1, this.isFullDay()); //TODO: choose between different resources } } private DateTime getDateTime(LocalDate date, LocalTime time) { return new DateTime(date.getYear(), date.getMonthValue(), date.getDayOfMonth(), time.getHour(), time.getMinute()); } }