/* spaceTimeSelector.java Paul Carlisle A control allowing the user to set their location, date and time. A composite of the yearSelector, locationSelector, and timeSelector classes. */ import java.awt.*; import yearSelector; import timeSelector; import DateConstants; import JDate; import locationSelector; import textHelpPanel; public class spaceTimeSelector extends Panel { public static int NONE = 0; public static int DATE_SELECTED = 1; public static int LOCATION_SELECTED = 2; public static int HELP_REQUESTED = 3; private int Width; private int Height; private textHelpPanel help; private JDate currentDate; private yearSelector yearPanel; private timeSelector timePanel; private Choice dayPanel; private Choice monthPanel; private locationSelector locationPanel; private GridBagLayout mainLayout; private GridBagConstraints mainConstraints; private boolean gotMessage = false; private int messageID = NONE; private Button dateLabel; // These have been changed from Labels to private Button timeLabel; // Buttons, because Netscape doesn't recognize private Button latLongLabel; // the events when they're Labels. // Event handling. JDate tempDate = new JDate(); spaceTimeSelector(int width, int height, JDate date, Font inputFont, Font checkFont, Font labelFont, textHelpPanel helpPanel) { Width = width; Height = height; currentDate = date; help = helpPanel; yearPanel = new yearSelector(currentDate.getYear(), inputFont, Width); timePanel = new timeSelector(currentDate.getFracTime(), inputFont, checkFont, labelFont, Width); locationPanel = new locationSelector(Width, Height, 42.0, 83.0, inputFont, checkFont); monthPanel = new Choice(); dayPanel = new Choice(); dateLabel = new Button("Date"); dateLabel.setBackground(Color.gray); dateLabel.setFont(checkFont); timeLabel = new Button("Time"); timeLabel.setBackground(Color.gray); timeLabel.setFont(checkFont); latLongLabel = new Button("Latitude and Longitude"); latLongLabel.setBackground(Color.gray); latLongLabel.setFont(checkFont); monthPanel.setBackground(Color.white); monthPanel.setFont(inputFont); for (int i = 0; i < 12; i++) monthPanel.addItem(DateConstants.MonthNames[i]); monthPanel.select(currentDate.getMonth() - 1); dayPanel.setBackground(Color.white); dayPanel.setFont(inputFont); for (int i = 1; i <= 31; i++) dayPanel.addItem(new String(Integer.toString(i)) ); dayPanel.select((int)currentDate.getDay() - 1); mainLayout = new GridBagLayout(); mainConstraints = new GridBagConstraints(); mainConstraints.fill = GridBagConstraints.NONE; mainConstraints.anchor = GridBagConstraints.CENTER; mainConstraints.gridx = 0; setLayout(mainLayout); // Build the date holder. Panel dateHolder = new Panel(); dateHolder.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 4)); dateHolder.add(monthPanel); dateHolder.add(dayPanel); // Add the date label. mainConstraints.gridy = 0; mainConstraints.insets = new Insets(0, 5, 0, 5); mainConstraints.fill = GridBagConstraints.HORIZONTAL; mainLayout.setConstraints(dateLabel, mainConstraints); add(dateLabel); // Add the date selector. mainConstraints.gridy = 1; mainConstraints.insets = new Insets(0, 5, 0, 5); mainConstraints.fill = GridBagConstraints.NONE; mainLayout.setConstraints(dateHolder, mainConstraints); add(dateHolder); mainConstraints.gridy = 2; mainConstraints.insets = new Insets(4, 5, 4, 5); mainLayout.setConstraints(yearPanel, mainConstraints); add(yearPanel); // Add the time label. mainConstraints.gridy = 3; mainConstraints.insets = new Insets(0, 5, 4, 5); mainConstraints.fill = GridBagConstraints.HORIZONTAL; mainLayout.setConstraints(timeLabel, mainConstraints); add(timeLabel); // Add the time selector. mainConstraints.gridy = 4; mainConstraints.insets = new Insets(0, 5, 4, 5); mainConstraints.fill = GridBagConstraints.NONE; mainLayout.setConstraints(timePanel, mainConstraints); add(timePanel); // Add the latLong Label. mainConstraints.gridy = 5; mainConstraints.insets = new Insets(0, 5, 4, 5); mainConstraints.fill = GridBagConstraints.HORIZONTAL; mainLayout.setConstraints(latLongLabel, mainConstraints); add(latLongLabel); mainConstraints.gridy = 6; mainConstraints.insets = new Insets(0, 5, 0, 5); mainConstraints.fill = GridBagConstraints.NONE; mainLayout.setConstraints(locationPanel, mainConstraints); add(locationPanel); resize(Width, Height); } // Checks to ensure that the date is within the proper range // for the selected month. Returns the original date if it // is, or the number of days in the month if it isn't. private int checkdate(JDate date, int day) { if (day > date.getMonthLength()) day = date.getMonthLength(); return day; } // Return the date the panel is currently set for. public JDate getDate() { return currentDate; } // Return the latitude, in radian measure. public double getLatitude() { return locationPanel.getLatitude(); } // Return the longitude, in radian measure. public double getLongitude() { return locationPanel.getLongitude(); } // See if we have a message. public boolean getMessage() { boolean temp = gotMessage; gotMessage = false; return temp; } // Return the ID of the last message. public int getMessageID() { return messageID; } public boolean handleEvent(Event e) { if ( (e.id != Event.ACTION_EVENT) && (e.id != Event.MOUSE_DOWN) ) return true; if (e.target == dateLabel) { gotMessage = true; messageID = HELP_REQUESTED; help.getHelp("DateDetail"); return false; } else if (e.target == timeLabel) { gotMessage = true; messageID = HELP_REQUESTED; help.getHelp("TimeDetail"); return false; } else if (e.target == latLongLabel) { gotMessage = true; messageID = HELP_REQUESTED; help.getHelp("LatLongDetail"); return false; } tempDate.setDate(yearPanel.getYear(), monthPanel.getSelectedIndex() + 1, 1); int tempDay = checkdate(tempDate, dayPanel.getSelectedIndex() + 1); boolean valid = currentDate.setDate(yearPanel.getYear(), monthPanel.getSelectedIndex() + 1, tempDay + timePanel.getTime() ); gotMessage = true; messageID = DATE_SELECTED; return false; } public void redraw() { repaint(); } // Set the panel to a new date. public void setDate(JDate date) { currentDate.setJD(date.getJD()); monthPanel.select(currentDate.getMonth() - 1); dayPanel.select((int)currentDate.getDay() - 1); yearPanel.setYear(currentDate.getYear()); timePanel.setTime(currentDate.getFracTime()); } // Set the latitude, in radian measure. public void setLatitude(double latitude) { locationPanel.setLatitude(latitude); } // Set the longitude, in radian measure. public void setLongitude(double longitude) { locationPanel.setLongitude(longitude); } public void update(Graphics g) { paint(g); } }