package calendar;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpringLayout.Constraints;
import javax.swing.SwingConstants;
import javax.swing.JSpinner.DateEditor;
import javax.swing.border.EtchedBorder;
import javax.swing.border.MatteBorder;
import com.toedter.calendar.JDateChooser;
/**
* @author Callum Eves & Thomas Milner
* This class was heavily modified by Thomas Milner
*/
@SuppressWarnings({ "serial", "unused" })
public class CalendarPanel extends JPanel implements ActionListener{
public static final int MONTH_VIEW = 0;
public static final int WEEK_VIEW = 1;
public static final int DAY_VIEW = 2;
public static final int LEFT_WIDTH = 370;
private int m_view;
private JPanel m_left_panel;
private MonthView m_right_panel;
Calindrom m_parent;
//button for left pane
static JTextField m_title_field;
static JTextArea m_description_field;
@SuppressWarnings("rawtypes")
static JComboBox m_type_box;
@SuppressWarnings("rawtypes")
JComboBox m_repetition_box;
JSpinner m_start_spinner;
JSpinner m_end_spinner;
JDateChooser m_date_field;
JButton m_submit_button, m_cancel_button;
JTextField m_guests_field;
JLabel m_guests_label, m_repetition_label, m_neworedit_label;
private boolean editing = false;
private int editingID = 0;
/**
* Constructor for CalendarPanel, the main Panel of the application
* @param parent The parent frame
*/
public CalendarPanel(Calindrom parent) {
m_parent = parent;
m_view = CalendarPanel.MONTH_VIEW;
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
//gradient background, credits to http://www.javarichclient.com/how-to-add-a-gradient-background-to-any-jcomponent/
m_left_panel = new JPanel() {
@Override
protected void paintComponent(Graphics grphcs) {
Graphics2D g2d = (Graphics2D) grphcs;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(0, 0,
Calindrom.gray_yellow, 0, getHeight(),
Calindrom.gray);
g2d.setPaint(gp);
g2d.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(grphcs);
}
};
m_left_panel.setOpaque(false);
m_left_panel.setPreferredSize(new Dimension(CalendarPanel.LEFT_WIDTH,
Integer.MAX_VALUE));
m_left_panel.setMaximumSize(new Dimension(CalendarPanel.LEFT_WIDTH,
Integer.MAX_VALUE));
InitLeftPanel();
m_right_panel = new MonthView(this);
//m_right_panel.setBackground(Color.blue);
//m_right_panel.setActionListener(this);
m_left_panel.setBorder(new MatteBorder(0,0,0,2, Color.black));
m_right_panel.setBorder(new MatteBorder(0,1,0,0, Color.gray));
//m_left_panel.setBackground(Color.red);
add(m_left_panel);
add(m_right_panel);
}
/**
* This method sets up the Left Panel for use, including the add event panel
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private void InitLeftPanel() { //init left panel, mini calendar | etc
final int TXT_MAX_HEIGHT = 50;
m_left_panel.setLayout(new BoxLayout(m_left_panel, BoxLayout.Y_AXIS));
JPanel title_panel = new JPanel();
title_panel.setBackground(Calindrom.brown);
title_panel.setBorder(new MatteBorder(0,0,1,0, Color.gray));
JLabel title_label = new JLabel("SwanCal v1.0");
title_label.setForeground(Calindrom.dirty_white);
title_panel.add(title_label);
m_left_panel.add(title_panel);
JPanel qadd_panel = new JPanel();
qadd_panel.setBackground(Calindrom.red);
qadd_panel.setBorder(new MatteBorder(2,0,2,0, Color.black));
JLabel qadd_label = new JLabel("Add New Event");
qadd_label.setForeground(Calindrom.dirty_white);
qadd_label.setHorizontalAlignment(SwingConstants.CENTER);
qadd_panel.setLayout(new GridLayout(1,1));
qadd_panel.add(qadd_label);
m_left_panel.add(qadd_panel);
JPanel add_panel = new JPanel();
add_panel.setBorder(new MatteBorder(1,0,1,0, Color.gray));
add_panel.setOpaque(false);
m_title_field = new JTextField();
m_date_field = new JDateChooser();
m_start_spinner = new JSpinner(new SpinnerDateModel());
m_start_spinner.setEditor(new DateEditor(m_start_spinner, "HH:mm"));
m_end_spinner = new JSpinner(new SpinnerDateModel());
m_end_spinner.setEditor(new DateEditor(m_end_spinner, "HH:mm"));
String[] types = {"General Event", "Work Event", "Social Event",
"Birthday Event"};
m_type_box = new JComboBox(types);
m_description_field = new JTextArea(5, 20);
m_description_field.setWrapStyleWord(true);
m_description_field.setLineWrap(true);
JScrollPane desc_panel = new JScrollPane(m_description_field);
//m_description_field.setMaximumSize(new Dimension(Integer.MAX_VALUE,
//TXT_MAX_HEIGHT));
desc_panel.setPreferredSize(new Dimension(Integer.MAX_VALUE,
TXT_MAX_HEIGHT));
String[] repetitions = {"Not Repeating", "Daily", "On Weekends",
"On Working Days", "Weekly",
"Every Two Weeks", "Every Four Weeks", "Monthly", "Yearly"};
m_repetition_box = new JComboBox(repetitions);
m_repetition_label = new JLabel("Repeating:");
m_submit_button = new JButton("Save");
m_cancel_button = new JButton("Cancel");
m_guests_field = new JTextField("0");
m_guests_label = new JLabel("No. Guests:");
m_neworedit_label = new JLabel("New Event:");
//m_left_panel.add(m_switch_button);
add_panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.ipady = 1;
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
add_panel.add(new JLabel("Type:"), c);
c.gridx++;
c.weightx = 1;
add_panel.add(m_type_box, c);
c.weightx = 0;
c.gridy++;
c.gridx=0;
add_panel.add(new JLabel("Title:"), c);
c.gridx++;
c.weightx = 1;
add_panel.add(m_title_field, c);
c.gridy++;
c.gridx=0;
c.weightx = 0;
add_panel.add(new JLabel("Date"), c);
c.gridx++;
c.weightx = 1;
add_panel.add(m_date_field, c);
c.gridy++;
c.gridx=0;
c.weightx = 0;
add_panel.add(new JLabel("Starting time:"), c);
c.gridx++;
c.weightx = 1;
add_panel.add(m_start_spinner, c);
c.gridy++;
c.gridx=0;
c.weightx = 0;
add_panel.add(new JLabel("Ending time:"), c);
c.gridx++;
c.weightx = 1;
add_panel.add(m_end_spinner, c);
c.gridy++;
c.gridx=0;
c.weightx = 0;
add_panel.add(m_guests_label, c);
c.gridx++;
c.weightx = 1;
add_panel.add(m_guests_field, c);
c.gridy++;
c.gridx=0;
c.weightx = 0;
add_panel.add(new JLabel("Description:"), c);
c.gridx++;
c.weightx = 1;
add_panel.add(desc_panel, c);
c.gridy++;
c.gridx=0;
c.weightx = 0;
add_panel.add(m_repetition_label, c);
c.gridx++;
c.weightx = 1;
add_panel.add(m_repetition_box, c);
c.gridy++;
add_panel.add(m_submit_button, c);
c.gridy++;
add_panel.add(m_cancel_button, c);
m_submit_button.addActionListener(this);
m_cancel_button.addActionListener(this);
m_type_box.addActionListener(this);
m_guests_field.setVisible(false);
m_guests_label.setVisible(false);
add_panel.setAlignmentY(Component.TOP_ALIGNMENT);
m_left_panel.add(add_panel);
JPanel cts_panel = new JPanel();
cts_panel.setBackground(Calindrom.red);
cts_panel.setBorder(new MatteBorder(2,0,2,0, Color.black));
JLabel cts_label = new JLabel("AddressBook");
cts_label.setForeground(Calindrom.dirty_white);
cts_label.setHorizontalAlignment(SwingConstants.CENTER);
cts_panel.setLayout(new GridLayout(1,1));
cts_panel.add(cts_label);
m_left_panel.add(cts_panel);
AddressBookPanel ab = new AddressBookPanel();
ab.setBorder(new MatteBorder(1,0,0,0,Color.gray));
ab.setOpaque(false);
m_left_panel.add(ab);
m_left_panel.add(Box.createVerticalGlue());
}
/**
* Creates a new event
* @throws Exception Exception if event is incorrect
*/
@SuppressWarnings("deprecation")
private void SubmitEvent() throws Exception{
String title = m_title_field.getText();
Date date_start = m_date_field.getDate();
Date date_end = m_date_field.getDate();
Date tmp = (Date)m_start_spinner.getValue();
date_start.setHours(tmp.getHours());
date_start.setMinutes(tmp.getMinutes());
tmp = (Date)m_end_spinner.getValue();
date_end.setHours(tmp.getHours());
date_end.setMinutes(tmp.getMinutes());
Calendar cal = Calendar.getInstance();
cal.setTime(date_start);
Calendar cale = Calendar.getInstance();
cale.setTime(date_end);
String description = m_description_field.getText();
int type = m_type_box.getSelectedIndex();
int repetition = m_repetition_box.getSelectedIndex();
int guests = new Integer(m_guests_field.getText()).intValue();
if(type == Event.BIRTHDAY_EVENT) {
if(editing) Data.EditBirthdayEvent(editingID, title,
cal, cale, description, guests);
else Data.AddBirthdayEvent(title, cal, cale, description,
guests);
} else if(type == Event.SOCIAL_EVENT) {
if(editing) Data.EditSocialEvent(editingID, title,
cal, cale, description, repetition, guests);
else Data.AddSocialEvent(title, cal, cale, description,
repetition, guests);
} else if(type == Event.WORK_EVENT) {
if(editing) Data.EditWorkEvent(editingID, title,
cal, cale, description, repetition, guests);
else Data.AddWorkEvent(title, cal, cale, description,
repetition, guests);
} else {
if(editing) Data.EditEvent(editingID, title, cal, cale,
description, repetition);
else Data.AddEvent(title, cal, cale, description,
repetition);
}
m_right_panel.RenderMV();
updateUI();
JOptionPane.showMessageDialog(this, "Event saved!", "OK!",
JOptionPane.INFORMATION_MESSAGE);
m_title_field.setText("");
m_description_field.setText("");
m_guests_field.setText("0");
m_type_box.setSelectedIndex(0);
m_repetition_box.setSelectedIndex(0);
}
/**
* Edits an event
* @throws Exception New exception if event is incorrect
*/
private void EditEvent() throws Exception{
Event temp = Data.GetEvents().GetEventById(editingID);
m_guests_field.setVisible(true);
m_guests_label.setVisible(true);
m_repetition_box.setVisible(true);
m_repetition_label.setVisible(true);
m_neworedit_label.setText("Edit an event:");
m_title_field.setText(temp.m_title);
m_description_field.setText(temp.m_description);
Calendar end = temp.m_end_date;
Calendar start = temp.m_start_date;
m_start_spinner.setValue(start.getTime());
m_end_spinner.setValue(end.getTime());
m_date_field.setCalendar(start);
m_repetition_box.setSelectedItem(temp.m_repetition);
if(temp.m_type == Event.GENERAL_EVENT){
m_guests_field.setVisible(false);
m_guests_label.setVisible(false);
m_type_box.setSelectedIndex(0);
}else if(temp.m_type == Event.BIRTHDAY_EVENT){
BirthdayEvent event = (BirthdayEvent)temp;
m_repetition_box.setSelectedIndex(8);
m_repetition_box.setVisible(false);
m_repetition_label.setVisible(false);
m_guests_field.setText("" + event.GetGuests());
m_type_box.setSelectedIndex(3);
}else{
if(temp.m_type == Event.WORK_EVENT){
m_guests_field.setText("" + ((WorkEvent)temp)
.GetGuests());
m_type_box.setSelectedIndex(1);
}else if(temp.m_type == Event.SOCIAL_EVENT){
m_guests_field.setText("" + ((SocialEvent)temp)
.GetGuests());
m_type_box.setSelectedIndex(2);
}
}
editing = true;
m_right_panel.RenderMV();
updateUI();
}
/**
* Event Handlers
*/
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == m_submit_button) {
try {
SubmitEvent();
editing = false;
m_neworedit_label.setText("New event:");
} catch(Exception exc) {
JOptionPane err = new JOptionPane(exc.getMessage(),
JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(this, exc.toString(),
"Error", JOptionPane.ERROR_MESSAGE);
Logger.getLogger(CalendarPanel.class.getName()).log(
Level.SEVERE, null, exc);
}
} else if(e.getSource() == m_cancel_button){
m_title_field.setText("");
m_description_field.setText("");
m_guests_field.setText("0");
m_type_box.setSelectedIndex(0);
m_repetition_box.setSelectedIndex(0);
updateUI();
m_neworedit_label.setText("New event:");
} else if(e.getSource() == m_type_box) {
if(m_type_box.getSelectedIndex() != 0) {
m_guests_field.setVisible(true);
m_guests_label.setVisible(true);
} else {
m_guests_field.setVisible(false);
m_guests_label.setVisible(false);
}
if(m_type_box.getSelectedIndex() == 3){
m_repetition_box.setSelectedIndex(8);
m_repetition_box.setVisible(false);
m_repetition_label.setVisible(false);
} else {
m_repetition_box.setVisible(true);
m_repetition_label.setVisible(true);
}
} else if(((JButton) e.getSource()).getToolTipText().equals("edit")){
editingID = Integer.parseInt(((JButton)e.getSource()).getName());
try {
EditEvent();
} catch (Exception ex) {
Logger.getLogger(EventPanel.class.getName()).log(Level.SEVERE,
null, ex);
}
} else if(((JButton) e.getSource()).getToolTipText().equals("delete")){
int id = Integer.parseInt(((JButton)e.getSource()).getName());
int reply = JOptionPane.showConfirmDialog(this, "Are you " +
"sure you wish to delete this event?",
"Confirm Delete",
JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
try {
Data.RemoveEvent(id);
m_right_panel.RenderMV();
updateUI();
} catch (Exception ex) {
Logger.getLogger(EventPanel.class.getName()).log(
Level.SEVERE, null, ex);
}
}
}
}
/*public static void main (String[] args) throws Exception{
Data.Init();
//Make a new frame for the test event
JFrame test = new JFrame();
Calendar cal = Calendar.getInstance();
cal.add(cal.BIRTHDAY_EVENT);
Data.AddEvent("TestEvent", Calendar.getInstance(), cal,
"Test", 0);
//creates a new panel for the test event
CalendarPanel calTest = new CalendarPanel(cal, 2012);
test.getContentPane().add(calTest);
test.pack();
test.setVisible(true);
}*/
}