/* * Copyright (c) 2006,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.view.swt; 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.view.SingleAssociationContainer; import com.foundation.view.SingleResourceAssociation; public class CoolBar extends Container { public static final int STYLE_FLAT = SWT.FLAT; /** The collection of CoolItem instances contained by the cool bar. */ private LiteList coolItems = new LiteList(10, 20); /** The collection of Menu instances associated with cool items. */ private LiteList menus = new LiteList(10, 20); /** * Encapsulates an item in the bar. */ public static class CoolItem extends AbstractComponent implements SelectionListener { public static final int STYLE_DROP_DOWN = SWT.DROP_DOWN; public static final int LINK_TARGET_IS_VISIBLE = AbstractComponent.LAST_LINK_TARGET + 1; public static final int LAST_LINK_TARGET = AbstractComponent.LAST_LINK_TARGET + 1; /** 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; /** The resource defining the visible state for the item. */ private SingleResourceAssociation isVisible = new SingleResourceAssociation(this, this, getViewContext(), SingleResourceAssociation.TYPE_BOOLEAN, false, Boolean.TRUE); /** * CoolItem constructor. * @param parent A composite control which will be the parent of the new instance (cannot be null). * @param name The unique component name. * @param style The style of control to construct. * @see #STYLE_DROP_DOWN */ public CoolItem(Container parent, String name, int style) { super(parent, style); this.style = style; ((CoolBar) parent).addCoolItem(this); }//CoolItem()// /* (non-Javadoc) * @see com.foundation.view.IAbstractComponent#getName() */ public String getName() { return "__CoolBarItem__"; }//getName()// /* (non-Javadoc) * @see com.foundation.view.swt.IInternalAbstractComponent#getShell() */ public Shell getShell() { return ((Container) getContainer()).getShell(); }//getShell()// /** * Sets the association container used to access the visibility. * @param container The visibility association metadata. */ public void setIsVisibleAssociation(SingleAssociationContainer container) { verifyThread(); this.isVisible.setAssociations(container); }//setIsVisibleAssociation()// /** * Sets whether the component is visible (by default if an attribute is bound to this value). * @param isVisible Whether the user can see the component. */ public void setIsVisible(boolean isVisible) { verifyThread(); this.isVisible.setDefaultValue(isVisible ? Boolean.TRUE : Boolean.FALSE); }//setIsVisible()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#initializeControl(int) */ protected void initializeControl(int style, Object data) { setSwtWidget(new org.eclipse.swt.widgets.CoolItem(((CoolBar) getContainer()).getSwtCoolBar(), style)); getSwtWidget().setData(this); if(customSize) { getSwtCoolItem().setSize(customWidth, customHeight); }//if// if(customPreferredSize) { getSwtCoolItem().setPreferredSize(customPreferredWidth, customPreferredHeight); }//if// if(customMinimumSize) { getSwtCoolItem().setMinimumSize(customMinimumWidth, customMinimumHeight); }//if// }//initializeControl()// /** * 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); isVisible.initialize(); getSwtCoolItem().setControl(control.getSwtControl()); super.internalViewInitialize(); }//internalViewInitialize()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewRelease() */ protected void internalViewRelease() { if(!getSwtCoolItem().isDisposed()) { getSwtCoolItem().setControl(null); getSwtCoolItem().dispose(); }//if// isVisible.release(); super.internalViewRelease(); }//internalViewRelease()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewRefresh() */ protected void internalViewRefresh() { super.internalViewRefresh(); internalViewRefreshIsVisible(); 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// }//internalViewRefresh()// /** * Refreshes whether the component is visible based on the attribute value or if null the default flag value. */ protected void internalViewRefreshIsVisible() { if(isVisible.refresh()) { controlSetIsVisible(isVisible.getValue() == null ? true : ((Boolean) isVisible.getValue()).booleanValue()); }//if// }//internalViewRefreshIsVisible()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalOnLinkInvoked(int, java.lang.Object) */ protected void internalOnLinkInvoked(int linkTarget, Object data) { switch(linkTarget) { case LINK_TARGET_IS_VISIBLE: { controlSetIsVisible(data != null && data instanceof Boolean ? ((Boolean) data).booleanValue() : false); break; }//case// default: { super.internalOnLinkInvoked(linkTarget, data); break; }//default// }//switch// }//internalOnLinkInvoked()// /* (non-Javadoc) * @see com.foundation.view.swt.Component#internalOnValueChanged(com.foundation.view.SingleResourceAssociation) */ protected void internalOnValueChanged(SingleResourceAssociation resourceAssociation, int flags) { if(resourceAssociation == isVisible) { internalViewRefreshIsVisible(); }//if// else { super.internalOnValueChanged(resourceAssociation, flags); }//else// }//internalOnValueChanged()// /** * 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, null); }//else// }//if// }//controlSetIsVisible()// /** * Sets the cool item initial size. * @param width * @param height */ public void setSize(int width, int height) { customSize = true; customWidth = width; customHeight = height; if(getSwtCoolItem() != null) { getSwtCoolItem().setSize(width, height); }//if// }//setSize()// /** * Sets the cool item minimum size. * @param width * @param height */ public void setMinimumSize(int width, int height) { customMinimumSize = true; customMinimumWidth = width; customMinimumHeight = height; if(getSwtCoolItem() != null) { getSwtCoolItem().setMinimumSize(width, height); }//if// }//setMinimumSize()// /** * Sets the cool item preferred size. * @param width * @param height */ public void setPreferredSize(int width, int height) { customPreferredSize = true; customPreferredWidth = width; customPreferredHeight = height; if(getSwtCoolItem() != null) { getSwtCoolItem().setPreferredSize(width, height); }//if// }//setPreferredSize()// /** * 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. Display a menu of items not visible on the menu bar associated with 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. * @param parent A composite control which will be the parent of the new instance (cannot be null). * @param name The unique component name. * @param style The style of control to construct. * @see #STYLE_FLAT */ public CoolBar(Container parent, String name, int style) { super(parent, name, style); }//CoolBar()// /** * Adds a cool item to the bar. * @param coolItem The item to be added. */ protected void addCoolItem(CoolItem coolItem) { coolItems.add(coolItem); 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.view.swt.AbstractComponent#initializeControl(int) */ protected void initializeControl(int style, Object data) { setSwtWidget(new org.eclipse.swt.widgets.CoolBar(((Container) getContainer()).getSwtParent(), style)); getSwtWidget().setData(this); }//initializeControl()// /** * 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.view.swt.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) { getSwtCoolBar().getParent().layout(true, true); getSwtCoolBar().getParent().redraw(); }//handleEvent()// }); super.internalViewInitialize(); }//internalViewInitialize()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewInitializeAll() */ public void internalViewInitializeAll() { super.internalViewInitializeAll(); //Initialize all the cool items.// for(int index = 0; index < coolItems.getSize(); index++) { ((CoolItem) coolItems.get(index)).internalViewInitialize(); }//for// //Initialize all the menus.// for(int index = 0; index < menus.getSize(); index++) { ((Menu) menus.get(index)).internalViewInitializeAll(); }//for// }//internalViewInitializeAll()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewReleaseAll() */ public void internalViewReleaseAll() { //Release all the cool items.// for(int index = 0; index < coolItems.getSize(); index++) { ((CoolItem) coolItems.get(index)).internalViewRelease(); }//for// //Release all the menus.// for(int index = 0; index < menus.getSize(); index++) { ((Menu) menus.get(index)).internalViewReleaseAll(); }//for// super.internalViewReleaseAll(); }//internalViewReleaseAll()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewRefreshAll() */ public void internalViewRefreshAll() { super.internalViewRefreshAll(); //Refresh all the cool items.// for(int index = 0; index < coolItems.getSize(); index++) { ((CoolItem) coolItems.get(index)).internalViewRefresh(); }//for// //Refresh all the menus.// for(int index = 0; index < menus.getSize(); index++) { ((Menu) menus.get(index)).internalViewRefreshAll(); }//for// }//internalViewRefreshAll()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewSynchronizeAll() */ public void internalViewSynchronizeAll() { //Synchronize all the tool items.// for(int index = 0; index < coolItems.getSize(); index++) { ((CoolItem) coolItems.get(index)).internalViewSynchronize(); }//for// //Synchronize all the menus.// for(int index = 0; index < menus.getSize(); index++) { ((Menu) menus.get(index)).internalViewSynchronizeAll(); }//for// super.internalViewSynchronizeAll(); }//internalViewSynchronizeAll()// /* (non-Javadoc) * @see com.foundation.view.swt.Component#setMenu(com.foundation.view.swt.Menu) */ public void setMenu(Menu menu) { verifyThread(); menus.add(menu); //A tool bar can have multiple menus associated with it. Each menu is tied to a drop down tool item. Since the tool item is not a control, the menu is linked to the bar instead.// if((isInitialized()) && (menu != null)) { menu.internalViewInitializeAll(); }//if// }//setMenu()// }//CoolBar//