/* Calendar.java Paul Carlisle An interactive screen, displaying a calendar grid, which allows the user to generate calendars for any month and year from 3999 BC to 3999 AD. In each day cell of the calendar, an image of the Moon in that day's phase is drawn. */ import java.awt.*; import JDate; import DateConstants; import calendarCanvas; import yearSelector; import textHelpPanel; public class Calendar extends Panel { public static int NONE = 0; public static int DATE_SELECTED = 1; public static int HELP_REQUESTED = 2; private int Width, Height; private JDate currentDate; // Records which date was selected, based on the user selecting // a day on the calendar grid. User can read this value with getSelectedDate(), // but note that it is reset to 0 after each read. private JDate selectedDate; private calendarCanvas gridPanel; private yearSelector yearPanel; private Panel calHolder; private Panel controlPanel; private Choice monthPanel; private Button GregorButton; private Button resetButton; private Button helpButton; private Button dateLabel; // Changed this from Label to Button, so Netscape // will recognize it's events. private boolean GregorOn = true; private textHelpPanel helpWindow; private GridBagLayout controlLayout; private GridBagConstraints controlConstraints; private GridBagLayout mainLayout; private GridBagConstraints mainConstraints; private int messageID = NONE; private boolean gotMessage = false; public Calendar(JDate date, int width, int height, textHelpPanel help, Font dayCellFont, Font weekdayFont, Font controlFont) { // Set private variables from passed values. currentDate = new JDate(); currentDate.setJD(date.getJD()); Width = width; Height = height; selectedDate = currentDate; mainLayout = new GridBagLayout(); mainConstraints = new GridBagConstraints(); setLayout(mainLayout); // Set up the calendar grid panel. makeGridPanel(weekdayFont, dayCellFont); // Add it to the layout. mainConstraints.gridy = 0; mainConstraints.gridx = 0; mainConstraints.gridwidth = 1; mainConstraints.gridheight = 1; mainConstraints.anchor = GridBagConstraints.CENTER; mainConstraints.fill = GridBagConstraints.BOTH; mainLayout.setConstraints(calHolder, mainConstraints); add(calHolder); // Set up the control panel. makeControlPanel(controlFont); // Add it to the layout. mainConstraints.gridx = 1; mainConstraints.gridy = 0; mainConstraints.gridwidth = 1; mainConstraints.gridheight = 1; mainConstraints.anchor = GridBagConstraints.NORTH; mainLayout.setConstraints(controlPanel, mainConstraints); add(controlPanel); helpWindow = help; } // Tell user whether we generated a message or not. public boolean getMessage() { boolean temp = gotMessage; gotMessage = false; return temp; } public int getMessageID() { return messageID; } // Return the date selected from the calendar grid. public JDate getSelectedDate() { return selectedDate; } public boolean handleEvent(Event e) { boolean result = true; // We're only interested in action events: mouse clicks, Choice selects, // etc. If the event is not an action event, we consume it and return // right away. if ( (e.id != Event.ACTION_EVENT) && (e.id != Event.MOUSE_DOWN) ) return true; if (e.target == helpButton) { gotMessage = true; messageID = HELP_REQUESTED; helpWindow.getHelp("Calendar"); result = false; } else if (e.target == dateLabel) { gotMessage = true; messageID = HELP_REQUESTED; helpWindow.getHelp("DateCalendar"); result = false; } else if (e.target == GregorButton) { gotMessage = true; messageID = HELP_REQUESTED; helpWindow.getHelp("Gregorian"); result = false; } else if (e.target == gridPanel) // User selected a date. { int temp = gridPanel.getDate(); if (temp != 0) { selectedDate.setDate(currentDate.getYear(), currentDate.getMonth(), temp); gotMessage = true; messageID = DATE_SELECTED; } else { result = true; // Consume event so user never hears about it. gotMessage = false; messageID = NONE; } result = false; } else if (e.target == resetButton) { currentDate.setToCurrent(); yearPanel.setYear(currentDate.getYear()); monthPanel.select(currentDate.getMonth() - 1); gridPanel.redraw(currentDate); redraw(currentDate); result = true; } else { int tempMonth = monthPanel.getSelectedIndex() + 1; int tempYear = yearPanel.getYear(); currentDate.setDate(tempYear, tempMonth, 1); gridPanel.redraw(currentDate); redraw(currentDate); result = true; } return result; } private void makeControlPanel(Font myFont) { // Make the control panel, which holds all of the control elements. controlPanel = new Panel(); // Set up the layout and formatting for the control panel. controlLayout = new GridBagLayout(); controlConstraints = new GridBagConstraints(); controlPanel.setLayout(controlLayout); controlConstraints.fill = GridBagConstraints.NONE; controlConstraints.anchor = GridBagConstraints.NORTH; controlConstraints.gridheight = 1; controlConstraints.gridx = 0; // Make the reset button. resetButton = new Button("Reset to Current"); resetButton.setBackground(Color.white); // Add the reset button to the control panel. controlConstraints.gridy = 0; controlConstraints.gridwidth = 1; controlConstraints.insets = new Insets(0,20,40,20); controlLayout.setConstraints(resetButton, controlConstraints); controlPanel.add(resetButton); // Make a label for the date selector. dateLabel = new Button("Date"); dateLabel.setBackground(Color.gray); // Add the date label to the control panel. controlConstraints.gridy = 1; controlConstraints.gridwidth = 1; controlConstraints.insets = new Insets(5, 20, 0, 20); controlConstraints.fill = GridBagConstraints.HORIZONTAL; controlLayout.setConstraints(dateLabel, controlConstraints); controlPanel.add(dateLabel); // Make the month selector object, and set some parameters for it. monthPanel = new Choice(); monthPanel.setFont(myFont); monthPanel.setBackground(Color.white); // Add the month names to the month selector. for (int i = 0; i < 12; i++) monthPanel.addItem(DateConstants.MonthNames[i]); // Set the month selector to the current date. monthPanel.select(currentDate.getMonth() - 1); // Add the month selector to the control panel. controlConstraints.gridy = 2; controlConstraints.gridwidth = 1; controlConstraints.insets = new Insets(5, 20, 5, 20); controlConstraints.fill = GridBagConstraints.NONE; controlLayout.setConstraints(monthPanel, controlConstraints); controlPanel.add(monthPanel); // Make the year selector panel. yearPanel = new yearSelector(currentDate.getYear(), myFont,7*(Width - Height)/10); // Add the year selector to the control panel. controlConstraints.gridy = 3; controlConstraints.gridwidth = 1; controlConstraints.insets = new Insets(5, 20, 20, 20); controlLayout.setConstraints(yearPanel, controlConstraints); controlPanel.add(yearPanel); // Make the help button. helpButton = new Button("Information"); helpButton.setBackground(Color.white); // Add the Help button to the control panel. controlConstraints.gridy = 4; controlConstraints.gridwidth = 1; controlConstraints.insets = new Insets(25, 20, 20, 20); controlLayout.setConstraints(helpButton, controlConstraints); controlPanel.add(helpButton); // Make the GregorButton. GregorButton = new Button("October 1582"); GregorButton.setBackground(Color.gray); controlConstraints.gridy = 5; controlConstraints.gridwidth = 1; controlConstraints.insets = new Insets(0, 20, 20, 20); controlLayout.setConstraints(GregorButton, controlConstraints); controlPanel.add(GregorButton); //GregorButton.hide(); } private void makeGridPanel(Font weekdayFont, Font dayCellFont) { // Set font to use for weekday labels. setFont(weekdayFont); // Set up a new Panel to hold the grid and weekday name panel. calHolder = new Panel(); calHolder.setBackground(Color.lightGray); // Set up the layout and formatting for the holding panel. GridBagLayout calLayout = new GridBagLayout(); calHolder.setLayout(calLayout); GridBagConstraints calConstraints = new GridBagConstraints(); calConstraints.fill = GridBagConstraints.BOTH; calConstraints.anchor = GridBagConstraints.CENTER; calConstraints.gridwidth = 1; calConstraints.gridheight = 1; // Create and populate the weekday name label panel. Panel weekdayPanel = new Panel(); weekdayPanel.setLayout(new GridLayout(1,0,0,0)); Label dayLabel; for (int i = 0; i < 7; i++) { dayLabel = new Label(DateConstants.WeekdayNames[i].substring(0,3)); dayLabel.setAlignment(Label.CENTER); weekdayPanel.add(dayLabel); } // Add the weekday names to the holding panel. calConstraints.gridx = 0; calConstraints.gridy = 0; calLayout.setConstraints(weekdayPanel, calConstraints); calHolder.add(weekdayPanel); // Create the grid of day cells representing the calendar. gridPanel = new calendarCanvas(currentDate, (9 * Height) / 10, dayCellFont); // Add the grid to the holding panel. calConstraints.gridx = 0; calConstraints.gridy = 1; calLayout.setConstraints(gridPanel, calConstraints); calHolder.add(gridPanel); } public void paint(Graphics g) { int h = GregorButton.size().height; if ( (currentDate.getYear() == 1582) && (currentDate.getMonth() == 10)) { if (!GregorOn) { // Add the GregorButton. controlConstraints.gridy = 4; controlConstraints.gridwidth = 1; controlConstraints.insets = new Insets(25, 20, 20, 20); controlLayout.setConstraints(helpButton, controlConstraints); controlConstraints.gridy = 5; controlConstraints.gridwidth = 1; controlConstraints.insets = new Insets(0, 20, 20, 20); controlLayout.setConstraints(GregorButton, controlConstraints); controlPanel.add(GregorButton); GregorButton.show(); GregorOn = true; } } else { if (GregorOn) { controlLayout.removeLayoutComponent(GregorButton); controlPanel.remove(GregorButton); //GregorButton.hide(); GregorOn = false; controlConstraints.gridy = 4; controlConstraints.gridwidth = 1; controlConstraints.insets = new Insets(25, 20, h + 20, 20); controlLayout.setConstraints(helpButton, controlConstraints); } } controlPanel.repaint(); } public void redraw(JDate date) { currentDate.setJD(date.getJD()); monthPanel.select(currentDate.getMonth() - 1); yearPanel.setYear(currentDate.getYear()); gridPanel.redraw(currentDate); repaint(); } }