gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / pickercontroller / DatePickerControllerImpl.java @ 1826
History | View | Annotate | Download (4 KB)
1 |
package org.gvsig.tools.swing.impl.pickercontroller; |
---|---|
2 |
|
3 |
import static java.awt.Component.RIGHT_ALIGNMENT; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.net.URL; |
7 |
import java.util.Date; |
8 |
import java.util.Objects; |
9 |
import javax.swing.ImageIcon; |
10 |
import javax.swing.JButton; |
11 |
import javax.swing.JPopupMenu; |
12 |
import javax.swing.JTextField; |
13 |
import org.apache.commons.lang3.mutable.Mutable; |
14 |
import org.apache.commons.lang3.mutable.MutableObject; |
15 |
import org.freixas.jcalendar.DateEvent; |
16 |
import org.freixas.jcalendar.DateListener; |
17 |
import org.freixas.jcalendar.JCalendar; |
18 |
import org.gvsig.tools.dataTypes.DataTypes; |
19 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
20 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
21 |
import org.gvsig.tools.swing.api.pickercontroller.DatePickerController; |
22 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
23 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
24 |
|
25 |
/**
|
26 |
*
|
27 |
* @author jjdelcerro
|
28 |
*/
|
29 |
public class DatePickerControllerImpl |
30 |
extends AbstractPickerController<Date> |
31 |
implements DatePickerController
|
32 |
{ |
33 |
|
34 |
private final JButton btnDate; |
35 |
private final JTextField txtDate; |
36 |
private final Mutable<Date> value; |
37 |
private JCalendar jcalendar;
|
38 |
|
39 |
public static void selfRegister() { |
40 |
URL imageResource = DatePickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-date.png"); |
41 |
if (imageResource != null) { |
42 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
43 |
IconTheme theme = iconThemeManager.getCurrent(); |
44 |
ImageIcon icon = new ImageIcon(imageResource); |
45 |
theme.registerDefault("tools", "picker", "picker-date", icon, imageResource); |
46 |
} |
47 |
} |
48 |
|
49 |
public DatePickerControllerImpl(JTextField txtDate, JButton btnDate) { |
50 |
this.value = new MutableObject<>(new Date()); |
51 |
this.txtDate = txtDate;
|
52 |
this.btnDate = btnDate;
|
53 |
this.txtDate.setAlignmentX(RIGHT_ALIGNMENT);
|
54 |
this.txtDate.setEditable(false); |
55 |
|
56 |
this.btnDate.setIcon(this.getIcon("picker-color")); |
57 |
this.btnDate.setText(""); |
58 |
|
59 |
this.btnDate.addActionListener(new ActionListener() { |
60 |
@Override
|
61 |
public void actionPerformed(ActionEvent e) { |
62 |
doShowCalendar(); |
63 |
} |
64 |
}); |
65 |
this.txtDate.setEditable(false); |
66 |
} |
67 |
|
68 |
@Override
|
69 |
public boolean isEmpty() { |
70 |
return this.value.getValue()==null; |
71 |
} |
72 |
|
73 |
@Override
|
74 |
public Date get() { |
75 |
return this.value.getValue(); |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public void set(Date date) { |
80 |
this.value.setValue(date);
|
81 |
this.txtDate.setText(Objects.toString(date.toString()));
|
82 |
} |
83 |
|
84 |
@Override
|
85 |
public void setEnabled(boolean enabled) { |
86 |
this.btnDate.setEnabled(enabled);
|
87 |
this.txtDate.setEnabled(enabled);
|
88 |
} |
89 |
|
90 |
@Override
|
91 |
public boolean isEnabled() { |
92 |
return this.btnDate.isEnabled(); |
93 |
} |
94 |
|
95 |
private void doShowCalendar() { |
96 |
if( !this.isEditable() ) { |
97 |
return;
|
98 |
} |
99 |
JPopupMenu menu = new JPopupMenu(); |
100 |
menu.add(getJCalendar()); |
101 |
Date v = this.value.getValue(); |
102 |
if (v != null) { |
103 |
getJCalendar().setDate(v); |
104 |
} |
105 |
menu.show(this.txtDate, 0, this.txtDate.getY() + this.txtDate.getPreferredSize().height); |
106 |
this.set(getJCalendar().getDate());
|
107 |
this.fireChangeEvent();
|
108 |
} |
109 |
|
110 |
protected JCalendar getJCalendar() {
|
111 |
if (this.jcalendar == null) { |
112 |
this.jcalendar = new JCalendar(); |
113 |
this.jcalendar.addDateListener(new DateListener() { |
114 |
@Override
|
115 |
public void dateChanged(DateEvent arg0) { |
116 |
set(getJCalendar().getDate()); |
117 |
fireChangeEvent(); |
118 |
} |
119 |
}); |
120 |
} |
121 |
return (JCalendar) this.jcalendar; |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public void coerceAndSet(Object value) { |
126 |
this.set((Date) this.coerce(DataTypes.DATE, value, new Date())); |
127 |
} |
128 |
|
129 |
} |