datepickercombo.java
来自「一个SWT日期时间选择控件类」· Java 代码 · 共 1,120 行 · 第 1/3 页
JAVA
1,120 行
* when listener is null
*/
public void removeSelectionListener(SelectionListener listener) {
checkWidget();
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
removeListener(SWT.Selection, listener);
removeListener(SWT.DefaultSelection, listener);
}
/**
* Set up background of control
* @param color background color
*/
public void setBackground(Color color) {
super.setBackground(color);
if (text != null) {
text.setBackground(color);
}
if (dp != null) {
dp.setBackground(color);
}
if (arrow != null) {
arrow.setBackground(color);
}
}
/**
* set if you need to double click a date in the expanded calender control to hide it
* default is false meaning you have to double click a date
* @author sebthom
* @param useSingleMouseClickToCommit
*/
public void setClosePopupWithSingleMouseClick(
boolean isClosePopupWithSingleMouseClick) {
this.isClosePopupWithSingleMouseClick = isClosePopupWithSingleMouseClick;
}
/**
* Set DateFormat object, which is parser/formatter of control's content
* @author andyglow
* @param format
*/
public void setFormat(DateFormat format) {
this.format = format;
}
/**
* Return current DateFormat used to format/parse control content
* @author andyglow
* @return current DateFormat
*/
protected DateFormat getFormat() {
if(format==null) format = DateFormat.getDateInstance(DateFormat.SHORT);
return format;
}
/**
* Set up the date of control
* @param date Date
*/
public void setDate(Date date) {
checkWidget();
//sebthom
if (date != null) {
text.setText(getFormat().format(date));
text.selectAll();
} else {
text.setText("");
}
dp.setDate(date);
}
/**
* Set the focus to control
*
* @return result of action
*/
public boolean setFocus() {
checkWidget();
return text.setFocus();
}
/**
* DOCUMENT ME!
*
* @param font DOCUMENT ME!
*/
public void setFont(Font font) {
super.setFont(font);
text.setFont(font);
dp.setFont(font);
internalLayout();
}
/**
* Set up the control foreground color
*
* @param color Foreground color
*/
public void setForeground(Color color) {
super.setForeground(color);
if (text != null) {
text.setForeground(color);
}
if (dp != null) {
dp.setForeground(color);
}
if (arrow != null) {
arrow.setForeground(color);
}
}
/**
* Sets the new selection.
*
* @param selection
* point representing the start and the end of the new selection
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
* when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
* when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
* when selection is null
*/
public void setSelection(Point selection) {
checkWidget();
if (selection == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
text.setSelection(selection.x, selection.y);
}
/**
* Sets the text limit
*
* @param limit
* new text limit
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
* when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
* when the widget has been disposed
* @exception SWTError(ERROR_CANNOT_BE_ZERO)
* when limit is 0
*/
public void setTextLimit(int limit) {
checkWidget();
text.setTextLimit(limit);
}
/**
* Set up control tool-tip
*
* @param string Text of tool-tip
*/
public void setToolTipText(String string) {
checkWidget();
super.setToolTipText(string);
arrow.setToolTipText(string);
text.setToolTipText(string);
}
/**
* Set up visibility of control
*
* @param visible visibility state
*/
public void setVisible(boolean visible) {
super.setVisible(visible);
if (!visible) {
popup.setVisible(false);
}
}
private void textEvent(Event event) {
switch (event.type) {
case SWT.FocusIn: {
if (hasFocus) {
return;
}
hasFocus = true;
if (getEditable()) {
text.selectAll();
}
Event e = new Event();
e.time = event.time;
notifyListeners(SWT.FocusIn, e);
break;
}
case SWT.FocusOut: {
Control focusControl = getDisplay().getFocusControl();
if ((focusControl == dp) || (focusControl == arrow)) {
return;
}
hasFocus = false;
Event e = new Event();
e.time = event.time;
notifyListeners(SWT.FocusOut, e);
break;
}
case SWT.KeyDown: {
if (event.character == SWT.ESC) {
// escape key cancels popup dp
dropDown(false);
}
if (event.character == SWT.CR) {
dropDown(false);
Event e = new Event();
e.time = event.time;
e.stateMask = event.stateMask;
notifyListeners(SWT.DefaultSelection, e);
}
//At this point the widget may have been disposed.
// If so, do not continue.
if (isDisposed()) {
break;
}
if ((event.keyCode == SWT.ARROW_UP)
|| (event.keyCode == SWT.ARROW_DOWN)) {
//Date oldDate = getDate();
//At this point the widget may have been disposed.
// If so, do not continue.
if (isDisposed()) {
break;
}
}
// Further work : Need to add support for incremental
// search in pop up dp as characters typed in text widget
Event e = new Event();
e.time = event.time;
e.character = event.character;
e.keyCode = event.keyCode;
e.stateMask = event.stateMask;
notifyListeners(SWT.KeyDown, e);
break;
}
case SWT.KeyUp: {
Event e = new Event();
e.time = event.time;
e.character = event.character;
e.keyCode = event.keyCode;
e.stateMask = event.stateMask;
notifyListeners(SWT.KeyUp, e);
break;
}
case SWT.Modify: {
// sebthom
if (!popup.isVisible()) {
if (text.getText().length() == 0) {
dp.setDate(null);
} else {
try {
dp.setDate(getFormat().parse(text
.getText()));
} catch (ParseException pe) {
dp.setDate(null);
}
}
}
// dp.deselectAll ();
Event e = new Event();
e.time = event.time;
notifyListeners(SWT.Modify, e);
break;
}
case SWT.MouseDown: {
if ((event.button != 1) || text.getEditable()) {
return;
}
boolean dropped = isDropped();
text.selectAll();
if (!dropped) {
setFocus();
}
dropDown(!dropped);
break;
}
case SWT.MouseUp: {
if ((event.button != 1) || text.getEditable()) {
return;
}
text.selectAll();
break;
}
case SWT.Traverse: {
switch (event.detail) {
case SWT.TRAVERSE_RETURN:
case SWT.TRAVERSE_ARROW_PREVIOUS:
case SWT.TRAVERSE_ARROW_NEXT:
// The enter causes default selection and
// the arrow keys are used to manipulate the dp
// contents so do not use them for traversal.
event.doit = false;
break;
}
Event e = new Event();
e.time = event.time;
e.detail = event.detail;
e.doit = event.doit;
e.keyCode = event.keyCode;
notifyListeners(SWT.Traverse, e);
event.doit = e.doit;
break;
}
}
}
/**
* Enable/disable widget
*
* @author andyglow
* @param enabled
*/
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
text.setEnabled(enabled);
dp.setEnabled(enabled);
arrow.setEnabled(enabled);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?