/** * Cal.java: An Abstract Java Based Calendar * * Copyright (c) 1999 * Ali Onur Cinar &060;cinar&064;zdo.com&062; * * License: * * Permission to use, copy, modify, and distribute this software and its * documentation for educational purposes and without fee is hereby * granted, provided that the above copyright notice appear in all copies * and that both the copyright notice and this permission notice and * warranty disclaimer appear in supporting documentation, and that the name * of Ali Onur Cinar not be used in advertising or publicity pertaining to * distribution of the software without specific, written prior permission. * * To Compile: * * $ javac Cal.java * * To Run: * * $ java Cal * * @author A. Onur Cinar &060;cinar&064;zdo.com&062; * @version %W% %G% * @version $Id: Cal.java,v 1.2 2004/12/07 15:12:38 cinar Exp $ * @since 1.0 */ import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.Calendar; import java.util.Vector; import java.util.StringTokenizer; public class Cal extends Frame implements ActionListener, ItemListener, MouseListener { /* variables */ protected Choice smonth; /* selection month */ protected Choice syear; /* selection year */ protected Label sday; /* selection day */ protected TextField edesc; /* desc text field */ protected TextField filename; /* filename */ protected List elist; /* event list */ protected int tyear,tmonth,tday; /* today */ protected Label[] days; /* calendar cells */ protected Calendar cal; /* calendar holder */ protected Vector events; /* event container */ /* constants */ protected static String[] months = {"January","February","March","April", "May","June","July","August","September","October","November","December"}; protected static int[] years = new int[20]; protected static final int ROWS = 7; /* calendar rows */ protected static final int COLS = 7; /* calendar cols */ protected static final int CELLS = (ROWS-1)*COLS; /* cell count */ protected static Color dbackground; /* OS default background */ /* Print Confirmation Class */ class PrintConfirm extends Frame implements ActionListener { public PrintConfirm () { setLayout(new BorderLayout()); add(new Label("Calendar printed as "+filename.getText() +".html",Label.CENTER), BorderLayout.CENTER); Button b = new Button("Close"); b.addActionListener(this); add(b,BorderLayout.SOUTH); setSize(300,100); show(); } public void actionPerformed (ActionEvent e) { dispose(); /* close this window */ } } /* Search Result List Class */ class SearchResults extends Frame implements ItemListener, ActionListener { public SearchResults (Vector r) { super("Search Results"); setLayout(new BorderLayout()); List slist = new List(10); /* create list */ slist.addItemListener(this); add(slist,BorderLayout.CENTER); Button b = new Button("Close"); /* close button */ b.addActionListener(this); add(b,BorderLayout.SOUTH); for (int i=0; i l) days[i].setText(" "); else { days[i].setText(String.valueOf(j)); days[i].setForeground((j==tday && month==tmonth && year==tyear) ? Color.blue : Color.black); ++j; /* next day */ } } /* updateevents */ updateevents(); } public void itemStateChanged(ItemEvent e) { cal.set(Calendar.MONTH,smonth.getSelectedIndex()); cal.set(Calendar.YEAR,Integer.parseInt(syear.getSelectedItem())); updatecal(); } public void actionPerformed (ActionEvent e) { String cmd = e.getActionCommand(); try { /* operations */ if ("Add".equals(cmd)) { if (!("".equals(edesc.getText()))) { events.add(new CalEvent( cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), edesc.getText() )); updateevents(); } } else if ("Delete".equals(cmd)) { if (elist.getSelectedIndex() != -1) { String s = elist.getSelectedItem(); /* remove from list */ for (int i=0; i" +"

"+months[month]+", "+String.valueOf(year)+"

" +"" +"" +""); /* print calendar including events */ int t = cal.get(Calendar.DAY_OF_MONTH); cal.set(Calendar.DAY_OF_MONTH,1); int d = cal.get(Calendar.DAY_OF_WEEK); /* find the last day of month */ int l = cal.getActualMaximum(Calendar.DAY_OF_MONTH); /* update */ for (int i=0,j=1; i l) pw.print(""); else { pw.print(""); ++j; /* next day */ } if (i != 0 && (i+1) % COLS == 0) /* make rows */ pw.print(""); } pw.println("
SunMonTueWedThuFriSat
 "); pw.print((j==tday && month==tmonth && year==tyear) ? ""+String.valueOf(j)+"" : String.valueOf(j)); /* show events */ for (int k=0; k
"+c.getDesc()+""); } pw.print("
"); pw.flush(); pw.close(); cal.set(Calendar.DAY_OF_MONTH,t); /* restore selection */ new PrintConfirm(); /* inform user */ } } else if ("Exit".equals(cmd)) { /* save default file */ actionPerformed(new ActionEvent(this,ActionEvent.ACTION_PERFORMED,"Save")); System.exit(0); } } catch (Exception ee) { System.out.println(ee.toString()); } } public void mouseClicked (MouseEvent e) { String s = ((Label) e.getSource()).getText(); if (!(" ".equals(s))) { /* update day */ sday.setText(s); cal.set(Calendar.DAY_OF_MONTH,Integer.parseInt(sday.getText())); /* update events */ updateevents(); } } public void updateevents () { elist.removeAll(); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); /* show events on that day */ for (int i=0; i 0) v.addElement(str.substring(p,i)); // reset p=i+1; } } // if last part not counted if (str.length()-1 > p) { v.addElement(str.substring(p,str.length())); } // return return v; } }