summaryrefslogtreecommitdiff
path: root/sources/client/JCalendarDatePicker.java
blob: 3d71a57c42b5c0de50c6243c4b2d79d2d42d044f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

/* NetCalendar 2006, 2009 (c) Dipl.-Inform. (FH) Paul C. Buetow
 * http://buetow.org - netcalendar@dev.buetow.org
 */

/**
 */

package client;

import shared.*;
import shared.remotecall.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Calendar;
import java.util.Locale;
import org.freixas.jcalendar.*;

/**
 * This is the class for the date picker
 * @author Paul C. Buetow
 */
public class JCalendarDatePicker extends SubWindow {
    private JCalendar jcalendar;
    private Calendar calendar;
    private CalendarEvent calendarEvent;

    /**
     * Creates a date picker. It can be used to pick a date.
     * @param netCalendarClient The NetCalendarClient object
     */
    public JCalendarDatePicker(NetCalendarClient netCalendarClient) {
        super("Calendar", netCalendarClient);

        initComponents();
        setResizable(false);
        pack();
        setVisible(true);
    }

    /**
     * Creates a date picker. It can be used to pick a date.
     * @param netCalendarClient The NetCalendarClient object
     */
    public JCalendarDatePicker(NetCalendarClient netCalendarClient,
                               CalendarEvent calendarEvent) {
        super("Set date for event #" + calendarEvent.getEventID() +
              ": " + calendarEvent.getDescription(), netCalendarClient);
        this.calendarEvent = calendarEvent;

        initComponents();
        setResizable(false);
        pack();
        setVisible(true);
    }

    /**
     * Inits the component
     */
    protected void initComponents() {
        JPanel contentPane = (JPanel) getContentPane();
        contentPane.setBackground(Color.BLACK);
        JCalendarDateListener listener = new JCalendarDateListener();

        jcalendar = new JCalendar(JCalendar.DISPLAY_DATE, false);
        if (calendarEvent != null)
            jcalendar.setDate(calendarEvent.getDate());
        jcalendar.addDateListener(listener);
        contentPane.add(jcalendar);
    }

    /**
     * Gets the current selected date of the date picker
     * @return Current selected date
     */
    public Calendar getSelectedDate() {
        synchronized (jcalendar) {
            return calendar;
        }
    }

    /**
     * Helper class for the DateListener of the JCalendar object
     */
    private class JCalendarDateListener implements DateListener {
        public void dateChanged(DateEvent dateEvent) {
            synchronized (jcalendar) {
                calendar = dateEvent.getSelectedDate();
                if (calendarEvent != null) {
                    calendarEvent.setDate(calendar.getTime());
                    ClientRequest clientRequest = new ClientRequest(ClientRequest.MODIFY_EVENT);
                    clientRequest.setEvent(calendarEvent);

                    ServerRequester.sendClientRequest(clientRequest);
                    netCalendarClient.updateLast();
                }
            }
        }

    }
}