273 lines
8.9 KiB
Java
273 lines
8.9 KiB
Java
|
|
/*
|
||
|
|
* Copyright (c) 2007,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.SelectionEvent;
|
||
|
|
import org.eclipse.swt.events.SelectionListener;
|
||
|
|
import org.eclipse.swt.graphics.Image;
|
||
|
|
import org.eclipse.swt.widgets.Event;
|
||
|
|
import org.eclipse.swt.widgets.Listener;
|
||
|
|
import org.eclipse.swt.widgets.Shell;
|
||
|
|
|
||
|
|
import com.common.comparison.Comparator;
|
||
|
|
import com.foundation.tcv.client.view.ResourceHolder;
|
||
|
|
import com.foundation.tcv.model.LinkInfo;
|
||
|
|
import com.foundation.tcv.swt.ITrayItem;
|
||
|
|
import com.foundation.tcv.swt.client.AbstractComponent.Linkage;
|
||
|
|
import com.foundation.tcv.view.ViewMessage;
|
||
|
|
import com.foundation.view.JefImage;
|
||
|
|
import com.foundation.view.LinkData;
|
||
|
|
import com.foundation.view.swt.SwtViewContext;
|
||
|
|
|
||
|
|
public class TrayItem extends AbstractComponent implements ITrayItem {
|
||
|
|
/** The reference to the popup menu. Not all components must allow a popup menu. */
|
||
|
|
private Menu menu = null;
|
||
|
|
/** A holder for the value of the tool tip text. */
|
||
|
|
private ResourceHolder toolTipTextHolder = new ResourceHolder(this);
|
||
|
|
/** A holder for the value of the visibility flag. */
|
||
|
|
private ResourceHolder isVisibleHolder = new ResourceHolder(this);
|
||
|
|
/** A holder for the value of the image. */
|
||
|
|
private ResourceHolder imageHolder = new ResourceHolder(this);
|
||
|
|
/** The swt image. */
|
||
|
|
private Image swtImage = null;
|
||
|
|
/** A shell used only to support the menu used by the tray item. */
|
||
|
|
private Shell shell = null;
|
||
|
|
/** Whether selection notifications are sent to the server. */
|
||
|
|
private boolean synchronizeSelections = false;
|
||
|
|
/** The linkage for the selection. */
|
||
|
|
private Linkage selectionLinkage = new Linkage();
|
||
|
|
/**
|
||
|
|
* TrayItem constructor.
|
||
|
|
*/
|
||
|
|
public TrayItem() {
|
||
|
|
}//TrayItem()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see AbstractComponent#destroy()
|
||
|
|
*/
|
||
|
|
protected void destroy() {
|
||
|
|
if(getSwtTrayItem() != null) {
|
||
|
|
if(!getSwtTrayItem().isDisposed()) {
|
||
|
|
getSwtTrayItem().setVisible(false);
|
||
|
|
}//if//
|
||
|
|
|
||
|
|
super.destroy();
|
||
|
|
}//if//
|
||
|
|
}//destroy()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.IDecoration#getMenuBar()
|
||
|
|
*/
|
||
|
|
public Menu getMenu() {
|
||
|
|
return menu;
|
||
|
|
}//getMenuBar()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.view.swt.IDecoration#setMenuBar(com.foundation.view.swt.Menu)
|
||
|
|
*/
|
||
|
|
public void setMenu(Menu menu) {
|
||
|
|
this.menu = menu;
|
||
|
|
}//setMenu()//
|
||
|
|
/**
|
||
|
|
* Gets the widget encapsulated by this component.
|
||
|
|
* @return The encapsulated widget, or null if this component does not have a viewable element.
|
||
|
|
*/
|
||
|
|
public org.eclipse.swt.widgets.TrayItem getSwtTrayItem() {
|
||
|
|
return (org.eclipse.swt.widgets.TrayItem) getSwtWidget();
|
||
|
|
}//getSwtTrayItem()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.AbstractComponent#getContainer()
|
||
|
|
*/
|
||
|
|
public Container getContainer() {
|
||
|
|
return null;
|
||
|
|
}//getContainer()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.AbstractComponent#getShell()
|
||
|
|
*/
|
||
|
|
public Shell getShell() {
|
||
|
|
return shell;
|
||
|
|
}//getShell()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.AbstractComponent#inernalViewInitializeAll()
|
||
|
|
*/
|
||
|
|
protected void internalViewInitializeAll() {
|
||
|
|
if(getMenu() != null) {
|
||
|
|
getMenu().internalViewInitializeAll();
|
||
|
|
}//if//
|
||
|
|
|
||
|
|
super.internalViewInitializeAll();
|
||
|
|
}//inernalViewInitializeAll()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.AbstractComponent#internalViewReleaseAll()
|
||
|
|
*/
|
||
|
|
protected void internalViewReleaseAll() {
|
||
|
|
if(getMenu() != null) {
|
||
|
|
getMenu().internalViewReleaseAll();
|
||
|
|
}//if//
|
||
|
|
|
||
|
|
super.internalViewReleaseAll();
|
||
|
|
}//internalViewReleaseAll()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.AbstractComponent#internalViewSynchronizeAll()
|
||
|
|
*/
|
||
|
|
protected void internalViewSynchronizeAll() {
|
||
|
|
if(getMenu() != null) {
|
||
|
|
getMenu().internalViewSynchronizeAll();
|
||
|
|
}//if//
|
||
|
|
|
||
|
|
super.internalViewSynchronizeAll();
|
||
|
|
}//internalViewSynchronizeAll()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.AbstractComponent#internalViewInitialize()
|
||
|
|
*/
|
||
|
|
protected void internalViewInitialize() {
|
||
|
|
getSwtTrayItem().addListener(SWT.MenuDetect, new Listener() {
|
||
|
|
public void handleEvent(Event event) {
|
||
|
|
if(menu != null) {
|
||
|
|
menu.getSwtMenu().setVisible(true);
|
||
|
|
}//if//
|
||
|
|
}//handleEvent()//
|
||
|
|
});
|
||
|
|
getSwtTrayItem().addSelectionListener(new SelectionListener() {
|
||
|
|
public void widgetSelected(SelectionEvent e) {
|
||
|
|
if(synchronizeSelections) {
|
||
|
|
sendRoundTripMessage(MESSAGE_VIEW_SYNCHRONIZE_SELECTION, null, null, e.x, e.y, null);
|
||
|
|
}//if//
|
||
|
|
else if(menu != null) {
|
||
|
|
menu.getSwtMenu().setVisible(true);
|
||
|
|
}//else if//
|
||
|
|
|
||
|
|
selectionLinkage.invoke(null);
|
||
|
|
}//widgetSelected()//
|
||
|
|
public void widgetDefaultSelected(SelectionEvent e) {
|
||
|
|
}//widgetDefaultSelected()//
|
||
|
|
});
|
||
|
|
|
||
|
|
super.internalViewInitialize();
|
||
|
|
}//internalViewInitialize()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.AbstractComponent#internalViewRelease()
|
||
|
|
*/
|
||
|
|
protected void internalViewRelease() {
|
||
|
|
destroyImage(swtImage);
|
||
|
|
|
||
|
|
if(getContainer() != null) {
|
||
|
|
getContainer().getComponents().remove(this);
|
||
|
|
}//if//
|
||
|
|
|
||
|
|
toolTipTextHolder.release();
|
||
|
|
isVisibleHolder.release();
|
||
|
|
imageHolder.release();
|
||
|
|
|
||
|
|
super.internalViewRelease();
|
||
|
|
}//internalViewRelease()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.AbstractComponent#internalResourceHolderChanged(com.foundation.tcv.client.view.ResourceHolder, java.lang.Object, java.lang.Object, int)
|
||
|
|
*/
|
||
|
|
protected void internalResourceHolderChanged(ResourceHolder resourceHolder, Object oldValue, Object newValue, int flags) {
|
||
|
|
if(resourceHolder == toolTipTextHolder) {
|
||
|
|
setToolTipText((String) newValue);
|
||
|
|
}//if//
|
||
|
|
else if(resourceHolder == imageHolder) {
|
||
|
|
destroyImage(swtImage);
|
||
|
|
|
||
|
|
if(!getSwtTrayItem().isDisposed()) {
|
||
|
|
swtImage = createImage((JefImage) imageHolder.getValue());
|
||
|
|
getSwtTrayItem().setImage(swtImage);
|
||
|
|
}//if//
|
||
|
|
}//else if//
|
||
|
|
else if(resourceHolder == isVisibleHolder) {
|
||
|
|
if(!getSwtTrayItem().isDisposed()) {
|
||
|
|
getSwtTrayItem().setVisible(((Boolean) newValue).booleanValue());
|
||
|
|
}//if//
|
||
|
|
}//else if//
|
||
|
|
else {
|
||
|
|
super.internalResourceHolderChanged(resourceHolder, oldValue, newValue, flags);
|
||
|
|
}//else//
|
||
|
|
}//internalOnAssociationChanged()//
|
||
|
|
/* (non-Javadoc)
|
||
|
|
* @see com.foundation.tcv.swt.client.AbstractComponent#internalOnLinkInvoked(int, java.lang.Object)
|
||
|
|
*/
|
||
|
|
protected void internalOnLinkInvoked(int linkTarget, Object data) {
|
||
|
|
switch(linkTarget) {
|
||
|
|
case LINK_TARGET_IS_VISIBLE: {
|
||
|
|
getSwtTrayItem().setVisible(data != null && data instanceof Boolean ? ((Boolean) data).booleanValue() : false);
|
||
|
|
break;
|
||
|
|
}//case//
|
||
|
|
case LINK_TARGET_TOOL_TIP_TEXT: {
|
||
|
|
getSwtTrayItem().setToolTipText(data instanceof String ? (String) data : "");
|
||
|
|
break;
|
||
|
|
}//case//
|
||
|
|
default: {
|
||
|
|
super.internalOnLinkInvoked(linkTarget, data);
|
||
|
|
break;
|
||
|
|
}//default//
|
||
|
|
}//switch//
|
||
|
|
}//internalOnLinkInvoked()//
|
||
|
|
/* (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(getSwtTrayItem() == null) {
|
||
|
|
int style = viewMessage.getMessageInteger();
|
||
|
|
|
||
|
|
//Create the SWT widget.//
|
||
|
|
setSwtWidget(new org.eclipse.swt.widgets.TrayItem(SwtViewContext.getSingleton().getDisplay().getSystemTray(), style));
|
||
|
|
getSwtWidget().setData(this);
|
||
|
|
shell = new Shell(SwtViewContext.getSingleton().getDisplay());
|
||
|
|
}//if//
|
||
|
|
break;
|
||
|
|
}//case//
|
||
|
|
case MESSAGE_SET_IS_VISIBLE: {
|
||
|
|
isVisibleHolder.setValue(viewMessage.getMessageData());
|
||
|
|
break;
|
||
|
|
}//case//
|
||
|
|
case MESSAGE_SET_TOOL_TIP_TEXT: {
|
||
|
|
toolTipTextHolder.setValue(viewMessage.getMessageData());
|
||
|
|
break;
|
||
|
|
}//case//
|
||
|
|
case MESSAGE_SET_IMAGE: {
|
||
|
|
imageHolder.setValue(viewMessage.getMessageData());
|
||
|
|
break;
|
||
|
|
}//case//
|
||
|
|
case MESSAGE_SET_SYNCHRONIZE_SELECTIONS: {
|
||
|
|
synchronizeSelections = ((Boolean) viewMessage.getMessageData()).booleanValue();
|
||
|
|
break;
|
||
|
|
}//case//
|
||
|
|
case MESSAGE_ADD_SELECTION_LINK: {
|
||
|
|
LinkInfo info = (LinkInfo) viewMessage.getMessageData();
|
||
|
|
|
||
|
|
selectionLinkage.add(new LinkData(getComponent(info.getComponent()), info.getTarget(), info.getData(), info.isBoolean(), info.invertLogic(), info.nullValue()));
|
||
|
|
break;
|
||
|
|
}//case//
|
||
|
|
default: {
|
||
|
|
result = super.internalProcessMessage(viewMessage);
|
||
|
|
}//case//
|
||
|
|
}//switch//
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}//internalProcessMessage()//
|
||
|
|
/**
|
||
|
|
* Gets this component's assigned tool tip text.
|
||
|
|
* @return The tool tip for this component.
|
||
|
|
*/
|
||
|
|
public String getToolTipText() {
|
||
|
|
return (String) toolTipTextHolder.getValue();
|
||
|
|
}//getToolTipText()//
|
||
|
|
/**
|
||
|
|
* Sets this component's assigned tool tip text.
|
||
|
|
* @param toolTipText The tool tip for this component.
|
||
|
|
*/
|
||
|
|
public void setToolTipText(String toolTipText) {
|
||
|
|
if(!Comparator.equals(toolTipText, getSwtTrayItem().getToolTipText())) {
|
||
|
|
getSwtTrayItem().setToolTipText(toolTipText);
|
||
|
|
}//if//
|
||
|
|
}//setToolTipText()//
|
||
|
|
}//TrayItem//
|