/* * Copyright (c) 2006,2007 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.Point; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import com.common.util.LiteList; import com.foundation.tcv.swt.ICoolBar; import com.foundation.tcv.view.ViewMessage; public class CoolBar extends Container implements ICoolBar { /** The collection of CoolItem instances contained by the cool bar. */ private LiteList coolItems = new LiteList(10, 20); /** * Encapsulates an item in the bar. */ public static class CoolItem extends AbstractComponent implements ICoolItem, SelectionListener { private Container container = null; /** The control displayed by the cool item. */ private Component control = null; /** The style used by the item. */ private int style; /** Whether a custom size is used for the item. */ private boolean customSize = false; /** The custom width for the item. */ private int customWidth = 0; /** The custom height for the item. */ private int customHeight = 0; /** Whether a custom preferred size is used for the item. */ private boolean customPreferredSize = false; /** The custom preferred width for the item. */ private int customPreferredWidth = 0; /** The custom preferred height for the item. */ private int customPreferredHeight = 0; /** Whether a custom minimum size is used for the item. */ private boolean customMinimumSize = false; /** The custom minimum width for the item. */ private int customMinimumWidth = 0; /** The custom minimum height for the item. */ private int customMinimumHeight = 0; /** Whether the item is currently visible. */ private boolean visible = true; /** * CoolItem constructor. */ public CoolItem() { }//CoolItem()// /* (non-Javadoc) * @see com.foundation.view.IAbstractComponent#getContainer() */ public Container getContainer() { return container; }//getContainer()// /* (non-Javadoc) * @see com.foundation.view.swt.IInternalAbstractComponent#getShell() */ public Shell getShell() { return container != null ? container.getShell() : null; }//getShell()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalProcessMessage(com.foundation.tcv.view.ViewMessage) */ public Object internalProcessMessage(ViewMessage viewMessage) { Object result = null; switch(viewMessage.getMessageNumber()) { case MESSAGE_INITIALIZE: { if(getSwtCoolItem() == null) { int[] data = (int[]) viewMessage.getMessageData(); style = data[1]; //Link to the parent container.// this.container = (Container) getComponent(data[0]); ((CoolBar) getContainer()).addCoolItem(this); initializeControl(style); }//if// break; }//case// case MESSAGE_SET_SIZE: { int[] data = (int[]) viewMessage.getMessageData(); customSize = true; customWidth = data[0]; customHeight = data[1]; if(getSwtCoolItem() != null) { getSwtCoolItem().setSize(customWidth, customHeight); }//if// break; }//case// case MESSAGE_SET_PREFERRED_SIZE: { int[] data = (int[]) viewMessage.getMessageData(); customPreferredSize = true; customPreferredWidth = data[0]; customPreferredHeight = data[1]; if(getSwtCoolItem() != null) { getSwtCoolItem().setPreferredSize(customPreferredWidth, customPreferredHeight); }//if// break; }//case// case MESSAGE_SET_MINIMUM_SIZE: { int[] data = (int[]) viewMessage.getMessageData(); customMinimumSize = true; customMinimumWidth = data[0]; customMinimumHeight = data[1]; if(getSwtCoolItem() != null) { getSwtCoolItem().setMinimumSize(customMinimumWidth, customMinimumHeight); }//if// break; }//case// case MESSAGE_SET_CONTROL: { int[] data = (int[]) viewMessage.getMessageData(); control = (Component) getComponent(data[0]); //TODO: If we are already initialized we should swap controls. Currently this could not happen, but we may allow it in the future. break; }//case// case MESSAGE_AUTO_SIZE: { if(!customPreferredSize && !customSize) { Point size; //control.getSwtControl().pack(); size = control.getSwtControl().computeSize(SWT.DEFAULT, SWT.DEFAULT); if(!customMinimumSize) { getSwtCoolItem().setMinimumSize(size); }//if// size = getSwtCoolItem().computeSize(size.x, size.y); size.x = Math.max(size.x, getSwtCoolItem().getMinimumSize().x); size.y = Math.max(size.y, getSwtCoolItem().getMinimumSize().y); getSwtCoolItem().setPreferredSize(size); getSwtCoolItem().setSize(size); }//if// break; }//case// case MESSAGE_SET_IS_VISIBLE: { boolean visible = ((Boolean) viewMessage.getMessageData()).booleanValue(); controlSetIsVisible(visible); break; }//case// default: { result = super.internalProcessMessage(viewMessage); }//default// }//switch// return result; }//internalProcessMessage()// /** * Initializes the control. * @param style The style for the control. */ protected void initializeControl(int style) { //Create the SWT widget.// setSwtWidget(new org.eclipse.swt.widgets.CoolItem(((CoolBar) getContainer()).getSwtCoolBar(), style)); getSwtWidget().setData(this); }//initializeControl()// /** * Sets whether the control is visible. * @param visible Whether the control is visible to the user. */ protected void controlSetIsVisible(boolean visible) { if(this.visible != visible) { this.visible = visible; //Destroy or create the cool item.// if(!visible) { if(getSwtCoolItem() != null && !getSwtCoolItem().isDisposed()) { getSwtCoolItem().dispose(); }//if// setSwtWidget(null); }//if// else { initializeControl(style); }//else// }//if// }//controlSetIsVisible()// /** * Gets the swt cool item for this cool item. * @return The SWT cool item providing visualization for this cool item. */ public org.eclipse.swt.widgets.CoolItem getSwtCoolItem() { return (org.eclipse.swt.widgets.CoolItem) getSwtWidget(); }//getSwtCoolItem()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewInitialize() */ protected void internalViewInitialize() { getSwtCoolItem().addSelectionListener(this); super.internalViewInitialize(); getSwtCoolItem().setControl(control.getSwtControl()); }//internalViewInitialize()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewRelease() */ protected void internalViewRelease() { if(!getSwtCoolItem().isDisposed()) { getSwtCoolItem().setControl(null); }//if// super.internalViewRelease(); }//internalViewRelease()// /** * Sets the control used by the cool item. * @param control The cool item's control. */ public void setControl(Component control) { this.control = control; }//setControl()// /* (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) { /* TODO: Enable the drop down cheveron to display a menu of items not visible in the cool item. if((menu != null) && (event.detail == SWT.ARROW)) { Rectangle rect = getSwtCoolItem().getBounds(); Point point = new Point(rect.x, rect.y + rect.height); //Show the drop down menu.// point = getSwtCoolItem().getParent().toDisplay(point); if(menu != null) { menu.getSwtMenu().setLocation(point.x, point.y); menu.getSwtMenu().setVisible(true); }//if// }//if// */ }//widgetSelected()// }//CoolItem// /** * CoolBar constructor. */ public CoolBar() { super(); }//CoolBar()// /** * Adds a cool item to the bar. * @param coolItem The item to be added. */ protected void addCoolItem(CoolItem coolItem) { coolItems.add(coolItem); //TODO: Is this necessary? if(isInitialized()) { //TODO: Initialize the cool item. //Will the cool item's component already be initilialized? //Would this ever occur? }//if// }//addCoolItem()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalProcessMessage(com.foundation.tcv.view.ViewMessage) */ public Object internalProcessMessage(ViewMessage viewMessage) { Object result = null; switch(viewMessage.getMessageNumber()) { case MESSAGE_INITIALIZE: { if(getSwtCoolBar() == 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.CoolBar(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// default: { result = super.internalProcessMessage(viewMessage); }//default// }//switch// return result; }//internalProcessMessage()// /** * Gets the SWT cool bar that represents this cool bar. * @return The SWT cool bar providing visualization for this cool bar. */ public org.eclipse.swt.widgets.CoolBar getSwtCoolBar() { return (org.eclipse.swt.widgets.CoolBar) getSwtControl(); }//getSwtCoolBar()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalViewInitialize() */ protected void internalViewInitialize() { //Add a listener to force the view to re-layout when the coolbar changes sizes (expands or contracts).// getSwtCoolBar().addListener(SWT.Resize, new Listener() { public void handleEvent(Event event) { getShell().layout(true, true); }//handleEvent()// }); super.internalViewInitialize(); }//internalViewInitialize()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalViewRelease() */ protected void internalViewRelease() { super.internalViewRelease(); }//internalViewRelease()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalViewInitializeAll() */ protected void internalViewInitializeAll() { super.internalViewInitializeAll(); //Initialize all the cool items.// for(int index = 0; index < coolItems.getSize(); index++) { ((CoolItem) coolItems.get(index)).internalViewInitialize(); }//for// }//internalViewInitializeAll()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalViewReleaseAll() */ protected void internalViewReleaseAll() { //Release all the cool items.// for(int index = 0; index < coolItems.getSize(); index++) { ((CoolItem) coolItems.get(index)).internalViewRelease(); }//for// super.internalViewReleaseAll(); }//internalViewReleaseAll()// /* (non-Javadoc) * @see com.foundation.tcv.swt.client.AbstractComponent#internalViewSynchronizeAll() */ protected void internalViewSynchronizeAll() { //Synchronize all the tool items.// for(int index = 0; index < coolItems.getSize(); index++) { ((CoolItem) coolItems.get(index)).internalViewSynchronize(); }//for// super.internalViewSynchronizeAll(); }//internalViewSynchronizeAll()// }//CoolBar//