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 @ 2146
History | View | Annotate | Download (4.2 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 |
ToolsSwingLocator.getToolsSwingManager().addClearButton(txtDate, new ActionListener() { |
53 |
@Override
|
54 |
public void actionPerformed(ActionEvent e) { |
55 |
set(null);
|
56 |
txtDate.setText("");
|
57 |
} |
58 |
}); |
59 |
this.btnDate = btnDate;
|
60 |
this.txtDate.setAlignmentX(RIGHT_ALIGNMENT);
|
61 |
this.txtDate.setEditable(true); |
62 |
|
63 |
this.btnDate.setIcon(this.getIcon("picker-date")); |
64 |
this.btnDate.setText(""); |
65 |
|
66 |
this.btnDate.addActionListener(new ActionListener() { |
67 |
@Override
|
68 |
public void actionPerformed(ActionEvent e) { |
69 |
doShowCalendar(); |
70 |
} |
71 |
}); |
72 |
} |
73 |
|
74 |
@Override
|
75 |
public boolean isEmpty() { |
76 |
return this.value.getValue()==null; |
77 |
} |
78 |
|
79 |
@Override
|
80 |
public Date get() { |
81 |
return this.value.getValue(); |
82 |
} |
83 |
|
84 |
@Override
|
85 |
public void set(Date date) { |
86 |
this.value.setValue(date);
|
87 |
this.txtDate.setText(Objects.toString(date, "")); |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public void setEnabled(boolean enabled) { |
92 |
this.btnDate.setEnabled(enabled);
|
93 |
this.txtDate.setEnabled(enabled);
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public boolean isEnabled() { |
98 |
return this.btnDate.isEnabled(); |
99 |
} |
100 |
|
101 |
private void doShowCalendar() { |
102 |
if( !this.isEditable() ) { |
103 |
return;
|
104 |
} |
105 |
JPopupMenu menu = new JPopupMenu(); |
106 |
menu.add(getJCalendar()); |
107 |
Date v = this.value.getValue(); |
108 |
if (v != null) { |
109 |
getJCalendar().setDate(v); |
110 |
} |
111 |
menu.show(this.txtDate, 0, this.txtDate.getY() + this.txtDate.getPreferredSize().height); |
112 |
this.set(getJCalendar().getDate());
|
113 |
this.fireChangeEvent();
|
114 |
} |
115 |
|
116 |
protected JCalendar getJCalendar() {
|
117 |
if (this.jcalendar == null) { |
118 |
this.jcalendar = new JCalendar(); |
119 |
this.jcalendar.addDateListener(new DateListener() { |
120 |
@Override
|
121 |
public void dateChanged(DateEvent arg0) { |
122 |
set(getJCalendar().getDate()); |
123 |
fireChangeEvent(); |
124 |
} |
125 |
}); |
126 |
} |
127 |
return (JCalendar) this.jcalendar; |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public void coerceAndSet(Object value) { |
132 |
this.set((Date) this.coerce(DataTypes.DATE, value, new Date())); |
133 |
} |
134 |
|
135 |
} |