374 lines
12 KiB
Java
374 lines
12 KiB
Java
|
|
/*
|
||
|
|
* Copyright (c) 2003,2009 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.SWT;
|
||
|
|
import org.eclipse.swt.events.*;
|
||
|
|
|
||
|
|
import com.common.thread.IRunnable;
|
||
|
|
import com.foundation.tcv.swt.*;
|
||
|
|
import com.foundation.tcv.swt.client.cell.CellComponent;
|
||
|
|
import com.foundation.tcv.view.*;
|
||
|
|
|
||
|
|
public class ComboBox extends IndexedCollectionComponent implements SelectionListener, IComboBox, ModifyListener {
|
||
|
|
/** Used to stop selection events from being handled while the items are being changed. */
|
||
|
|
private boolean suspendSelectionEvents = false;
|
||
|
|
/**
|
||
|
|
* ComboBox constructor.
|
||
|
|
*/
|
||
|
|
public ComboBox() {
|
||
|
|
super();
|
||
|
|
}//ComboBox()//
|
||
|
|
/**
|
||
|
|
* Gets the SWT combo that represents this combo box.
|
||
|
|
* @return The SWT combo providing visualization for this combo box.
|
||
|
|
*/
|
||
|
|
public org.eclipse.swt.widgets.Combo getSwtCombo() {
|
||
|
|
return (org.eclipse.swt.widgets.Combo) getSwtWidget();
|
||
|
|
}//getSwtCombo()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.AbstractComponent#internalViewInitialize()
|
||
|
|
*/
|
||
|
|
protected void internalViewInitialize() {
|
||
|
|
getSwtCombo().addSelectionListener(this);
|
||
|
|
getSwtCombo().addModifyListener(this);
|
||
|
|
super.internalViewInitialize();
|
||
|
|
}//internalViewInitialize()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.AbstractComponent#internalViewRelease()
|
||
|
|
*/
|
||
|
|
protected void internalViewRelease() {
|
||
|
|
if((getSwtCombo() != null) && (!getSwtCombo().isDisposed())) {
|
||
|
|
getSwtCombo().removeSelectionListener(this);
|
||
|
|
getSwtCombo().removeModifyListener(this);
|
||
|
|
}//if//
|
||
|
|
|
||
|
|
super.internalViewRelease();
|
||
|
|
}//internalViewRelease()//
|
||
|
|
/* (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.Combo(getContainer().getSwtParent(), style));
|
||
|
|
getSwtWidget().setData(this);
|
||
|
|
//Set the allows user items flag.//
|
||
|
|
setAllowUserItems(!((style & SWT.READ_ONLY) > 0));
|
||
|
|
|
||
|
|
//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_TEXT_LIMIT: {
|
||
|
|
getSwtCombo().setTextLimit(((Integer) viewMessage.getMessageData()).intValue());
|
||
|
|
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 event) {
|
||
|
|
widgetSelected(event);
|
||
|
|
}//widgetDefaultSelected()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
|
||
|
|
*/
|
||
|
|
public void widgetSelected(SelectionEvent event) {
|
||
|
|
updateSelectionLinks();
|
||
|
|
|
||
|
|
if(getAutoSynchronizeSelection()) {
|
||
|
|
if(getAutoSynchronizeSelectionDelay() > 0) {
|
||
|
|
//TODO: Multiple selections are never allowed for a combo...
|
||
|
|
int[] selectedObjectIds = getAllowMultiSelection() ? getSelectedObjectIds() : null;
|
||
|
|
int selectedObjectId = !getAllowMultiSelection() && !hasCustomSelection() ? getSelectedObjectId() : -1;
|
||
|
|
String customSelection = !getAllowMultiSelection() && hasCustomSelection() ? controlGetText() : null;
|
||
|
|
|
||
|
|
if((selectedObjectIds != null) || ((customSelection != null) && (!customSelection.equals(getCustomSelectionOnServer()))) || (selectedObjectId != getSelectedObjectIdOnServer())) {
|
||
|
|
final Object selection = customSelection != null ? (Object) customSelection : selectedObjectIds != null ? (Object) selectedObjectIds : selectedObjectId != -1 ? new Integer(selectedObjectId) : null;
|
||
|
|
|
||
|
|
//Update the known server state.//
|
||
|
|
if(selectedObjectIds == null) {
|
||
|
|
setSelectedObjectIdOnServer(selectedObjectId);
|
||
|
|
setCustomSelectionOnServer(customSelection);
|
||
|
|
}//if//
|
||
|
|
|
||
|
|
//Start a task to send the selection 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(ComboBox.this) {
|
||
|
|
if(autoSynchronizeSelectionTask == this) {
|
||
|
|
//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, getAutoSynchronizeSelectionDelay());
|
||
|
|
}//synchronized//
|
||
|
|
}//if//
|
||
|
|
}//if//
|
||
|
|
else {
|
||
|
|
synchronizeSelection();
|
||
|
|
}//else//
|
||
|
|
}//if//
|
||
|
|
}//widgetSelected()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
|
||
|
|
*/
|
||
|
|
public void modifyText(ModifyEvent event) {
|
||
|
|
if(!suspendSelectionEvents) {
|
||
|
|
//TODO: Should we remove this if block? Shouldn't widgetSelected(null) be called regardless?
|
||
|
|
if(controlGetSelection() == -1) {
|
||
|
|
widgetSelected(null);
|
||
|
|
}//if//
|
||
|
|
}//if//
|
||
|
|
}//modifyText()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlRemoveAll()
|
||
|
|
*/
|
||
|
|
protected void controlRemoveAll() {
|
||
|
|
suspendSelectionEvents = true;
|
||
|
|
|
||
|
|
try {
|
||
|
|
getSwtCombo().removeAll();
|
||
|
|
}//try//
|
||
|
|
finally {
|
||
|
|
suspendSelectionEvents = false;
|
||
|
|
}//finally//
|
||
|
|
}//controlRemoveAll()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlSetItems(String[])
|
||
|
|
*/
|
||
|
|
protected void controlSetItems(Object[] items) {
|
||
|
|
String[] itemTexts = new String[items.length];
|
||
|
|
|
||
|
|
System.arraycopy(items, 0, itemTexts, 0, itemTexts.length);
|
||
|
|
suspendSelectionEvents = true;
|
||
|
|
|
||
|
|
try {
|
||
|
|
getSwtCombo().setItems(itemTexts);
|
||
|
|
}//try//
|
||
|
|
finally {
|
||
|
|
suspendSelectionEvents = false;
|
||
|
|
}//finally//
|
||
|
|
}//controlSetItems()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlSetItem(int, Object)
|
||
|
|
*/
|
||
|
|
protected void controlSetItem(int index, Object itemData) {
|
||
|
|
suspendSelectionEvents = true;
|
||
|
|
|
||
|
|
try {
|
||
|
|
getSwtCombo().setItem(index, (String) itemData);
|
||
|
|
}//try//
|
||
|
|
finally {
|
||
|
|
suspendSelectionEvents = false;
|
||
|
|
}//finally//
|
||
|
|
}//controlSetItem()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlAddItem(Object, int)
|
||
|
|
*/
|
||
|
|
protected void controlAddItem(Object itemData, int index) {
|
||
|
|
suspendSelectionEvents = true;
|
||
|
|
|
||
|
|
try {
|
||
|
|
getSwtCombo().add((String) itemData, index);
|
||
|
|
}//try//
|
||
|
|
finally {
|
||
|
|
suspendSelectionEvents = false;
|
||
|
|
}//finally//
|
||
|
|
}//controlAddItem()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlAddItem(Object)
|
||
|
|
*/
|
||
|
|
protected void controlAddItem(Object itemData) {
|
||
|
|
suspendSelectionEvents = true;
|
||
|
|
|
||
|
|
try {
|
||
|
|
getSwtCombo().add((String) itemData);
|
||
|
|
}//try//
|
||
|
|
finally {
|
||
|
|
suspendSelectionEvents = false;
|
||
|
|
}//finally//
|
||
|
|
}//controlAddItem()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlRemoveItem(int)
|
||
|
|
*/
|
||
|
|
protected void controlRemoveItem(int index) {
|
||
|
|
suspendSelectionEvents = true;
|
||
|
|
|
||
|
|
try {
|
||
|
|
getSwtCombo().remove(index);
|
||
|
|
}//try//
|
||
|
|
finally {
|
||
|
|
suspendSelectionEvents = false;
|
||
|
|
}//finally//
|
||
|
|
}//controlRemoveItem()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlGetSelections()
|
||
|
|
*/
|
||
|
|
protected int[] controlGetSelections() {
|
||
|
|
//return getSwtCombo().getSelectionIndices();
|
||
|
|
throw new RuntimeException("Method not supported - controlGetSelections");
|
||
|
|
}//controlGetSelections()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlGetSelection()
|
||
|
|
*/
|
||
|
|
protected int controlGetSelection() {
|
||
|
|
return getSwtCombo().getSelectionIndex();
|
||
|
|
}//controlGetSelection()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlSetSelections()
|
||
|
|
*/
|
||
|
|
protected void controlSetSelections(int[] indices) {
|
||
|
|
//getSwtCombo().setSelection(indices);
|
||
|
|
throw new RuntimeException("Method not supported - controlSetSelections");
|
||
|
|
}//controlSetSelections()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlSetSelection()
|
||
|
|
*/
|
||
|
|
protected void controlSetSelection(int index) {
|
||
|
|
suspendSelectionEvents = true;
|
||
|
|
|
||
|
|
try {
|
||
|
|
if(index == -1) {
|
||
|
|
getSwtCombo().deselectAll();
|
||
|
|
}//if//
|
||
|
|
else {
|
||
|
|
getSwtCombo().select(index);
|
||
|
|
}//else//
|
||
|
|
}//try//
|
||
|
|
finally {
|
||
|
|
suspendSelectionEvents = false;
|
||
|
|
}//finally//
|
||
|
|
}//controlSetSelection()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.IndexedCollectionComponent#controlSetSelection(java.lang.String)
|
||
|
|
*/
|
||
|
|
protected void controlSetSelection(String text) {
|
||
|
|
suspendSelectionEvents = true;
|
||
|
|
|
||
|
|
try {
|
||
|
|
if(text == null) {
|
||
|
|
getSwtCombo().deselectAll();
|
||
|
|
}//if//
|
||
|
|
else {
|
||
|
|
getSwtCombo().deselectAll();
|
||
|
|
getSwtCombo().setText(text);
|
||
|
|
}//else//
|
||
|
|
}//try//
|
||
|
|
finally {
|
||
|
|
suspendSelectionEvents = false;
|
||
|
|
}//finally//
|
||
|
|
}//controlSetSelection()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlAddSelection()
|
||
|
|
*/
|
||
|
|
protected void controlAddSelection(int index) {
|
||
|
|
//getSwtCombo().select(index);
|
||
|
|
throw new RuntimeException("Method not supported - controlAddSelection");
|
||
|
|
}//controlAddSelection()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlRemoveSelection()
|
||
|
|
*/
|
||
|
|
protected void controlRemoveSelection(int index) {
|
||
|
|
suspendSelectionEvents = true;
|
||
|
|
|
||
|
|
try {
|
||
|
|
getSwtCombo().deselect(index);
|
||
|
|
}//try//
|
||
|
|
finally {
|
||
|
|
suspendSelectionEvents = false;
|
||
|
|
}//finally//
|
||
|
|
}//controlRemoveSelection()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlRemoveAllSelections()
|
||
|
|
*/
|
||
|
|
protected void controlRemoveAllSelections() {
|
||
|
|
suspendSelectionEvents = true;
|
||
|
|
getSwtCombo().deselectAll();
|
||
|
|
suspendSelectionEvents = false;
|
||
|
|
}//controlRemoveAllSelections()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlIsSelected()
|
||
|
|
*/
|
||
|
|
protected boolean controlIsSelected(int index) {
|
||
|
|
return getSwtCombo().getSelectionIndex() == index;
|
||
|
|
}//controlIsSelected()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlGetText()
|
||
|
|
*/
|
||
|
|
protected String controlGetText() {
|
||
|
|
return getSwtCombo().getText();
|
||
|
|
}//controlGetText()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.CollectionComponent#controlSetText(String)
|
||
|
|
*/
|
||
|
|
protected void controlSetText(String text) {
|
||
|
|
suspendSelectionEvents = true;
|
||
|
|
|
||
|
|
if(text == null) {
|
||
|
|
getSwtCombo().deselectAll();
|
||
|
|
}//if//
|
||
|
|
else {
|
||
|
|
getSwtCombo().setText(text);
|
||
|
|
}//else//
|
||
|
|
|
||
|
|
suspendSelectionEvents = false;
|
||
|
|
}//controlSetText()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.CollectionComponent#controlGetSelectionCount()
|
||
|
|
*/
|
||
|
|
protected int controlGetSelectionCount() {
|
||
|
|
return getSwtCombo().getText().length() > 0 ? 1 : 0;
|
||
|
|
}//controlGetSelectionCount()//
|
||
|
|
/**
|
||
|
|
* Forces the selection event handler to be called.
|
||
|
|
*/
|
||
|
|
protected void forceSelectionEvent() { //Should call widgetSelected implemented by the subclass. This name may need to change.//
|
||
|
|
widgetSelected(null);
|
||
|
|
}//forceSelectionEvent()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.ICellContainer#addComponent(com.foundation.tcv.swt.client.cell.CellComponent)
|
||
|
|
*/
|
||
|
|
public void addComponent(CellComponent component) {
|
||
|
|
//Never used.//
|
||
|
|
}//addComponent()//
|
||
|
|
}//ComboBox//
|