下面列出了怎么用org.eclipse.swt.widgets.DateTime的API类实例代码及写法,或者点击链接到github查看源代码。
public void updateDate2Composite() {
if (isBetweenDates()) {
date2Widget = new DateTime(widgetComp, SWT.CALENDAR);
date2Widget.addListener(SWT.Selection, e-> setDate2Selection());
time2Widget = new DateTime(widgetComp, SWT.TIME);
time2Widget.addListener(SWT.Selection, e-> setDate2Selection());
time2Widget.setHours(0);
time2Widget.setMinutes(0);
time2Widget.setSeconds(0);
} else {
if (date2Widget != null) {
date2Widget.dispose();
date2Widget = null;
time2Widget.dispose();
time2Widget = null;
}
}
widgetComp.layout(true, true);
final Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
getShell().setSize(newSize);
}
private Date toDate(DateTime dayPicker, DateTime timePicker) throws Exception {
String input = "";
int year = dayPicker.getYear();
int month = dayPicker.getMonth() + 1;
int day = dayPicker.getDay();
int hours = timePicker.getHours();
int minutes = timePicker.getMinutes();
int seconds = timePicker.getSeconds();
input = year + "/" + month + "/" + day + "/" + hours + "/" + minutes + "/" + seconds;
SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss");
Date t;
t = ft.parse(input);
return t;
}
@Override
public Control createCustomParameterControl(final Composite compo) {
edit = new Composite(compo, SWT.NONE);
final GridLayout pointEditorLayout = new GridLayout(2, true);
pointEditorLayout.horizontalSpacing = 10;
pointEditorLayout.verticalSpacing = 0;
pointEditorLayout.marginHeight = 0;
pointEditorLayout.marginWidth = 0;
edit.setLayout(pointEditorLayout);
date = new DateTime(edit, SWT.DROP_DOWN | SWT.BORDER | SWT.DATE | SWT.LONG);
time = new DateTime(edit, SWT.DROP_DOWN | SWT.BORDER | SWT.TIME | SWT.LONG);
date.setBackground(IGamaColors.PARAMETERS_BACKGROUND.color());
date.addSelectionListener(this);
time.setBackground(IGamaColors.PARAMETERS_BACKGROUND.color());
time.addSelectionListener(this);
edit.setBackground(IGamaColors.PARAMETERS_BACKGROUND.color());
displayParameterValue();
return edit;
}
/**
* 获取当前过滤条件的值
* @return ;
*/
public ExportFilterComponentBean getValue() {
if (valueText instanceof Text) {
this.baseDataBean.setFilterVlaue(((Text) valueText).getText());
}
else if (valueText instanceof DateTime) {
DateTime temp = (DateTime) valueText;
StringBuffer bf = new StringBuffer();
bf.append(temp.getYear());
bf.append("-");
DecimalFormat df = new DecimalFormat("00");
bf.append(df.format(temp.getMonth() + 1));
bf.append("-");
bf.append(df.format(temp.getDay()));
bf.append(" 00:00:00"); // 补全时间
this.baseDataBean.setFilterVlaue(bf.toString());
}
return this.baseDataBean;
}
@Override
public void createPartControl(Composite parent) {
GridLayout layout = new GridLayout();
layout.numColumns = 2;
parent.setLayout(layout);
new Label(parent, SWT.NONE).setText("Summary");
Text text = new Text(parent, SWT.BORDER);
text.setText(todo.getSummary());
text.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
new Label(parent, SWT.NONE).setText("Description");
Text lastName = new Text(parent, SWT.BORDER);
lastName.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
lastName.setText(todo.getDescription());
new Label(parent, SWT.NONE).setText("Done");
Button doneBtn = new Button(parent, SWT.CHECK);
doneBtn.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
doneBtn.setSelection(todo.isDone());
new Label(parent, SWT.NONE).setText("Due Date");
DateTime dueDate = new DateTime(parent, SWT.CHECK);
dueDate.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
Date date = todo.getDueDate();
dueDate.setDate(date.getYear(), date.getMonth(), date.getDay());
}
/**
* 获取当前过滤条件的值
* @return ;
*/
public ExportFilterComponentBean getValue() {
if (valueText instanceof Text) {
this.baseDataBean.setFilterVlaue(((Text) valueText).getText());
}
else if (valueText instanceof DateTime) {
DateTime temp = (DateTime) valueText;
StringBuffer bf = new StringBuffer();
bf.append(temp.getYear());
bf.append("-");
DecimalFormat df = new DecimalFormat("00");
bf.append(df.format(temp.getMonth() + 1));
bf.append("-");
bf.append(df.format(temp.getDay()));
bf.append(" 00:00:00"); // 补全时间
this.baseDataBean.setFilterVlaue(bf.toString());
}
return this.baseDataBean;
}
protected void createDatePicker( int style )
{
btnDate = new Button( this, SWT.CHECK );
btnDate.addListener( SWT.Selection, this );
pickerDate = new DateTime( this, SWT.DATE | style );
pickerDate.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
pickerDate.addListener( SWT.Selection, this );
btnTime = new Button( this, SWT.CHECK );
btnTime.addListener( SWT.Selection, this );
pickerTime = new DateTime( this, SWT.TIME | style );
pickerTime.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
pickerTime.addListener( SWT.Selection, this );
}
private Composite createCalendarArea(Composite parent){
Composite composite = new Composite(parent, SWT.NONE);
GridLayout gd = new GridLayout(1, false);
gd.marginLeft = 2; // SWT BUG
composite.setLayout(gd);
dateSelection = new DateTime(composite, SWT.CALENDAR);
dateSelection.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
Composite dateComposite = new Composite(composite, SWT.NONE);
dateComposite.setLayout(new GridLayout(2, true));
dateComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Label label = new Label(dateComposite, SWT.NONE);
label.setText("Zeitpunkt");
label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));
timeSelection = new DateTime(dateComposite, SWT.TIME);
timeSelection.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
timeSelection.setTime(date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE),
date.get(Calendar.SECOND));
dateSelection.setDate(date.get(Calendar.YEAR), date.get(Calendar.MONTH),
date.get(Calendar.DAY_OF_MONTH));
getShell().setText(Messages.DateTimeSelectorDialog_enterDate); //$NON-NLS-1$
return composite;
}
private Composite createDefaultArea(Composite parent){
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(2, false));
Label label = new Label(composite, SWT.NONE);
label.setText("Zeitpunkt");
label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));
Composite dateComposite = new Composite(composite, SWT.NONE);
dateComposite.setLayout(new GridLayout(2, true));
dateComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
timeSelection = new DateTime(dateComposite, SWT.TIME);
timeSelection.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
dateSelection = new DateTime(dateComposite, SWT.CALENDAR);
dateSelection.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
timeSelection.setTime(date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE),
date.get(Calendar.SECOND));
dateSelection.setDate(date.get(Calendar.YEAR), date.get(Calendar.MONTH),
date.get(Calendar.DAY_OF_MONTH));
getShell().setText(Messages.DateTimeSelectorDialog_enterDate); //$NON-NLS-1$
return composite;
}
/**
* Returns this field editor's text control.
* <p>
* The control is created if it does not yet exist
* </p>
*
* @param parent
* the parent
* @return the text control
*/
public DateTime getDateTimeControl(Composite parent) {
if (this.dateTimeField == null) {
this.dateTimeField = new DateTime(parent, SWT.DATE | SWT.MEDIUM);
this.dateTimeField.setFont(parent.getFont());
this.dateTimeField.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
valueChanged();
}
});
this.dateTimeField.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent event) {
DateTimeFieldEditor.this.dateTimeField = null;
}
});
} else {
checkParent(this.dateTimeField, parent);
}
return this.dateTimeField;
}
private void setDateValue(Object bean, String property, DateTime dateTime) {
log.trace("Change value {} @ {}", property, bean);
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_MONTH, dateTime.getDay());
calendar.set(Calendar.YEAR, dateTime.getYear());
calendar.set(Calendar.MONTH, dateTime.getMonth());
try {
if (Bean.getType(bean, property) == Date.class)
Bean.setValue(bean, property, calendar.getTime());
else if (Bean.getType(bean, property) == GregorianCalendar.class)
Bean.setValue(bean, property, calendar);
else
log.error("Cannot set bean value");
} catch (Exception e) {
error("Cannot set bean value", e);
}
}
/**
* @see net.uniopt.jface.viewers.XViewerControlFactory#createControl(net.uniopt.jface.viewers.CellEditDescriptor,
* org.eclipse.nebula.widgets.xviewer.XViewer)
*/
@Override
public Control createControl(CellEditDescriptor ced, XViewer xv) {
if (ced.getControl().equals(Text.class)) {
return new Text(xv.getTree(), ced.getSwtStyle());
} else if (ced.getControl().equals(Combo.class)) {
return new Combo(xv.getTree(), ced.getSwtStyle());
} else if (ced.getControl().equals(DateTime.class)) {
return new DateTime(xv.getTree(), ced.getSwtStyle());
} else {
return null;
}
}
/**
* DateTimeで選択されている日付からCalendarインスタンスを作成します
*
* @param dateTime
* @return
*/
private static Calendar getCalendar(DateTime dateTime) {
Calendar cal = DateUtils.truncate(Calendar.getInstance(TimeZone.getDefault()), Calendar.DAY_OF_MONTH);
cal.set(Calendar.YEAR, dateTime.getYear());
cal.set(Calendar.MONTH, dateTime.getMonth());
cal.set(Calendar.DAY_OF_MONTH, dateTime.getDay());
return cal;
}
protected Control createCalendarEditor(final Composite stackedComposite) {
final Composite calendarControl = new Composite(stackedComposite, SWT.NONE);
calendarControl.setLayout(GridLayoutFactory.fillDefaults().numColumns(4).margins(15, 15).create());
calendarControl.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
final Label selectDateLabel = new Label(calendarControl, SWT.NONE);
selectDateLabel.setText(Messages.selectDateLabel);
final DateTime dateChooser = new DateTime(calendarControl, SWT.DATE | SWT.DROP_DOWN | SWT.BORDER);
final Label atLabel = new Label(calendarControl, SWT.NONE);
atLabel.setText(Messages.at);
final DateTime timeChooser = new DateTime(calendarControl, SWT.TIME | SWT.BORDER);
final Button generateFixedDate = new Button(calendarControl, SWT.PUSH);
generateFixedDate.setLayoutData(GridDataFactory.swtDefaults().span(4, 1).create());
generateFixedDate.setText(Messages.generateFixedDateLabel);
generateFixedDate.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
final String displayDate = DateUtil.getWidgetDisplayDate(dateChooser, timeChooser);
condition.setName(displayDate);
condition.setContent(DateUtil.getDateExpressionContent(dateChooser.getYear(),
dateChooser.getMonth(),
dateChooser.getDay(),
timeChooser.getHours(),
timeChooser.getMinutes(),
timeChooser.getSeconds()));
condition.setType(ExpressionConstants.SCRIPT_TYPE);
condition.setInterpreter(ExpressionConstants.GROOVY);
condition.setReturnType(Date.class.getName());
conditionViewer.setSelection(new StructuredSelection(condition));
}
});
return calendarControl;
}
private void updateTimeSpan(DateTime dateTime){
if (timeSpan == null) {
timeSpan = new TimeSpan();
}
if (timespanFrom == dateTime) {
setDateTime(dateTime, timeSpan.from);
} else if (timespanTo == dateTime) {
setDateTime(dateTime, timeSpan.until);
}
}
private TimeTool getDate(DateTime selDate, int hour, int minute, int second){
Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, selDate.getDay());
cal.set(Calendar.MONTH, selDate.getMonth());
cal.set(Calendar.YEAR, selDate.getYear());
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, second);
TimeTool date = new TimeTool(cal.getTime());
return date;
}
public static DateTime date(Composite parent, String label, String property, ModelEditor<?> editor,
FormToolkit toolkit) {
toolkit.createLabel(parent, label, SWT.NONE);
DateTime dateTime = new DateTime(parent, SWT.DATE | SWT.DROP_DOWN);
GridData data = new GridData();
data.widthHint = 150;
dateTime.setLayoutData(data);
editor.getBinding().onDate(() -> editor.getModel(), property, dateTime);
new CommentControl(parent, toolkit, property, editor.getComments());
return dateTime;
}
@Override
protected void createExtendedArea(Composite parent) {
super.createExtendedArea(parent);
if (column.getSortDataType() == SortDataType.Date) {
widgetComp = new Composite(parent, SWT.NONE);
widgetComp.setLayout(new GridLayout(6, false));
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
widgetComp.setLayoutData(gd);
Label label = new Label(widgetComp, SWT.NONE);
label.setText("Date Match: ");
dateRangeTypeCombo = new ComboViewer(widgetComp, SWT.NONE);
dateRangeTypeCombo.setContentProvider(new ArrayContentProvider());
dateRangeTypeCombo.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
return ((DateRangeType) element).getDisplayName();
}
});
dateRangeTypeCombo.setInput(DateRangeType.values());
dateRangeTypeCombo.addSelectionChangedListener(event -> {
String text2 = dateRangeTypeCombo.getCombo().getText();
dateRangeType = DateRangeType.get(text2);
updateDate2Composite();
});
date1Widget = new DateTime(widgetComp, SWT.CALENDAR);
date1Widget.addListener(SWT.Selection, e-> setDate1Selection());
// set initial date
Calendar cal = Calendar.getInstance();
cal.set(date1Widget.getYear(), date1Widget.getMonth(), date1Widget.getDay(), 0, 0);
date1 = cal.getTime();
time1Widget = new DateTime(widgetComp, SWT.TIME);
time1Widget.addListener(SWT.Selection, e-> setDate1Selection());
time1Widget.setHours(0);
time1Widget.setMinutes(0);
time1Widget.setSeconds(0);
}
}
/**
* Create the editor's GUI.
*
* @param parent Parent Composite.
* @return the top level Control for the GUI.
*/
private Control createControl(Composite parent) {
// controls
Composite topLevel = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
topLevel.setLayout(layout);
Label labelId = new Label(topLevel, SWT.NONE);
id = new Text(topLevel, SWT.BORDER);
Label labelName = new Label(topLevel, SWT.NONE);
name = new Text(topLevel, SWT.BORDER);
Label labelDescription = new Label(topLevel, SWT.NONE);
description = new Text(
topLevel, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
Label labelQuarter = new Label(topLevel, SWT.NONE);
quarter = new Text(topLevel, SWT.BORDER);
Label labelUpdatedBy = new Label(topLevel, SWT.NONE);
updatedBy = new Text(topLevel, SWT.BORDER);
Label labelUpdateDate = new Label(topLevel, SWT.NONE);
updateDate = new DateTime(topLevel, SWT.CALENDAR);
Label labelEngineers = new Label(topLevel, SWT.None);
Control engineersEdit = createEngineersEditor(topLevel);
// content
labelId.setText("ID");
labelName.setText("Name");
labelDescription.setText("Description");
labelQuarter.setText("Quarter");
labelUpdatedBy.setText("Updated by");
labelUpdateDate.setText("Updated date");
labelEngineers.setText("Engineers");
// layout
labelUpdateDate.setLayoutData(
new GridData(SWT.FILL, SWT.TOP, false, false));
labelDescription.setLayoutData(
new GridData(SWT.FILL, SWT.TOP, false, false));
labelEngineers.setLayoutData(
new GridData(SWT.FILL, SWT.TOP, false, false));
id.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
name.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
quarter.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
updatedBy.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
GridData descriptionLayout = new GridData(SWT.FILL, SWT.FILL, true, false);
descriptionLayout.heightHint = 150;
description.setLayoutData(descriptionLayout);
engineersEdit.setLayoutData(descriptionLayout);
fillContent();
return topLevel;
}
public DateTimeControl(final Composite parent) {
super(parent, SWT.NONE);
setLayout(GridLayoutFactory.fillDefaults().numColumns(2).create());
dateControl = new DateTime(this, SWT.BORDER | SWT.DATE | SWT.DROP_DOWN);
timeControl = new DateTime(this, SWT.BORDER | SWT.TIME);
}
public DateTimeObservable(final DateTime dateTime) {
this.dateTime = dateTime;
this.dateTime.addSelectionListener(listener);
}
private void getTime(DateTime widget, TimeTool time){
time.set(Calendar.HOUR_OF_DAY, widget.getHours());
time.set(Calendar.MINUTE, widget.getMinutes());
time.set(Calendar.SECOND, widget.getSeconds());
}
private void getDate(DateTime widget, TimeTool date){
date.set(Calendar.YEAR, widget.getYear());
date.set(Calendar.MONTH, widget.getMonth());
date.set(Calendar.DAY_OF_MONTH, widget.getDay());
}
private void getTime(DateTime widget, TimeTool time){
time.set(Calendar.HOUR_OF_DAY, widget.getHours());
time.set(Calendar.MINUTE, widget.getMinutes());
time.set(Calendar.SECOND, widget.getSeconds());
}
private void getDate(DateTime widget, TimeTool date){
date.set(Calendar.YEAR, widget.getYear());
date.set(Calendar.MONTH, widget.getMonth());
date.set(Calendar.DAY_OF_MONTH, widget.getDay());
}
private void getTime(DateTime widget, TimeTool time){
time.set(Calendar.HOUR_OF_DAY, widget.getHours());
time.set(Calendar.MINUTE, widget.getMinutes());
time.set(Calendar.SECOND, widget.getSeconds());
}
private void getDate(DateTime widget, TimeTool date){
date.set(Calendar.YEAR, widget.getYear());
date.set(Calendar.MONTH, widget.getMonth());
date.set(Calendar.DAY_OF_MONTH, widget.getDay());
}
protected DateTime date(Composite parent, String label, String property) {
return Widgets.date(parent, label, property, getEditor(), getToolkit());
}
/**
* DateTimeにCalendarの年月日をセットします
*
* @param cal
* @param dateTime
*/
private static void setCalendar(Calendar cal, DateTime dateTime) {
dateTime.setDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
}
/**
* Update the date value of the DateTime.
*
* @param time
* @param dateTime
*/
private void setDate(TimeTool time, DateTime dateTime){
dateTime.setDay(time.get(TimeTool.DAY_OF_MONTH));
dateTime.setMonth(time.get(TimeTool.MONTH));
dateTime.setYear(time.get(TimeTool.YEAR));
}