81,99 → 81,16 |
this.add(spacer); |
} |
|
final ImageIcon icon = new ImageIcon(this.getClass().getResource("calendar.png")); |
String iconFileName = "calendar.png"; |
if (getFont().getSize() > 16) { |
iconFileName = "calendar_2x.png"; |
} |
final ImageIcon icon = new ImageIcon(this.getClass().getResource(iconFileName)); |
button = new JButton(new AbstractAction(null, icon) { |
|
@Override |
public void actionPerformed(ActionEvent e) { |
final JComponent source = (JComponent) e.getSource(); |
|
if (text != null) { |
// if the button isn't focusable, no FOCUS_LOST will occur and the text won't |
// get |
// committed (or reverted). So act as if the user requested a commit, so that |
// invalidEdit() can be called (usually causing a beep). |
if (!source.isFocusable()) { |
for (final Action a : text.getActions()) { |
final String name = (String) a.getValue(Action.NAME); |
if (JTextField.notifyAction.equals(name)) |
a.actionPerformed(new ActionEvent(text, e.getID(), null)); |
} |
} |
|
// if after trying to commit, the value is invalid then don't show the popup |
if (!text.isEditValid()) |
return; |
} |
if (dialog == null) { |
final JDialog d = new JDialog(SwingUtilities.getWindowAncestor(DatePicker.this)); |
pickerPanel = new DatePickerPanel(); |
|
d.setContentPane(pickerPanel); |
d.setUndecorated(true); |
pickerPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY)); |
d.setSize(330, 270); |
|
pickerPanel.addPropertyChangeListener(DatePickerPanel.TIME_IN_MILLIS, new PropertyChangeListener() { |
|
@Override |
public void propertyChange(PropertyChangeEvent evt) { |
if (!DatePicker.this.isEditable()) |
return; |
final Long millis = (Long) evt.getNewValue(); |
final Calendar c = Calendar.getInstance(); |
c.setTimeInMillis(millis); |
setDate(c.getTime()); |
if (dialog != null) { |
dialog.dispose(); |
dialog = null; |
} |
fireActionPerformed(); |
} |
|
}); |
d.addWindowFocusListener(new WindowFocusListener() { |
|
public void windowGainedFocus(WindowEvent e) { |
// do nothing |
} |
|
public void windowLostFocus(WindowEvent e) { |
if (dialog != null) { |
final Window oppositeWindow = e.getOppositeWindow(); |
if (oppositeWindow != null && SwingUtilities.isDescendingFrom(oppositeWindow, dialog)) { |
return; |
} |
dialog.dispose(); |
dialog = null; |
} |
dialogLostFocusTime = System.currentTimeMillis(); |
} |
|
}); |
dialog = d; |
} |
// Set picker panel date |
final Calendar calendar = Calendar.getInstance(); |
if (date != null) { |
calendar.setTime(date); |
pickerPanel.setSelectedDate(calendar); |
} else { |
pickerPanel.setSelectedDate(null); |
} |
// Show dialog |
final int x = source.getLocation().x; |
final int y = source.getLocation().y + source.getHeight(); |
Point p = new Point(x, y); |
SwingUtilities.convertPointToScreen(p, DatePicker.this); |
p = adjustPopupLocationToFitScreen(p.x, p.y, dialog.getSize(), source.getSize()); |
dialog.setLocation(p.x, p.y); |
final long time = System.currentTimeMillis() - dialogLostFocusTime; |
if (time > RELEASE_TIME_MS) { |
dialog.setVisible(true); |
} else { |
dialogLostFocusTime = System.currentTimeMillis() - RELEASE_TIME_MS; |
} |
calendarButtonPressed(e); |
} |
|
}); |
345,7 → 262,98 |
} |
} |
|
static public final boolean equals(Object o1, Object o2) { |
private void calendarButtonPressed(ActionEvent e) { |
final JComponent source = (JComponent) e.getSource(); |
|
if (text != null) { |
// if the button isn't focusable, no FOCUS_LOST will occur and the text won't |
// get |
// committed (or reverted). So act as if the user requested a commit, so that |
// invalidEdit() can be called (usually causing a beep). |
if (!source.isFocusable()) { |
for (final Action a : text.getActions()) { |
final String name = (String) a.getValue(Action.NAME); |
if (JTextField.notifyAction.equals(name)) |
a.actionPerformed(new ActionEvent(text, e.getID(), null)); |
} |
} |
|
// if after trying to commit, the value is invalid then don't show the popup |
if (!text.isEditValid()) |
return; |
} |
if (dialog == null) { |
final JDialog d = new JDialog(SwingUtilities.getWindowAncestor(DatePicker.this)); |
pickerPanel = new DatePickerPanel(); |
|
d.setContentPane(pickerPanel); |
d.setUndecorated(true); |
pickerPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY)); |
d.pack(); |
|
pickerPanel.addPropertyChangeListener(DatePickerPanel.TIME_IN_MILLIS, new PropertyChangeListener() { |
|
@Override |
public void propertyChange(PropertyChangeEvent evt) { |
if (!DatePicker.this.isEditable()) |
return; |
final Long millis = (Long) evt.getNewValue(); |
final Calendar c = Calendar.getInstance(); |
c.setTimeInMillis(millis); |
setDate(c.getTime()); |
if (dialog != null) { |
dialog.dispose(); |
dialog = null; |
} |
fireActionPerformed(); |
} |
|
}); |
d.addWindowFocusListener(new WindowFocusListener() { |
|
public void windowGainedFocus(WindowEvent e) { |
// do nothing |
} |
|
public void windowLostFocus(WindowEvent e) { |
if (dialog != null) { |
final Window oppositeWindow = e.getOppositeWindow(); |
if (oppositeWindow != null && SwingUtilities.isDescendingFrom(oppositeWindow, dialog)) { |
return; |
} |
dialog.dispose(); |
dialog = null; |
} |
dialogLostFocusTime = System.currentTimeMillis(); |
} |
|
}); |
dialog = d; |
} |
// Set picker panel date |
final Calendar calendar = Calendar.getInstance(); |
if (date != null) { |
calendar.setTime(date); |
pickerPanel.setSelectedDate(calendar); |
} else { |
pickerPanel.setSelectedDate(null); |
} |
// Show dialog |
final int x = source.getLocation().x; |
final int y = source.getLocation().y + source.getHeight(); |
Point p = new Point(x, y); |
SwingUtilities.convertPointToScreen(p, DatePicker.this); |
p = adjustPopupLocationToFitScreen(p.x, p.y, dialog.getSize(), source.getSize()); |
dialog.setLocation(p.x, p.y); |
final long time = System.currentTimeMillis() - dialogLostFocusTime; |
if (time > RELEASE_TIME_MS) { |
dialog.setVisible(true); |
} else { |
dialogLostFocusTime = System.currentTimeMillis() - RELEASE_TIME_MS; |
} |
} |
|
public static final boolean equals(Object o1, Object o2) { |
if (o1 == null && o2 == null) |
return true; |
if (o1 == null || o2 == null) |