/******************************************************************* 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.event.*; import java.util.*; import javax.swing.*; import apprisant.diagram.*; import apprisant.schedule.*; import apprisant.diagram.env.*; import apprisant.diagram.awtext.Dimension2D; /* The main class of the Schedule demo. This class assembles the user interface. This demo uses the toolkit's Weekly Schedule model to format the schedule chart. The demo has its own element class, BookingElement. The model instantiates a BookingElement object for each data object using the BookingElement's factory class. */ public class Schedule extends JApplet { static final int DAY_WIDTH = 100; static final int HOUR_HEIGHT = 32; static final int INSET = 1; static final String[] PERIOD_LABELS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; Diagram diagram; ScheduleData dataModel; DiagramSelectionModel selectionModel; /* Applet initialization method assembles the user interface. */ public void init() { setLayout(new BorderLayout()); // Create and configure the model WeeklyModel model = new WeeklyModel(); model.setPeriodLabels(PERIOD_LABELS); String[] hourLabels = new String[ScheduleData.HOUR_END - ScheduleData.HOUR_START]; for (int h = ScheduleData.HOUR_START; h < ScheduleData.HOUR_END; h++) { String s = (h < 12) ? "AM" : "PM"; hourLabels[h - ScheduleData.HOUR_START] = String.valueOf(((h-1) % 12) + 1) + s; } model.setIntervalLabels(hourLabels); // Set the number of intervals explicitly because it would // default to 1 less than the number of labels model.setIntervals(hourLabels.length); model.setPeriodSize(DAY_WIDTH); model.setIntervalSize(HOUR_HEIGHT); // so the booking outlines don't overlap the chart lines model.setItemInset(INSET); // for rounded rectangles model.setItemArc(10); // because the chart is outlined with stroke width 2 model.setFramePadding(1); // Set the factory to instantiate our element class for each // data object. model.setElementFactory(new BookingElement.Factory()); try { // Properties file sets rendering hints AGEnvironment.loadProperties("schedule/schedule.properties"); // Create the diagram diagram = new Diagram("Schedule", model); diagram.setBackground(new Color(255,253,252)); ViewManager viewMgr = diagram.getViewManager(); viewMgr.setMargin(new Dimension(20,20)); viewMgr.setScrollIncrements (new Dimension2D.Float(DAY_WIDTH/2, HOUR_HEIGHT)); selectionModel = diagram.getSelectionModel(); // Renderers diagram.readRenderers("schedule/schedule.rdr"); } catch (AGException ae) { AGLog.log("Schedule", "initializing", AGLog.ERROR, ae); return; } // Data model dataModel = new ScheduleData(); dataModel.initBookings(); diagram.setDataModel(dataModel); // Add the diagram to the parent add(new JScrollPane(diagram), BorderLayout.CENTER); // Button panel Box buttonPanel = buildButtonPanel(); add(buttonPanel, BorderLayout.SOUTH); } Box buildButtonPanel() { Box buttonPanel = new Box(BoxLayout.X_AXIS); JButton addButton = new JButton("Add"); buttonPanel.add(Box.createGlue()); buttonPanel.add(addButton); buttonPanel.add(Box.createHorizontalStrut(5)); addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { dataModel.addBooking(); } }); JButton removeButton = new JButton("Remove"); buttonPanel.add(removeButton); buttonPanel.add(Box.createHorizontalStrut(5)); removeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { Set keys = selectionModel.getSelection(); dataModel.removeBookings(keys); } }); JButton changeButton = new JButton("Change"); buttonPanel.add(changeButton); buttonPanel.add(Box.createGlue()); changeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { dataModel.changeBookings(); } }); return buttonPanel; } public static void main (String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JApplet applet = new Schedule(); applet.init(); JFrame frame = new JFrame("Schedule Demo"); frame.setLocation(100, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(applet, BorderLayout.CENTER); frame.pack(); frame.setVisible (true); } }); } }