Project: Calendar-Application
package calendar;
 
import java.awt.Color; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.Locale; 
import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EtchedBorder; 
import javax.swing.border.TitledBorder; 
 
/**
 * This class will format the view on the main calendar panel 
 * to show any events for a single day on a particular month. This will 
 * be partnered with the month view. 
 * 
 * @author James Alfei (631989) 
 * @date 22/02/2012 
 * @brief This class will display the day events on the main calendar  
 * interface 
 */
 
 
@SuppressWarnings({ "serial""unused" }) 
public class DayPanel extends JPanel{ 
 
 /**
  * An array list of events to be displayed on the day panel 
  */
 
 ArrayList<Event> m_events; 
 
 /**
  * Main Constructor 
  *  
  * @param date The date of the day to show events for 
  * @param showing_month The month of the date 
  * @param showing_year The year of the date 
  */
 
 
 public DayPanel(final Calendar date, final int showing_month,  
   final int showing_year, ActionListener CalListener) { 
  super(); 
  final int day = date.get(Calendar.DAY_OF_MONTH); 
  final int month = date.get(Calendar.MONTH); 
  final int year = date.get(Calendar.YEAR); 
  String print_day = new Integer(day).toString(); 
 
  //if day is 1, print month as well 
  if(day == 1
   print_day += " "+date.getDisplayName(Calendar.MONTH,Calendar.SHORT, 
     new Locale(Data.GetSettings().Get("locale"))); 
  print_day = " "+print_day+" "
 
 
  //getting the events 
  m_events = Data.GetEvents().GetEventsByDay(date); 
 
 
  EtchedBorder temp_border; 
  //highlight todays date 
  if(Data.GetCurrentDay() == date.get(Calendar.DAY_OF_MONTH)  
    && Data.GetCurrentMonth() == date.get(Calendar.MONTH) &&  
    Data.GetCurrentYear() == date.get(Calendar.YEAR)){ 
   temp_border = new EtchedBorder(EtchedBorder.LOWERED, Color.black,  
     Color.black); 
  else { 
   temp_border = new EtchedBorder(EtchedBorder.LOWERED); 
  
 
  //set layout for each day 
  final TitledBorder border = new TitledBorder(temp_border, print_day); 
  if(month == showing_month && year == showing_year) 
   border.setTitleColor(Color.black); 
  else 
   border.setTitleColor(Color.GRAY); 
 
  setBackground(Color.WHITE); 
  setBorder(border); 
  setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
 
 
  //sort the events by hour 
  sortEvents(); 
 
  for(int i = 0; i < m_events.size(); i++) { 
   add(new EventPanel(m_events.get(i),CalListener)); 
  
 
 
 
 /**
  * This method will sort the events which are displayed on the day panel 
  *  by hour 
  */
 
 private boolean sortEvents() { 
  // (overall complexity is O(m*n*n), where m = the number of days in  
  // the view and n is the maximum number of events in a day) 
  boolean found; 
  do { 
   found = false
   for(int i = 0; i < m_events.size() - 1; i++) { 
    if(m_events.get(i).GetStart_date() 
      .compareTo(m_events.get(i+1).GetStart_date()) > 0) { 
     final Event temp = m_events.get(i); 
     m_events.set(i, m_events.get(i+1)); 
     m_events.set(i+1, temp); 
     found = true
    
   
  }while(found); 
  return true
 
 
 /*
  * This class will be mainly used for testing and will be commented 
  * out in the final submission 
  * @param args command line arguments 
  * @throws Exception Any exceptions thrown with making events 
  */
 
 
 /*
    public static void main (String[] args) throws Exception{ 
     Data.Init(); 
     //make new frame 
     JFrame test = new JFrame(); 
     Calendar cal = Calendar.getInstance(); 
     cal.add(cal.HOUR, 2); 
     // add a SECOND event (time) 
     Data.AddEvent("This should come after", Calendar.getInstance(), cal,  
       "Test", 0); 
     Calendar cal2 = Calendar.getInstance(); 
     cal2.add(cal.HOUR, -1); 
     //Add a FIRST event (time wise) 
        Data.AddEvent("First", cal2, cal, "Test", 1); 
     //Make a new frame with panel for events 
        //This will also test the sort method to ensure the events 
        // are displayed in order 
        DayPanel testPan = new DayPanel(cal, 2, 2012); 
     test.getContentPane().add(testPan); 
     test.pack(); 
     test.setVisible(true); 
     
  */
 
}