/* * Copyright (c) 2007,2008 Declarative Engineering LLC. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Declarative Engineering LLC * verson 1 which accompanies this distribution, and is available at * http://declarativeengineering.com/legal/DE_Developer_License_v1.txt */ package com.foundation.tcv.swt.client; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import com.common.debug.Debug; import com.common.thread.IRunnable; import com.foundation.tcv.swt.IDateTime; import com.foundation.tcv.view.ViewMessage; public class DateTime extends Component implements IDateTime, SelectionListener { /** Whether the selection state should be auto-synchronized. */ private boolean autoSynchronizeSelection = true; /** The delay to be used when auto synchronizing changes. */ private long autoSynchronizeSelectionDelay = 500; /** The task that auto synchronizes the selection state after a short delay. */ private Task autoSynchronizeSelectionTask = null; /** Whether to send the double click events to the server for processing. */ private boolean sendDoubleClick = false; /** Whether the year should be transfered to/from the view/model. */ private boolean includeYear = false; /** Whether the month should be transfered to/from the view/model. */ private boolean includeMonth = false; /** Whether the day should be transfered to/from the view/model. */ private boolean includeDay = false; /** Whether the hour should be transfered to/from the view/model. */ private boolean includeHour = false; /** Whether the minutes should be transfered to/from the view/model. */ private boolean includeMinute = false; /** Whether the seconds should be transfered to/from the view/model. */ private boolean includeSecond = false; /** The calendar used to transfer the date/time data to/from the model. */ private Calendar calendar = GregorianCalendar.getInstance(); /** * DateTime constructor. */ public DateTime() { }//DateTime()// /** * Gets the SWT button that represents this button. * @return The SWT button providing visualization for this button. */ public org.eclipse.swt.widgets.DateTime getSwtDateTime() { return (org.eclipse.swt.widgets.DateTime) getSwtControl(); }//getSwtDateTime()// /** * Determines whether the control should send double click events to the server. * @return Whether double click events need to be forwarded to the server for processing. */ protected boolean getSendDoubleClick() { return sendDoubleClick; }//getSendDoubleClick()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalViewInitialize() */ protected void internalViewInitialize() { getSwtDateTime().addSelectionListener(this); super.internalViewInitialize(); }//internalViewInitialize()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalViewRelease() */ protected void internalViewRelease() { if((getSwtDateTime() != null) && (!getSwtDateTime().isDisposed())) { getSwtDateTime().removeSelectionListener(this); }//if// synchronized(this) { if(autoSynchronizeSelectionTask != null) { removeTask(autoSynchronizeSelectionTask); autoSynchronizeSelectionTask = null; }//if// }//synchronized// super.internalViewRelease(); }//internalViewRelease()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalViewSynchronize() */ protected void internalViewSynchronize() { super.internalViewSynchronize(); if(!autoSynchronizeSelection) { Calendar calendar = GregorianCalendar.getInstance(); calendar.set(getSwtDateTime().getYear(), getSwtDateTime().getMonth(), getSwtDateTime().getDay(), getSwtDateTime().getHours(), getSwtDateTime().getMinutes(), getSwtDateTime().getSeconds()); sendMessage(MESSAGE_VIEW_SYNCHRONIZE_SELECTION, calendar.getTime()); }//if// }//internalViewSynchronize()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalProcessMessage(com.foundation.tcv.model.ViewMessage) */ public Object internalProcessMessage(ViewMessage viewMessage) { Object result = null; switch(viewMessage.getMessageNumber()) { case MESSAGE_INITIALIZE: { if(getSwtControl() == null) { int style = viewMessage.getMessageSecondaryInteger(); //Link to the parent container.// setContainer((Container) getComponent(viewMessage.getMessageInteger())); getContainer().addComponent(this); //Create the SWT widget.// setSwtWidget(new org.eclipse.swt.widgets.DateTime(getContainer().getSwtParent(), style)); getSwtWidget().setData(this); //If the container has already been initialized then force the parent to re-layout so that this component will appear.// if(getContainer().isInitialized()) { getContainer().getSwtComposite().layout(true, true); }//if// includeYear = (((style & STYLE_DATE) > 0) || ((style & STYLE_CALENDAR) > 0)); includeMonth = (((style & STYLE_DATE) > 0) || ((style & STYLE_CALENDAR) > 0)); includeDay = ((((style & STYLE_DATE) > 0) && ((style & STYLE_SHORT) == 0)) || ((style & STYLE_CALENDAR) > 0)); includeHour = ((style & STYLE_TIME) > 0); includeMinute = ((style & STYLE_TIME) > 0); includeSecond = (((style & STYLE_TIME) > 0) && ((style & STYLE_SHORT) == 0)); }//if// break; }//case// case MESSAGE_VIEW_REFRESH_SELECTION: { Date date = (Date) viewMessage.getMessageData(); date = date == null ? new Date() : date; calendar.setTime(date); if(includeYear) { getSwtDateTime().setYear(calendar.get(Calendar.YEAR)); }//if// if(includeMonth) { getSwtDateTime().setMonth(calendar.get(Calendar.MONTH)); }//if// if(includeDay) { getSwtDateTime().setDay(calendar.get(Calendar.DAY_OF_MONTH)); }//if// if(includeHour) { getSwtDateTime().setHours(calendar.get(Calendar.HOUR_OF_DAY)); }//if// if(includeMinute) { getSwtDateTime().setMinutes(calendar.get(Calendar.MINUTE)); }//if// if(includeSecond) { getSwtDateTime().setSeconds(calendar.get(Calendar.SECOND)); }//if// break; }//case// case MESSAGE_SET_AUTO_SYNCHRONIZE_SELECTION: { autoSynchronizeSelection = ((Boolean) viewMessage.getMessageData()).booleanValue(); break; }//case// case MESSAGE_SET_AUTO_SYNCHRONIZE_SELECTION_DELAY: { autoSynchronizeSelectionDelay = ((Long) viewMessage.getMessageData()).longValue(); break; }//case// case MESSAGE_SEND_DOUBLE_CLICK: { sendDoubleClick = ((Boolean) viewMessage.getMessageData()).booleanValue(); break; }//case// default: { result = super.internalProcessMessage(viewMessage); }//default// }//switch// return result; }//internalProcessMessage()// /* (non-Javadoc) * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) */ public void widgetDefaultSelected(SelectionEvent event) { //Called when the user double clicks a calendar date.// if(getSendDoubleClick()) { long oldAutoSynchronizeSelectionDelay = autoSynchronizeSelectionDelay; try { //Hold the messages so they get sent together.// addMessageHold(); //Set the synchronize delay to zero so the synchronize occurs immediatly.// autoSynchronizeSelectionDelay = 0L; //Force any selection task to complete prior to sending the double click, or force any selection change to be synchronized.// synchronized(this) { if(autoSynchronizeSelectionTask != null) { autoSynchronizeSelectionTask.run(); }//if// else if(!autoSynchronizeSelection) { selectionChanged(calendar.getTime()); }//else if// }//synchronized// //Send the double click message.// sendRoundTripMessage(MESSAGE_DOUBLE_CLICK, null, null); }//try// finally { //Reset the delay and send the messages.// autoSynchronizeSelectionDelay = oldAutoSynchronizeSelectionDelay; removeMessageHold(); }//finally// }//if// }//widgetDefaultSelected()// /* (non-Javadoc) * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) */ public void widgetSelected(SelectionEvent event) { if(includeYear) { calendar.set(Calendar.YEAR, getSwtDateTime().getYear()); }//if// if(includeMonth) { calendar.set(Calendar.MONTH, getSwtDateTime().getMonth()); }//if// if(includeDay) { calendar.set(Calendar.DAY_OF_MONTH, getSwtDateTime().getDay()); }//if// if(includeHour) { calendar.set(Calendar.HOUR_OF_DAY, getSwtDateTime().getHours()); }//if// if(includeMinute) { calendar.set(Calendar.MINUTE, getSwtDateTime().getMinutes()); }//if// if(includeSecond) { calendar.set(Calendar.SECOND, getSwtDateTime().getSeconds()); }//if// selectionChanged(calendar.getTime()); }//widgetSelected()// /** * Called when the selection is changed in the view control. * This method updates all bindings and * @param date The selected value. */ protected void selectionChanged(final Date date) { if(autoSynchronizeSelection) { if(autoSynchronizeSelectionDelay > 0) { //Start a task to send the text to the server after a short delay. This will reduce the overhead of auto synchronizing the selection.// synchronized(this) { if(autoSynchronizeSelectionTask != null) { removeTask(autoSynchronizeSelectionTask); }//if// autoSynchronizeSelectionTask = new Task() { public void execute() { //Make sure that this task is still valid and is not being superceeded.// synchronized(DateTime.this) { if(autoSynchronizeSelectionTask == this) { //Cleanup after the task.// autoSynchronizeSelectionTask = null; getEventLoop().executeAsync(new IRunnable() { public Object run() { sendMessage(MESSAGE_VIEW_SYNCHRONIZE_SELECTION, date); return null; }//run()// }); }//if// }//synchronized// }//run()// }; addTask(autoSynchronizeSelectionTask, autoSynchronizeSelectionDelay); }//synchronized// }//if// else { sendMessage(MESSAGE_VIEW_SYNCHRONIZE_SELECTION, date); }//else// }//if// }//selectionChanged()// }//DateTime//