1 19 package org.lucane.applications.calendar.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.text.DateFormat ; 27 import java.util.ArrayList ; 28 import java.util.Calendar ; 29 import java.util.Date ; 30 import java.util.Iterator ; 31 32 import javax.swing.JButton ; 33 import javax.swing.JLabel ; 34 import javax.swing.JPanel ; 35 36 import org.lucane.applications.calendar.Attendee; 37 import org.lucane.applications.calendar.CalendarPlugin; 38 import org.lucane.applications.calendar.Event; 39 import org.lucane.applications.calendar.widget.CalendarListener; 40 import org.lucane.applications.calendar.widget.DayItem; 41 import org.lucane.applications.calendar.widget.EventLabel; 42 import org.lucane.applications.calendar.widget.freebusy.FreeBusyPanel; 43 import org.lucane.client.Client; 44 import org.lucane.client.widgets.DialogBox; 45 import org.lucane.client.widgets.ManagedWindow; 46 47 public class FreeBusyDialog extends ManagedWindow 48 implements ActionListener , CalendarListener 49 { 50 private CalendarPlugin plugin; 51 private Event event; 52 private Calendar calendar; 53 private EventDescriptionPanel description; 54 55 private JButton previousDay; 56 private JButton nextDay; 57 private JLabel day; 58 private FreeBusyPanel freebusy; 59 60 private int workStart = 8; 61 private int workEnd = 17; 62 private Color unworkedHour = new Color (160, 155, 150); 63 private Color workedHour = new Color (255, 255, 222); 64 65 public FreeBusyDialog(CalendarPlugin plugin, Event event, EventDescriptionPanel description) 66 { 67 super(plugin, plugin.tr("dialog.findFreeTime")); 68 setName("freebusy"); 69 this.plugin = plugin; 70 this.event = event; 71 this.description = description; 72 73 this.calendar = Calendar.getInstance(); 74 calendar.setTime(event.getStartDate()); 75 calendar.set(Calendar.HOUR_OF_DAY, 0); 76 calendar.set(Calendar.MINUTE, 0); 77 calendar.set(Calendar.SECOND, 0); 78 calendar.set(Calendar.MILLISECOND, 0); 79 80 this.workStart = plugin.getLocalConfig().getInt("workStart", workStart); 81 this.workEnd = plugin.getLocalConfig().getInt("workEnd", workEnd); 82 this.unworkedHour = plugin.getColor("unworked", unworkedHour); 83 this.workedHour = plugin.getColor("worked", workedHour); 84 85 this.previousDay = new JButton (plugin.getImageIcon("previous.png")); 86 previousDay.addActionListener(this); 87 this.nextDay = new JButton (plugin.getImageIcon("next.png")); 88 nextDay.addActionListener(this); 89 this.day = new JLabel (); 90 this.day.setHorizontalAlignment(JLabel.CENTER); 91 92 JPanel top = new JPanel (new BorderLayout ()); 93 top.add(previousDay, BorderLayout.WEST); 94 top.add(day, BorderLayout.CENTER); 95 top.add(nextDay, BorderLayout.EAST); 96 getContentPane().add(top, BorderLayout.NORTH); 97 98 this.freebusy = new FreeBusyPanel(workStart, workEnd, unworkedHour, workedHour); 99 freebusy.addCalendarListener(this); 100 getContentPane().add(freebusy, BorderLayout.CENTER); 101 102 setPreferredSize(new Dimension (600, 300)); 103 104 refreshView(); 105 } 106 107 private void refreshView() 108 { 109 long startTime = calendar.getTimeInMillis(); 110 long endTime = startTime + 24*60*60*1000; 111 112 DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); 113 this.day.setText(df.format(calendar.getTime())); 114 115 freebusy.resetLines(); 116 117 String userName = Client.getInstance().getMyInfos().getName(); 119 try { 120 ArrayList events = plugin.getMyEvents(startTime, endTime); 121 if(event.getId() >= 0) 122 events.remove(event); 123 freebusy.addLine(Client.getInstance().getMyInfos().getName(), events, true, startTime, endTime); 124 } catch(Exception e) { 125 DialogBox.error(plugin.tr("err.unableToGetEventsForUser") + userName); 126 } 127 128 Iterator i = event.getAttendees(); 130 while(i.hasNext()) 131 { 132 Attendee a = (Attendee)i.next(); 133 try { 134 ArrayList events = plugin.getEventsForUser(a.getUser(), startTime, endTime); 135 if(event.getId() >= 0) 136 events.remove(event); 137 freebusy.addLine(a.getUser(), events, a.isMandatory(), startTime, endTime); 138 } catch(Exception e) { 139 DialogBox.error(plugin.tr("err.unableToGetEventsForUser") + a.getUser()); 140 } 141 } 142 143 freebusy.scrollToHour(workStart); 145 } 146 147 public void actionPerformed(ActionEvent ae) 148 { 149 if(ae.getSource() == previousDay) 150 calendar.add(Calendar.DAY_OF_YEAR, -1); 151 else if(ae.getSource() == nextDay) 152 calendar.add(Calendar.DAY_OF_YEAR, 1); 153 154 refreshView(); 155 } 156 157 public void onDayClick(DayItem day, int clickCount) {} 158 public void onEventClick(EventLabel event, int clickCount) {} 159 public void onHourClick(int hour, int minute, int clickCount) 160 { 161 if(minute < 7) 162 minute = 0; 163 else if(minute < 22) 164 minute = 15; 165 else if(minute < 38) 166 minute = 30; 167 else if(minute < 55) 168 minute = 45; 169 else { 170 hour++; 171 minute = 0; 172 } 173 174 Calendar cal = Calendar.getInstance(); 175 cal.setTimeInMillis(calendar.getTimeInMillis()); 176 cal.set(Calendar.HOUR_OF_DAY, hour); 177 cal.set(Calendar.MINUTE, minute); 178 Date start = cal.getTime(); 179 180 long duration = event.getEndDate().getTime() - event.getStartDate().getTime(); 181 cal.setTimeInMillis(start.getTime() + duration); 182 Date end = cal.getTime(); 183 184 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT); 185 String msg = plugin.tr("msg.changeEventDate"); 186 msg += "\n\n"; 187 msg += plugin.tr("event.start") + " : \t" + df.format(start); 188 msg += '\n'; 189 msg += plugin.tr("event.end") + " : \t" + df.format(end); 190 msg += '\n'; 191 192 if(DialogBox.question(plugin.tr("dialog.changeEventDate"), msg)) 193 { 194 event.setStartDate(start); 195 event.setEndDate(end); 196 197 description.resetDates(); 198 dispose(); 199 } 200 } 201 } | Popular Tags |