📄 swtdaychooser.java
字号:
* @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
*/
public void focusGained(FocusEvent event) {
DayControl selectedDay = getSelectedDayControl();
selectedDay.setBackground(getSelectionBackgroundColor());
selectedDay.setForeground(getSelectionForegroundColor());
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
*/
public void focusLost(FocusEvent event) {
DayControl selectedDay = getSelectedDayControl();
selectedDay.setBackground(getSelectionBackgroundColor());
selectedDay.setForeground(getSelectionForegroundColor());
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.TraverseListener#keyTraversed(org.eclipse.swt.events.TraverseEvent)
*/
public void keyTraversed(TraverseEvent event) {
switch (event.detail) {
case SWT.TRAVERSE_ARROW_PREVIOUS:
case SWT.TRAVERSE_ARROW_NEXT:
case SWT.TRAVERSE_PAGE_PREVIOUS:
case SWT.TRAVERSE_PAGE_NEXT:
event.doit = false;
break;
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_TAB_PREVIOUS:
event.doit = true;
}
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)
*/
public void keyPressed(KeyEvent event) {
switch (event.keyCode) {
case SWT.ARROW_LEFT:
selectDay(calendar.get(Calendar.DAY_OF_MONTH) - 1);
break;
case SWT.ARROW_RIGHT:
selectDay(calendar.get(Calendar.DAY_OF_MONTH) + 1);
break;
case SWT.ARROW_UP:
selectDay(calendar.get(Calendar.DAY_OF_MONTH) - 7);
break;
case SWT.ARROW_DOWN:
selectDay(calendar.get(Calendar.DAY_OF_MONTH) + 7);
break;
case SWT.PAGE_UP:
setMonth(calendar.get(Calendar.MONTH) - 1);
break;
case SWT.PAGE_DOWN:
setMonth(calendar.get(Calendar.MONTH) + 1);
break;
}
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
*/
public void keyReleased(KeyEvent event) {
}
/**
* Finds position of a control in <code>days</code> array.
*
* @param dayControl a control to find.
* @return an index of <code>dayControl</code> in <code>days</code> array, or -1 if not found.
*/
private int findDay(Widget dayControl) {
for (int i = 0; i < days.length; i++) {
if (days[i] == dayControl) {
return i;
}
}
return -1;
}
private void selectDay(int day) {
calendar.get(Calendar.DAY_OF_YEAR); // Force calendar update
if (day >= calendar.getActualMinimum(Calendar.DAY_OF_MONTH) && day <= calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) {
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
// Stay on the same month.
DayControl selectedDay = getSelectedDayControl();
selectedDay.setBackground(getBackground());
if (dayOfWeek == Calendar.SUNDAY) {
selectedDay.setForeground(getDisplay().getSystemColor(SWT.COLOR_DARK_RED));
} else {
selectedDay.setForeground(getForeground());
}
calendar.set(Calendar.DAY_OF_MONTH, day);
selectedDay = getSelectedDayControl();
selectedDay.setBackground(getSelectionBackgroundColor());
selectedDay.setForeground(getSelectionForegroundColor());
} else {
// Move to a different month.
calendar.set(Calendar.DAY_OF_MONTH, day);
drawDays();
}
dateChanged();
}
private DayControl getSelectedDayControl() {
return days[calendar.get(Calendar.DAY_OF_MONTH) - 1 - dayOffset];
}
private Color getSelectionBackgroundColor() {
return isFocusControl() ? activeSelectionBackground : inactiveSelectionBackground;
}
private Color getSelectionForegroundColor() {
return isFocusControl() ? activeSelectionForeground : inactiveSelectionForeground;
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#isFocusControl()
*/
public boolean isFocusControl() {
for (Control control = getDisplay().getFocusControl(); control != null; control = control.getParent()) {
if (control == this) {
return true;
}
}
return false;
}
public void addSWTCalendarListener(SWTCalendarListener listener) {
this.listeners.add(listener);
}
public void removeSWTCalendarListener(SWTCalendarListener listener) {
this.listeners.remove(listener);
}
private void dateChanged() {
if (!listeners.isEmpty()) {
SWTCalendarListener[] listenersArray = new SWTCalendarListener[listeners.size()];
listeners.toArray(listenersArray);
for (int i = 0; i < listenersArray.length; i++) {
Event event = new Event();
event.widget = this;
event.display = getDisplay();
event.time = (int) System.currentTimeMillis();
event.data = calendar.clone();
listenersArray[i].dateChanged(new SWTCalendarEvent(event));
}
}
}
public Calendar getCalendar() {
return (Calendar) calendar.clone();
}
public void setLocale(Locale locale) {
this.locale = locale;
init();
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#setFont(org.eclipse.swt.graphics.Font)
*/
public void setFont(Font font) {
super.setFont(font);
for (int i = 0; i < dayTitles.length; i++) {
dayTitles[i].setFont(font);
}
for (int i = 0; i < days.length; i++) {
days[i].setFont(font);
}
}
static private class DayControl extends Composite implements Listener {
private Composite filler;
private Label label;
public DayControl(Composite parent) {
super(parent, SWT.NO_FOCUS);
{
final GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 1;
gridLayout.marginHeight = 1;
setLayout(gridLayout);
}
filler = new Composite(this, SWT.NO_FOCUS);
filler.setLayoutData(new GridData(GridData.FILL_BOTH));
{
final GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 2;
gridLayout.marginHeight = 0;
filler.setLayout(gridLayout);
}
filler.addListener(SWT.MouseDown, this);
filler.addListener(SWT.MouseUp, this);
filler.addListener(SWT.MouseDoubleClick, this);
label = new DayLabel(filler, SWT.RIGHT | SWT.NO_FOCUS);
label.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_CENTER));
label.addListener(SWT.MouseDown, this);
label.addListener(SWT.MouseUp, this);
label.addListener(SWT.MouseDoubleClick, this);
setBorderColor(parent.getBackground());
setBackground(parent.getBackground());
setFont(parent.getFont());
}
public void setText(String text) {
label.setText(text);
}
public String getText() {
return label.getText();
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#setFont(org.eclipse.swt.graphics.Font)
*/
public void setFont(Font font) {
super.setFont(font);
filler.setFont(font);
label.setFont(font);
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#setBackground(org.eclipse.swt.graphics.Color)
*/
public void setBackground(Color color) {
filler.setBackground(color);
label.setBackground(color);
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#setForeground(org.eclipse.swt.graphics.Color)
*/
public void setForeground(Color color) {
label.setForeground(color);
}
public void setBorderColor(Color color) {
super.setBackground(color);
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
*/
public void handleEvent(Event event) {
notifyListeners(event.type, event);
}
}
static private class DayLabel extends Label {
public DayLabel(Composite parent, int style) {
super(parent, style);
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#computeSize(int, int, boolean)
*/
public Point computeSize(int wHint, int hHint, boolean changed) {
if (wHint == SWT.DEFAULT) {
GC gc = new GC(this);
wHint = gc.textExtent("22").x; //$NON-NLS-1$
gc.dispose();
}
return super.computeSize(wHint, hHint, changed);
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Widget#checkSubclass()
*/
protected void checkSubclass() {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -