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

Java Open Source » Groupware » lucane 
lucane » org » lucane » applications » calendar » gui » CalendarViewer.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 javax.swing.*;

import org.lucane.client.widgets.DialogBox;
import org.lucane.client.widgets.ManagedWindow;
import org.lucane.applications.calendar.CalendarPlugin;
import org.lucane.applications.calendar.widget.*;
import org.lucane.applications.calendar.Event;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class CalendarViewer extends ManagedWindow
implements ActionListener, CalendarListener
{
  private MonthPanel monthPanel;
  private WeekPanel weekPanel;
  private DayPanel dayPanel;
  
  private JButton goToCurrentMonth;
  private JButton goToCurrentDay;
  private JButton close;
  
  private transient CalendarPlugin plugin;
  
  public CalendarViewer(CalendarPlugin plugin, String userName)
  {
    super(plugin, plugin.getTitle() + " - " +userName);
    getContentPane().setLayout(new BorderLayout());
    this.plugin = plugin;
    
    monthPanel = new MonthPanel(plugin, this, userName);
    weekPanel = new WeekPanel(plugin, this, userName);
    dayPanel = new DayPanel(plugin, this, userName);
  
    goToCurrentMonth = new JButton(tr("btn.thisMonth"));
    goToCurrentDay = new JButton(tr("btn.today"));
    close = new JButton(tr("btn.close"));
    
    try {
      goToCurrentMonth.setIcon(plugin.getImageIcon("jumpTo.png"));
      goToCurrentDay.setIcon(plugin.getImageIcon("jumpTo.png"));
      close.setIcon(plugin.getImageIcon("close.png"));
    } catch(Exception e) {
      //nothing, no icons :-/
    }

    goToCurrentMonth.addActionListener(this);
    goToCurrentDay.addActionListener(this);
    close.addActionListener(this);

    JPanel topbar = new JPanel(new BorderLayout());
    initTopBar(topbar);
    
    getContentPane().add(topbar, BorderLayout.NORTH);
    getContentPane().add(monthPanel, BorderLayout.CENTER);
  }
  
  private void initTopBar(JPanel bar)
  {    
    JPanel buttons = new JPanel(new GridLayout(1, 3));
    
    buttons.add(goToCurrentMonth);
    buttons.add(goToCurrentDay);
    buttons.add(close);
    
    bar.add(buttons, BorderLayout.EAST);
    bar.setBorder(BorderFactory.createEmptyBorder(2, 15, 5, 1));
  }

  public void actionPerformed(ActionEvent ae)
  {
    if(ae.getSource() == goToCurrentMonth)
    {
      monthPanel.showMonth(Calendar.getInstance());
      if(getContentPane().getComponent(1) != monthPanel)
      {
        getContentPane().remove(dayPanel);
        getContentPane().add(monthPanel, BorderLayout.CENTER);
        getContentPane().validate();
        getContentPane().repaint();
      }
    }
    else if(ae.getSource() == goToCurrentDay)
    {
      dayPanel.showDay(Calendar.getInstance());
      if(getContentPane().getComponent(1) != dayPanel)
      {
        getContentPane().remove(monthPanel);
        getContentPane().add(dayPanel, BorderLayout.CENTER);
        getContentPane().validate();
        getContentPane().repaint();
      }
    }
    else if(ae.getSource() == close)
    {
      this.dispose();
    }
  } 
  
  public void onDayClick(DayItem day, int clickCount) 
  {
    if(clickCount == 1) 
    {
      int dayOfMonth = day.getDayOfMonth();
      Calendar cal = monthPanel.getCalendar();
      cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
      dayPanel.showDay(cal);
      getContentPane().remove(monthPanel);
      getContentPane().add(dayPanel, BorderLayout.CENTER);
      getContentPane().validate();
      getContentPane().repaint();
    }
  }
  
  public void onHourClick(int hour, int minute, int clickCount)
  {
    //nothing, only viewing here
  }

  public void onEventClick(EventLabel label, int clickCount) 
  {
    if(clickCount == 1)
    {
      Event event = (Event)label.getEvent();
      if(event.isPublic())
        new EventFrame(plugin, event, dayPanel, weekPanel, monthPanel).show();
      else
        DialogBox.info(tr("event.is.private"));
    }
  }
  
  private String tr(String s)
  {
    return plugin.tr(s);
  }
}
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.