1 19 package org.lucane.applications.calendar.widget.freebusy; 20 21 import java.awt.Color ; 22 import java.awt.Cursor ; 23 import java.awt.Dimension ; 24 import java.awt.Font ; 25 import java.awt.Graphics ; 26 import java.awt.event.MouseEvent ; 27 import java.awt.event.MouseListener ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 31 import javax.swing.JPanel ; 32 33 import org.lucane.applications.calendar.widget.CalendarListener; 34 35 public class FreeBusyRuler extends JPanel 36 implements MouseListener 37 { 38 private int workStart; 39 private int workEnd; 40 private Color unworkedHour; 41 private Color workedHour; 42 43 private Font font = new Font ("Verdana", Font.PLAIN, 10); 44 private ArrayList listeners = new ArrayList (); 45 46 public FreeBusyRuler(int workStart, int workEnd, Color unworkedHour, Color workedHour) 47 { 48 this.workStart = workStart; 49 this.workEnd = workEnd; 50 this.unworkedHour = unworkedHour; 51 this.workedHour = workedHour; 52 53 setPreferredSize(new Dimension (24*FreeBusyPanel.HOUR_WIDTH, FreeBusyPanel.HEIGHT)); 54 55 setOpaque(true); 56 setBackground(Color.WHITE); 57 setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 58 addMouseListener(this); 59 } 60 61 public void addCalendarListener(CalendarListener listener) 62 { 63 listeners.add(listener); 64 } 65 66 public void removeCalendarListener(CalendarListener listener) 67 { 68 listeners.remove(listener); 69 } 70 71 protected void paintComponent(Graphics g) { 72 super.paintComponent(g); 73 74 g.setColor(unworkedHour); 76 g.fillRect(0, 0, 24*FreeBusyPanel.HOUR_WIDTH, FreeBusyPanel.HEIGHT); 77 g.setColor(workedHour); 78 int x = workStart*FreeBusyPanel.HOUR_WIDTH; 79 int w = (workEnd-workStart)*FreeBusyPanel.HOUR_WIDTH; 80 g.fillRect(x, 0, w, FreeBusyPanel.HEIGHT); 81 82 g.setColor(Color.BLACK); 84 for(int i=0;i<24*4;i++) 85 { 86 x = i*FreeBusyPanel.HOUR_WIDTH/4; 87 int h = 2; 88 if(i%2 == 0) 89 h = 3; 90 if(i%4 == 0) 91 h = 5; 92 g.drawLine(x, FreeBusyPanel.HEIGHT, x, FreeBusyPanel.HEIGHT-h); 93 } 94 95 g.setColor(Color.RED); 97 int xStart = workStart*FreeBusyPanel.HOUR_WIDTH; 98 g.drawLine(xStart, 0, xStart, 2); 99 g.drawLine(xStart, FreeBusyPanel.HEIGHT, xStart, FreeBusyPanel.HEIGHT-5); 100 int xEnd = workEnd*FreeBusyPanel.HOUR_WIDTH; 101 g.drawLine(xEnd, 0, xEnd, 2); 102 g.drawLine(xEnd, FreeBusyPanel.HEIGHT, xEnd, FreeBusyPanel.HEIGHT-5); 103 104 g.setColor(Color.BLACK); 106 g.setFont(font); 107 for(int i=0;i<24;i++) 108 { 109 x = i*FreeBusyPanel.HOUR_WIDTH; 110 x = i<10 ? x-3 : x-6; 111 g.drawString(String.valueOf(i), x, 14); 112 } 113 } 114 115 public void mouseEntered(MouseEvent e) {} 116 public void mouseExited(MouseEvent e) {} 117 public void mousePressed(MouseEvent e) {} 118 public void mouseReleased(MouseEvent e) {} 119 public void mouseClicked(MouseEvent e) 120 { 121 int x = e.getPoint().x; 122 123 int hour = x / FreeBusyPanel.HOUR_WIDTH; 124 int minute = ((x % FreeBusyPanel.HOUR_WIDTH) * 60) / FreeBusyPanel.HOUR_WIDTH; 125 126 fireHourClick(hour, minute, e.getClickCount()); 127 } 128 129 private void fireHourClick(int hour, int minute, int clickCount) 130 { 131 Iterator i = listeners.iterator(); 132 while(i.hasNext()) 133 { 134 CalendarListener cl = (CalendarListener)i.next(); 135 cl.onHourClick(hour, minute, clickCount); 136 } 137 } 138 } | Popular Tags |