import java.util.Scanner; import java.io.File; import java.io.IOException; import javax.swing.*; import java.awt.*; import java.awt.event.*; /* Application that presents a Graphical User Interface for exercising the mutator methods of the augmented CalendarDate class. P.M.J. & R.W.M., November 2022 */ public class CalendarDateGUI { static DialogBox Input = new DialogBox("CalendarDateGUI..."); //------------------------------------------------------------------------------------------------- // // ActionListeners // //------------------------------------------------------------------------------------------------- public static class retreatDay_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.retreatDay(); display(PREVDAY); } } static retreatDay_CommandListener retreatDay_CommandHandler = new retreatDay_CommandListener(); public static class retreatDayN_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.retreatDay(Input.nextInt("N:")); display(PREVDAYN); } } static retreatDayN_CommandListener retreatDayN_CommandHandler = new retreatDayN_CommandListener(); public static class advanceDay_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.advanceDay(); display(NEXTDAY); } } static advanceDay_CommandListener advanceDay_CommandHandler = new advanceDay_CommandListener(); public static class advanceDayN_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.advanceDay(Input.nextInt("N:")); display(NEXTDAYN); } } static advanceDayN_CommandListener advanceDayN_CommandHandler = new advanceDayN_CommandListener(); //------------------------------------------------------------------------------------------------- public static class retreatWeek_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.retreatWeek(); display(PREVWEEK); } } static retreatWeek_CommandListener retreatWeek_CommandHandler = new retreatWeek_CommandListener(); public static class retreatWeekN_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.retreatWeek(Input.nextInt("N:")); display(PREVWEEKN); } } static retreatWeekN_CommandListener retreatWeekN_CommandHandler = new retreatWeekN_CommandListener(); public static class advanceWeek_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.advanceWeek(); display(NEXTWEEK); } } static advanceWeek_CommandListener advanceWeek_CommandHandler = new advanceWeek_CommandListener(); public static class advanceWeekN_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.advanceWeek(Input.nextInt("N:")); display(NEXTWEEKN); } } static advanceWeekN_CommandListener advanceWeekN_CommandHandler = new advanceWeekN_CommandListener(); //------------------------------------------------------------------------------------------------- public static class retreatMonth_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.retreatMonth(); display(PREVMONTH); } } static retreatMonth_CommandListener retreatMonth_CommandHandler = new retreatMonth_CommandListener(); public static class retreatMonthN_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.retreatMonth(Input.nextInt("N:")); display(PREVMONTHN); } } static retreatMonthN_CommandListener retreatMonthN_CommandHandler = new retreatMonthN_CommandListener(); public static class advanceMonth_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.advanceMonth(); display(NEXTMONTH); } } static advanceMonth_CommandListener advanceMonth_CommandHandler = new advanceMonth_CommandListener(); public static class advanceMonthN_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.advanceMonth(Input.nextInt("N:")); display(NEXTMONTHN); } } static advanceMonthN_CommandListener advanceMonthN_CommandHandler = new advanceMonthN_CommandListener(); //------------------------------------------------------------------------------------------------- public static class retreatYear_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.retreatYear(); display(PREVYEAR); } } static retreatYear_CommandListener retreatYear_CommandHandler = new retreatYear_CommandListener(); public static class retreatYearN_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.retreatYear(Input.nextInt("N:")); display(PREVYEARN); } } static retreatYearN_CommandListener retreatYearN_CommandHandler = new retreatYearN_CommandListener(); public static class advanceYear_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.advanceYear(); display(NEXTYEAR); } } static advanceYear_CommandListener advanceYear_CommandHandler = new advanceYear_CommandListener(); public static class advanceYearN_CommandListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { update(); Current.advanceYear(Input.nextInt("N:")); display(NEXTYEARN); } } static advanceYearN_CommandListener advanceYearN_CommandHandler = new advanceYearN_CommandListener(); public static void update() { Previous = new CalendarDate(Current.getMonth(),Current.getDay(),Current.getYear()); } //------------------------------------------------------------------------------------------------- // // T a b l e // //------------------------------------------------------------------------------------------------- public static class MonthTable extends JFrame { // Instance attributes used in this example private JPanel topPanel; private JTable table; private JScrollPane scrollPane; private String columnNames[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; // Constructor of main frame public MonthTable(String preface, CalendarDate sd, int xLoc, int yLoc) { // Set the frame characteristics setTitle(preface+MonthNames[sd.getMonth()]+" "+sd.getYear()); setLocation (xLoc,yLoc); setSize( 500, 160 ); setBackground( Color.gray ); // Create a panel to hold all other components topPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); getContentPane().add( topPanel ); // Create some data String dataValues[][] = contentSequence(sd); // Create a new table instance table = new JTable( dataValues, columnNames ); // Add the table to a scrolling pane scrollPane = new JScrollPane( table ); table.setFillsViewportHeight(true); topPanel.add( scrollPane, BorderLayout.CENTER ); } public void doUpdate(String preface, CalendarDate sd) { setTitle(preface+MonthNames[sd.getMonth()]+" "+sd.getYear()); table = new JTable( contentSequence(sd), columnNames ); // Add the table to a scrolling pane scrollPane = new JScrollPane( table ); table.setFillsViewportHeight(true); topPanel.removeAll(); topPanel.add(scrollPane, BorderLayout.CENTER); //topPanel.revalidate(); } } //------------------------------------------------------------------------------------------------- // // A p p l i c a t i o n // //------------------------------------------------------------------------------------------------- public static MonthTable MTCurrent; public static MonthTable MTPrevious; public static void display(String Operation) { MTPrevious.doUpdate("PREVIOUS: ",Previous); MTCurrent.doUpdate("CURRENT: ",Current); MTCurrent.setVisible(true); MTPrevious.setVisible(true); System.out.print(Operation+": "); System.out.print("Previous is "+Previous+", "); System.out.print("Current is "+Current+"; and "); int relative = Current.compareTo(Previous); if(relative > 0) { System.out.print("Current is now later than Previous"); } else if(relative < 0) { System.out.print("Current is now earlier than Previous"); } else { System.out.print("Current and Previous are now the same"); } System.out.println("."); } public static String[][] contentSequence(CalendarDate date) { String[][] result = new String[6][7]; CalendarDate sd = new CalendarDate(date.getMonth(),1,date.getYear()); final String EMPTY = " "; //Fill empty part of first week int weekIndex = 0; int dofwIndex = 0; int sdDayOfWeek = sd.getDayOfWeek(); for(int dof=sd.SUNDAY; dof>"); String content = intToString2(sd.getDay()); if(sd.equals(date)) { content = "((("+content+")))"; } result[weekIndex][dofwIndex] = content; dofwIndex = dofwIndex + 1; sd.advanceDay(); } //Fill empty part of the rest of the month while((weekIndex <= 5) && (dofwIndex < sd.SATURDAY)) { if(dofwIndex == sd.SATURDAY) { weekIndex = weekIndex + 1; dofwIndex = 0; } result[weekIndex][dofwIndex] = EMPTY; dofwIndex = dofwIndex + 1; } return result; } public static String intToString2(int i) { String fmt = "%2d"; //DecimalFormat nf = new DecimalFormat("##"); return String.format(fmt,i); } /* Functional Method that as passed the command line arguments and the index of the argument desired. If that argument exists then it as returned as the value of this function; if not then the user as prompted to enter a string value, which as then returned as the result of this function. */ static String getString(String[] commandLineArgs, int i) { String result; if (commandLineArgs.length > i) { result = commandLineArgs[i]; } else { Scanner keyboard = new Scanner(System.in); System.out.print("Enter Date "+i+":>"); result = keyboard.nextLine(); } return result; } static final String PREVDAY = "retreatDay()"; static final String PREVDAYN = "retreatDay(n)"; static final String NEXTDAY = "advanceDay()"; static final String NEXTDAYN = "advanceDay(n)"; static final String PREVWEEK = "retreatWeek()"; static final String PREVWEEKN = "retreatWeek(n)"; static final String NEXTWEEK = "advanceWeek()"; static final String NEXTWEEKN = "advanceWeek(n)"; static final String PREVMONTH = "retreatMonth()"; static final String PREVMONTHN = "retreatMonth(n)"; static final String NEXTMONTH = "advanceMonth()"; static final String NEXTMONTHN = "advanceMonth(n)"; static final String PREVYEAR = "retreatYear()"; static final String PREVYEARN = "retreatYear(n)"; static final String NEXTYEAR = "advanceYear()"; static final String NEXTYEARN = "advanceYear(n)"; static final String[] COMMANDS = {PREVDAY, PREVDAYN, NEXTDAY, NEXTDAYN, PREVWEEK, PREVWEEKN, NEXTWEEK, NEXTWEEKN, PREVMONTH, PREVMONTHN, NEXTMONTH, NEXTMONTHN, PREVYEAR, PREVYEARN, NEXTYEAR, NEXTYEARN, }; static CommandButtons CBs = new CommandButtons("", COMMANDS, retreatDay_CommandHandler,retreatDayN_CommandHandler,advanceDay_CommandHandler,advanceDayN_CommandHandler, retreatWeek_CommandHandler,retreatWeekN_CommandHandler,advanceWeek_CommandHandler,advanceWeekN_CommandHandler, retreatMonth_CommandHandler,retreatMonthN_CommandHandler,advanceMonth_CommandHandler,advanceMonthN_CommandHandler, retreatYear_CommandHandler,retreatYearN_CommandHandler,advanceYear_CommandHandler,advanceYearN_CommandHandler ); static CalendarDate Previous; static CalendarDate Current; static final String[] MonthNames = {"","January","February","March","April","May","June", "July","August","September","October","November","December"}; // main method of program public static void main (String[] args) throws IOException { System.out.println("CalendarGUI ... "); Previous = new CalendarDate(); Current = new CalendarDate(getString(args,0)); MTPrevious = new MonthTable("PREVIOUS: ",Previous,350,100); MTCurrent = new MonthTable("CURRENT: ",Current,350,300); display("Initially"); CBs.Show(); } }