Revision 270 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.serv/org.gvsig.tools.swing.serv.field/src/main/java/org/gvsig/tools/swing/serv/field/component/date/DateCalendarPicker.java

View differences:

DateCalendarPicker.java
27 27
import org.freixas.jcalendar.DateListener;
28 28
import org.freixas.jcalendar.JCalendar;
29 29

  
30
public class DateCalendarPicker extends JPanel implements DateListener{
30
public class DateCalendarPicker extends JPanel implements DateListener {
31 31

  
32
	private JCalendar jCal = null;
33
	boolean okPressed = false;
34
	private Calendar calendar;
32
    private JCalendar jCal = null;
33
    boolean okPressed = false;
34
    private Calendar calendar;
35 35
    private JLabel dateLabel;
36
	private DateFormat formatter;
37
	private JButton buttonOK;
38
	private JButton buttonCancel;
39
	private Date iniDate;
36
    private DateFormat formatter;
37
    private JButton buttonOK;
38
    private JButton buttonCancel;
39
    private Date iniDate;
40 40

  
41
	
42
	public DateCalendarPicker(Date date) {
43
		this(date, Locale.getDefault());
44
	}	
45
	
46
	public DateCalendarPicker(Date date, Locale loc) {
47
		super();
48
		if (date == null)
49
			date = new Date();
50
		this.iniDate = date;
51
		this.setLocale(loc);
52
		beforeUI(date);
53
		initUI();
54
	}
41
    public DateCalendarPicker(Date date) {
42
        this(date, Locale.getDefault());
43
    }
55 44

  
56
	public JLabel getLbl(){
57
		if (this.dateLabel==null){
58
			this.dateLabel = new JLabel(getDateString());
59
			// Label settings
60
			this.dateLabel.setBorder(BorderFactory.createEmptyBorder(8, 8, 5, 8));
61
			this.dateLabel.setHorizontalAlignment(JFormattedTextField.CENTER);
62
			this.dateLabel.setBackground(Color.BLUE.brighter());
63
			this.dateLabel.setForeground(Color.BLUE.brighter());
64
			this.dateLabel.setFont(new Font("SansSerif", Font.BOLD, 18));		
65
		}
66
		return this.dateLabel;
67
	}
68
	
69
	private void initUI() {
70
		//Setting border
71
		Border compoundBorder = createCompoundBorder();
72
		this.setBorder(compoundBorder);
73
		
74
		JPanel datePanel = new JPanel(new GridBagLayout());
75
		GridBagConstraints c = new GridBagConstraints();
76
		c.insets = new Insets(2, 2, 4, 2);
77
		c.fill = GridBagConstraints.NONE;
78
		c.gridx = 0;
79
		datePanel.add(this.getUICalendar(), c);
45
    public DateCalendarPicker(Date date, Locale loc) {
46
        super();
47
        if (date == null)
48
            date = new Date();
49
        this.iniDate = date;
50
        this.setLocale(loc);
51
        beforeUI(date);
52
        initUI();
53
    }
80 54

  
55
    public JLabel getLbl() {
56
        if (this.dateLabel == null) {
57
            this.dateLabel = new JLabel(getDateString());
58
            // Label settings
59
            this.dateLabel.setBorder(BorderFactory
60
                .createEmptyBorder(8, 8, 5, 8));
61
            this.dateLabel.setHorizontalAlignment(JFormattedTextField.CENTER);
62
            this.dateLabel.setBackground(Color.BLUE.brighter());
63
            this.dateLabel.setForeground(Color.BLUE.brighter());
64
            this.dateLabel.setFont(new Font("SansSerif", Font.BOLD, 18));
65
        }
66
        return this.dateLabel;
67
    }
81 68

  
82
		JPanel buttonsPanel = new JPanel(new GridBagLayout());
83
		c = new GridBagConstraints();
84
		c.insets = new Insets(2, 2, 4, 2);
85
		c.fill = GridBagConstraints.NONE;
86
		buttonsPanel.add(getButtonOK(), c);
87
		c.insets = new Insets(2, 4, 2, 2);
88
		c.fill = GridBagConstraints.NONE;
89
		buttonsPanel.add(getButtonCancel(), c);
69
    private void initUI() {
70
        // Setting border
71
        Border compoundBorder = createCompoundBorder();
72
        this.setBorder(compoundBorder);
90 73

  
91
		JPanel headerPanel = new JPanel(new GridBagLayout());
92
		headerPanel.add(getLbl());
93
		headerPanel.add(new JSeparator());
94
		headerPanel.setBackground( new Color(0, 0, 255, 20) );
95
		
96
//		this.add(getLbl(), BorderLayout.PAGE_START);
97
		this.add(headerPanel, BorderLayout.PAGE_START);
98
		this.add(datePanel, BorderLayout.CENTER);
99
		this.add(buttonsPanel, BorderLayout.PAGE_END);
100
		
101
	}
74
        JPanel datePanel = new JPanel(new GridBagLayout());
75
        GridBagConstraints c = new GridBagConstraints();
76
        c.insets = new Insets(2, 2, 4, 2);
77
        c.fill = GridBagConstraints.NONE;
78
        c.gridx = 0;
79
        datePanel.add(this.getUICalendar(), c);
102 80

  
81
        JPanel buttonsPanel = new JPanel(new GridBagLayout());
82
        c = new GridBagConstraints();
83
        c.insets = new Insets(2, 2, 4, 2);
84
        c.fill = GridBagConstraints.NONE;
85
        buttonsPanel.add(getButtonOK(), c);
86
        c.insets = new Insets(2, 4, 2, 2);
87
        c.fill = GridBagConstraints.NONE;
88
        buttonsPanel.add(getButtonCancel(), c);
103 89

  
104
	private JButton getButtonOK() {
105
		if (buttonOK == null){
106
			buttonOK = new JButton("Ok");
107
			buttonOK.setActionCommand("BTN_OK");
108
			buttonOK.setSize(90, 23);
109
		}
110
		return buttonOK;
111
	}
90
        JPanel headerPanel = new JPanel(new GridBagLayout());
91
        headerPanel.add(getLbl());
92
        headerPanel.add(new JSeparator());
93
        headerPanel.setBackground(new Color(0, 0, 255, 20));
112 94

  
113
	private JButton getButtonCancel() {
114
		if (buttonCancel == null){
115
			buttonCancel = new JButton("Cancel");
116
			buttonCancel.setActionCommand("BTN_CANCEL");
117
			buttonCancel.setSize(90, 23);
118
		}
119
		return buttonCancel;
120
	}
121
	
122
	private Calendar getDateCalendar(){
123
		if (this.calendar==null){
124
			this.calendar = Calendar.getInstance(this.getLocale());
125
			this.calendar.setTime(this.iniDate);
126
		}
127
		return this.calendar;
128
	}
129
	
130
	private void beforeUI(Date date) {
131
		//DateFormat 
132
		formatter = DateFormat.getDateInstance(DateFormat.FULL, this.getLocale());
133
		// Calendar settings
134
		JCalendar cal = getUICalendar();
135
		cal.setDate(date);
95
        // this.add(getLbl(), BorderLayout.PAGE_START);
96
        this.add(headerPanel, BorderLayout.PAGE_START);
97
        this.add(datePanel, BorderLayout.CENTER);
98
        this.add(buttonsPanel, BorderLayout.PAGE_END);
136 99

  
137
		//Setting fonts
138
		// Set fonts rather than using defaults
139
		cal.setTitleFont(new Font("SansSerif", Font.BOLD, 22));
140
		cal.setDayOfWeekFont(new Font("SansSerif", Font.ITALIC, 12));
141
		cal.setDayFont(new Font("SansSerif", Font.BOLD, 14));
142
		cal.setTimeFont(new Font("DialogInput", Font.PLAIN, 10));
143
		cal.setTodayFont(new Font("Dialog", Font.PLAIN, 14));
144
	}
145
	
146
	private Border createCompoundBorder() {
147
		// Create a border for all calendars
148
	    Border etchedBorder =
149
		BorderFactory.createEtchedBorder();
150
	    Border emptyBorder =
151
		BorderFactory.createEmptyBorder(10, 10, 10, 10);
152
	    return BorderFactory.createCompoundBorder(etchedBorder, emptyBorder);
153
	}
100
    }
154 101

  
155
	private JCalendar getUICalendar(){
156
		if  (jCal == null){
157
			jCal = new JCalendar();
158
			jCal.setSize(300, 300);
159
			jCal.addDateListener(this);
160
			jCal.setLocale(this.getLocale());
161
			jCal.setDate(this.getDateCalendar().getTime());
162
		}
163
		return jCal;
164
	}
165
	
166
	public void addDateListener(DateListener listener){
167
		this.getUICalendar().addDateListener(this);
168
	}
102
    private JButton getButtonOK() {
103
        if (buttonOK == null) {
104
            buttonOK = new JButton("Ok");
105
            buttonOK.setActionCommand("BTN_OK");
106
            buttonOK.setSize(90, 23);
107
        }
108
        return buttonOK;
109
    }
169 110

  
170
	
171
	public void dateChanged(DateEvent dateEvent) {
172
		this.calendar = dateEvent.getSelectedDate();
173
		refreshLbl();
174
	}
175
	
176
	private void refreshLbl() {
177
		this.getLbl().setText(getDateString());
178
	}
111
    private JButton getButtonCancel() {
112
        if (buttonCancel == null) {
113
            buttonCancel = new JButton("Cancel");
114
            buttonCancel.setActionCommand("BTN_CANCEL");
115
            buttonCancel.setSize(90, 23);
116
        }
117
        return buttonCancel;
118
    }
179 119

  
180
	public String getLblText(Date date){
181
		return formatter.format(this.getSelectedDate());
182
	}
183
	
184
	public Date getSelectedDate(){
185
		return getDateCalendar().getTime();
186
	}
187
	
188
	public String getDateString(){
189
		return getLblText(getSelectedDate());
190
	}
120
    private Calendar getDateCalendar() {
121
        if (this.calendar == null) {
122
            this.calendar = Calendar.getInstance(this.getLocale());
123
            this.calendar.setTime(this.iniDate);
124
        }
125
        return this.calendar;
126
    }
191 127

  
192
	public void addButtonsActionListener(ActionListener listener) {
193
		this.getButtonCancel().addActionListener(listener);
194
		this.getButtonOK().addActionListener(listener);
195
	}
128
    private void beforeUI(Date date) {
129
        // DateFormat
130
        formatter =
131
            DateFormat.getDateInstance(DateFormat.FULL, this.getLocale());
132
        // Calendar settings
133
        JCalendar cal = getUICalendar();
134
        cal.setDate(date);
196 135

  
197
	public void setSelectedDate(Date date) {
198
		if (date==null){
199
			date = new Date();
200
		}
201
		this.getDateCalendar().setTime(date);
202
		this.getUICalendar().setDate(date);
203
		refreshLbl();
204
	}
205
}
136
        // Setting fonts
137
        // Set fonts rather than using defaults
138
        cal.setTitleFont(new Font("SansSerif", Font.BOLD, 22));
139
        cal.setDayOfWeekFont(new Font("SansSerif", Font.ITALIC, 12));
140
        cal.setDayFont(new Font("SansSerif", Font.BOLD, 14));
141
        cal.setTimeFont(new Font("DialogInput", Font.PLAIN, 10));
142
        cal.setTodayFont(new Font("Dialog", Font.PLAIN, 14));
143
    }
206 144

  
145
    private Border createCompoundBorder() {
146
        // Create a border for all calendars
147
        Border etchedBorder = BorderFactory.createEtchedBorder();
148
        Border emptyBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
149
        return BorderFactory.createCompoundBorder(etchedBorder, emptyBorder);
150
    }
151

  
152
    private JCalendar getUICalendar() {
153
        if (jCal == null) {
154
            jCal = new JCalendar();
155
            jCal.setSize(300, 300);
156
            jCal.addDateListener(this);
157
            jCal.setLocale(this.getLocale());
158
            jCal.setDate(this.getDateCalendar().getTime());
159
        }
160
        return jCal;
161
    }
162

  
163
    public void addDateListener(DateListener listener) {
164
        this.getUICalendar().addDateListener(this);
165
    }
166

  
167
    public void dateChanged(DateEvent dateEvent) {
168
        this.calendar = dateEvent.getSelectedDate();
169
        refreshLbl();
170
    }
171

  
172
    private void refreshLbl() {
173
        this.getLbl().setText(getDateString());
174
    }
175

  
176
    public String getLblText(Date date) {
177
        return formatter.format(this.getSelectedDate());
178
    }
179

  
180
    public Date getSelectedDate() {
181
        return getDateCalendar().getTime();
182
    }
183

  
184
    public String getDateString() {
185
        return getLblText(getSelectedDate());
186
    }
187

  
188
    public void addButtonsActionListener(ActionListener listener) {
189
        this.getButtonCancel().addActionListener(listener);
190
        this.getButtonOK().addActionListener(listener);
191
    }
192

  
193
    public void setSelectedDate(Date date) {
194
        if (date == null) {
195
            date = new Date();
196
        }
197
        this.getDateCalendar().setTime(date);
198
        this.getUICalendar().setDate(date);
199
        refreshLbl();
200
    }
201
}

Also available in: Unified diff