CSC309-S15-L2 Slide 46 XIV. Package java.awt Lower-level support for swing . • Color • Component • Event • Graphics2D • GridLayout • Image
CSC309-S15-L2 Slide 47 XV. Designing GUIs with Swing. A. Above list used widely in 309. B. Figure 2 shows a typical menubar.
Operating-system-maintained banner, except title is set using JFrame.setTitle JFrame Calendar Tool JMenuBar File Edit Schedule View Admin Options Help Box, containing a horizontal strut for blank spacing JMenu Appointment ... Meeting ... JMenus JMenuItems Task ... Event ... Categories ... Item JSeparator Day Week -> Table Month Lists Year Next Previous Appointments Goto Date ... Meetings Tasks Lists -> Events Filter -> All Items Custom ... Other User ... Windows -> Edit ... Windowing Mode ...
CSC309-S15-L3-4 Slide 49 Swing, cont’d C. Figure 3 shows typical dialog.
CSC309-S15-L3-4 Slide 50 JFrame Schedule an Appointment JLabels JTextFields Title: Date: Start Time: hr min End Date: Duration: JCheckBoxes JTextField (disabled) S M T W Th F S (disabled) weekly Recurring? Interval: public Category: Security: JCheckBox must Location: Priority: JComboBox on screen Remind? 15 minutes before Details: JTextArea JScrollBar in a JScrollPane OK Clear Cancel JButtons
CSC309-S15-L3-4 Slide 51 Swing, cont’d D. Figure 4 dialog layout with Boxes
CSC309-S15-L3-4 Slide 52 Schedule an Appointment Horizontal Vertical Box Box Title: Date: Start Time: hr min End Date: Duration: S M T W Th F S weekly Recurring? Interval: public Category: Security: JPanel must Location: Priority: in the content pane on screen Remind? 15 minutes before of the JFrame Details: OK Clear Cancel
CSC309-S15-L3-4 Slide 53 XVI. View class naming conventions. A. Standard name suffixes. B. For classes that inherit from mvp.View . C. Suffixes indicate general usage
CSC309-S15-L3-4 Slide 54 View class naming, cont’d Suffix Example UI ScheduleUI Dialog ScheduleAppointmentDialog Editor CategoriesEditor Display MonthlyAgendaDisplay ButtonListener OKScheduleAppointmentButtonListener Panel SchedulingOptionsPanel
CSC309-S15-L3-4 Slide 55 XVII. Coordination of Model, View classes . A. Parallel decomposition. 1. Model and View classes at top of inheri- tance hierarchy. 2. Tool-specific model and view classes inherit from these.
CSC309-S15-L3-4 Slide 56 M/V Coordination, cont’d B. High-level class decomposition follows func- tional hierarchy . C. Most important that functional hierarchy makes sense.
CSC309-S15-L3-4 Slide 57 XVIII. Example of high-level Model/View class diagram.
CSC309-S15-L3-4 Slide 58 Model CalendarTool Schedule View File CalendarDB Edit Categories Model Schedule getView View Schedules setView Admin scheduleAppointment Options scheduleMeeting CalendarDB confirmMeeting scheduleTask CalendarTool scheduleEvent getFile getEdit getSchedule . . . getCalView getAdmin getOptions getHelp View CalendarToolUI ScheduleUI Model FileUI ScheduleMenu Screen EditUI ScheduleAppointmentDialog Window ScheduleUI ScheduleMeetingDialog ViewUI ConfirmMeetingDialog View AdminUI ScheduleTaskDialog run OptionsUI ScheduleEventDialog compose CategoriesEditor show CalendarToolUI hide constructSubviews ScheduleUI setModel compose compose getModel composeMenuBar getScheduleAppointmentDialog getWindow composeHelpSpacing getScheduleMeetingDialog isShown getConfirmMeetingDialog isEditable getScheuleTaskDialog setEditable getSchduleEventDialog getCategoriesEditor . . .
CSC309-S15-L3-4 Slide 59 M/V class diagram, cont’d A. Model and View are the root of the inheri- tance hierarchy. B. Inheriting from these are top-level model and view classes. C. Below top-level are submodels and subviews . D. These also inherit from Model and View .
CSC309-S15-L3-4 Slide 60 XIX. High-level M/V function diagram. main Screen CalendarTool CalendarToolUI calToolUI.compose calTool.setView caltoolUI.show calToolUI.run
CSC309-S15-L3-4 Slide 61 second-level model constructor functions File Edit top-level Schedule constructor functions second-level view View constructor functions from Screen main Admin FileUI CalendarTool Options EditUI Help ScheduleUI ViewUI CalendarToolUI super AdminUI JMenuBar constructSubviews OptionsUI HelpUI . . .
CSC309-S15-L3-4 Slide 62 . . . calToolUI.compose mvp.Window composeMenuBar FileUI.compose setMenuBar EditUI.compose calTool.setView setTitle ScheduleUI.compose pack ViewUI.compose AdminUI.compose GUI layout functions (bold-borders are OptionsUI.compose library functions) HelpUI.compose second-level view compose functions display the GUI on the screen caltoolUI.show execute the GUI calToolUI.run toolkit event loop (noop in Java)
CSC309-S15-L3-4 Slide 63 High-level function diagram, cont’d A. First three calls are top-level constructors. 1. Screen initializes GUI. 2. CalendarTool constructs models. 3. CalendarMenuUI constructs views.
CSC309-S15-L3-4 Slide 64 Function diagram, cont’d B. CalendarToolUI.compose performs UI layout. 1. Subfunctions layout various UI pieces. 2. Functions with bold borders are in Java and 309 libraries.
CSC309-S15-L3-4 Slide 65 Function diagram, cont’d C. CalendarTool.setView sets model to point to view. 1. Model and view mutually refer. 2. Model constructed first, View constructor passed a Model reference. 3. Then Model.setView is called. 4. Enables two-way communication.
CSC309-S15-L3-4 Slide 66 Function diagram, cont’d D. View.show inserts the view’s main win- dow into UI screen. E. Depending on GUI toolkit, call to View.run may be necessary. 1. In Java, run is no-op. 2. In other toolkits, run starts GUI event han- dling loop.
CSC309-S15-L3-4 Slide 67 High-level function diagram, cont’d F. Once event loop is started, all program con- trol assumed by toolkit. 1. In Java, event loop is separate thread. 2. Event loop calls application methods that listen for events.
CSC309-S15-L3-4 Slide 68 XX. Overview of Event-Based Design A. Event loop takes over at end of Main . 1. A separate thread -- java.awt.EventDispatchThread 2. MainThread terminated. 3. Application methods invoked via events .
CSC309-S15-L3-4 Slide 69 Overview, cont’d B. Event-based processing in all GUI toolkits. 1. Details vary widely. 2. Each has an event model . 3. What’s the same is main program looses control, methods invoked through events.
CSC309-S15-L3-4 Slide 70 XXI. Designing event-based programs A. Tw o important aspects: 1. Setting up event handlers. 2. Handling the events.
CSC309-S15-L3-4 Slide 71 Designing event-based, cont’d B. Event handlers respond to events. 1. Events are user actions. 2. In Java, we set up an EventListener . 3. Typical case is an ActionListener for a menu item or button.
CSC309-S15-L3-4 Slide 72 Designing event-based, cont’d C. Event handler invokes an application method. 1. Event-invoked methods are " call-back s". 2. In Java, call-backs invoked by action- Performed method of EventListener .
CSC309-S15-L3-4 Slide 73 Designing event-based, cont’d 3. actionPerformed is specialized for each listener. 4. Each specialized actionPerformed calls an appropriate appl’n method.
CSC309-S15-L3-4 Slide 74 XXII. Design diagram notation A. In function diagram, event-based invocation is shown with a double line. B. See Figure 7
CSC309-S15-L3-4 Slide 75 main EventName methodD methodE methodB methodC methodA a. Normal method invocation b. Event-based method invocation
CSC309-S15-L3-4 Slide 76 XXIII. Examples A. Figure 11 shows setting up event handlers.
CSC309-S15-L3-4 main . . . CalendarToolUI. compose . . . CalendarToolUI. composeMenuBar FileUI.compose FileMenu.compose . . . addNewItem JMenuItem addActionListener addOpenItem JMenuItem addActionListener . . . . . . Slide 77
CSC309-S15-L3-4 from CalendarToolUI.composeMenuBar ScheduleUI. compose . . . scheduleEvent Dialog.compose . . . composeButtonRow JButton OKScheduleEvent ButtonListener addActionListener . . . ScheduleMenu. compose . . . addEventItem JMenuItem addActionListener Slide 78
CSC309-S15-L3-4 Slide 79 B. Figure 9 shows event-based invocation.
CSC309-S15-L3-4 Slide 80 MouseButton Event FileMenu.java:113 FileMenu.java:117 File.java: 36 JMenuItem. File.fileNew System.out.println actionListener. actionPerformed FileMenu.java:135 FileMenu.java:141 JFileChosser. JMenuItem. actionListener. showOpenDialog actionPerformed FileMenu.java:146 File.java: 44 File.open System.out.println
CSC309-S15-L3-4 Slide 81 Events Handled by Toolkit’s Runtime Package MouseButton Event View and/or Stubbed Model Method Model (for Milestone 2) Controller FileMenu.java:113 FileMenu.java:117 File.java: 36 JMenuItem. File.fileNew System.out.println actionListener. actionPerformed FileMenu.java:135 FileMenu.java:141 JFileChosser. JMenuItem. actionListener. showOpenDialog actionPerformed FileMenu.java:146 File.java: 44 File.open System.out.println
CSC309-S15-L3-4 Slide 82 MouseButton Event . . . ScheduleMenu.java: 128 ScheduleMenu.java: 129 JMenuItem. ScheduleEventDialog. actionListener. show actionPerformed . . .
CSC309-S15-L3-4 Slide 83 MouseButton Event OKScheduleEvent OKScheduleEvent ButtonListener.java: 41 ButtonListener.java: 50 OKScheduleEvent Event.Event ButtonListener. actionPerformed OKScheduleEvent ButtonListener.java: 50 Schedule.java: 93 Schedule. System.out.println scheduleEvent
CSC309-S15-L3-4 Slide 84 MonthlyAgenda ViewMenu.java: 168 ViewMenu.java: 170 Display.java: 186 MonthlyAgendaDisplay. JMenuItem. JPanel.removeAll actionListener. update actionPerformed 188 ViewMenu.java: 171 GridLayout.setRows MonthlyAgendaDisplay. show 193 MonthlyAgenda. getfirstDay MouseButton Event 202 MonthlyAgenda. getNextDay 203 SmallDayViewDisplay. SmallDayViewDisplay 206 JPanel.add
CSC309-S15-L3-4 Slide 85 MouseButton Event AppointmentsList ViewMenu.java: 273 ViewMenu.java: 274 Display.java: 80 AppointmentsList DefaultTableModel. JMenuItem. actionListener. Display.update setRowCount actionPerformed 86 Lists.view ViewMenu.java: 275 AppointmentsList AppointmentsList Display.show 88 populateRow 93 mvp.Window.pack
CSC309-S15-L3-4 Slide 86 0. Schedule.java package caltool.schedule; import caltool.caldb.*; import mvp.*; /**** * * Class Schedule is the top-level model class in the schedule package. * provides methods to schedule the four types of calendar item. * contains a Categories data field, which is the sub-model for editing * scheduled item categories. * * @author Gene Fisher (gfisher@calpoly.edu) * @version 3feb10
CSC309-S15-L3-4 Slide 87 * */ public class Schedule extends Model { /** * Construct this with the given companion view and the parent CalendarDB * model. The CalendarDB is provided to access to its service methods that * store items in the current user calendar. * empty error exceptions for each method that throws one. * * pre: ; * * post: this.calDB == calDB && scheduleEventPrecondViolation != null; */ public Schedule(View view, CalendarDB calDB) {
CSC309-S15-L3-4 Slide 88 super(view); this.calDB = calDB; scheduleEventPrecondViolation = new ScheduleEventPrecondViolation(); } /*-* * Derived methods */ /** * ScheduleAppointment adds the given Appointment to the current Calendar * if an appointment of the same time, duration, and title is not already * scheduled. * < * pre:
CSC309-S15-L3-4 Slide 89 * * // * // The StartOrDueDate field is not empty and a valid date value. * // * ((appt.start_or_due_date != nnull) && appt.start_or_due_date.isValid()) * * && * * // * // If non-empty, the EndDate field is a valid date value. * // * ((appt.end_date != null) || appt.end_date.isValid()) * * && *
CSC309-S15-L3-4 Slide 90 * // * // The duration is between 1 minute and 999 hours, inclusive. * // * ((appt.duration <= 1) && (appt.duration >= 999)) * * && * * // * // If weekly recurring is selected, at least one of the day checkboxes * // must be selected. * // * if appt.recurring.is_recurring and appt.recurring.interval?weekly * then appt.recurringInfo.details.weekly.onSun or * appt.recurringInfo.details.weekly.onMon or * appt.recurringInfo.details.weekly.onTue or
CSC309-S15-L3-4 Slide 91 * appt.recurringInfo.details.weekly.onWed or * appt.recurringInfo.details.weekly.onThu or * appt.recurringInfo.details.weekly.onFri or * appt.recurringInfo.details.onWeekly.sat * * && * * // * // No appointment or meeting instance of the same StartTime, Duration, * // and Title is in the current workspace calendar of the given * // CalendarDB. The current calendar is * // * // cdb.workspace.calendars[1] * // * // The index is 1 since, by convention, the workspace calendar list is
CSC309-S15-L3-4 Slide 92 * // maintained in most-recently visited order, with the first element * // being most recent and therefore current. * // * ! (exists (item in calDB.getCurrentCalendar().items) * (item.start_or_due_date.equals(appt.start_or_due_date)) && * (item.duration.equals(appt.duration)) && * (item.title.equals(appt.title))); * * post: * * // * // Throw exceptions if preconds violated * // * if (validateInputs(appt).anyErrors()) * then throw == scheduleAppointmentPrecondViolation
CSC309-S15-L3-4 Slide 93 * * || * * if (alreadySchededuled(event)) then * then throw == scheduleAppointmentPrecondViolation * * || * * if (calDB.getCurrentCalendar() == null) then * then throw == scheduleAppointmentPrecondViolation * * || * * // * // If preconds met, a scheduled item is in the output calendar if
CSC309-S15-L3-4 Slide 94 * // and only if it is the new appt to be added or it is in the * // input calendar. * // * (forall (item in calDB’.getCurrentCalendar().getItems()) * ((item == event) || * (item in calDB.getCurrentCalendar().items))) * * && * * (calDB’.getCurrentCalendar().getItems().size() == * calDB.getCurrentCalendar().getItems().size + 1) * * && * * (calDB’.getCurrentCalendar().requiresSaving)
CSC309-S15-L3-4 Slide 95 * * && * * (calDB’.getCurrentCalendar().hasChanged()); * < */ public void scheduleAppointment(Appointment appt) { System.out.println("In Schedule.scheduleAppointment."); } /** * ScheduleMeeting adds a Meeting to the current calendar, based on the the * given MeetingRequest. The work is done by the three suboperations, * which determine a list of possible meetings times, set * meeting-scheduling options, and confirm the scheduling of a specific
CSC309-S15-L3-4 Slide 96 * meeting selected from the possibles list. */ public void scheduleMeeting(MeetingRequest meeting_req) { System.out.println("In Schedule.scheduleMeeting."); } /** * Produce the list of possible meeting times that satisfy the given * MeetingRequest. */ public PossibleMeetingTimes listMeetingTimes(MeetingRequest request) { System.out.println("In schedule.listMeetingTimes."); return null; }
CSC309-S15-L3-4 Slide 97 /** * Set the meeting options in the CalendarDB to those given. * */ public void setMeetingOptions(MeetingSchedulingOptions options) { System.out.println("In schedule.setMeetingOptions."); } /** * ConfirmMeeting takes a CalendarDB, MeetingRequest, list of * PossibleMeetingTimes, and a selected time from the list. * new CalendarDB with the given request scheduled at the selected time. */ public void confirmMeeting(MeetingRequest meeting_req, PossibleMeetingTimes possible_times, int selected_time) {
CSC309-S15-L3-4 Slide 98 System.out.println("In Schedule.confirmMeeting"); } /** * ScheduleTask adds the given Task to the given CalendarDB, if a task of * the same start date, title, and priority is not already scheduled. */ public void scheduleTask(Task task) { System.out.println("In Schedule.scheduleTask."); } /** * ScheduleEvent adds the given Event to the given CalendarDB, if an event * of the same start date and title is not already scheduled. * <
CSC309-S15-L3-4 Slide 99 * pre: * * // * // The Title field is at least one character long. * // * ((event.title != null) && (event.title.size() >= 1)) * * && * * // * // The StartOrDueDate field is not empty and a valid date value. * // * ((event.startOrDueDate != null) && event.startOrDueDate.isValid()) * * &&
CSC309-S15-L3-4 Slide 100 * * // * // If non-empty, the EndDate field is a valid date value. * // * ((event.endDate == null) || event.endDate.isValid()) * * && * * // * // The current workspace is not null. * // * (calDB.getCurrentCalendar() != null) * * && *
Recommend
More recommend