1 19 20 package org.lucane.applications.calendar.widget; 21 22 import java.awt.*; 23 import java.awt.event.MouseEvent ; 24 import java.awt.event.MouseListener ; 25 import java.util.ArrayList ; 26 import java.util.Calendar ; 27 import java.util.Iterator ; 28 29 import javax.swing.*; 30 31 import org.lucane.applications.calendar.CalendarPlugin; 32 import org.lucane.applications.calendar.util.*; 33 34 35 38 public class MonthView extends JPanel 39 implements MouseListener 40 { 41 public JPanel headerPanel; 43 public JPanel contentPanel; 44 45 private ArrayList listeners; 47 48 private int displayedMonth; 50 private int displayedYear; 51 52 private boolean sundayFirst = false; 53 54 58 public MonthView(CalendarPlugin plugin) 59 { 60 super(new BorderLayout()); 61 62 this.listeners = new ArrayList (); 63 64 this.headerPanel = new JPanel(new GridLayout(1, 7)); 65 this.contentPanel = new JPanel(new GridLayout(6, 7)); 66 this.add(headerPanel, BorderLayout.NORTH); 67 this.add(contentPanel, BorderLayout.CENTER); 68 sundayFirst = plugin.getLocalConfig().getInt("sundayFirst", 0) == 1; 69 70 if(sundayFirst) 71 headerPanel.add(new DayHeader(plugin.tr("day.7"))); 72 for(int i=1;i<=6;i++) 73 headerPanel.add(new DayHeader(plugin.tr("day."+i))); 74 if(!sundayFirst) 75 headerPanel.add(new DayHeader(plugin.tr("day.7"))); 76 77 for(int i=0;i<42;i++) 78 { 79 DayItem day = new DayItem(""+i); 80 day.addMouseListener(this); 81 contentPanel.add(day); 82 } 83 84 Calendar c = Calendar.getInstance(); 85 setDisplayedMonth(c.get(Calendar.MONTH)+1, c.get(Calendar.YEAR)); 86 setCurrentDay(c.get(Calendar.DAY_OF_MONTH)); 87 } 88 89 95 public void setDisplayedMonth(int month, int year) 96 { 97 this.displayedMonth = month; 98 this.displayedYear = year; 99 int curMonth = Calendar.getInstance().get(Calendar.MONTH)+1; 100 int curYear = Calendar.getInstance().get(Calendar.YEAR); 101 102 int day = this.getFirstDayOfDisplayedMonth(); 103 int max = this.getDaysOfDisplayedMonth(); 104 for(int i=0;i<day;i++) 105 { 106 DayItem item = getDayItem(i); 107 item.setBackground(Color.LIGHT_GRAY); 108 item.reset(""); 109 } 110 for(int i=0;i<max;i++) 111 { 112 DayItem item = getDayItem(i+day); 113 item.setBackground(Color.WHITE); 114 item.reset(""+ (i+1)); 115 } 116 for(int i=max+day;i<42;i++) 117 { 118 DayItem item = getDayItem(i); 119 item.setBackground(Color.LIGHT_GRAY); 120 item.reset(""); 121 } 122 123 if(curMonth == month && curYear == year) 124 setCurrentDay(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)); 125 else 126 setCurrentDay(0); 127 } 128 129 134 public void addCalendarListener(CalendarListener listener) 135 { 136 listeners.add(listener); 137 } 138 139 public void addEvent(int dayOfMonth, BasicEvent event, long fakeStart) 140 { 141 long duration = event.getEndDate().getTime() - fakeStart; 142 int dayLength = 24*60*60*1000; 143 int dayMax = getDaysOfDisplayedMonth() + getFirstDayOfDisplayedMonth()-1; 144 for(int i=0;i*dayLength<=duration;i++) 145 { 146 int dayIndex = i + dayOfMonth + getFirstDayOfDisplayedMonth()-1; 147 if(dayIndex > dayMax) 148 break; 149 150 getDayItem(dayIndex).addEvent(event, this); 151 } 152 } 153 154 159 private void setCurrentDay(int dayOfMonth) 160 { 161 for(int i=0;i<42;i++) 162 getDayItem(i).setCurrentDay(false); 163 164 if(dayOfMonth > 0) 165 getDayItem(dayOfMonth + getFirstDayOfDisplayedMonth()-1).setCurrentDay(true); 166 } 167 168 173 private int getFirstDayOfDisplayedMonth() 174 { 175 Calendar c = Calendar.getInstance(); 176 c.set(Calendar.MONTH, this.displayedMonth-1); 177 c.set(Calendar.YEAR, this.displayedYear); 178 179 return CalendarUtils.getFirstDayOfMonth(c, sundayFirst); 180 } 181 182 187 private int getDaysOfDisplayedMonth() 188 { 189 Calendar c = Calendar.getInstance(); 190 c.set(Calendar.DAY_OF_MONTH, 1); 191 c.set(Calendar.MONTH, this.displayedMonth-1); 192 c.set(Calendar.YEAR, this.displayedYear); 193 194 return CalendarUtils.getDaysOfMonth(c); 195 } 196 197 203 private DayItem getDayItem(int index) 204 { 205 return (DayItem)contentPanel.getComponent(index); 206 } 207 208 210 public void mousePressed(MouseEvent me) {} 211 public void mouseReleased(MouseEvent me) {} 212 public void mouseEntered(MouseEvent me) {} 213 public void mouseExited(MouseEvent me) {} 214 public void mouseClicked(MouseEvent me) 215 { 216 if(me.getSource() instanceof DayItem) 217 doDayClick((DayItem)me.getSource(), me.getClickCount()); 218 else if(me.getSource() instanceof EventLabel) 219 doEventClick((EventLabel)me.getSource(), me.getClickCount()); 220 } 221 222 private void doDayClick(DayItem day, int clickCount) 223 { 224 if(day.getDayOfMonth() < 0) 225 return; 226 227 Iterator i = listeners.iterator(); 228 while(i.hasNext()) 229 { 230 CalendarListener listener = (CalendarListener)i.next(); 231 listener.onDayClick(day, clickCount); 232 } 233 } 234 235 private void doEventClick(EventLabel event, int clickCount) 236 { 237 Iterator i = listeners.iterator(); 238 while(i.hasNext()) 239 { 240 CalendarListener listener = (CalendarListener)i.next(); 241 listener.onEventClick(event, clickCount); 242 } 243 } 244 } 245 246 247 | Popular Tags |