/******************************************************************* Copyright (c) 2001-2010 Apprisant Technologies Inc. All rights reserved. Apprisant Technologies permits non-exclusive royalty free use, copying and modification of this demo program source code for the purposes of evaluating the Apprisant Toolkit or building applications that use it. This software and documentation is provided "AS IS". APPRISANT TECHNOLOGIES DISCLAIMS ANY REPRESENTATIONS AND WARRANTIES, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTY OF MERCHANTABILITY AND FITNESS OF THIS PRODUCT FOR ANY PARTICULAR PURPOSE. APPRISANT TECHNOLOGIES SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY ANY PARTY, INCLUDING LOST PROFITS, ARISING FROM THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ITS DERIVATIONS. ********************************************************************/ package schedule; import java.awt.*; import java.awt.geom.*; import java.io.*; import apprisant.diagram.*; import apprisant.schedule.*; /* The display element class for the Schedule demo. This class makes the data available in the format required by the model, and builds the arrows shown in the bookings. This class extends WeeklyElement, which is the display element class used by the WeeklyModel. The element factory that instantiates the display elements is a nested class at the end of this file, and is registered with the element model in the Schedule class. */ public class BookingElement extends WeeklyElement { // Display attribute to store the arrow static final String ARROW = "arrow"; // Geometry values for the arrows static final float ARROWHEAD_HEIGHT = 16; static final float ARROW_TOP = 2; static final float ARROW_LEFT = .25f * Schedule.DAY_WIDTH; static final float ARROW_RIGHT = .75f * Schedule.DAY_WIDTH; // Precalculated arrows for the sizes we will need, to be shared // by the elements. static GeneralPath ARROW_60 = makeArrow(60); static GeneralPath ARROW_90 = makeArrow(90); static GeneralPath ARROW_120 = makeArrow(120); public BookingElement (Object key, Object dataObject) { super(key, dataObject); } /** Data value required by WeeklyModel */ public int getPeriodIndex() { Booking booking = (Booking) getDataObject(); return (booking != null) ? booking.getDay().ordinal() : -1; } /** Data value required by WeeklyModel, in interval units (ie. hours) */ public double getStart() { Booking booking = (Booking) getDataObject(); return (booking != null) ? booking.getStartTime() - ScheduleData.HOUR_START : ScheduleData.HOUR_END; } /** Data value required by WeeklyModel, in interval units (ie. hours) */ public double getDuration() { Booking booking = (Booking) getDataObject(); return (booking != null) ? booking.getDuration() / 60.0 : 0; } /** Called by the model when the element is positioned or the data object is set or updated. This method is also overridden by WeeklyElement to calculate the rectangle representing the booking. */ @Override public void update() { if (getModel() == null || ! getModel().isValid()) return; Booking booking = (Booking) getDataObject(); if (booking == null) return; // Have WeeklyElement calculate the position and rectangle super.update(); // Set the arrow int duration = booking.getDuration(); if (duration <= 60) { setAttribute(ARROW, ARROW_60); } else if (duration <= 90) { setAttribute(ARROW, ARROW_90); } else { setAttribute(ARROW, ARROW_120); } } /** Helper method to construct the arrow for the given duration in minutes. The arrow will be displayed relative to the element's point, which WeeklyElement sets to the upper left corner of the rectangle. */ static GeneralPath makeArrow (int duration) { float height = (duration / 60f) * Schedule.HOUR_HEIGHT; GeneralPath p = new GeneralPath(); p.moveTo(ARROW_LEFT, ARROW_TOP); p.lineTo(ARROW_RIGHT, ARROW_TOP); p.lineTo(ARROW_RIGHT, height - ARROWHEAD_HEIGHT); p.lineTo(Schedule.DAY_WIDTH - Schedule.INSET, height - ARROWHEAD_HEIGHT); p.lineTo(Schedule.DAY_WIDTH / 2f, height - Schedule.INSET - 1); p.lineTo(Schedule.INSET, height - ARROWHEAD_HEIGHT); p.lineTo(ARROW_LEFT, height - ARROWHEAD_HEIGHT); p.closePath(); return p; } /* Element factory class to instantiate WeeklyElements. */ public static class Factory implements ElementFactory { public Factory() { } public BookingElement createElement (Object key, Object data) { return new BookingElement(key, data); } } }