FreeBusyDialog.java :  » Groupware » lucane » org » lucane » applications » calendar » gui » Java Open Source

Java Open Source » Groupware » lucane 
lucane » org » lucane » applications » calendar » gui » FreeBusyDialog.java
/*
 * Lucane - a collaborative platform
 * Copyright (C) 2004  Vincent Fiack <vfiack@mail15.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.lucane.applications.calendar.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.lucane.applications.calendar.Attendee;
import org.lucane.applications.calendar.CalendarPlugin;
import org.lucane.applications.calendar.Event;
import org.lucane.applications.calendar.widget.CalendarListener;
import org.lucane.applications.calendar.widget.DayItem;
import org.lucane.applications.calendar.widget.EventLabel;
import org.lucane.applications.calendar.widget.freebusy.FreeBusyPanel;
import org.lucane.client.Client;
import org.lucane.client.widgets.DialogBox;
import org.lucane.client.widgets.ManagedWindow;

public class FreeBusyDialog extends ManagedWindow
implements ActionListener, CalendarListener
{
  private CalendarPlugin plugin;
  private Event event;
  private Calendar calendar;
  private EventDescriptionPanel description;
  
  private JButton previousDay;
  private JButton nextDay;
  private JLabel day;
  private FreeBusyPanel freebusy;
  
  private int workStart = 8;
  private int workEnd = 17;
  private Color unworkedHour = new Color(160, 155, 150);
  private Color workedHour = new Color(255, 255, 222);

  public FreeBusyDialog(CalendarPlugin plugin, Event event, EventDescriptionPanel description)
  {
    super(plugin, plugin.tr("dialog.findFreeTime"));
    setName("freebusy");
    this.plugin = plugin;
    this.event = event;
    this.description = description;
    
    this.calendar = Calendar.getInstance();
    calendar.setTime(event.getStartDate());
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    
    this.workStart = plugin.getLocalConfig().getInt("workStart", workStart);
    this.workEnd = plugin.getLocalConfig().getInt("workEnd", workEnd);
    this.unworkedHour = plugin.getColor("unworked", unworkedHour);
    this.workedHour = plugin.getColor("worked", workedHour);

    this.previousDay = new JButton(plugin.getImageIcon("previous.png"));
    previousDay.addActionListener(this);
    this.nextDay = new JButton(plugin.getImageIcon("next.png"));
    nextDay.addActionListener(this);
    this.day = new JLabel();
    this.day.setHorizontalAlignment(JLabel.CENTER);
    
    JPanel top = new JPanel(new BorderLayout());
    top.add(previousDay, BorderLayout.WEST);
    top.add(day, BorderLayout.CENTER);
    top.add(nextDay, BorderLayout.EAST);    
    getContentPane().add(top, BorderLayout.NORTH);
    
    this.freebusy = new FreeBusyPanel(workStart, workEnd, unworkedHour, workedHour);
    freebusy.addCalendarListener(this);
    getContentPane().add(freebusy, BorderLayout.CENTER);
    
    setPreferredSize(new Dimension(600, 300));
    
    refreshView();    
  }
  
  private void refreshView()
  {    
    long startTime = calendar.getTimeInMillis();
    long endTime = startTime + 24*60*60*1000;    

    DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
    this.day.setText(df.format(calendar.getTime()));

    freebusy.resetLines();
    
    //get events for organizer
    String userName = Client.getInstance().getMyInfos().getName();
    try {
      ArrayList events = plugin.getMyEvents(startTime, endTime);
      if(event.getId() >= 0)
        events.remove(event);
      freebusy.addLine(Client.getInstance().getMyInfos().getName(), events, true, startTime, endTime);
    } catch(Exception e) {
      DialogBox.error(plugin.tr("err.unableToGetEventsForUser") + userName);
    }
    
    //get events for attendees
    Iterator i = event.getAttendees();
    while(i.hasNext())
    {
      Attendee a = (Attendee)i.next();      
      try {
        ArrayList events = plugin.getEventsForUser(a.getUser(), startTime, endTime);
        if(event.getId() >= 0)
          events.remove(event);
        freebusy.addLine(a.getUser(), events, a.isMandatory(), startTime, endTime);
      } catch(Exception e) {
        DialogBox.error(plugin.tr("err.unableToGetEventsForUser") + a.getUser());
      }
    }
    
    //auto scroll
    freebusy.scrollToHour(workStart);
  }
  
  public void actionPerformed(ActionEvent ae)
  {
    if(ae.getSource() == previousDay)
      calendar.add(Calendar.DAY_OF_YEAR, -1);
    else if(ae.getSource() == nextDay)
      calendar.add(Calendar.DAY_OF_YEAR, 1);
    
    refreshView();
  }

  public void onDayClick(DayItem day, int clickCount) {}
  public void onEventClick(EventLabel event, int clickCount) {}
  public void onHourClick(int hour, int minute, int clickCount) 
  {
    if(minute < 7)
      minute = 0;
    else if(minute < 22)
      minute = 15;
    else if(minute < 38)
      minute = 30;
    else if(minute < 55)
      minute = 45;
    else {
      hour++;
      minute = 0;
    }
      
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(calendar.getTimeInMillis());
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE, minute);
    Date start = cal.getTime();
    
    long duration = event.getEndDate().getTime() - event.getStartDate().getTime();
    cal.setTimeInMillis(start.getTime() + duration);
    Date end = cal.getTime();
    
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT);
    String msg = plugin.tr("msg.changeEventDate");
    msg += "\n\n";
    msg += plugin.tr("event.start") + " : \t" + df.format(start); 
    msg += '\n';
    msg += plugin.tr("event.end") + " : \t" + df.format(end);
    msg += '\n';
    
    if(DialogBox.question(plugin.tr("dialog.changeEventDate"), msg))
    {
      event.setStartDate(start);
      event.setEndDate(end);
      
      description.resetDates();
      dispose();
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.