/* * Copyright (c) 2006,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 org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import com.common.thread.IRunnable; import com.foundation.tcv.swt.ISpinner; import com.foundation.tcv.view.ViewMessage; public class Spinner extends Component implements ISpinner, SelectionListener { /** Whether the selection should be automatically sent to the model. */ private boolean autoSynchronizeSelection = false; /** The number of milliseconds of delay before auto synchronizing the selection. */ private long autoSynchronizeSelectionDelay = 500; /** The task that auto synchronizes the selection after a short delay. */ protected Task autoSynchronizeSelectionTask = null; /** * Spinner constructor. */ public Spinner() { super(); }//Spinner()// /** * Gets the SWT spinner that represents this spinner. * @return The SWT spinner providing visualization for this spinner. */ public org.eclipse.swt.widgets.Spinner getSwtSpinner() { return (org.eclipse.swt.widgets.Spinner) getSwtWidget(); }//getSwtSpinner()// /** * Determines whether the control should automatically synchronize all selections. * @return Whether selections should automatically get sent to the server. */ protected boolean getAutoSynchronizeSelection() { return autoSynchronizeSelection; }//getAutoSynchronizeSelection()// /** * Gets the delay in milliseconds between the selection event and updating the model. * @return The number of milliseconds to wait before updating the model. */ protected long getAutoSynchronizeSelectionDelay() { return autoSynchronizeSelectionDelay; }//getAutoSynchronizeSelectionDelay()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalViewInitialize() */ protected void internalViewInitialize() { getSwtSpinner().addSelectionListener(this); super.internalViewInitialize(); }//internalViewInitialize()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalViewRelease() */ protected void internalViewRelease() { if(getSwtSpinner() != null && !getSwtSpinner().isDisposed()) { getSwtSpinner().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(!getAutoSynchronizeSelection()) { int selection = getSwtSpinner().getSelection(); sendMessage(MESSAGE_VIEW_SYNCHRONIZE_SELECTION, new Integer(selection)); }//if// else { synchronized(this) { if(autoSynchronizeSelectionTask != null) { removeTask(autoSynchronizeSelectionTask); autoSynchronizeSelectionTask.execute(); }//if// }//synchronized// }//else if// }//internalViewSynchronize()// /** * Synchronizes the selection(s) with the server. */ private void internalSynchronizeSelection() { if(getAutoSynchronizeSelection()) { if(autoSynchronizeSelectionDelay > 0) { //Start a task to send the text to the server after a short delay.// synchronized(this) { final Integer selection = new Integer(getSwtSpinner().getSelection()); if(autoSynchronizeSelectionTask != null) { removeTask(autoSynchronizeSelectionTask); }//if// autoSynchronizeSelectionTask = new Task() { boolean hasRun = false; public void execute() { //Make sure that this task is still valid and is not being superceeded.// synchronized(Spinner.this) { if((autoSynchronizeSelectionTask == this) && (!hasRun)) { hasRun = true; //Cleanup after the task.// autoSynchronizeSelectionTask = null; getEventLoop().executeAsync(new IRunnable() { public Object run() { sendMessage(MESSAGE_VIEW_SYNCHRONIZE_SELECTION, selection); return null; }//run()// }); }//if// }//synchronized// }//run()// }; addTask(autoSynchronizeSelectionTask, autoSynchronizeSelectionDelay); }//synchronized// }//if// else { sendMessage(MESSAGE_VIEW_SYNCHRONIZE_SELECTION, new Integer(getSwtSpinner().getSelection())); }//else// }//if// }//internalSynchronizeSelection()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalProcessMessage(com.foundation.tcv.model.ViewMessage) */ public Object internalProcessMessage(ViewMessage viewMessage) { Object retVal = null; switch(viewMessage.getMessageNumber()) { case MESSAGE_INITIALIZE: { if(getSwtControl() == null) { int[] data = (int[]) viewMessage.getMessageData(); int style = data[1]; //Link to the parent container.// setContainer((Container) getComponent(data[0])); getContainer().addComponent(this); //Create the SWT widget.// setSwtWidget(new org.eclipse.swt.widgets.Spinner(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// }//if// break; }//case// case MESSAGE_SET_MAXIMUM: { getSwtSpinner().setMaximum(((Number) viewMessage.getMessageData()).intValue()); break; }//case// case MESSAGE_SET_MINIMUM: { getSwtSpinner().setMinimum(((Number) viewMessage.getMessageData()).intValue()); break; }//case// case MESSAGE_SET_SELECTION: { getSwtSpinner().setSelection(((Number) viewMessage.getMessageData()).intValue()); break; }//case// case MESSAGE_SET_INCREMENT: { getSwtSpinner().setIncrement(((Number) viewMessage.getMessageData()).intValue()); break; }//case// case MESSAGE_SET_PAGE_INCREMENT: { getSwtSpinner().setPageIncrement(((Number) viewMessage.getMessageData()).intValue()); 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// default: { retVal = super.internalProcessMessage(viewMessage); }//default// }//switch// return retVal; }//internalProcessMessage()// /* (non-Javadoc) * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) */ public void widgetDefaultSelected(SelectionEvent e) { widgetSelected(e); }//widgetDefaultSelected()// /* (non-Javadoc) * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) */ public void widgetSelected(SelectionEvent e) { //TODO: Is there some way to determine exactly what selection or deselection occured given the event object?// internalSynchronizeSelection(); }//widgetSelected()// }//Spinner//