/* * This file is part of gwt-cal * Copyright (C) 2009 Scottsdale Software LLC * * gwt-cal is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see

*

* The key responsibilities of the AppointmentManager are:

* * @author Brad Rydzewski * @author Carlos D. Morales */ public class AppointmentManager { /** * A reference to the "currently selected appointment". Will be * null when no currently selected appointment has been set. */ private Appointment selectedAppointment = null; /** * A reference to the most recent appointment to receive * a mouse over event. */ private Appointment hoveredAppointment = null; /** * A copy of the last appointment that was updated, * prior to the update taking place. */ private Appointment rollbackAppointment = null; /** * A reference to the last appointment that was updated. */ private Appointment committedAppointment = null; /** * The collection of appointments to be maintained by this * AppointmentManager. */ private ArrayList appointments = new ArrayList(); /** * Internal state flag indicating whether the collection of appointments * needs to be sorted. */ private boolean sortPending = true; /** * Returns the collection of appointments that this AppointmentManager * maintains. Warning: this method returns a modifiable * reference to the internally managed list of appointments; client code * might break the invariants that this AppointmentManager * enforces if it performs any operations that modify the returned * collection. * * @return The appointments managed by this AppointmentManager. */ public List getAppointments() { return appointments; } /** * Adds an appointment to the collection of appointments maintained by this * ApplicationManager. * * @param appt The appointment to be made part of this manager's managed * collection */ public void addAppointment(Appointment appt) { if (appt != null) { appointments.add(appt); sortPending = true; } } /** * Adds multiple appointments to the collection maintained by this * ApplicationManager. * * @param appointments The appointments that will be made part of this * manager's managed collection */ public void addAppointments(List appointments) { if (appointments != null) { for (Appointment appointment : appointments) { addAppointment(appointment); } } } /** * Removes the appointment from this manager's managed * collection. * * @param appointment The appointment to remove */ public void removeAppointment(Appointment appointment) { if (appointment != null) { boolean wasRemoved = appointments.remove(appointment); if (wasRemoved) { sortPending = true; } //I'd rather have the client keep the reference to the selected one if they need it than having here //a reference to a thing I no longer should know about... if (hasAppointmentSelected() && getSelectedAppointment().equals(appointment)) { selectedAppointment = null; } } } /** * Removes the "currently selected" appointment from this * manager's collection. */ public void removeCurrentlySelectedAppointment() { if (hasAppointmentSelected()) { removeAppointment(getSelectedAppointment()); selectedAppointment = null; } } /** * Empties the collection of managed appointments. */ public void clearAppointments() { appointments.clear(); } /** * Sets the appointment that should be considered the "currently * selected" appointment. * * @param selectedAppointment The appointment to consider "currently * selected" */ public void setSelectedAppointment(Appointment selectedAppointment) { if (selectedAppointment != null && appointments.contains(selectedAppointment)) { this.selectedAppointment = selectedAppointment; } } /** * Indicates whether there is a "currently selected" appointment * at the moment. * * @return true if there is a currently selected appointment * for the collection managed by this component, false * otherwise */ public boolean hasAppointmentSelected() { return selectedAppointment != null; } /** * Returns the appointment in this manager's collection that is * "currently selected". * * @return The currently selected appointment */ public Appointment getSelectedAppointment() { return selectedAppointment; } /** * Sorts the collection of appointments by their natural order. */ public void sortAppointments() { if (sortPending) { Collections.sort(appointments); sortPending = false; } } /** * Moves the "currently selected" to the previous appointment in * the managed collection of this AppointmentManager.
The * "previous" appointment will be the appointment before the * currently selected appointment in the set of appointments (whether * ordered or not). *

*
Because this operation depends on a "currently selected * appointment", no previous appointment is considered to exist if * there is no "currently selected appointment." or it is the * first in the set. * * @return true if selecting the previous appointment was * successful, false no currently selected appointment * is set or the currently selected appointment is the first in the * collection. */ public boolean selectPreviousAppointment() { boolean moveSucceeded = false; if (getSelectedAppointment() != null) { int selectedApptIndex = getAppointments().indexOf(getSelectedAppointment()); Appointment prevAppt; if (selectedApptIndex > 0 && (prevAppt = getAppointments().get(selectedApptIndex - 1)) != null) { selectedAppointment = prevAppt; moveSucceeded = true; } } return moveSucceeded; } /** * Moves the "currently selected" to the next appointment in the * managed collection of this AppointmentManager.
The * "next" appointment will be the appointment after the currently * selected appointment in the set of appointments (whether ordered or * not). *

*
Because this operation depends on a "currently selected * appointment", no next appointment is considered to exist if there is * no "currently selected appointment or it is the last in the * set." * * @return true if selecting the previous appointment was * successful, false no currently selected appointment * is set or the currently selected appointment is the last in the * collection. */ public boolean selectNextAppointment() { boolean moveSucceeded = false; if (getSelectedAppointment() != null) { int selectedApptIndex = getAppointments().indexOf(getSelectedAppointment()); int lastIndex = getAppointments().size() - 1; Appointment nextAppt; if (selectedApptIndex < lastIndex && (nextAppt = getAppointments().get(selectedApptIndex + 1)) != null) { selectedAppointment = nextAppt; moveSucceeded = true; } } return moveSucceeded; } /** * Resets the "currently selected" appointment of this manager. *

If this manager has a currently selected appointment, the * appointment selected property will be set to * false and this manager's selectedAppointment * property will be set to null. */ public void resetSelectedAppointment() { if (hasAppointmentSelected()) { //selectedAppointment.setSelected(false); selectedAppointment = null; } } /** * Tells whether the passed appointment is the same one as the * one that is currently selected in this manager. * * @param appointment The appointment to test to be the same as the * currently selected * @return true if there is a currently selected appointment * and it is equal to the passed appointment */ public boolean isTheSelectedAppointment(Appointment appointment) { return hasAppointmentSelected() && selectedAppointment.equals( appointment); } public void commit() { rollbackAppointment = null; committedAppointment = null; } public void rollback() { //if the snapshot block is empty, we can do nothing if(rollbackAppointment==null && committedAppointment==null) return; //if there is no committed appointment, we assume // this was a delete operation. We re-add the appointment if (committedAppointment == null) { addAppointment(rollbackAppointment); //if there is no rollback appointment, we assume // this was an add or update operation. We remove the appointment } else if (rollbackAppointment == null) { removeAppointment(committedAppointment); //else, we assume this is an update } else { removeAppointment(committedAppointment); addAppointment(rollbackAppointment); } commit(); } public void setRollbackAppointment(Appointment appt) { sortPending = true; commit(); rollbackAppointment = appt; } public void setCommittedAppointment(Appointment appt) { sortPending = true; committedAppointment = appt; } public void resetHoveredAppointment() { this.hoveredAppointment = null; } public void setHoveredAppointment(Appointment hoveredAppointment) { this.hoveredAppointment = hoveredAppointment; } public Appointment getHoveredAppointment() { return hoveredAppointment; } }