package calendar; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calendar; import java.util.Locale; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; import javax.swing.border.MatteBorder; /** * * @author Callum Eves */ @SuppressWarnings("serial") public class MonthView extends JPanel implements ActionListener{ public static final int DAYS_IN_A_WEEK = 7; public static final int WEEKS_IN_A_MONTH = 6; public static final int TOP_PADDING = 0; public static final int MIDDLE_PADDING = 2; private JPanel m_top_panel; private JPanel m_middle_panel; private JPanel m_bottom_panel; private int m_month; private int m_year; /* * buttons to be displayed on the middle pane */ private JLabel m_date_label; private JButton m_next_button; private JButton m_prev_button; private ActionListener CalListener; /* * method to create MonthView */ public MonthView(ActionListener CalListener) { m_month = Data.GetCurrentMonth(); m_year = Data.GetCurrentYear(); /* * layout, orientation and colour of the views and the panes within */ setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1; c.weighty = 0; c.gridx = 0; c.gridy = 0; m_top_panel = new JPanel(new BorderLayout()); m_top_panel.setBackground(Calindrom.brown); m_middle_panel = new JPanel(new GridLayout( 1, MonthView.DAYS_IN_A_WEEK)); m_middle_panel.setBackground(Calindrom.red); m_middle_panel.setForeground(Calindrom.dirty_white); m_bottom_panel = new JPanel(new GridLayout(MonthView.WEEKS_IN_A_MONTH, MonthView.DAYS_IN_A_WEEK)); m_bottom_panel.setBackground(Calindrom.dirty_white); m_top_panel.setBorder(new MatteBorder(0, 0, 1, 0, Color.gray)); m_middle_panel.setBorder(new MatteBorder(2, 0, 2, 0, Color.black)); m_bottom_panel.setBorder(new MatteBorder(1, 0, 0, 0, Color.gray)); m_prev_button = new JButton("prev"); m_prev_button.setBackground(Calindrom.red); m_prev_button.setForeground(Calindrom.dirty_white); m_next_button = new JButton("next"); m_next_button.setBackground(Calindrom.red); m_next_button.setForeground(Calindrom.dirty_white); m_date_label = new JLabel(); m_date_label.setForeground(Calindrom.dirty_white); m_date_label.setHorizontalAlignment(SwingConstants.CENTER); /* * listeners for buttons waiting to be clicked */ m_prev_button.addActionListener(this); m_next_button.addActionListener(this); /* * Orientation of panels displayed */ m_top_panel.add(m_prev_button, BorderLayout.WEST); m_top_panel.add(m_next_button, BorderLayout.EAST); m_top_panel.add(m_date_label, BorderLayout.CENTER); c.ipady = MonthView.TOP_PADDING; add(m_top_panel, c); c.ipady = MonthView.MIDDLE_PADDING; c.gridy++; add(m_middle_panel, c); c.ipady = 0; c.weighty = 1; c.gridy++; c.fill = GridBagConstraints.BOTH; add(m_bottom_panel, c); this.CalListener = CalListener; RenderMV(); } /* * visualising the view */ public void RenderMV() { /* * remove components */ m_middle_panel.removeAll(); m_bottom_panel.removeAll(); /* * setting the date */ Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.MONTH, m_month); cal.set(Calendar.YEAR, m_year); /* * set month and year in the label */ m_date_label.setText(cal.getDisplayName(Calendar.MONTH, Calendar.LONG, new Locale(Data.GetSettings().Get("locale")))+" "+m_year); /* * find the first monday */ while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) { cal.add(Calendar.DAY_OF_MONTH, -1); } /* * putting the panels in bottom_panel and days in middle_panel */ for(int i = 0; i < MonthView.WEEKS_IN_A_MONTH; i++) { for(int j = 0; j < MonthView.DAYS_IN_A_WEEK; j++) { if(i == 0) { JLabel lbl = new JLabel(cal.getDisplayName( Calendar.DAY_OF_WEEK, Calendar.SHORT, new Locale(Data.GetSettings().Get("locale")))); lbl.setForeground(Calindrom.dirty_white); m_middle_panel.add(lbl); } JScrollPane spane = new JScrollPane(new DayPanel(cal, m_month, m_year, CalListener)); spane.setBorder(new EmptyBorder(0, 0, 0, 0)); m_bottom_panel.add(spane); cal.add(Calendar.DAY_OF_MONTH, 1); } } } @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == m_prev_button) { m_month--; if(m_month < 0) { m_month += 12; m_year--; } RenderMV(); } else if(e.getSource() == m_next_button) { m_month++; if(m_month > 11) { m_month -= 12; m_year++; } RenderMV(); } } /* * Test to create a new event in the MonthView class */ /*public static void main (String[] args) throws Exception{ Data.Init(); //makes new frame for month view JFrame test = new JFrame(); Calendar cal = Calendar.getInstance(); cal.add(cal.DAY_OF_MONTH, 1); // add a test event to month view Data.AddEvent("NEW EVENT", Calendar.getInstance(), cal, "Test", 2); //Make a new frame MonthView testM = new MonthView(cal, 2012); test.getContentPane().add(testM); test.pack(); test.setVisible(true); } */ }