Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 33172 Details for
Bug 123257
[RCP] [CoolBar] Need ability to provide a custom toolbar similar to the way we can supply a custom statusbar
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
patch for the presentations example plugin to demo other patches
ActionBarPresentation_examples_presentations_HEAD_patch.txt (text/plain), 180.78 KB, created by
Matthew Hatem
on 2006-01-17 16:26:49 EST
(
hide
)
Description:
patch for the presentations example plugin to demo other patches
Filename:
MIME Type:
Creator:
Matthew Hatem
Created:
2006-01-17 16:26:49 EST
Size:
180.78 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.ui.examples.presentation >Index: plugin.xml >=================================================================== >RCS file: /home/eclipse/org.eclipse.ui.examples.presentation/plugin.xml,v >retrieving revision 1.2 >diff -u -r1.2 plugin.xml >--- plugin.xml 25 Feb 2005 22:19:09 -0000 1.2 >+++ plugin.xml 17 Jan 2006 20:12:14 -0000 >@@ -46,9 +46,6 @@ > class="org.eclipse.ui.examples.presentation.wrappedtabs.WrappedTabsPresentationFactory" > name="Wrapped tabs presentation" > id="org.eclipse.ui.examples.presentation.wrappedtabs"/> >- </extension> >- <extension >- point="org.eclipse.ui.presentationFactories"> > <factory > class="org.eclipse.ui.internal.presentations.defaultpresentation.NativePresentationFactory" > name="Native Tabs" >@@ -61,6 +58,10 @@ > class="org.eclipse.ui.examples.presentation.sidewinder.SideWinderPresentationFactory" > id="org.eclipse.ui.examples.presentation.factory2" > name="Side Winder"/> >+ <factory >+ class="org.eclipse.ui.examples.presentation.customtoolbar.CustomToolBarPresentationFactory" >+ id="org.eclipse.ui.examples.presentation.customtoolbar" >+ name="Custom Toolbar"/> > </extension> > > <!-- =================================================================================== --> >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/HannoverGlobalCoolBarSkin.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/HannoverGlobalCoolBarSkin.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/HannoverGlobalCoolBarSkin.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/HannoverGlobalCoolBarSkin.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,200 @@ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.graphics.Color; >+import org.eclipse.swt.graphics.Font; >+import org.eclipse.swt.graphics.GC; >+import org.eclipse.swt.graphics.Image; >+import org.eclipse.swt.graphics.Point; >+import org.eclipse.swt.graphics.Rectangle; >+import org.eclipse.swt.widgets.Display; >+ >+public class HannoverGlobalCoolBarSkin implements Skin { >+ >+ // border colors >+ Color borderTopColor = new Color(null, 255, 255, 255); >+ Color borderBottomColor = new Color(null, 144, 161, 181); >+ Color borderLeftRightColor = new Color(null, 255, 255, 255); >+ Color draggerLineColor = new Color(null, 168, 191, 197); >+ Color draggerLineColor2 = new Color(null, 255, 255, 255); >+ >+ // background colors >+ Color shadowColor = new Color(null, 109, 131, 180); >+ Color backgroundTopColor = new Color(null, 247, 247, 247); >+ Color backgroundBottomColor = new Color(null, 189, 198, 216); >+ Color hoverItemBackgroundColor = new Color(null, 224, 233, 237); >+ Color pressedItemBackgroundColor = new Color(null, 231, 231, 231); >+ >+ // arrows and text colors >+ Color arrowColor = Display.getCurrent().getSystemColor(SWT.COLOR_BLACK); >+ Color textColor = new Color(null, 43,73,111); >+ Color hoverTextColor = new Color(null, 20, 38, 54); >+ >+ // gripper >+ private final static int GRIPPER_DOT_HEIGHT = 3; >+ private final static int GRIPPER_DOT_COUNT = 3; >+ >+ Color[] gripperColors = { >+ new Color(null,213,218,225),new Color(null,174,186,197),new Color(null,214,219,225), >+ new Color(null,246,246,243),new Color(null,247,248,246),new Color(null,245,246,242), >+ new Color(null,247,248,248),new Color(null,249,249,249),new Color(null,247,248,248), >+ new Color(null,208,241,222),new Color(null,173,184,197),new Color(null,208,214,222), >+ new Color(null,246,247,245),new Color(null,247,248,246),new Color(null,246,247,245), >+ new Color(null,240,241,244),new Color(null,247,248,247),new Color(null,240,242,244), >+ new Color(null,200,208,217),new Color(null,171,183,196),new Color(null,200,208,217), >+ new Color(null,244,245,244),new Color(null,247,248,246),new Color(null,244,245,244), >+ new Color(null,232,235,239),new Color(null,246,247,246),new Color(null,232,235,240) >+ }; >+ >+ public HannoverGlobalCoolBarSkin() { >+ super(); >+ } >+ >+ public Rectangle getInsets(int type, int state) { >+ return null; >+ } >+ >+ public Point getSize(int type, int state) { >+ if (type == Skin.TYPE_DECORATION_GRIPPER) { >+ return new Point(6, SWT.DEFAULT); >+ } else if (type == Skin.TYPE_ITEM_MIN_MAX_HEIGHT) { >+ return new Point(26, SWT.DEFAULT); >+ } else if (type == Skin.TYPE_ITEM_MIN_MAX_WIDTHS) { >+ return new Point(26, SWT.DEFAULT); >+ } >+ return null; >+ } >+ >+ public Font getFont(int type, int state) { >+ return null; >+ } >+ >+ public Rectangle getMargins(int type, int state) { >+ return new Rectangle(0,0,0,0); >+ } >+ >+ public Rectangle getRect(int type, int state) { >+ if (type == Skin.TYPE_ITEM_BORDER) { >+ /* >+ * x - border on left of cool item (does not include gripper decorator. >+ * y - border on top of cool item >+ * width - border on right of cool item >+ * height - border on bottom of cool item >+ */ >+ return new Rectangle(0, 0, 2, 0); >+ } else if (type == Skin.TYPE_BORDER) { >+ /* >+ * x - border on left of coolbar >+ * y - border on top of coolbar >+ * width - border on right of coolbar >+ * height - border on bottom of coolbar >+ */ >+ return new Rectangle(1, 0, 1, 0); >+ } >+ return null; >+ } >+ >+ public void drawBorder(GC gc, Rectangle rect, SWidget widget, int type, int state) { >+ if (type == Skin.TYPE_BORDER) { >+ // left/right edge >+ gc.setForeground(borderLeftRightColor); >+ gc.drawLine(rect.x, rect.y, rect.x, rect.y+rect.height-1); >+ gc.drawLine(rect.x+rect.width-1, rect.y, rect.x+rect.width-1, rect.y+rect.height-1); >+ >+ // top edge >+ gc.setForeground(borderTopColor); >+ gc.drawLine(rect.x, rect.y, rect.x + rect.width -1, rect.y); >+ // bottom edge >+ gc.setForeground(borderBottomColor); >+ gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width -1, rect.y + rect.height - 1); >+ } else if (type == Skin.TYPE_ITEM_BORDER) { >+ // top edge >+ gc.setForeground(borderTopColor); >+ gc.drawLine(rect.x, rect.y, rect.x + rect.width -1, rect.y); >+ // bottom edge >+ gc.setForeground(borderBottomColor); >+ gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width -1, rect.y + rect.height - 1); >+ >+ // Right edge. >+ gc.setForeground(draggerLineColor); >+ gc.drawLine(rect.x + rect.width -2, rect.y, rect.x + rect.width -2, rect.y + rect.height - 2); >+ gc.setForeground(draggerLineColor2); >+ gc.drawLine(rect.x + rect.width -1, rect.y, rect.x + rect.width -1, rect.y + rect.height - 2); >+ } >+ } >+ >+ public void drawBackground(GC gc, Rectangle rect, SWidget widget, int type, int state) { >+ >+ if (type == Skin.TYPE_BACKGROUND) { >+ gc.setForeground(backgroundTopColor); >+ gc.setBackground(backgroundBottomColor); >+ gc.fillGradientRectangle(rect.x, rect.y, rect.width, rect.height, true); >+ } >+ } >+ >+ public void drawDecoration(GC gc, Rectangle rect, SWidget widget, int type, int state) { >+ if (type == Skin.TYPE_DECORATION_GRIPPER) { >+ drawGripper(gc, rect, widget, type, state); >+ } >+ } >+ >+ public void drawText(GC gc, Rectangle rect, String text, int style, SWidget widget, int type, int state) { >+ } >+ >+ public void drawImage(GC gc, Rectangle rect, Image image, SWidget widget, int type, int state) { >+ } >+ >+ public void dispose() { >+ if (borderTopColor != null && borderTopColor.isDisposed() == false) >+ borderTopColor.dispose(); >+ if (draggerLineColor != null && draggerLineColor.isDisposed() == false) >+ draggerLineColor.dispose(); >+ >+ if (shadowColor != null && shadowColor.isDisposed() == false) >+ shadowColor.dispose(); >+ if (backgroundTopColor != null && backgroundTopColor.isDisposed() == false) >+ backgroundTopColor.dispose(); >+ if (backgroundBottomColor != null && backgroundBottomColor.isDisposed() == false) >+ backgroundBottomColor.dispose(); >+ if (hoverItemBackgroundColor != null && hoverItemBackgroundColor.isDisposed() == false) >+ hoverItemBackgroundColor.dispose(); >+ if (pressedItemBackgroundColor != null && pressedItemBackgroundColor.isDisposed() == false) >+ pressedItemBackgroundColor.dispose(); >+ >+ if (arrowColor != null && arrowColor.isDisposed() == false) >+ arrowColor.dispose(); >+ if (textColor != null && textColor.isDisposed() == false) >+ textColor.dispose(); >+ if (hoverTextColor != null && hoverTextColor.isDisposed() == false) >+ hoverTextColor.dispose(); >+ >+ for (int i = 0; i < gripperColors.length; i++) { >+ if (gripperColors[i] != null && gripperColors[i].isDisposed() == false) >+ gripperColors[i].dispose(); >+ } >+ } >+ >+ private void drawGripper(GC gc, Rectangle rect, SWidget widget, int type, int state) { >+ >+ int dotGap = GRIPPER_DOT_HEIGHT; >+ int gripperImageHeight = GRIPPER_DOT_HEIGHT * GRIPPER_DOT_COUNT + dotGap * (GRIPPER_DOT_COUNT-1); >+ int left = rect.x + rect.width - 3; >+ int top = rect.y + (rect.height - gripperImageHeight) / 2; >+ >+ int x = left; >+ int y = top; >+ for (int i = 0; i < gripperColors.length; i++) { >+ if (i != 0 && i % 9 == 0) { >+ x = left; >+ y += dotGap+1; >+ } else if (i != 0 && i % 3 == 0) { >+ x = left; >+ y++; >+ } >+ gc.setForeground(gripperColors[i]); >+ gc.drawPoint(x,y); >+ x++; >+ } >+ } >+ >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SWidget.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SWidget.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SWidget.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SWidget.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,31 @@ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import org.eclipse.swt.widgets.Canvas; >+import org.eclipse.swt.widgets.Composite; >+ >+public abstract class SWidget extends Canvas { >+ >+ public static final int HOVER = 1 << 1; >+ public static final int SELECTED = 1 << 2; >+ public static final int PRESSED = 1 << 3; >+ >+ private Skin skin; >+ >+ public SWidget(Composite parent, int style) { >+ super(parent, style); >+ } >+ >+ public void setSkin(Skin skin) { >+ if (this.skin != null) { >+ this.skin.dispose(); >+ this.skin = null; >+ } >+ >+ this.skin = skin; >+ } >+ >+ public Skin getSkin() { >+ return skin; >+ } >+ >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SCoolBar.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SCoolBar.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SCoolBar.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SCoolBar.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,411 @@ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import java.util.ArrayList; >+ >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.graphics.GC; >+import org.eclipse.swt.graphics.Point; >+import org.eclipse.swt.graphics.Rectangle; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Event; >+import org.eclipse.swt.widgets.Listener; >+ >+ >+public class SCoolBar extends SWidget { >+ >+ private ArrayList items = new ArrayList(); >+ >+ static final int DEFAULT_WIDTH = 64; >+ static final int DEFAULT_HEIGHT = 64; >+ static final int ITEM_INSET_TOP = 0; >+ static final int ITEM_INSET_BOTTOM = 0; >+ static final int LEFT_MARGIN = 6; >+ static final int SHADOW_SIZE = 4; >+ >+ public SCoolBar(Composite parent, int style) { >+ super(parent, checkStyle(style)); >+ >+ Listener listener = new Listener() { >+ public void handleEvent(Event event) { >+ switch (event.type) { >+ case SWT.Dispose: >+ onDispose(event); >+ break; >+ case SWT.MouseDown: >+ onMouseDown(event); >+ break; >+ case SWT.MouseExit: >+ onMouseExit(event); >+ break; >+ case SWT.MouseMove: >+ onMouseMove(event); >+ break; >+ case SWT.MouseUp: >+ onMouseUp(event); >+ break; >+ case SWT.MouseDoubleClick: >+ onMouseDoubleClick(event); >+ break; >+ case SWT.Paint: >+ onPaint(event); >+ break; >+ case SWT.Resize: >+ onResize(event); >+ break; >+ } >+ } >+ }; >+ int[] events = new int[] { >+ SWT.Dispose, >+ SWT.MouseDown, >+ SWT.MouseExit, >+ SWT.MouseMove, >+ SWT.MouseUp, >+ SWT.MouseDoubleClick, >+ SWT.Paint, >+ SWT.Resize >+ }; >+ for (int i = 0; i < events.length; i++) { >+ addListener(events[i], listener); >+ } >+ } >+ >+ private static int checkStyle(int style) { >+ return style; >+ } >+ >+ public Point computeSize(int wHint, int hHint) { >+ return computeSize(wHint, hHint, true); >+ } >+ >+ public Point computeSize(int wHint, int hHint, boolean changed) { >+ checkWidget(); >+ int width = 0; >+ int height = 0; >+ int rowWidth = 0; >+ int rowHeight = 0; >+ for (int i = 0; i < items.size(); i++) { >+ SCoolItem item = (SCoolItem)items.get(i); >+ Point pt = item.getPreferredSize(); >+ if (i != 0 && item.wrap == true) { >+ width = Math.max(rowWidth, width); >+ height += rowHeight; >+ rowWidth = 0; >+ rowHeight = 0; >+ } >+ rowWidth += pt.x; >+ rowHeight = Math.max(pt.y, rowHeight); >+ } >+ width = Math.max(rowWidth, width); >+ height += rowHeight; >+ if (wHint == 0) { >+ width = DEFAULT_WIDTH; >+ } >+ if (hHint == 0) { >+ height = DEFAULT_HEIGHT; >+ } >+ >+ Skin skin = getSkin(); >+ Rectangle borders = null; >+ >+ if (skin != null) { >+ borders = skin.getRect(Skin.TYPE_BORDER, Skin.NORMAL); >+ } >+ if (borders == null) { >+ borders = new Rectangle(0, 0, 0, 0); >+ } >+ >+ width += borders.x + borders.width; >+ height += borders.y + borders.height; >+ >+ if (wHint != SWT.DEFAULT) { >+ width = wHint; >+ } >+ if (hHint != SWT.DEFAULT) { >+ height = hHint; >+ } >+ Rectangle trim = computeTrim(0, 0, width, height); >+ Point pt = new Point(trim.width, trim.height);; >+ >+ return pt; >+ } >+ >+ void createItem (SCoolItem item, int index) { >+ int itemCount = getItemCount(); >+ if (!(0 <= index && index <= itemCount)) >+ SWT.error (SWT.ERROR_INVALID_RANGE); >+ items.add(index, item); >+ layout(); >+ } >+ >+ /** >+ * @return >+ */ >+ public int[] getAlignmentIndices () { >+ checkWidget (); >+ int itemCount = items.size(); >+ int[] indices = new int[itemCount]; >+ int count = 0; >+ for (int i = 0; i < itemCount; i++) { >+ SCoolItem item = (SCoolItem)items.get(i); >+ if (item.alignment) { >+ indices[count++] = i; >+ } >+ } >+ int[] result = new int[count]; >+ System.arraycopy (indices, 0, result, 0, count); >+ return result; >+ } >+ >+ public SCoolItem getItem(int index) { >+ checkWidget(); >+ SCoolItem item = (SCoolItem)items.get(index); >+ return item; >+ } >+ >+ public int getItemCount() { >+ checkWidget(); >+ return items.size(); >+ } >+ >+ public SCoolItem[] getItems() { >+ checkWidget(); >+ if (items.size() == 0) >+ return new SCoolItem[0]; >+ return (SCoolItem[])items.toArray(new SCoolItem[1]); >+ } >+ >+ public boolean getLocked() { >+ return false; >+ } >+ >+ /** >+ * >+ * @return >+ */ >+ public int[] getWrapIndices () { >+ checkWidget (); >+ int itemCount = items.size(); >+ int[] indices = new int[itemCount]; >+ int count = 0; >+ for (int i = 0; i < itemCount; i++) { >+ SCoolItem item = (SCoolItem)items.get(i); >+ if (item.wrap) { >+ indices[count++] = i; >+ } >+ } >+ int[] result = new int[count]; >+ System.arraycopy (indices, 0, result, 0, count); >+ return result; >+ } >+ >+ public int indexOf(SCoolItem item) { >+ checkWidget(); >+ if (item == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); >+ if (item.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT); >+ return items.indexOf(item); >+ } >+ >+ protected void onDispose(Event event) { >+ // TODO Auto-generated method stub >+ >+ } >+ >+ protected void onMouseDown(Event event) { >+ // TODO Auto-generated method stub >+ >+ } >+ >+ protected void onMouseExit(Event event) { >+ // TODO Auto-generated method stub >+ >+ } >+ >+ protected void onMouseMove(Event event) { >+ // TODO Auto-generated method stub >+ >+ } >+ >+ protected void onMouseUp(Event event) { >+ // TODO Auto-generated method stub >+ >+ } >+ >+ protected void onMouseDoubleClick(Event event) { >+ // TODO Auto-generated method stub >+ >+ } >+ >+ protected void onPaint(Event event) { >+ Skin skin = getSkin(); >+ GC gc = event.gc; >+ >+ if (skin == null) >+ return; >+ >+ Rectangle rect = getClientArea(); >+ if (rect.width == 0 || rect.height == 0) >+ return; >+ >+ int state = Skin.NORMAL; >+ >+ // paint background >+ skin.drawBackground(gc, rect, this, Skin.TYPE_BACKGROUND, state); >+ >+ // paint border >+ skin.drawBorder(gc, rect, this, Skin.TYPE_BORDER, state); >+ >+ // paint each item >+ for (int i = 0; i < items.size(); i++) { >+ SCoolItem item = getItem(i); >+ if (item != null) { >+ item.paint(gc, state); >+ } >+ } >+ } >+ >+ protected void onResize(Event event) { >+ layout(); >+ } >+ >+ /** >+ * Sets the indices of all item(s) in the receiver >+ * that will begin right alignment on each row. >+ * For example, if indice contains 2, >+ * that mean all the items from item2 to the end row are right aligned. >+ * >+ * @param indices >+ */ >+ public void setAlignmentIndices(int[] indices) { >+ checkWidget(); >+ if (indices == null) >+ indices = new int[0]; >+ int count = items.size(); >+// for (int i = 0; i< indices.length; i++) { >+// if (indices[i] < 0 || indices[i] >= count) { >+// SWT.error (SWT.ERROR_INVALID_ARGUMENT); >+// } >+// } >+ for (int i = 0; i < items.size(); i++) { >+ ((SCoolItem)items.get(i)).alignment = false; >+ } >+ for (int i = 0; i < indices.length; i++) { >+ int index = indices[i]; >+ if (index < items.size()) { >+ ((SCoolItem)items.get(index)).alignment = true; >+ } >+ } >+ layout(); >+ } >+ >+ public void setBounds(int x, int y, int width, int height) { >+ super.setBounds(x, y, width, height); >+ } >+ >+ public void setBounds(Rectangle rect) { >+ super.setBounds(rect); >+ } >+ >+ public void setSize(int width, int height) { >+ super.setSize(width, height); >+ } >+ >+ >+ public void setLocked(boolean locked) { >+ // TODO Auto-generated method stub >+ } >+ >+ public void setWrapIndices(int[] wrapIndices) { >+ // TODO Auto-generated method stub >+ } >+ >+ public void layout(boolean changed) { >+ int x = 1; >+ int y = 0; >+ int rowHeight = 0; >+ int maxHeightInRow = 0; >+ int maxWidth; >+ int i; >+ Skin skin = getSkin(); >+ Rectangle borders = null; >+ >+ if (skin != null) { >+ borders = skin.getRect(Skin.TYPE_BORDER, Skin.NORMAL); >+ } >+ if (borders == null) { >+ borders = new Rectangle(0, 0, 0, 0); >+ } >+ >+ maxWidth = getClientArea().width; >+ if (maxWidth == 0) >+ return; >+ >+ x = borders.x; >+ >+ for (i = 0; i < items.size(); i++) { >+ SCoolItem item = (SCoolItem)items.get(i); >+ Point itemSize = item.computeSize(SWT.DEFAULT, SWT.DEFAULT); >+ maxHeightInRow = Math.max(itemSize.y, maxHeightInRow); >+ } >+ >+ for (i = 0; i < items.size(); i++) { >+ >+ // TODO: multiple row support >+ >+ // calc max height in row >+// if (i == 0 || ((SCoolItem)items.get(i)).wrap) { >+// rowHeight = maxHeightInRow; >+// maxHeightInRow = 0; >+// for (int j = i; j < items.size(); j++) { >+// SCoolItem item = (SCoolItem)items.get(j); >+// if (j != 0 && item.wrap) >+// break; >+// Point itemSize = item.computeSize(SWT.DEFAULT, SWT.DEFAULT); >+// maxHeightInRow = Math.max(itemSize.y, maxHeightInRow); >+// } >+// } >+ >+ // layout item >+ SCoolItem item = (SCoolItem)items.get(i); >+ Point itemSize = item.computeSize(SWT.DEFAULT, SWT.DEFAULT); >+ Rectangle oldBounds = item.getBounds(); >+ if (i != 0 && item.wrap) { >+ x = borders.x; >+ y += rowHeight; >+ } else if (item.alignment) { >+ // right align from this item till the last item in the row >+ >+ int rightAlignWidth = 0; >+ for (int j = i; j < items.size(); j++) { >+ SCoolItem rightItem = (SCoolItem)items.get(j); >+ int itemWidth = rightItem.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; >+ rightAlignWidth += itemWidth; >+ if (rightItem.wrap == true) >+ break; >+ } >+ int prevx = x; >+ x = maxWidth - rightAlignWidth; // new x position after right alingn >+ if (i > 0 && prevx != x) { >+ // change the previous item width wider >+ SCoolItem prevItem = (SCoolItem)items.get(i-1); >+ Point pt = prevItem.computeSize(SWT.DEFAULT, SWT.DEFAULT); >+ Rectangle r = prevItem.getBounds(); >+ prevItem.setBounds(r.x, r.y, pt.x + x - prevx, pt.y); >+ //prevItem.setBounds(r.x, r.y, r.width + x - prevx, r.height); >+ } >+ } >+ if (x + itemSize.x > maxWidth) { >+ itemSize.x -= (x + itemSize.x - maxWidth); >+ } >+ Rectangle newBounds = new Rectangle(x, y, itemSize.x, maxHeightInRow); >+ if (!oldBounds.equals(newBounds)) { >+ item.setBounds(newBounds.x, newBounds.y, newBounds.width, newBounds.height); >+ } >+ x += newBounds.width; >+ } >+ y += maxHeightInRow; >+ } >+ >+ >+ >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SCoolBarManager.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SCoolBarManager.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SCoolBarManager.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SCoolBarManager.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,1037 @@ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import java.util.ArrayList; >+import java.util.HashMap; >+import java.util.Iterator; >+import java.util.List; >+import java.util.ListIterator; >+ >+import org.eclipse.jface.action.ContributionManager; >+import org.eclipse.jface.action.IContributionItem; >+import org.eclipse.jface.action.ICoolBarManager; >+import org.eclipse.jface.action.IMenuManager; >+import org.eclipse.jface.action.IToolBarManager; >+import org.eclipse.jface.action.MenuManager; >+import org.eclipse.jface.action.Separator; >+import org.eclipse.jface.action.ToolBarContributionItem; >+import org.eclipse.jface.util.Assert; >+import org.eclipse.jface.util.Policy; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Control; >+import org.eclipse.swt.widgets.CoolBar; >+import org.eclipse.swt.widgets.Menu; >+ >+public class SCoolBarManager extends ContributionManager implements ICoolBarManager { >+ >+ /** >+ * A separator created by the end user. >+ */ >+ public final static String USER_SEPARATOR = "UserSeparator"; //$NON-NLS-1$ >+ >+ /** >+ * The original creation order of the contribution items. >+ */ >+ private ArrayList cbItemsCreationOrder = new ArrayList(); >+ >+ /** >+ * MenuManager for cool bar pop-up menu, or null if none. >+ */ >+ private MenuManager contextMenuManager = null; >+ >+ /** >+ * The cool bar control; <code>null</code> before creation and after >+ * disposal. >+ */ >+ private SCoolBar coolBar = null; >+ >+ /** >+ * The cool bar items style; <code>SWT.NONE</code> by default. >+ */ >+ private int itemStyle = SWT.NONE; >+ >+ /** >+ * Creates a new cool bar manager with the default style. Equivalent to >+ * <code>CoolBarManager(SWT.NONE)</code>. >+ */ >+ public SCoolBarManager() { >+ // do nothing >+ } >+ >+ /** >+ * Creates a cool bar manager for an existing cool bar control. This >+ * manager becomes responsible for the control, and will dispose of it when >+ * the manager is disposed. >+ * >+ * @param coolBar >+ * the cool bar control >+ */ >+ public SCoolBarManager(SCoolBar coolBar) { >+ this(); >+ Assert.isNotNull(coolBar); >+ this.coolBar = coolBar; >+ itemStyle = coolBar.getStyle(); >+ } >+ >+ /** >+ * Creates a cool bar manager with the given SWT style. Calling <code>createControl</code> >+ * will create the cool bar control. >+ * >+ * @param style >+ * the cool bar item style; see >+ * {@link org.eclipse.swt.widgets.CoolBar CoolBar}for for valid >+ * style bits >+ */ >+ public SCoolBarManager(int style) { >+ itemStyle = style; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.action.ICoolBarManager#add(org.eclipse.jface.action.IToolBarManager) >+ */ >+ public void add(IToolBarManager toolBarManager) { >+ Assert.isNotNull(toolBarManager); >+ SToolBarContributionItem2 toolBarContributionItem = null; >+ if (toolBarContributionItem == null) >+ toolBarContributionItem = new SToolBarContributionItem2(toolBarManager); >+ super.add(toolBarContributionItem); >+ } >+ >+ public void add(IContributionItem item) { >+ super.add(item); >+ } >+ >+ /** >+ * Collapses consecutive separators and removes a separator from the >+ * beginning and end of the list. >+ * >+ * @param contributionList >+ * the list of contributions; must not be <code>null</code>. >+ * @return The contribution list provided with extraneous separators >+ * removed; this value is never <code>null</code>, but may be >+ * empty. >+ */ >+ private ArrayList adjustContributionList(ArrayList contributionList) { >+ IContributionItem item; >+ // Fist remove a separator if it is the first element of the list >+ if (contributionList.size() != 0) { >+ item = (IContributionItem) contributionList.get(0); >+ if (item.isSeparator()) { >+ contributionList.remove(0); >+ } >+ >+ ListIterator iterator = contributionList.listIterator(); >+ // collapse consecutive separators >+ while (iterator.hasNext()) { >+ item = (IContributionItem) iterator.next(); >+ if (item.isSeparator()) { >+ while (iterator.hasNext()) { >+ item = (IContributionItem) iterator.next(); >+ if (item.isSeparator()) { >+ iterator.remove(); >+ } else { >+ break; >+ } >+ } >+ >+ } >+ } >+ // Now check last element to see if there is a separator >+ item = (IContributionItem) contributionList.get(contributionList >+ .size() - 1); >+ if (item.isSeparator()) { >+ contributionList.remove(contributionList.size() - 1); >+ } >+ } >+ return contributionList; >+ >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ContributionManager#checkDuplication(org.eclipse.jface.action.IContributionItem) >+ */ >+ protected boolean allowItem(IContributionItem itemToAdd) { >+ /* We will allow as many null entries as they like, though there should >+ * be none. >+ */ >+ if (itemToAdd == null) { >+ return true; >+ } >+ >+ /* Null identifiers can be expected in generic contribution items. >+ */ >+ String firstId = itemToAdd.getId(); >+ if (firstId == null) { >+ return true; >+ } >+ >+ // Cycle through the current list looking for duplicates. >+ IContributionItem[] currentItems = getItems(); >+ for (int i = 0; i < currentItems.length; i++) { >+ IContributionItem currentItem = currentItems[i]; >+ >+ // We ignore null entries. >+ if (currentItem == null) { >+ continue; >+ } >+ >+ String secondId = currentItem.getId(); >+ if (firstId.equals(secondId)) { >+ if (Policy.TRACE_TOOLBAR) { //$NON-NLS-1$ >+ System.out.println("Trying to add a duplicate item."); //$NON-NLS-1$ >+ new Exception().printStackTrace(System.out); >+ System.out.println("DONE --------------------------"); //$NON-NLS-1$ >+ } >+ return false; >+ } >+ } >+ >+ return true; >+ } >+ >+ /** >+ * Positions the list iterator to the end of all the separators. Calling >+ * <code>next()</code> the iterator should return the immediate object >+ * following the last separator. >+ * >+ * @param iterator >+ * the list iterator. >+ */ >+ private void collapseSeparators(ListIterator iterator) { >+ >+ while (iterator.hasNext()) { >+ IContributionItem item = (IContributionItem) iterator.next(); >+ if (!item.isSeparator()) { >+ iterator.previous(); >+ return; >+ } >+ } >+ } >+ >+ /** >+ * Returns whether the cool bar control has been created and not yet >+ * disposed. >+ * >+ * @return <code>true</code> if the control has been created and not yet >+ * disposed, <code>false</code> otherwise >+ */ >+ private boolean coolBarExist() { >+ return coolBar != null && !coolBar.isDisposed(); >+ } >+ >+ /** >+ * Creates and returns this manager's cool bar control. Does not create a >+ * new control if one already exists. >+ * >+ * @param parent >+ * the parent control >+ * @return the cool bar control >+ */ >+ public Control createControl2(Composite parent) { >+ Assert.isNotNull(parent); >+ if (!coolBarExist()) { >+ coolBar = new SCoolBar(parent, itemStyle); >+ ((SCoolBar)coolBar).setSkin(new HannoverGlobalCoolBarSkin()); >+ coolBar.setMenu(getContextMenuControl()); >+ coolBar.setLocked(false); >+ coolBar.setSkin(new HannoverGlobalCoolBarSkin()); >+ update(false); >+ } >+ return coolBar; >+ } >+ >+ /** >+ * Disposes of this cool bar manager and frees all allocated SWT resources. >+ * Notifies all contribution items of the dispose. Note that this method >+ * does not clean up references between this cool bar manager and its >+ * associated contribution items. Use <code>removeAll</code> for that >+ * purpose. >+ */ >+ public void dispose() { >+ if (coolBarExist()) { >+ IContributionItem[] items = getItems(); >+ for (int i = 0; i < items.length; i++) { >+ // Disposes of the contribution item. >+ // If Contribution Item is a toolbar then it will dispose of >+ // all the nested >+ // contribution items. >+ items[i].dispose(); >+ } >+ coolBar.dispose(); >+ coolBar = null; >+ } >+ // If a context menu existed then dispose of it. >+ if (contextMenuManager != null) { >+ contextMenuManager.dispose(); >+ contextMenuManager = null; >+ } >+ >+ } >+ >+ /** >+ * Disposes the given cool item. >+ * >+ * @param item >+ * the cool item to dispose >+ */ >+ private void dispose(SCoolItem item) { >+ if ((item != null) && !item.isDisposed()) { >+ >+ item.setData(null); >+ Control control = item.getControl(); >+ // if the control is already disposed, setting the coolitem >+ // control to null will cause an SWT exception, workaround >+ // for 19630 >+ if ((control != null) && !control.isDisposed()) { >+ item.setControl(null); >+ } >+ item.dispose(); >+ } >+ } >+ >+ /** >+ * Finds the cool item associated with the given contribution item. >+ * >+ * @param item >+ * the contribution item >+ * @return the associated cool item, or <code>null</code> if not found >+ */ >+ private SCoolItem findCoolItem(IContributionItem item) { >+ if (coolBar == null) >+ return null; >+ SCoolItem[] items = coolBar.getItems(); >+ for (int i = 0; i < items.length; i++) { >+ SCoolItem coolItem = items[i]; >+ IContributionItem data = (IContributionItem) coolItem.getData(); >+ if (data != null && data.equals(item)) >+ return coolItem; >+ } >+ return null; >+ } >+ >+ /** >+ * Return a consistent set of wrap indices. The return value will always >+ * include at least one entry and the first entry will always be zero. >+ * CoolBar.getWrapIndices() is inconsistent in whether or not it returns an >+ * index for the first row. >+ * >+ * @param wraps >+ * the wrap indicies from the cool bar widget >+ * @return the adjusted wrap indicies. >+ */ >+ private int[] getAdjustedWrapIndices(int[] wraps) { >+ int[] adjustedWrapIndices; >+ if (wraps.length == 0) { >+ adjustedWrapIndices = new int[] { 0 }; >+ } else { >+ if (wraps[0] != 0) { >+ adjustedWrapIndices = new int[wraps.length + 1]; >+ adjustedWrapIndices[0] = 0; >+ for (int i = 0; i < wraps.length; i++) { >+ adjustedWrapIndices[i + 1] = wraps[i]; >+ } >+ } else { >+ adjustedWrapIndices = wraps; >+ } >+ } >+ return adjustedWrapIndices; >+ } >+ >+ /** >+ * Returns the control of the Menu Manager. If the menu manager does not >+ * have a control then one is created. >+ * >+ * @return menu control associated with manager, or null if none >+ */ >+ private Menu getContextMenuControl() { >+ if ((contextMenuManager != null) && (coolBar != null)) { >+ Menu menuWidget = contextMenuManager.getMenu(); >+ if ((menuWidget == null) || (menuWidget.isDisposed())) { >+ menuWidget = contextMenuManager.createContextMenu(coolBar); >+ } >+ return menuWidget; >+ } >+ return null; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.action.ICoolBarManager#isLayoutLocked() >+ */ >+ public IMenuManager getContextMenuManager() { >+ return contextMenuManager; >+ } >+ >+ /** >+ * Returns the cool bar control for this manager. >+ * >+ * @return the cool bar control, or <code>null</code> if none >+ */ >+ public CoolBar getControl() { >+ return null; >+ } >+ >+ /** >+ * Returns an array list of all the contribution items in the manager. >+ * >+ * @return an array list of contribution items. >+ */ >+ private ArrayList getItemList() { >+ IContributionItem[] cbItems = getItems(); >+ ArrayList list = new ArrayList(cbItems.length); >+ for (int i = 0; i < cbItems.length; i++) { >+ list.add(cbItems[i]); >+ } >+ return list; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.action.ICoolBarManager#isLayoutLocked() >+ */ >+ public boolean getLockLayout() { >+ if (!coolBarExist()) { >+ return false; >+ } >+ return coolBar.getLocked(); >+ } >+ >+ /** >+ * Returns the number of rows that should be displayed visually. >+ * >+ * @param items >+ * the array of contributin items >+ * @return the number of rows >+ */ >+ private int getNumRows(IContributionItem[] items) { >+ int numRows = 1; >+ boolean separatorFound = false; >+ for (int i = 0; i < items.length; i++) { >+ if (items[i].isSeparator()) { >+ separatorFound = true; >+ } >+ if ((separatorFound) && (items[i].isVisible()) >+ && (!items[i].isGroupMarker()) && (!items[i].isSeparator())) { >+ numRows++; >+ separatorFound = false; >+ } >+ } >+ return numRows; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.action.ICoolBarManager#getStyle() >+ */ >+ public int getStyle() { >+ return itemStyle; >+ } >+ >+ /** >+ * Subclasses may extend this <code>ContributionManager</code> method, >+ * but must call <code>super.itemAdded</code>. >+ * >+ * @see org.eclipse.jface.action.ContributionManager#itemAdded(org.eclipse.jface.action.IContributionItem) >+ */ >+ protected void itemAdded(IContributionItem item) { >+ Assert.isNotNull(item); >+ super.itemAdded(item); >+ int insertedAt = indexOf(item); >+ boolean replaced = false; >+ final int size = cbItemsCreationOrder.size(); >+ for (int i = 0; i < size; i++) { >+ IContributionItem created = (IContributionItem) cbItemsCreationOrder >+ .get(i); >+ if (created.getId() != null && created.getId().equals(item.getId())) { >+ cbItemsCreationOrder.set(i, item); >+ replaced = true; >+ break; >+ } >+ } >+ >+ if (!replaced) { >+ cbItemsCreationOrder.add(Math.min(Math.max(insertedAt, 0), >+ cbItemsCreationOrder.size()), item); >+ } >+ } >+ >+ /** >+ * Subclasses may extend this <code>ContributionManager</code> method, >+ * but must call <code>super.itemRemoved</code>. >+ * >+ * @see org.eclipse.jface.action.ContributionManager#itemRemoved(org.eclipse.jface.action.IContributionItem) >+ */ >+ protected void itemRemoved(IContributionItem item) { >+ Assert.isNotNull(item); >+ super.itemRemoved(item); >+ SCoolItem coolItem = findCoolItem(item); >+ if (coolItem != null) { >+ coolItem.setData(null); >+ } >+ } >+ >+ /** >+ * Positions the list iterator to the starting of the next row. By calling >+ * next on the returned iterator, it will return the first element of the >+ * next row. >+ * >+ * @param iterator >+ * the list iterator of contribution items >+ * @param ignoreCurrentItem >+ * Whether the current item in the iterator should be considered >+ * (as well as subsequent items). >+ */ >+ private void nextRow(ListIterator iterator, boolean ignoreCurrentItem) { >+ >+ IContributionItem currentElement = null; >+ if (!ignoreCurrentItem && iterator.hasPrevious()) { >+ currentElement = (IContributionItem) iterator.previous(); >+ iterator.next(); >+ } >+ >+ if ((currentElement != null) && (currentElement.isSeparator())) { >+ collapseSeparators(iterator); >+ return; >+ } else { >+ //Find next separator >+ while (iterator.hasNext()) { >+ IContributionItem item = (IContributionItem) iterator.next(); >+ if (item.isSeparator()) { >+ // we we find a separator, collapse any consecutive >+ // separators >+ // and return >+ collapseSeparators(iterator); >+ return; >+ } >+ } >+ } >+ } >+ >+ /* >+ * Used for debuging. Prints all the items in the internal structures. >+ */ >+ // private void printContributions(ArrayList contributionList) { >+ // int index = 0; >+ // System.out.println("----------------------------------\n"); //$NON-NLS-1$ >+ // for (Iterator i = contributionList.iterator(); i.hasNext(); index++) { >+ // IContributionItem item = (IContributionItem) i.next(); >+ // if (item.isSeparator()) { >+ // System.out.println("Separator"); //$NON-NLS-1$ >+ // } else { >+ // System.out.println(index + ". Item id: " + item.getId() //$NON-NLS-1$ >+ // + " - is Visible: " //$NON-NLS-1$ >+ // + item.isVisible()); >+ // } >+ // } >+ // } >+ /** >+ * Synchronizes the visual order of the cool items in the control with this >+ * manager's internal data structures. This method should be called before >+ * requesting the order of the contribution items to ensure that the order >+ * is accurate. >+ * <p> >+ * Note that <code>update()</code> and <code>refresh()</code> are >+ * converses: <code>update()</code> changes the visual order to match the >+ * internal structures, and <code>refresh</code> changes the internal >+ * structures to match the visual order. >+ * </p> >+ */ >+ public void refresh() { >+ if (!coolBarExist()) { >+ return; >+ } >+ >+ // Retreives the list of contribution items as an array list >+ ArrayList contributionList = getItemList(); >+ >+ // Check the size of the list >+ if (contributionList.size() == 0) >+ return; >+ >+ // The list of all the cool items in their visual order >+ SCoolItem[] coolItems = coolBar.getItems(); >+ // The wrap indicies of the coolbar >+ int[] wrapIndicies = getAdjustedWrapIndices(coolBar.getWrapIndices()); >+ >+ int row = 0; >+ int coolItemIndex = 0; >+ >+ // Traverse through all cool items in the coolbar add them to a new >+ // data structure >+ // in the correct order >+ ArrayList displayedItems = new ArrayList(coolBar.getItemCount()); >+ for (int i = 0; i < coolItems.length; i++) { >+ SCoolItem coolItem = coolItems[i]; >+ if (coolItem.getData() instanceof IContributionItem) { >+ IContributionItem cbItem = (IContributionItem) coolItem >+ .getData(); >+ displayedItems.add(Math.min(i, displayedItems.size()), cbItem); >+ } >+ } >+ >+ // Add separators to the displayed Items data structure >+ int offset = 0; >+ for (int i = 1; i < wrapIndicies.length; i++) { >+ int insertAt = wrapIndicies[i] + offset; >+ displayedItems.add(insertAt, new Separator(USER_SEPARATOR)); >+ offset++; >+ } >+ >+ // Determine which rows are invisible >+ ArrayList existingVisibleRows = new ArrayList(4); >+ ListIterator rowIterator = contributionList.listIterator(); >+ collapseSeparators(rowIterator); >+ int numRow = 0; >+ while (rowIterator.hasNext()) { >+ // Scan row >+ while (rowIterator.hasNext()) { >+ IContributionItem cbItem = (IContributionItem) rowIterator >+ .next(); >+ if (displayedItems.contains(cbItem)) { >+ existingVisibleRows.add(new Integer(numRow)); >+ break; >+ } >+ if (cbItem.isSeparator()) { >+ break; >+ } >+ } >+ nextRow(rowIterator, false); >+ numRow++; >+ } >+ >+ Iterator existingRows = existingVisibleRows.iterator(); >+ // Adjust row number to the first visible >+ if (existingRows.hasNext()) { >+ row = ((Integer) existingRows.next()).intValue(); >+ } >+ >+ HashMap itemLocation = new HashMap(); >+ for (ListIterator locationIterator = displayedItems.listIterator(); locationIterator >+ .hasNext();) { >+ IContributionItem item = (IContributionItem) locationIterator >+ .next(); >+ if (item.isSeparator()) { >+ if (existingRows.hasNext()) { >+ Integer value = (Integer) existingRows.next(); >+ row = value.intValue(); >+ } else { >+ row++; >+ } >+ } else { >+ itemLocation.put(item, new Integer(row)); >+ } >+ >+ } >+ >+ // Insert the contribution items in their correct location >+ for (ListIterator iterator = displayedItems.listIterator(); iterator >+ .hasNext();) { >+ IContributionItem cbItem = (IContributionItem) iterator.next(); >+ if (cbItem.isSeparator()) { >+ coolItemIndex = 0; >+ } else { >+ relocate(cbItem, coolItemIndex, contributionList, itemLocation); >+ cbItem.saveWidgetState(); >+ coolItemIndex++; >+ } >+ } >+ >+ if (contributionList.size() != 0) { >+ contributionList = adjustContributionList(contributionList); >+ IContributionItem[] array = new IContributionItem[contributionList >+ .size() - 1]; >+ array = (IContributionItem[]) contributionList.toArray(array); >+ internalSetItems(array); >+ } >+ >+ } >+ >+ /** >+ * Relocates the given contribution item to the specified index. >+ * >+ * @param cbItem >+ * the conribution item to relocate >+ * @param index >+ * the index to locate this item >+ * @param contributionList >+ * the current list of conrtributions >+ * @param itemLocation >+ */ >+ private void relocate(IContributionItem cbItem, int index, >+ ArrayList contributionList, HashMap itemLocation) { >+ >+ if (!(itemLocation.get(cbItem) instanceof Integer)) >+ return; >+ int targetRow = ((Integer) itemLocation.get(cbItem)).intValue(); >+ >+ int cbInternalIndex = contributionList.indexOf(cbItem); >+ >+ // by default add to end of list >+ int insertAt = contributionList.size(); >+ // Find the row to place this item in. >+ ListIterator iterator = contributionList.listIterator(); >+ // bypass any separators at the begining >+ collapseSeparators(iterator); >+ int currentRow = -1; >+ while (iterator.hasNext()) { >+ >+ currentRow++; >+ if (currentRow == targetRow) { >+ // We found the row to insert the item >+ int virtualIndex = 0; >+ insertAt = iterator.nextIndex(); >+ // first check the position of the current element (item) >+ // then get the next element >+ while (iterator.hasNext()) { >+ IContributionItem item = (IContributionItem) iterator >+ .next(); >+ Integer itemRow = (Integer) itemLocation.get(item); >+ if (item.isSeparator()) >+ break; >+ // if the item has an associate widget >+ if ((itemRow != null) && (itemRow.intValue() == targetRow)) { >+ // if the next element is the index we are looking for >+ // then break >+ if (virtualIndex >= index) >+ break; >+ virtualIndex++; >+ >+ } >+ insertAt++; >+ } >+ // If we don't need to move it then we return >+ if (cbInternalIndex == insertAt) >+ return; >+ break; >+ } >+ nextRow(iterator, true); >+ } >+ contributionList.remove(cbItem); >+ >+ // Adjust insertAt index >+ if (cbInternalIndex < insertAt) { >+ insertAt--; >+ } >+ >+ // if we didn't find the row then add a new row >+ if (currentRow != targetRow) { >+ contributionList.add(new Separator(USER_SEPARATOR)); >+ insertAt = contributionList.size(); >+ } >+ insertAt = Math.min(insertAt, contributionList.size()); >+ contributionList.add(insertAt, cbItem); >+ >+ } >+ >+ /** >+ * Restores the canonical order of this cool bar manager. The canonical >+ * order is the order in which the contribution items where added. >+ */ >+ public void resetItemOrder() { >+ for (ListIterator iterator = cbItemsCreationOrder.listIterator(); iterator >+ .hasNext();) { >+ IContributionItem item = (IContributionItem) iterator.next(); >+ // if its a user separator then do not include in original order. >+ if ((item.getId() != null) && (item.getId().equals(USER_SEPARATOR))) { >+ iterator.remove(); >+ } >+ } >+ IContributionItem[] itemsToSet = new IContributionItem[cbItemsCreationOrder >+ .size()]; >+ cbItemsCreationOrder.toArray(itemsToSet); >+ setItems(itemsToSet); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.action.ICoolBarManager#setContextMenuManager(org.eclipse.jface.action.IMenuManager) >+ */ >+ public void setContextMenuManager(IMenuManager contextMenuManager) { >+ this.contextMenuManager = (MenuManager) contextMenuManager; >+ if (coolBar != null) { >+ coolBar.setMenu(getContextMenuControl()); >+ } >+ } >+ >+ /** >+ * Replaces the current items with the given items. >+ * Forces an update. >+ * >+ * @param newItems the items with which to replace the current items >+ */ >+ public void setItems(IContributionItem[] newItems) { >+ // dispose of all the cool items on the cool bar manager >+/* FIXME TFS >+ * This is disposing of the coolItems but references are still hanging around. Might >+ * be a problem with SCoolBar and or SCoolItem. [Terry Smith 12/9/2005] >+ * >+ if (coolBar != null) { >+ SCoolItem[] coolItems = coolBar.getItems(); >+ for (int i = 0; i < coolItems.length; i++) { >+ dispose(coolItems[i]); >+ } >+ } >+ // Set the internal structure to this order >+ internalSetItems(newItems); >+*/ >+ // Force and update >+ update(true); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.action.ICoolBarManager#lockLayout(boolean) >+ */ >+ public void setLockLayout(boolean value) { >+ if (!coolBarExist()) { >+ return; >+ } >+ coolBar.setLocked(value); >+ } >+ >+ /** >+ * Subclasses may extend this <code>IContributionManager</code> method, >+ * but must call <code>super.update</code>. >+ * >+ * @see org.eclipse.jface.action.IContributionManager#update(boolean) >+ */ >+ public void update(boolean force) { >+ if ((!isDirty() && !force) || (!coolBarExist())) { >+ return; >+ } >+ >+ boolean relock = false; >+ boolean changed = false; >+ >+ try { >+ coolBar.setRedraw(false); >+ >+ // Refresh the widget data with the internal data structure. >+ refresh(); >+ >+ if (coolBar.getLocked()) { >+ coolBar.setLocked(false); >+ relock = true; >+ } >+ >+ /* >+ * Make a list of items including only those items that are >+ * visible. Separators should stay because they mark line breaks in >+ * a cool bar. >+ */ >+ final IContributionItem[] items = getItems(); >+ final List visibleItems = new ArrayList(items.length); >+ for (int i = 0; i < items.length; i++) { >+ final IContributionItem item = items[i]; >+ if (item.isVisible()) { >+ visibleItems.add(item); >+ } >+ } >+ >+ /* >+ * Make a list of CoolItem widgets in the cool bar for which there >+ * is no current visible contribution item. These are the widgets >+ * to be disposed. Dynamic items are also removed. >+ */ >+ SCoolItem[] coolItems = coolBar.getItems(); >+ final ArrayList coolItemsToRemove = new ArrayList(coolItems.length); >+ for (int i = 0; i < coolItems.length; i++) { >+ final Object data = coolItems[i].getData(); >+ if ((data == null) >+ || (!visibleItems.contains(data)) >+ || ((data instanceof IContributionItem) && ((IContributionItem) data) >+ .isDynamic())) { >+ coolItemsToRemove.add(coolItems[i]); >+ } >+ } >+ >+ // Dispose of any items in the list to be removed. >+ for (int i = coolItemsToRemove.size() - 1; i >= 0; i--) { >+ SCoolItem coolItem = (SCoolItem) coolItemsToRemove.get(i); >+ if (!coolItem.isDisposed()) { >+ Control control = coolItem.getControl(); >+ if (control != null) { >+ coolItem.setControl(null); >+ control.dispose(); >+ } >+ coolItem.dispose(); >+ } >+ } >+ >+ // Add any new items by telling them to fill. >+ coolItems = coolBar.getItems(); >+ IContributionItem sourceItem; >+ IContributionItem destinationItem; >+ int sourceIndex = 0; >+ int destinationIndex = 0; >+ final Iterator visibleItemItr = visibleItems.iterator(); >+ while (visibleItemItr.hasNext()) { >+ sourceItem = (IContributionItem) visibleItemItr.next(); >+ >+ // Retrieve the corresponding contribution item from SWT's >+ // data. >+ if (sourceIndex < coolItems.length) { >+ destinationItem = (IContributionItem) coolItems[sourceIndex] >+ .getData(); >+ } else { >+ destinationItem = null; >+ } >+ >+ // The items match is they are equal or both separators. >+ if (destinationItem != null) { >+ if (sourceItem.equals(destinationItem)) { >+ sourceIndex++; >+ destinationIndex++; >+ sourceItem.update(); >+ continue; >+ >+ } else if ((destinationItem.isSeparator()) >+ && (sourceItem.isSeparator())) { >+ coolItems[sourceIndex].setData(sourceItem); >+ sourceIndex++; >+ destinationIndex++; >+ sourceItem.update(); >+ continue; >+ >+ } >+ } >+ >+ // Otherwise, a new item has to be added. >+ final int start = coolBar.getItemCount(); >+ if (sourceItem instanceof ISContributionItem) >+ ((ISContributionItem)sourceItem).fill(coolBar, destinationIndex); >+ final int newItems = coolBar.getItemCount() - start; >+ for (int i = 0; i < newItems; i++) { >+ coolBar.getItem(destinationIndex++).setData(sourceItem); >+ } >+ changed = true; >+ } >+ >+ // Remove any old widgets not accounted for. >+ for (int i = coolItems.length - 1; i >= sourceIndex; i--) { >+ final SCoolItem item = coolItems[i]; >+ if (!item.isDisposed()) { >+ Control control = item.getControl(); >+ if (control != null) { >+ item.setControl(null); >+ control.dispose(); >+ } >+ item.dispose(); >+ changed = true; >+ } >+ } >+ >+ // Update wrap indices. >+ updateWrapIndices(); >+ >+ // Update the sizes. >+ for (int i = 0; i < items.length; i++) { >+ IContributionItem item = items[i]; >+ item.update(SIZE); >+ } >+ >+ // if the coolBar was previously locked then lock it >+ if (relock) { >+ coolBar.setLocked(true); >+ } >+ >+ if (changed) { >+ updateTabOrder(); >+ } >+ >+ // We are no longer dirty. >+ setDirty(false); >+ } finally { >+ coolBar.setRedraw(true); >+ } >+ } >+ >+ /** >+ * Sets the tab order of the coolbar to the visual order of its items. >+ */ >+ /* package */void updateTabOrder() { >+ if (coolBar != null) { >+ SCoolItem[] items = coolBar.getItems(); >+ if (items != null) { >+ ArrayList children = new ArrayList(items.length); >+ for (int i = 0; i < items.length; i++) { >+ if ((items[i].getControl() != null) >+ && (!items[i].getControl().isDisposed())) { >+ children.add(items[i].getControl()); >+ } >+ } >+ // Convert array >+ Control[] childrenArray = new Control[0]; >+ childrenArray = (Control[]) children.toArray(childrenArray); >+ >+ if (childrenArray != null) { >+ coolBar.setTabList(childrenArray); >+ } >+ >+ } >+ } >+ } >+ >+ /** >+ * Updates the indices at which the cool bar should wrap. >+ */ >+ private void updateWrapIndices() { >+ final IContributionItem[] items = getItems(); >+ final int numRows = getNumRows(items) - 1; >+ >+ // Generate the list of wrap indices. >+ final int[] wrapIndices = new int[numRows]; >+ boolean foundSeparator = false; >+ int j = 0; >+ for (int i = 0; i < items.length; i++) { >+ IContributionItem item = items[i]; >+ SCoolItem coolItem = findCoolItem(item); >+ if (item.isSeparator()) { >+ foundSeparator = true; >+ } >+ if ((!item.isSeparator()) && (!item.isGroupMarker()) >+ && (item.isVisible()) && (coolItem != null) >+ && (foundSeparator)) { >+ wrapIndices[j] = coolBar.indexOf(coolItem); >+ j++; >+ foundSeparator = false; >+ } >+ } >+ >+ /* >+ * Check to see if these new wrap indices are different than the old >+ * ones. >+ */ >+ final int[] oldIndices = coolBar.getWrapIndices(); >+ boolean shouldUpdate = false; >+ if (oldIndices.length == wrapIndices.length) { >+ for (int i = 0; i < oldIndices.length; i++) { >+ if (oldIndices[i] != wrapIndices[i]) { >+ shouldUpdate = true; >+ break; >+ } >+ } >+ } else { >+ shouldUpdate = true; >+ } >+ >+ if (shouldUpdate) { >+ coolBar.setWrapIndices(wrapIndices); >+ } >+ } >+ >+ public CoolBar createControl(Composite parent) { >+ return null; >+ } >+ >+ public Control getControl2() { >+ return coolBar; >+ } >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/Skin.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/Skin.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/Skin.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/Skin.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,94 @@ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import org.eclipse.swt.graphics.Font; >+import org.eclipse.swt.graphics.GC; >+import org.eclipse.swt.graphics.Image; >+import org.eclipse.swt.graphics.Point; >+import org.eclipse.swt.graphics.Rectangle; >+ >+public interface Skin { >+ >+ // states >+ public static final int NORMAL = 0; >+ public static final int HOVER = 1 << 1; >+ public static final int SELECTED = 1 << 2; >+ public static final int PRESSED = 1 << 3; >+ public static final int DISABLED = 1 << 4; >+ public static final int COLLAPSED = 1 << 5; >+ >+ // decorations >+ public final static int TYPE_DECORATION_CLOSE = 1; >+ public final static int TYPE_DECORATION_SEPARATOR = 2; >+ public final static int TYPE_DECORATION_FOCUS = 3; >+ public final static int TYPE_DECORATION_ARROW = 4; >+ public final static int TYPE_DECORATION_CHEVRON = 5; >+ public final static int TYPE_DECORATION_DROPDOWN_BUTTON = 6; >+ public final static int TYPE_DECORATION_COLLAPSE_BUTTON = 7; >+ public final static int TYPE_DECORATION_EXPAND_BUTTON = 8; >+ public final static int TYPE_DECORATION_GRIPPER = 9; >+ public final static int TYPE_DECORATION_INTERNAL_SEPARATOR = 10; >+ >+ // borders >+ public final static int TYPE_ITEM_MENU_BORDER = 11; >+ public final static int TYPE_ITEM_AREA_BORDER = 12; >+ public final static int TYPE_ITEM_BORDER = 13; >+ public final static int TYPE_BORDER = 14; >+ public final static int TYPE_GROUP_ITEM_BORDER = 15; >+ public final static int TYPE_GROUP_BORDER = 16; >+ public final static int TYPE_HEADER_ITEM_BORDER = 17; >+ public final static int TYPE_HEADER_BORDER = 18; >+ >+ // backgrounds >+ public final static int TYPE_ITEM_MENU_BACKGROUND = 19; >+ public final static int TYPE_ITEM_AREA_BACKGROUND = 20; >+ public final static int TYPE_ITEM_BACKGROUND = 21; >+ public final static int TYPE_BACKGROUND = 22; >+ public final static int TYPE_GROUP_ITEM_BACKGROUND = 23; >+ public final static int TYPE_HEADER_ITEM_BACKGROUND = 24; >+ public final static int TYPE_HEADER_BACKGROUND = 25; >+ >+ // fonts >+ public final static int TYPE_ITEM_MENU_FONT = 26; >+ public final static int TYPE_ITEM_FONT = 27; >+ public final static int TYPE_FONT = 28; >+ >+ // text >+ public final static int TYPE_ITEM_MENU_TEXT = 29; >+ public final static int TYPE_ITEM_TEXT = 30; >+ public final static int TYPE_TEXT= 31; >+ public final static int TYPE_GROUP_ITEM_TEXT = 32; >+ public final static int TYPE_HEADER_ITEM_TEXT = 33; >+ >+ // images >+ public final static int TYPE_ITEM_IMAGE = 34; >+ public final static int TYPE_IMAGE = 35; >+ >+ // properties >+ public final static int TYPE_MARGINS = 36; >+ public final static int TYPE_INSETS = 37; >+ public final static int TYPE_ITEM_MARGINS = 38; >+ public final static int TYPE_ITEM_INSETS = 39; >+ public final static int TYPE_ITEM_AREA_MARGINS = 40; >+ public final static int TYPE_ITEM_AREA_INSETS = 41; >+ public final static int TYPE_ITEM_MENU_MARGINS = 42; >+ public final static int TYPE_ITEM_MENU_INSETS = 43; >+ public final static int TYPE_DECORATION_CLOSE_INSETS = 44; >+ public final static int TYPE_ITEM_MIN_MAX_WIDTHS = 45; >+ public final static int TYPE_ITEM_ICON_SIZE = 46; >+ public final static int TYPE_ITEM_MIN_MAX_HEIGHT = 47; >+ >+ abstract public Rectangle getInsets(int type, int state); >+ abstract public Point getSize(int type, int state); >+ abstract public Font getFont(int type, int state); >+ abstract public Rectangle getMargins(int type, int state); >+ abstract public Rectangle getRect(int type, int state); >+ >+ public abstract void drawBorder(GC gc, Rectangle rect, SWidget widget, int type, int state); >+ public abstract void drawBackground(GC gc, Rectangle rect, SWidget widget, int type, int state); >+ public abstract void drawDecoration(GC gc, Rectangle rect, SWidget widget, int type, int state); >+ public abstract void drawText(GC gc, Rectangle rect, String text, int style, SWidget widget, int type, int state); >+ public abstract void drawImage(GC gc, Rectangle rect, Image image, SWidget widget, int type, int state); >+ >+ abstract public void dispose(); >+ >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolItem.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolItem.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolItem.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolItem.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,731 @@ >+/* >+* Copyright IBM Corporation 2003, 2005 >+* All rights reserved. >+* US Government Users Restricted Rights - Use, duplication or disclosure >+* restricted by GS ADP Schedule Contract with IBM Corp. >+*/ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.events.SelectionListener; >+import org.eclipse.swt.graphics.FontMetrics; >+import org.eclipse.swt.graphics.GC; >+import org.eclipse.swt.graphics.Image; >+import org.eclipse.swt.graphics.Point; >+import org.eclipse.swt.graphics.Rectangle; >+import org.eclipse.swt.widgets.Control; >+import org.eclipse.swt.widgets.Event; >+import org.eclipse.swt.widgets.Item; >+import org.eclipse.swt.widgets.TypedListener; >+ >+public class SToolItem extends Item { >+ SToolBar parent; >+ Control control; >+ int index; >+ String toolTipText; >+ Image disabledImage; >+ Image hotImage; >+ Image pressedImage; >+ boolean enabled = true; >+ boolean selected; >+ int separatorWidth; >+ int width = -1; >+ Point arrowSize = new Point(5, 5); >+ int state; >+ int arrowState; >+ >+ /** the alignment. Either CENTER, RIGHT, LEFT. Default is LEFT */ >+ private int align = SWT.LEFT; >+ >+ Rectangle itemRect = new Rectangle(0,0,0,0); >+ Rectangle arrowRect; >+ >+ /** >+ * @param parent >+ * @param style >+ */ >+ public SToolItem(SToolBar parent, int style) { >+ super(parent, style); >+ this.parent = parent; >+ parent.createItem(this, parent.getItemCount()); >+ } >+ >+ /** >+ * @param parent >+ * @param style >+ * @param index >+ */ >+ public SToolItem(SToolBar parent, int style, int index) { >+ super(parent, style, index); >+ this.parent = parent; >+ parent.createItem(this, index); >+ } >+ >+ public void addSelectionListener(SelectionListener listener) { >+ checkWidget(); >+ if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); >+ TypedListener typedListener = new TypedListener (listener); >+ addListener (SWT.Selection, typedListener); >+ addListener (SWT.DefaultSelection, typedListener); >+ } >+ >+ public Point computeSize(int wHint, int hHint, boolean changed) { >+ checkWidget(); >+ Skin skin = parent.getSkin(); >+ >+ Rectangle insets = skin.getRect(Skin.TYPE_ITEM_INSETS, Skin.NORMAL); >+ if (insets == null) { >+ insets = new Rectangle(0, 0, 0, 0); >+ } >+ >+ Point size = null; >+ String t = getText(); >+ if (t != null && t.length() == 0) { >+ t = null; >+ } >+ >+ if (control == null && (getStyle() & SWT.SEPARATOR) == SWT.SEPARATOR && >+ t == null) { >+ size = skin.getSize(Skin.TYPE_DECORATION_SEPARATOR, Skin.NORMAL); >+ } >+ else { >+ size = getContentSize(getImage(), getText()); >+ } >+ if (size == null) { >+ size = new Point(0, 0); >+ } >+ >+ if (wHint == SWT.DEFAULT) { >+ size.x += (insets.x + insets.width); >+ } else { >+ size.x = wHint; >+ } >+ if (hHint == SWT.DEFAULT) { >+ if (size.y != SWT.DEFAULT) { >+ if (getControl() == null) { >+ size.y += (insets.y + insets.height); >+ } >+ } >+ } else { >+ size.y = hHint; >+ } >+ >+ // >+ // Everything gets measured for a min/max width and height except if >+ // it is exclusively a SEPARATOR or if it is a separator with a control. >+ // >+ boolean measureMinMax = true; >+ if ((getStyle() & SWT.SEPARATOR) == SWT.SEPARATOR) { >+ if (control != null || t == null) { >+ measureMinMax = false; >+ } >+ } >+ >+ if (measureMinMax) { >+ Point minMax = skin.getSize(Skin.TYPE_ITEM_MIN_MAX_WIDTHS, Skin.NORMAL); >+ if (minMax != null) { >+ if (minMax.x > size.x && minMax.x != SWT.DEFAULT) { >+ size.x = minMax.x; >+ } >+ if (minMax.y < size.x && minMax.y != SWT.DEFAULT) { >+ size.x = minMax.y; >+ } >+ } >+ >+ minMax = skin.getSize(Skin.TYPE_ITEM_MIN_MAX_HEIGHT, Skin.NORMAL); >+ if (minMax != null) { >+ if (minMax.x > size.y && minMax.x != SWT.DEFAULT) { >+ size.y = minMax.x; >+ } >+ if (minMax.y < size.y && minMax.y != SWT.DEFAULT) { >+ size.y = minMax.y; >+ } >+ } >+ } >+ >+ return size; >+ } >+ >+ public void dispose() { >+ if (isDisposed ()) return; >+ parent.destroyItem(this); >+ super.dispose(); >+ } >+ >+ /** >+ * @return >+ */ >+ public Rectangle getBounds() { >+ return itemRect; >+ } >+ >+ /** >+ * @return >+ */ >+ public Control getControl() { >+ return control; >+ } >+ >+ Skin getSkin() { >+ Skin skin = parent.getSkin(); >+ return skin; >+ } >+ >+ /** >+ * @return >+ */ >+ public Image getDisabledImage() { >+ return disabledImage; >+ } >+ >+ /** >+ * @return >+ */ >+ public boolean getEnabled() { >+ return enabled; >+ } >+ >+ /** >+ * @return >+ */ >+ public Image getHotImage() { >+ return hotImage; >+ } >+ >+ /** >+ * @return pressed image. >+ */ >+ public Image getPressedImage() { >+ return pressedImage; >+ } >+ >+ /** >+ * @return >+ */ >+ public SToolBar getParent() { >+ return this.parent; >+ } >+ >+ /** >+ * @return >+ */ >+ public boolean getSelection() { >+ return selected; >+ } >+ >+ private int getState() { >+ return state; >+ } >+ >+ private int getArrowState() { >+ return arrowState; >+ } >+ >+ /** >+ * Compute the minimum size. >+ */ >+ private Point getContentSize(Image image, String text) { >+ Point size = new Point(0, 0); >+ Skin skin = getSkin(); >+ >+ Control control = getControl(); >+ if (control != null) { >+ size = control.computeSize(SWT.DEFAULT, SWT.DEFAULT); >+ if (width != -1) >+ size.x = width; >+ return size; >+ } >+ >+ if (image != null) { >+ Rectangle r; >+ Point imageSize = skin.getSize(Skin.TYPE_ITEM_ICON_SIZE, state); >+ if (imageSize != null && imageSize.y != SWT.DEFAULT) { >+ r = new Rectangle(0, 0, imageSize.x, imageSize.y); >+ } else { >+ r = image.getBounds(); >+ } >+ size.x += r.width; >+ size.y += r.height; >+ } >+ >+ if (skin != null) { >+ Rectangle margins = skin.getRect(Skin.TYPE_ITEM_MARGINS, Skin.NORMAL); >+ if (margins == null) { >+ margins = new Rectangle(0, 0, 0, 0); >+ } >+ >+ if (text != null && text.length() > 0) { >+ GC gc = new GC(this.getParent()); >+ gc.setFont(skin.getFont(Skin.TYPE_ITEM_FONT, Skin.NORMAL)); >+ FontMetrics fm = gc.getFontMetrics(); >+ Point e = gc.textExtent(text, SWT.DRAW_TRANSPARENT); >+ size.x += e.x; >+ size.y = Math.max(size.y, fm.getAscent()); >+ if (image != null) { >+ size.x += margins.x; >+ } >+ gc.dispose(); >+ } >+ >+ if ((getStyle() & SWT.DROP_DOWN) != 0) { >+ Point hinkySize = skin.getSize(Skin.TYPE_DECORATION_ARROW, Skin.NORMAL); >+ if (hinkySize == null) { >+ hinkySize = new Point(0, 0); >+ } >+ size.x += hinkySize.x + margins.y; >+ >+ if ((getStyle() & SWT.SIMPLE) != SWT.SIMPLE) { >+ Point separatorSize = skin.getSize(Skin.TYPE_DECORATION_INTERNAL_SEPARATOR, Skin.NORMAL); >+ if (separatorSize == null) { >+ separatorSize = new Point(0, 0); >+ } >+ size.x += separatorSize.x; >+ } >+ } >+ } >+ >+ return size; >+ } >+ >+ /** >+ * @return >+ */ >+ public String getToolTipText() { >+ return toolTipText; >+ } >+ >+ /** >+ * @return >+ */ >+ public int getWidth() { >+ return itemRect.width; >+ } >+ >+ public Rectangle getArrowRect() { >+ return arrowRect; >+ } >+ >+ /** >+ * @return >+ */ >+ public boolean isEnabled() { >+ return enabled; >+ } >+ >+ /** >+ * >+ */ >+ void onDispose() { >+ // TODO Auto-generated method stub >+ } >+ >+ /** >+ * @param event >+ */ >+ void onFocus(Event event) { >+ // TODO Auto-generated method stub >+ } >+ >+ /** >+ * @param event >+ */ >+ void onMouseDoubleClick(Event event) { >+ } >+ >+ /** >+ * @param event >+ */ >+ void onMouseDown(Event event) { >+ if (arrowRect != null) { >+ if (arrowRect.contains(event.x, event.y)) { >+ setArrowPressed(true); >+ } >+ } >+ if ((arrowState & Skin.PRESSED) == 0) { >+ setPressed(true); >+ } >+ notifyListeners(SWT.MouseDown, event); >+ >+ if ((getStyle() & SWT.RADIO) == SWT.RADIO) { >+ Event e = new Event(); >+ e.button = event.button; >+ e.character = event.character; >+ e.count = event.count; >+ e.data = event.data; >+ e.detail = event.detail; >+ e.display = event.display; >+ e.doit = event.doit; >+ e.end = event.end; >+ e.gc = event.gc; >+ e.height = event.height; >+ e.item = event.item; >+ e.keyCode = event.keyCode; >+ e.start = event.start; >+ e.stateMask = event.stateMask; >+ e.text = event.text; >+ e.time = event.time; >+ e.widget = event.widget; >+ e.width = event.width; >+ e.x = event.x; >+ e.y = event.y; >+ >+ e.type = SWT.Selection; >+ notifyListeners(SWT.Selection, e); >+ setSelected(true); >+ } >+ } >+ >+ /** >+ * @param event >+ */ >+ void onMouseExit(Event event) { >+ } >+ >+ /** >+ * @param event >+ */ >+ void onMouseHover(Event event) { >+ notifyListeners(SWT.MouseHover, event); >+ } >+ >+ void setHot(boolean hot) { >+ if (hot) >+ state |= Skin.HOVER; >+ else if ((state & Skin.HOVER) != 0) >+ state = (state & ~Skin.HOVER); >+ } >+ >+ void setArrowHot(boolean hot) { >+ if (hot) { >+ arrowState |= Skin.HOVER; >+ } else if ((arrowState & Skin.HOVER) == Skin.HOVER) { >+ arrowState = (arrowState & ~Skin.HOVER); >+ } >+ } >+ >+ void setPressed(boolean pressed) { >+ if (pressed) >+ state |= Skin.PRESSED; >+ else if ((state & Skin.PRESSED) != 0) >+ state = (state & ~Skin.PRESSED); >+ } >+ >+ void setArrowPressed(boolean pressed) { >+ if (pressed) >+ arrowState |= Skin.PRESSED; >+ else if ((arrowState & Skin.PRESSED) == Skin.PRESSED) >+ arrowState = (arrowState & ~Skin.PRESSED); >+ } >+ >+ public void setSelected(boolean selected) { >+ if (selected) { >+ /* Clear all adjacent selected radio buttons */ >+ int itemIndex = parent.indexOf(this); >+ int i; >+ SToolItem item; >+ >+ for (i = itemIndex -1; i > 0; i--) { >+ item = parent.getItem(i); >+ if ((item.getStyle() & SWT.RADIO) == SWT.RADIO) { >+ item.setSelected(false); >+ } else { >+ break; >+ } >+ } >+ for (i = itemIndex +1; i < parent.getItemCount(); i++) { >+ item = parent.getItem(i); >+ if ((item.getStyle() & SWT.RADIO) == SWT.RADIO) { >+ item.setSelected(false); >+ } else { >+ break; >+ } >+ } >+ state |= Skin.SELECTED; >+ } else if ((state & Skin.SELECTED) == Skin.SELECTED) { >+ state = (state & ~Skin.SELECTED); >+ } >+ } >+ >+ /** >+ * @param event >+ */ >+ void onMouseMove(Event event) { >+ } >+ >+ /** >+ * @param event >+ */ >+ void onMouseUp(Event event) { >+ setPressed(false); >+ >+ Event e = new Event(); >+ e.type = SWT.MouseUp; >+ e.widget = this; >+ e.gc = event.gc; >+ e.x = event.x; >+ e.y = event.y; >+ notifyListeners(SWT.MouseUp, event); >+ >+ if ((getStyle() & SWT.RADIO) != SWT.RADIO) { >+ e = new Event(); >+ e.type = SWT.Selection; >+ e.widget = this; >+ e.gc = event.gc; >+ e.x = event.x; >+ e.y = event.y; >+ if (arrowRect != null && arrowRect.contains(event.x, event.y)) >+ e.detail |= SWT.ARROW; >+ notifyListeners(SWT.Selection, e); >+ } >+ } >+ >+ >+ /* >+ * Process the paint event >+ */ >+ void onPaint(GC gc) { >+ Skin skin = parent.getSkin(); >+ String t = getText(); >+ if (t != null && t.length() == 0) { >+ t = null; >+ } >+ Image img = null; >+ >+ int state = getState(); >+ >+ /* >+ * A separator with text will just draw static text rather than a line separator >+ * so always keep the state normal so it doesn't behave like a button. >+ */ >+ if (control == null && (getStyle() & SWT.SEPARATOR) == SWT.SEPARATOR && t != null) { >+ state = Skin.NORMAL; >+ } >+ >+ if (isEnabled() == false) { >+ img = getDisabledImage(); >+ } else if ((state & Skin.PRESSED) == Skin.PRESSED) { >+ img = getPressedImage(); >+ } else if ((state & Skin.HOVER) == Skin.HOVER) { >+ img = getHotImage(); >+ } >+ >+ if (img == null) { >+ img = getImage(); >+ } >+ >+ Rectangle insets = skin.getRect(Skin.TYPE_ITEM_INSETS, state); >+ if (insets == null) { >+ insets = new Rectangle(0, 0, 0, 0); >+ } >+ gc.setFont(skin.getFont(Skin.TYPE_ITEM_FONT, state)); >+ >+ Rectangle rect = getBounds(); >+ if (rect.width == 0 || rect.height == 0) >+ return; >+ >+ int availableWidth = rect.width - (insets.x + insets.width); >+ Point extent = getContentSize(img, t); >+ if (extent.x > availableWidth) { >+ img = null; >+ extent = getContentSize(img, t); >+ } >+ >+ // determine horizontal position >+ int x = (rect.x + insets.x); >+ if (align == SWT.CENTER) { >+ x = (rect.width - extent.x) / 2; >+ } >+ else if (align == SWT.RIGHT) { >+ x = rect.width - extent.x - insets.width; >+ } >+//gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_RED)); >+//gc.drawRectangle(rect); >+ if (control == null && (getStyle() & SWT.SEPARATOR) == SWT.SEPARATOR && t == null) { >+ state = (state & ~(Skin.PRESSED)); >+ skin.drawDecoration(gc, rect, parent, Skin.TYPE_DECORATION_SEPARATOR, state); >+ return; >+ } >+ >+ // paint background >+ skin.drawBackground(gc, rect, parent, Skin.TYPE_ITEM_BACKGROUND, state); >+ >+ // paint border >+ skin.drawBorder(gc, rect, parent, Skin.TYPE_ITEM_BORDER, state); >+ >+ Rectangle itemMargins = skin.getRect(Skin.TYPE_ITEM_MARGINS, state); >+ if (itemMargins == null) { >+ itemMargins = new Rectangle(0, 0, 0, 0); >+ } >+ >+ // draw the image >+ >+ if (img != null) { >+ Rectangle imageRect; >+ Point imageSize = skin.getSize(Skin.TYPE_ITEM_ICON_SIZE, state); >+ if (imageSize != null && imageSize.y != SWT.DEFAULT) { >+ imageRect = new Rectangle(0, 0, imageSize.x, imageSize.y); >+ } else { >+ imageRect = img.getBounds(); >+ } >+ >+ imageRect = new Rectangle(x, rect.y + ((rect.height - imageRect.height) / 2), >+ imageRect.width, imageRect.height); >+ skin.drawImage(gc, imageRect, img, parent, Skin.TYPE_ITEM_IMAGE, state); >+ >+ x += imageRect.width + itemMargins.x; >+ } >+ >+ // draw the text >+ if (t != null) { >+ FontMetrics fm = gc.getFontMetrics(); >+ int textHeight = fm.getAscent(); >+ int textWidth = gc.textExtent(getText(), SWT.DRAW_TRANSPARENT | SWT.DRAW_MNEMONIC).x; >+ int y = rect.y; >+ int availableHeight = rect.height; >+ y = y + ((availableHeight / 2) - (textHeight / 2)); >+ Rectangle textRect = new Rectangle(x, y, textWidth, textHeight); >+//Used for debugging text rect rendering. >+// gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_GREEN)); >+// gc.fillRectangle(textRect); >+ /* Don't include leading so top of text lines up when expected in the drawing rect. */ >+ textRect.y -= fm.getLeading(); >+ skin.drawText(gc, textRect, getText(), SWT.DRAW_TRANSPARENT | SWT.DRAW_MNEMONIC, parent, Skin.TYPE_ITEM_TEXT, state); >+ /* Add leading back in to get the "text rect" that the text is actually rendered in. */ >+ textRect.y += fm.getLeading(); >+ x += textRect.width + itemMargins.y; >+ } >+ >+ // draw the hinky and text/hinky separator >+ if ((getStyle() & SWT.DROP_DOWN) != 0) { >+ Point hinkySize = skin.getSize(Skin.TYPE_DECORATION_ARROW, state); >+ if (hinkySize == null) { >+ hinkySize = new Point(0, 0); >+ } >+ int myX = x; >+ int myY = rect.y + ((rect.height /2) - ((hinkySize.y == SWT.DEFAULT ? 0 : hinkySize.y) / 2)); >+ Point separatorSize = skin.getSize(Skin.TYPE_DECORATION_INTERNAL_SEPARATOR, state); >+ if (separatorSize == null) { >+ separatorSize = new Point(0, 0); >+ } >+ int sepLineX = myX -2; >+ >+ int arrowState = state; >+ // separator >+ Rectangle sepLineRectangle = null; >+ if ((getStyle() & SWT.SIMPLE) != SWT.SIMPLE) { >+ int y = rect.y + (((rect.height / 2) - ((separatorSize.y == SWT.DEFAULT ? rect.height : separatorSize.y)) / 2)); >+ sepLineRectangle = new Rectangle(sepLineX, y, separatorSize.x, (separatorSize.y == SWT.DEFAULT ? rect.height : separatorSize.y)); >+ skin.drawDecoration(gc, sepLineRectangle, parent, Skin.TYPE_DECORATION_INTERNAL_SEPARATOR, state); >+ myX += separatorSize.x; >+ /* Since it is separate, use the arrow state to draw the hinky below. */ >+ arrowState = getArrowState(); >+ } >+ >+ // hinky >+ int height = (hinkySize.y == SWT.DEFAULT ? rect.height : hinkySize.y); >+ Rectangle arrowImageRect = new Rectangle(myX, myY, hinkySize.x, height); >+ skin.drawDecoration(gc, arrowImageRect, parent, Skin.TYPE_DECORATION_ARROW, arrowState); >+ >+ if (sepLineRectangle != null) >+ arrowRect = new Rectangle(sepLineRectangle.x, 0, myX, rect.height); >+ else >+ arrowRect = null; >+ } >+ } >+ >+ public void removeSelectionListener (SelectionListener listener) { >+ checkWidget(); >+ if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); >+ removeListener(SWT.Selection, listener); >+ removeListener(SWT.DefaultSelection,listener); >+ } >+ >+ /** >+ * @param control >+ */ >+ public void setControl(Control control) { >+ this.control = control; >+ if (control != null) { >+ Point pt = control.computeSize(SWT.DEFAULT, SWT.DEFAULT); >+ itemRect = new Rectangle(0, 0, pt.x, pt.y); >+ control.setBounds(itemRect); >+ parent.itemChanged(this); >+ } >+ } >+ >+ /** >+ * @param image >+ */ >+ public void setDisabledImage(Image image) { >+ this.disabledImage = image; >+ parent.itemChanged(this); >+ } >+ >+ /** >+ * @param enabled >+ */ >+ public void setEnabled(boolean enabled) { >+ this.enabled = enabled; >+ parent.itemChanged(this); >+ } >+ >+ /** >+ * @param image >+ */ >+ public void setHotImage(Image image) { >+ this.hotImage = image; >+ parent.itemChanged(this); >+ } >+ >+ /** >+ * @param pressed image >+ */ >+ public void setPressedImage(Image image) { >+ this.pressedImage = image; >+ parent.itemChanged(this); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.swt.widgets.Item#setImage(org.eclipse.swt.graphics.Image) >+ */ >+ public void setImage(Image image) { >+ if ((getStyle() & SWT.SEPARATOR) != 0) >+ return; >+ super.setImage(image); >+ parent.itemChanged(this); >+ } >+ >+ /** >+ * @param selected >+ */ >+ public void setSelection(boolean selected) { >+ this.selected = selected; >+ parent.itemChanged(this); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.swt.widgets.Item#setText(java.lang.String) >+ */ >+ public void setText(String string) { >+ super.setText(string); >+ parent.itemChanged(this); >+ } >+ >+ /** >+ * @param string >+ */ >+ public void setToolTipText(String string) { >+ toolTipText = string; >+ parent.itemChanged(this); >+ } >+ >+ public void setWidth(int width) { >+ this.width = width; >+ if (control != null) { >+ Point size = control.getSize(); >+ size.x = width; >+ control.setSize(size); >+ } >+ } >+ >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolBar.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolBar.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolBar.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolBar.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,504 @@ >+/* >+* Copyright IBM Corporation 2003, 2005 >+* All rights reserved. >+* US Government Users Restricted Rights - Use, duplication or disclosure >+* restricted by GS ADP Schedule Contract with IBM Corp. >+*/ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import java.util.ArrayList; >+ >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.graphics.GC; >+import org.eclipse.swt.graphics.Image; >+import org.eclipse.swt.graphics.ImageData; >+import org.eclipse.swt.graphics.PaletteData; >+import org.eclipse.swt.graphics.Point; >+import org.eclipse.swt.graphics.Rectangle; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Control; >+import org.eclipse.swt.widgets.Event; >+import org.eclipse.swt.widgets.Listener; >+ >+public class SToolBar extends SWidget { >+ >+ private ArrayList items = new ArrayList(); >+ private SToolItem hoverItem; >+ private SToolItem mouseDownItem; >+ >+ private Point offscreenImageSize = new Point(-1, -1); >+ private Image offscreenImage = null; >+ private GC offscreenGc = null; >+ >+ static final int ITEM_INSET_TOP = 3; >+ static final int ITEM_INSET_BOTTOM = 4; >+ static final int LEFT_MARGIN = 6; >+ static final int SHADOW_SIZE = 4; >+ >+ /** >+ * @param parent >+ * @param style >+ */ >+ public SToolBar(Composite parent, int style) { >+ super(parent, checkStyle(style)); >+ >+ // Add all listeners >+ Listener listener = new Listener() { >+ public void handleEvent(Event event) { >+ switch (event.type) { >+ case SWT.Dispose: onDispose(); break; >+ case SWT.FocusIn: onFocus(event); break; >+ case SWT.FocusOut: onFocus(event); break; >+ case SWT.MouseDoubleClick: onMouseDoubleClick(event); break; >+ case SWT.MouseDown: onMouseDown(event); break; >+ case SWT.MouseExit: onMouseExit(event); break; >+ case SWT.MouseHover: onMouseHover(event); break; >+ case SWT.MouseMove: onMouseMove(event); break; >+ case SWT.MouseUp: onMouseUp(event); break; >+ case SWT.Paint: onPaint(event); break; >+ case SWT.Traverse: onTraverse(event); break; >+ } >+ } >+ }; >+ >+ int[] toolBarEvents = new int[]{ >+ >+ SWT.Dispose, >+ SWT.FocusIn, >+ SWT.FocusOut, >+ SWT.KeyDown, >+ SWT.MouseDoubleClick, >+ SWT.MouseDown, >+ SWT.MouseExit, >+ SWT.MouseHover, >+ SWT.MouseMove, >+ SWT.MouseUp, >+ SWT.Paint, >+ SWT.Resize, >+ SWT.Traverse >+ }; >+ for (int i = 0; i < toolBarEvents.length; i++) { >+ addListener(toolBarEvents[i], listener); >+ } >+ } >+ >+ private static int checkStyle(int style) { >+ return SWT.NO_BACKGROUND; >+ } >+ >+ void createItem(SToolItem item, int index) { >+ if (!(0 <= index && index <= items.size())) >+ SWT.error(SWT.ERROR_INVALID_RANGE); >+ items.add(index, item); >+ redraw(); >+ } >+ >+ public Point computeSize(int wHint, int hHint, boolean changed) { >+ if (getLayout() != null) { >+ return super.computeSize (wHint, hHint, changed); >+ } >+ >+ Skin skin = getSkin(); >+ Rectangle itemAreaInsets = null; >+ Rectangle itemMargins = null; >+ if (skin != null) { >+ itemAreaInsets = skin.getRect(Skin.TYPE_ITEM_AREA_INSETS, Skin.NORMAL); >+ itemMargins = skin.getRect(Skin.TYPE_ITEM_AREA_MARGINS, Skin.NORMAL); >+ } >+ if (itemAreaInsets == null) { >+ itemAreaInsets = new Rectangle(0, 0, 0, 0); >+ } >+ if (itemMargins == null) { >+ itemMargins = new Rectangle(0, 0, 0, 0); >+ } >+ >+ int x; >+ int y; >+ Point size = getSize(); >+ int length = (items != null) ? items.size() : 0; >+ if ((getStyle() & SWT.VERTICAL) == SWT.VERTICAL) { >+ x = 0; >+ y = itemAreaInsets.x + itemMargins.x; >+ for (int i = 0; i < length; i++) { >+ SToolItem item = (SToolItem)items.get(i); >+ Point pt = item.computeSize(wHint, hHint, changed); >+ x = (pt.x > x) ? pt.x : x; >+ y += (pt.y + itemMargins.x); >+ } >+ x += itemAreaInsets.y + itemAreaInsets.height; >+ y += itemAreaInsets.width; >+ } else { >+ x = itemAreaInsets.x + itemMargins.x; >+ y = 0; >+ for (int i = 0; i < length; i++) { >+ SToolItem item = (SToolItem)items.get(i); >+ Point pt = item.computeSize(wHint, hHint, changed); >+ x += (pt.x + itemMargins.x); >+ y = (pt.y > y) ? pt.y : y; >+ } >+ y += itemAreaInsets.y + itemAreaInsets.height; >+ x += itemAreaInsets.width; >+ } >+ >+ if (wHint != SWT.DEFAULT && wHint >= x) >+ x = wHint; >+ if (hHint != SWT.DEFAULT && hHint >= y) >+ y = hHint; >+ // TODO: to be refactored. we should not use the value from getSize(). >+ // Otherwise toolbar cannot be smaller. >+ //if (wHint == SWT.DEFAULT && size.x > x) >+ // x = size.x; >+ if (hHint == SWT.DEFAULT && size.y > y) >+ y = size.y; >+ return new Point(x, y); >+ } >+ >+ public Rectangle computeTrim(int x, int y, int width, int height) { >+ return new Rectangle(x+SHADOW_SIZE, y+SHADOW_SIZE, >+ width-(SHADOW_SIZE*2), height-(SHADOW_SIZE*2)); >+ } >+ >+ void destroyItem(SToolItem item) { >+ if (items.contains(item)) >+ items.remove(item); >+ } >+ >+ public SToolItem getItem(int index) { >+ return (SToolItem)items.get(index); >+ } >+ >+ public SToolItem getItem(Point point) { >+ return null; >+ } >+ >+ public int getItemCount() { >+ return items.size(); >+ } >+ >+ public SToolItem[] getItems() { >+ return (SToolItem[])items.toArray(new SToolItem[0]); >+ } >+ >+ public int getRowCount() { >+ return 0; >+ } >+ >+ public int indexOf(SToolItem item) { >+ return items.indexOf(item); >+ } >+ >+ void itemChanged(SToolItem item) { >+ updateItems(); >+ redraw(); >+ } >+ >+ /** >+ * >+ */ >+ protected void onDispose() { >+ Skin skin = getSkin(); >+ if (skin != null) >+ skin.dispose(); >+ } >+ >+ /** >+ * @param event >+ */ >+ protected void onFocus(Event event) { >+ // TODO Auto-generated method stub >+ } >+ >+ /** >+ * @param event >+ */ >+ protected void onMouseDoubleClick(Event event) { >+ // TODO Auto-generated method stub >+ } >+ >+ /** >+ * @param event >+ */ >+ protected void onMouseDown(Event event) { >+ SToolItem item = findToolItem(event.x, event.y); >+ if (item != null) { >+ item.onMouseDown(event); >+ mouseDownItem = item; >+ redraw(); >+ } >+ } >+ >+ /** >+ * @param event >+ */ >+ protected void onMouseExit(Event event) { >+ boolean needsRedraw = false; >+ if (hoverItem != null) { >+ hoverItem.setHot(false); >+ hoverItem.setArrowHot(false); >+ hoverItem = null; >+ needsRedraw = true; >+ } >+ if (mouseDownItem != null) { >+ mouseDownItem.setPressed(false); >+ mouseDownItem.setArrowPressed(false); >+ mouseDownItem = null; >+ needsRedraw = true; >+ } >+ if (needsRedraw) >+ redraw(); >+ } >+ >+ /** >+ * @param event >+ */ >+ protected void onMouseHover(Event event) { >+ // TODO Auto-generated method stub >+ } >+ >+ /** >+ * @param event >+ */ >+ protected void onMouseMove(Event event) { >+ SToolItem item = findToolItem(event.x, event.y); >+ if (item != null) { >+ if (hoverItem != item) { >+ item.setHot(true); >+ Rectangle arrowRect = item.getArrowRect(); >+ if (arrowRect != null) { >+ if (arrowRect.contains(event.x, event.y)) { >+ item.setArrowHot(true); >+ } >+ } >+ >+ if (hoverItem != null) { >+ hoverItem.setHot(false); >+ hoverItem.setArrowHot(false); >+ } >+ >+ hoverItem = item; >+ redraw(); >+ } >+ } else if (hoverItem != null) { >+ hoverItem.setHot(false); >+ hoverItem.setArrowHot(false); >+ hoverItem = null; >+ redraw(); >+ } >+ } >+ >+ /** >+ * @param event >+ */ >+ protected void onMouseUp(Event event) { >+ boolean needsRedraw = false; >+ >+ try { >+ SToolItem item = findToolItem(event.x, event.y); >+ if (item != null && item == mouseDownItem) { >+ needsRedraw = true; >+ item.onMouseUp(event); >+ } else if (mouseDownItem != null) { >+ needsRedraw = true; >+ mouseDownItem.setPressed(false); >+ mouseDownItem.setArrowPressed(false); >+ } >+ } finally { >+ if (needsRedraw) >+ redraw(); >+ } >+ } >+ >+ >+ /** >+ * @param event >+ */ >+ protected void onPaint(Event event) { >+ Point size = getSize(); >+ GC gc = event.gc; >+ >+ try { >+ if (offscreenImageSize.x != size.x || offscreenImageSize.y != size.y) { >+ if (offscreenImage != null) { >+ offscreenImage.dispose(); >+ offscreenImage = null; >+ } >+ if (offscreenGc != null) { >+ offscreenGc.dispose(); >+ offscreenGc = null; >+ } >+ PaletteData offscreenImagePalette = new PaletteData(0x0000FF00, 0x00FF0000, 0xFF000000); >+ ImageData offscreenImageData = new ImageData(size.x, size.y, 32, offscreenImagePalette); >+ offscreenImageData.type = SWT.BITMAP; >+ offscreenImage = new Image(null, offscreenImageData); >+ offscreenGc = new GC(offscreenImage, getStyle()); >+ offscreenImageSize.x = size.x; >+ offscreenImageSize.y = size.y; >+ } >+ } finally { >+ if (offscreenGc != null) { >+ offscreenGc.setForeground(event.gc.getForeground()); >+ offscreenGc.setBackground(event.gc.getBackground()); >+ offscreenGc.fillRectangle(0, 0, size.x, size.y); >+ gc = offscreenGc; >+ } >+ } >+ >+ updateItems(); >+ Skin skin = getSkin(); >+ >+ if (skin == null) >+ return; >+ >+ Rectangle rect = getClientArea(); >+ if (rect.width == 0 || rect.height == 0) >+ return; >+ >+ int state = Skin.NORMAL; >+ >+ // paint background >+ skin.drawBackground(gc, rect, this, Skin.TYPE_BACKGROUND, state); >+ >+ // paint tool items >+ for (int i = 0; i < items.size(); i++) { >+ SToolItem item = this.getItem(i); >+ item.onPaint(gc); >+ } >+ >+ // paint border >+ skin.drawBorder(gc, rect, this, Skin.TYPE_BORDER, state); >+ >+ // draw the offscreen image >+ if (gc == offscreenGc) >+ event.gc.drawImage(offscreenImage, 0, 0); >+ >+ } >+ >+ /** >+ * @param event >+ */ >+ protected void onTraverse(Event event) { >+ // TODO Auto-generated method stub >+ } >+ >+ private SToolItem findToolItem(int x, int y) { >+ SToolItem item = null; >+ for (int i=0; i<items.size(); i++) { >+ Rectangle bounds = ((SToolItem)items.get(i)).getBounds(); >+ if (bounds.contains(x, y)){ >+ item = (SToolItem)items.get(i); >+ } >+ } >+ return item; >+ } >+ >+ public void setSkin(Skin skin) { >+ super.setSkin(skin); >+ } >+ >+ boolean setItemLocation() { >+ if (items.size() == 0) >+ return false; >+ >+ Skin skin = getSkin(); >+ Rectangle itemAreaInsets = null; >+ Rectangle itemMargins = null; >+ if (skin != null) { >+ itemAreaInsets = skin.getRect(Skin.TYPE_ITEM_AREA_INSETS, Skin.NORMAL); >+ itemMargins = skin.getRect(Skin.TYPE_ITEM_AREA_MARGINS, Skin.NORMAL); >+ } >+ if (itemAreaInsets == null) { >+ itemAreaInsets = new Rectangle(0, 0, 0, 0); >+ } >+ if (itemMargins == null) { >+ itemMargins = new Rectangle(0, 0, 0, 0); >+ } >+ >+ /* >+ * TODO Handle vertical toolbars. >+ */ >+ >+ Rectangle barBounds = getBounds(); >+ int height = 0; >+ if (barBounds != null) { >+ height = barBounds.height; >+ } >+ >+ boolean changed = false; >+ int x = itemAreaInsets.x + itemMargins.x; >+ int y; >+ for (int i = 0; i < items.size(); i++) { >+ SToolItem item = (SToolItem)items.get(i); >+ if (item.itemRect.x != x) { >+ item.itemRect.x = x; >+ changed = true; >+ } >+ >+ /* >+ * Center vertically. >+ */ >+ y = itemAreaInsets.y + (((height - itemAreaInsets.y - itemAreaInsets.height) / 2) - (item.itemRect.height / 2)); >+ >+ if (item.itemRect.y != y) { >+ item.itemRect.y = y; >+ changed = true; >+ } >+ if (changed == true) { >+ Control control = item.getControl(); >+ if (control != null) { >+ Rectangle r = control.getBounds(); >+ r.x = item.itemRect.x + (item.itemRect.width - r.width) / 2; >+ r.y = item.itemRect.y + (item.itemRect.height - r.height) / 2; >+ control.setLocation(r.x, r.y); >+ } >+ } >+ x += (item.itemRect.width + itemMargins.x); >+ } >+ return changed; >+ } >+ >+ boolean setItemSize() { >+ if (items.size() == 0) >+ return false; >+ >+ boolean changed = false; >+ >+ Skin skin = getSkin(); >+ Rectangle itemAreaInsets = null; >+ if (skin != null) { >+ itemAreaInsets = skin.getRect(Skin.TYPE_ITEM_AREA_INSETS, Skin.NORMAL); >+ } >+ if (itemAreaInsets == null) { >+ itemAreaInsets = new Rectangle(0, 0, 0, 0); >+ } >+ >+ for (int i = 0; i < items.size(); i++) { >+ SToolItem item = (SToolItem)items.get(i); >+ Point pt = item.computeSize(SWT.DEFAULT, SWT.DEFAULT, false); >+ >+ if (pt.y == SWT.DEFAULT) { >+ if ((getStyle() & SWT.VERTICAL) == SWT.VERTICAL) { >+ pt.y = pt.x; >+ pt.x = getSize().x - (itemAreaInsets.y + itemAreaInsets.height); >+ } else { >+ pt.y = getSize().y - (itemAreaInsets.y + itemAreaInsets.height); >+ } >+ } >+ >+ if (pt.x != item.itemRect.width) { >+ item.itemRect.width = pt.x; >+ changed = true; >+ } >+ if (pt.y != item.itemRect.height) { >+ item.itemRect.height = pt.y; >+ changed = true; >+ } >+ } >+ return changed; >+ } >+ >+ void updateItems() { >+ setItemSize(); >+ setItemLocation(); >+ } >+ >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolBarContributionItem2.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolBarContributionItem2.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolBarContributionItem2.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolBarContributionItem2.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,709 @@ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import java.util.ArrayList; >+import java.util.Iterator; >+ >+import org.eclipse.jface.action.ActionContributionItem; >+import org.eclipse.jface.action.IContributionItem; >+import org.eclipse.jface.action.ICoolBarManager; >+import org.eclipse.jface.action.IToolBarManager; >+import org.eclipse.jface.action.MenuManager; >+import org.eclipse.jface.action.Separator; >+import org.eclipse.jface.action.SubContributionItem; >+import org.eclipse.jface.action.ToolBarContributionItem; >+import org.eclipse.jface.action.ToolBarManager; >+import org.eclipse.jface.util.Assert; >+import org.eclipse.jface.util.Policy; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.events.DisposeEvent; >+import org.eclipse.swt.events.DisposeListener; >+import org.eclipse.swt.events.SelectionAdapter; >+import org.eclipse.swt.events.SelectionEvent; >+import org.eclipse.swt.graphics.Point; >+import org.eclipse.swt.graphics.Rectangle; >+import org.eclipse.swt.widgets.Control; >+import org.eclipse.swt.widgets.CoolBar; >+import org.eclipse.swt.widgets.CoolItem; >+import org.eclipse.swt.widgets.Event; >+import org.eclipse.swt.widgets.Listener; >+import org.eclipse.swt.widgets.Menu; >+import org.eclipse.swt.widgets.ToolBar; >+import org.eclipse.swt.widgets.ToolItem; >+ >+public class SToolBarContributionItem2 extends ToolBarContributionItem implements ISContributionItem { >+ >+ /** >+ * A constant used by <code>setMinimumItemsToShow</code> and <code>getMinimumItemsToShow</code> >+ * to indicate that all tool items should be shown in the cool item. >+ */ >+ public static final int SHOW_ALL_ITEMS = -1; >+ >+ /** >+ * The pull down menu used to list all hidden tool items if the current >+ * size is less than the preffered size. >+ */ >+ private MenuManager chevronMenuManager = null; >+ >+ /** >+ * The widget created for this item; <code>null</code> before creation >+ * and after disposal. >+ */ >+ private SCoolItem coolItem = null; >+ >+ /** >+ * Current height of cool item >+ */ >+ private int currentHeight = -1; >+ >+ /** >+ * Current width of cool item. >+ */ >+ private int currentWidth = -1; >+ >+ /** >+ * A flag indicating that this item has been disposed. This prevents future >+ * method invocations from doing things they shouldn't. >+ */ >+ private boolean disposed = false; >+ >+ /** >+ * Mininum number of tool items to show in the cool item widget. >+ */ >+ private int minimumItemsToShow = SHOW_ALL_ITEMS; >+ >+ /** >+ * The tool bar manager used to manage the tool items contained in the cool >+ * item widget. >+ */ >+ private IToolBarManager toolBarManager = null; >+ >+ /** >+ * Enable/disable chevron support. >+ */ >+ private boolean useChevron = true; >+ >+ private String id; >+ >+ /** >+ * Convenience method equivalent to <code>ToolBarContributionItem(new ToolBarManager(), null)</code>. >+ */ >+ public SToolBarContributionItem2() { >+ this(new SToolBarManager2(), (String)null); >+ } >+ >+ /** >+ * Convenience method equivalent to <code>ToolBarContributionItem(toolBarManager, null)</code>. >+ * >+ * @param toolBarManager >+ * the tool bar manager >+ */ >+ public SToolBarContributionItem2(IToolBarManager toolBarManager) { >+ this(toolBarManager, (String)null); >+ } >+ >+ /** >+ * Creates a tool bar contribution item. >+ * >+ * @param toolBarManager >+ * the tool bar manager to wrap >+ * @param id >+ * the contribution item id, or <code>null</code> if none >+ */ >+ public SToolBarContributionItem2(IToolBarManager toolBarManager, String id) { >+ super(); >+ this.toolBarManager = toolBarManager; >+ this.id = id; >+ } >+ >+ /** >+ * Checks whether this contribution item has been disposed. If it has, and >+ * the tracing options are active, then it prints some debugging >+ * information. >+ * >+ * @return <code>true</code> if the item is disposed; <code>false</code> >+ * otherwise. >+ * >+ */ >+ private final boolean checkDisposed() { >+ if (disposed) { >+ if (Policy.TRACE_TOOLBAR) { //$NON-NLS-1$ >+ System.out >+ .println("Method invocation on a disposed tool bar contribution item."); //$NON-NLS-1$ >+ new Exception().printStackTrace(System.out); >+ } >+ >+ return true; >+ } >+ >+ return false; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.action.IContributionItem#dispose() >+ */ >+ public void dispose() { >+ // Dispose of the ToolBar and all its contributions >+ if (toolBarManager != null) { >+ // TODO: handle dispose >+ if (toolBarManager instanceof ToolBarManager) >+ ((ToolBarManager)toolBarManager).dispose(); >+ else if (toolBarManager instanceof SToolBarManager2) >+ ((SToolBarManager2)toolBarManager).dispose(); >+ toolBarManager = null; >+ } >+ >+ /* >+ * We need to dispose the cool item or we might be left holding a cool >+ * item with a disposed control. >+ */ >+ if ((coolItem != null) && (!coolItem.isDisposed())) { >+ coolItem.dispose(); >+ coolItem = null; >+ } >+ >+ // Mark this item as disposed. >+ disposed = true; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.CoolBar, >+ * int) >+ */ >+ public void fill(CoolBar coolBar, int index) { >+ // do nothing >+ } >+ >+ /** >+ * Returns a consistent set of wrap indices. The return value will always >+ * include at least one entry and the first entry will always be zero. >+ * CoolBar.getWrapIndices() is inconsistent in whether or not it returns an >+ * index for the first row. >+ */ >+ private int[] getAdjustedWrapIndices(int[] wraps) { >+ int[] adjustedWrapIndices; >+ if (wraps.length == 0) { >+ adjustedWrapIndices = new int[] { 0 }; >+ } else { >+ if (wraps[0] != 0) { >+ adjustedWrapIndices = new int[wraps.length + 1]; >+ adjustedWrapIndices[0] = 0; >+ for (int i = 0; i < wraps.length; i++) { >+ adjustedWrapIndices[i + 1] = wraps[i]; >+ } >+ } else { >+ adjustedWrapIndices = wraps; >+ } >+ } >+ return adjustedWrapIndices; >+ } >+ >+ /** >+ * Returns the current height of the corresponding cool item. >+ * >+ * @return the current height >+ */ >+ public int getCurrentHeight() { >+ if (checkDisposed()) { >+ return -1; >+ } >+ return currentHeight; >+ } >+ >+ /** >+ * Returns the current width of the corresponding cool item. >+ * >+ * @return the current size >+ */ >+ public int getCurrentWidth() { >+ if (checkDisposed()) { >+ return -1; >+ } >+ return currentWidth; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * @see org.eclipse.jface.action.IContributionItem#getId() >+ */ >+ public String getId() { >+ return this.id; >+ } >+ >+ /** >+ * Returns the minimum number of tool items to show in the cool item. >+ * >+ * @return the minimum number of tool items to show, or <code>SHOW_ALL_ITEMS</code> >+ * if a value was not set >+ * @see #setMinimumItemsToShow(int) >+ */ >+ public int getMinimumItemsToShow() { >+ if (checkDisposed()) { >+ return -1; >+ } >+ return minimumItemsToShow; >+ } >+ >+ /** >+ * Returns the internal tool bar manager of the contribution item. >+ * >+ * @return the tool bar manager, or <code>null</code> if one is not >+ * defined. >+ * @see IToolBarManager >+ */ >+ public IToolBarManager getToolBarManager() { >+ if (checkDisposed()) { >+ return null; >+ } >+ return toolBarManager; >+ } >+ >+ /** >+ * Returns whether chevron support is enabled. >+ * >+ * @return <code>true</code> if chevron support is enabled, <code>false</code> >+ * otherwise >+ */ >+ public boolean getUseChevron() { >+ if (checkDisposed()) { >+ return false; >+ } >+ return useChevron; >+ } >+ >+ /** >+ * Create and display the chevron menu. >+ */ >+ private void handleChevron(SelectionEvent event) { >+ CoolItem item = (CoolItem) event.widget; >+ Control control = item.getControl(); >+ if ((control instanceof ToolBar) == false) { >+ return; >+ } >+ CoolBar coolBar = item.getParent(); >+ ToolBar toolBar = (ToolBar) control; >+ Rectangle toolBarBounds = toolBar.getBounds(); >+ ToolItem[] items = toolBar.getItems(); >+ ArrayList hidden = new ArrayList(); >+ for (int i = 0; i < items.length; ++i) { >+ Rectangle itemBounds = items[i].getBounds(); >+ if (!((itemBounds.x + itemBounds.width <= toolBarBounds.width) && (itemBounds.y >+ + itemBounds.height <= toolBarBounds.height))) { >+ hidden.add(items[i]); >+ } >+ } >+ >+ // Create a pop-up menu with items for each of the hidden buttons. >+ if (chevronMenuManager != null) { >+ chevronMenuManager.dispose(); >+ } >+ chevronMenuManager = new MenuManager(); >+ for (Iterator i = hidden.iterator(); i.hasNext();) { >+ ToolItem toolItem = (ToolItem) i.next(); >+ IContributionItem data = (IContributionItem) toolItem.getData(); >+ if (data instanceof ActionContributionItem) { >+ ActionContributionItem contribution = new ActionContributionItem( >+ ((ActionContributionItem) data).getAction()); >+ chevronMenuManager.add(contribution); >+ } else if (data instanceof SubContributionItem) { >+ IContributionItem innerData = ((SubContributionItem) data) >+ .getInnerItem(); >+ if (innerData instanceof ActionContributionItem) { >+ ActionContributionItem contribution = new ActionContributionItem( >+ ((ActionContributionItem) innerData).getAction()); >+ chevronMenuManager.add(contribution); >+ } >+ } else if (data.isSeparator()) { >+ chevronMenuManager.add(new Separator()); >+ } >+ } >+ Menu popup = chevronMenuManager.createContextMenu(coolBar); >+ Point chevronPosition = coolBar.toDisplay(event.x, event.y); >+ popup.setLocation(chevronPosition.x, chevronPosition.y); >+ popup.setVisible(true); >+ } >+ >+ /** >+ * Handles the event when the toobar item does not have its own context >+ * menu. >+ * >+ * @param event >+ * the event object >+ */ >+ private void handleContextMenu(Event event) { >+ Control toolBar = null; >+ if (toolBarManager instanceof SToolBarManager2) >+ toolBar = ((SToolBarManager2)toolBarManager).getControl(); >+ else if (toolBarManager instanceof ToolBarManager) >+ toolBar = ((ToolBarManager)toolBarManager).getControl(); >+ else >+ return; >+ // If parent has a menu then use that one >+ Menu parentMenu = toolBar.getParent().getMenu(); >+ if ((parentMenu != null) && (!parentMenu.isDisposed())) { >+ toolBar.setMenu(parentMenu); >+ // Hook listener to remove menu once it has disapeared >+ parentMenu.addListener(SWT.Hide, new Listener() { >+ >+ public void handleEvent(Event innerEvent) { >+ Control innerToolBar = null; >+ if (toolBarManager instanceof ToolBarManager) >+ innerToolBar = ((ToolBarManager)toolBarManager).getControl(); >+ else if (toolBarManager instanceof SToolBarManager2) >+ innerToolBar = ((SToolBarManager2)toolBarManager).getControl(); >+ if (innerToolBar != null) { >+ innerToolBar.setMenu(null); >+ Menu innerParentMenu = innerToolBar.getParent() >+ .getMenu(); >+ if (innerParentMenu != null) { >+ innerParentMenu.removeListener(SWT.Hide, this); >+ } >+ } >+ } >+ }); >+ } >+ } >+ >+ /** >+ * Handles the disposal of the widget. >+ * >+ * @param event >+ * the event object >+ */ >+ private void handleWidgetDispose(DisposeEvent event) { >+ coolItem = null; >+ } >+ >+ /** >+ * A contribution item is visible iff its internal state is visible <em>or</em> >+ * the tool bar manager contains something other than group markers and >+ * separators. >+ * >+ * @return <code>true</code> if the tool bar manager contains something >+ * other than group marks and separators, and the internal state is >+ * set to be visible. >+ */ >+ public boolean isVisible() { >+ if (checkDisposed()) { >+ return false; >+ } >+ >+ boolean visibleItem = false; >+ if (toolBarManager != null) { >+ IContributionItem[] contributionItems = toolBarManager.getItems(); >+ for (int i = 0; i < contributionItems.length; i++) { >+ IContributionItem contributionItem = contributionItems[i]; >+ if ((!contributionItem.isGroupMarker()) >+ && (!contributionItem.isSeparator())) { >+ visibleItem = true; >+ break; >+ } >+ } >+ } >+ >+ return (visibleItem || super.isVisible()); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.action.IContributionItem#saveWidgetState() >+ */ >+ public void saveWidgetState() { >+ if (checkDisposed()) { >+ return; >+ } >+ if (coolItem == null) >+ return; >+ >+ //1. Save current size >+ SCoolBar coolBar = (SCoolBar)coolItem.getParent(); >+ boolean isLastOnRow = false; >+ int lastIndex = coolBar.getItemCount() - 1; >+ int coolItemIndex = coolBar.indexOf(coolItem); >+ int[] wrapIndicies = getAdjustedWrapIndices(coolBar.getWrapIndices()); >+ // Traverse through all wrap indicies backwards >+ for (int row = wrapIndicies.length - 1; row >= 0; row--) { >+ if (wrapIndicies[row] <= coolItemIndex) { >+ >+ int nextRow = row + 1; >+ int nextRowStartIndex; >+ if (nextRow > (wrapIndicies.length - 1)) { >+ nextRowStartIndex = lastIndex + 1; >+ } else { >+ nextRowStartIndex = wrapIndicies[nextRow]; >+ } >+ >+ // Check to see if its the last item on the row >+ if (coolItemIndex == (nextRowStartIndex - 1)) { >+ isLastOnRow = true; >+ } >+ break; >+ } >+ } >+ >+ // Save the preferred size as actual size for the last item on a row >+ int nCurrentWidth; >+ if (isLastOnRow) { >+ nCurrentWidth = coolItem.getPreferredSize().x; >+ } else { >+ nCurrentWidth = coolItem.getSize().x; >+ } >+ setCurrentWidth(nCurrentWidth); >+ setCurrentHeight(coolItem.getSize().y); >+ } >+ >+ /** >+ * Sets the current height of the cool item. Update(SIZE) should be called >+ * to adjust the widget. >+ * >+ * @param currentHeight >+ * the current height to set >+ */ >+ public void setCurrentHeight(int currentHeight) { >+ if (checkDisposed()) { >+ return; >+ } >+ this.currentHeight = currentHeight; >+ } >+ >+ /** >+ * Sets the current width of the cool item. Update(SIZE) should be called >+ * to adjust the widget. >+ * >+ * @param currentWidth >+ * the current width to set >+ */ >+ public void setCurrentWidth(int currentWidth) { >+ if (checkDisposed()) { >+ return; >+ } >+ this.currentWidth = currentWidth; >+ } >+ >+ /** >+ * Sets the minimum number of tool items to show in the cool item. If this >+ * number is less than the total tool items, a chevron will appear and the >+ * hidden tool items appear in a drop down menu. By default, all the tool >+ * items are shown in the cool item. >+ * >+ * @param minimumItemsToShow >+ * the minimum number of tool items to show. >+ * @see #getMinimumItemsToShow() >+ * @see #setUseChevron(boolean) >+ */ >+ public void setMinimumItemsToShow(int minimumItemsToShow) { >+ if (checkDisposed()) { >+ return; >+ } >+ this.minimumItemsToShow = minimumItemsToShow; >+ } >+ >+ /** >+ * Enables or disables chevron support for the cool item. By default, >+ * chevron support is enabled. >+ * >+ * @param value >+ * <code>true</code> to enable chevron support, <code>false</code> >+ * otherwise. >+ */ >+ public void setUseChevron(boolean value) { >+ if (checkDisposed()) { >+ return; >+ } >+ useChevron = value; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.action.IContributionItem#update(java.lang.String) >+ */ >+ public void update(String propertyName) { >+ if (checkDisposed()) { >+ return; >+ } >+ if (coolItem != null) { >+ IToolBarManager manager = getToolBarManager(); >+ if (manager != null) { >+ manager.update(true); >+ } >+ >+ if ((propertyName == null) >+ || propertyName.equals(ICoolBarManager.SIZE)) { >+ updateSize(true); >+ } >+ } >+ } >+ >+ /** >+ * Updates the cool items' preferred, minimum, and current size. The >+ * preferred size is calculated based on the tool bar size and extra trim. >+ * >+ * @param changeCurrentSize >+ * <code>true</code> if the current size should be changed to >+ * the preferred size, <code>false</code> to not change the >+ * current size >+ */ >+ private void updateSize(boolean changeCurrentSize) { >+ if (checkDisposed()) { >+ return; >+ } >+ // cannot set size if coolItem is null >+ if (coolItem == null || coolItem.isDisposed()) { >+ return; >+ } >+ boolean locked = false; >+ SCoolBar coolBar = (SCoolBar)coolItem.getParent(); >+ try { >+ // Fix odd behaviour with locked tool bars >+ if (coolBar != null) { >+ if (coolBar.getLocked()) { >+ coolBar.setLocked(false); >+ locked = true; >+ } >+ } >+ SToolBar toolBar = (SToolBar) coolItem.getControl(); >+ if ((toolBar == null) || (toolBar.isDisposed()) >+ || (toolBar.getItemCount() <= 0)) { >+ // if the toolbar does not contain any items then dispose of >+ // coolItem >+ coolItem.setData(null); >+ Control control = coolItem.getControl(); >+ if ((control != null) && !control.isDisposed()) { >+ control.dispose(); >+ coolItem.setControl(null); >+ } >+ if (!coolItem.isDisposed()) { >+ coolItem.dispose(); >+ } >+ } else { >+ // If the toolbar item exists then adjust the size of the cool >+ // item >+ Point toolBarSize = toolBar.computeSize(SWT.DEFAULT, >+ SWT.DEFAULT); >+ // Set the preffered size to the size of the toolbar plus trim >+/* FIXME TFS Why is this needed? For repositioning..??? >+ * Oh well. Interferring with the LS build so commenting out. >+ * Point preferredSize = coolItem.computeSize(toolBarSize.x, >+ toolBarSize.y); >+ coolItem.setPreferredSize(preferredSize); >+*/ // note setMinimumSize must be called before setSize, see PR >+ // 15565 >+ // Set minimum size >+ if (getMinimumItemsToShow() != SHOW_ALL_ITEMS) { >+ int toolItemWidth = toolBar.getItems()[0].getWidth(); >+ int minimumWidth = toolItemWidth * getMinimumItemsToShow(); >+ coolItem.setMinimumSize(minimumWidth, toolBarSize.y); >+ } else { >+ coolItem.setMinimumSize(toolBarSize.x, toolBarSize.y); >+ } >+/* FIXME TFS See the fixme above. >+ * if (changeCurrentSize) { >+ // Set current size to preferred size >+ coolItem.setSize(preferredSize); >+ } >+*/ } >+ } finally { >+ // If the cool bar was locked, then set it back to locked >+ if ((locked) && (coolBar != null)) { >+ coolBar.setLocked(true); >+ } >+ } >+ } >+ >+ public void fill(SCoolBar coolBar, int index) { >+ if (checkDisposed()) { >+ return; >+ } >+ >+ if (coolItem == null && coolBar != null) { >+ Control oldToolBar = null; >+ Control toolBar = null; >+ if (toolBarManager instanceof ToolBarManager) { >+ oldToolBar = ((ToolBarManager)toolBarManager).getControl(); >+ toolBar = ((ToolBarManager)toolBarManager).createControl(coolBar); >+ } else if (toolBarManager instanceof SToolBarManager2) { >+ oldToolBar = ((SToolBarManager2)toolBarManager).getControl(); >+ toolBar = ((SToolBarManager2)toolBarManager).createControl(coolBar); >+ } >+ if ((oldToolBar != null) && (oldToolBar.equals(toolBar))) { >+ // We are using an old tool bar, so we need to update. >+ toolBarManager.update(true); >+ } >+ >+ // Do not create a coolItem if the toolbar is empty >+ if (toolBar instanceof ToolBar) { >+ if (((ToolBar)toolBar).getItemCount() < 1) >+ return; >+ } else if (toolBar instanceof SToolBar) { >+ if (((SToolBar)toolBar).getItemCount() < 1) >+ return; >+ } >+ int flags = SWT.DROP_DOWN; >+ if (index >= 0) { >+ coolItem = new SCoolItem(coolBar, flags, index); >+ } else { >+ coolItem = new SCoolItem(coolBar, flags); >+ } >+ // sets the back reference >+ coolItem.setData(this); >+ // Add the toolbar to the CoolItem widget >+ coolItem.setControl(toolBar); >+ >+ // Handle Context Menu >+ // ToolBarManager.createControl can actually return a pre-existing control. >+ // Only add the listener if the toolbar was newly created (bug 62097). >+ if (oldToolBar != toolBar) { >+ toolBar.addListener(SWT.MenuDetect, new Listener() { >+ >+ public void handleEvent(Event event) { >+ // if the toolbar does not have its own context menu then >+ // handle the event >+ if (toolBarManager instanceof ToolBarManager) { >+ if (((ToolBarManager)toolBarManager).getContextMenuManager() == null) { >+ handleContextMenu(event); >+ } >+ } else if (toolBarManager instanceof SToolBarManager2) { >+ if (((SToolBarManager2)toolBarManager).getContextMenuManager() == null) { >+ handleContextMenu(event); >+ } >+ } >+ } >+ }); >+ } >+ >+ // Handle for chevron clicking >+ if (getUseChevron()) { >+ // Chevron Support >+ coolItem.addSelectionListener(new SelectionAdapter() { >+ >+ public void widgetSelected(SelectionEvent event) { >+ if (event.detail == SWT.ARROW) { >+ handleChevron(event); >+ } >+ } >+ }); >+ } >+ >+ // Handle for disposal >+ coolItem.addDisposeListener(new DisposeListener() { >+ >+ public void widgetDisposed(DisposeEvent event) { >+ handleWidgetDispose(event); >+ } >+ }); >+ >+ // Sets the size of the coolItem >+ updateSize(true); >+ } >+ } >+ >+ public void fill(SToolBar parent, int index) { >+ // Do nothing >+ } >+ >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/HannoverGlobalToolBarSkin.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/HannoverGlobalToolBarSkin.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/HannoverGlobalToolBarSkin.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/HannoverGlobalToolBarSkin.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,207 @@ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.graphics.Color; >+import org.eclipse.swt.graphics.Font; >+import org.eclipse.swt.graphics.FontData; >+import org.eclipse.swt.graphics.GC; >+import org.eclipse.swt.graphics.Image; >+import org.eclipse.swt.graphics.Point; >+import org.eclipse.swt.graphics.Rectangle; >+import org.eclipse.swt.widgets.Control; >+import org.eclipse.swt.widgets.Display; >+ >+public class HannoverGlobalToolBarSkin implements Skin { >+ >+ // border colors >+ Color borderTopColor = new Color(null, 255, 255, 255); >+ Color borderBottomColor = new Color(null, 144, 161, 181); >+ Color borderColor = new Color(null, 132, 132, 132); >+ Color borderLeftRightColor = new Color(null, 255, 255, 255); >+ Color itemBorderColor = new Color(null, 180, 180, 180); >+ >+ // background colors >+ Color shadowColor = new Color(null, 109, 131, 180); >+ Color backgroundTopColor = new Color(null, 247, 247, 247); >+ Color backgroundBottomColor = new Color(null, 189, 198, 216); >+ Color hoverItemBackgroundColor = new Color(null, 224, 233, 237); >+ Color pressedItemBackgroundColor = new Color(null, 231, 231, 231); >+ >+ // arrows and text colors >+ Color arrowColor = Display.getCurrent().getSystemColor(SWT.COLOR_BLACK); >+ Color textColor = new Color(null, 43,73,111); >+ Color hoverTextColor = new Color(null, 20, 38, 54); >+ >+ Color separatorColor = new Color(null, 168, 181, 197); >+ Font textFont; >+ >+ public HannoverGlobalToolBarSkin() { >+ super(); >+ FontData fd = new FontData("Tahoma", 9, SWT.NORMAL); >+ textFont = new Font(null, fd); >+ } >+ >+ /** >+ * @deprecated use getRect() instead. >+ */ >+ public Rectangle getInsets(int type, int state) { >+ return null; >+ } >+ >+ /** >+ * @deprecated use getRect() instead. >+ */ >+ public Rectangle getMargins(int type, int state) { >+ return new Rectangle(0,0,0,0); >+ } >+ >+ public Font getFont(int type, int state) { >+ return textFont; >+ } >+ >+ public Point getSize(int type, int state) { >+ if (type == Skin.TYPE_ITEM_MIN_MAX_HEIGHT) { >+ return new Point(SWT.DEFAULT, SWT.DEFAULT); >+ } else if (type == Skin.TYPE_ITEM_MIN_MAX_WIDTHS) { >+ return new Point(SWT.DEFAULT, SWT.DEFAULT); >+ } else if (type == Skin.TYPE_DECORATION_ARROW) { >+ return new Point(5, 3); >+ } else if (type == Skin.TYPE_DECORATION_SEPARATOR) { >+ return new Point(3, SWT.DEFAULT); >+ } else if (type == Skin.TYPE_ITEM_ICON_SIZE) { >+ return new Point(20, 20); >+ } >+ return null; >+ } >+ >+ public Rectangle getRect(int type, int state) { >+ if (type == Skin.TYPE_ITEM_AREA_INSETS) { >+ /* >+ * x - leading space before first item. >+ * y - space between top of toolbar and top of item >+ * width - trailing space after last item >+ * height - space between bottom of toolbar and bottom of item. >+ */ >+ return new Rectangle(1, 3, 1, 3); >+ } else if (type == Skin.TYPE_ITEM_AREA_MARGINS) { >+ /* >+ * x - space between items >+ * y - undefined >+ * width - undefined; >+ * height - undefined; >+ */ >+ return new Rectangle(5, 0, 0, 0); >+ } else if (type == Skin.TYPE_ITEM_MARGINS) { >+ /* >+ * x = horizontal space between image and text. >+ * y = horizontal space between image or text and hinky. >+ * width = undefined >+ * height = undefined >+ */ >+ return new Rectangle(3, 2, 0, 0); >+ } else if (type == Skin.TYPE_ITEM_INSETS) { >+ /* >+ * x = left edge of tool item to left edge of image or text >+ * y = top edge of tool item to top edge of image or text >+ * width = right edge of last internal element and right edge of tool item. >+ * height = bottom edge of last internal element and bottom edge of tool item. >+ */ >+ return new Rectangle(0, 0, 0, 0); >+ } >+ >+ return new Rectangle(0, 0, 0, 0); >+ } >+ >+ public void drawBorder(GC gc, Rectangle rect, SWidget widget, int type, int state) { >+ if (type == Skin.TYPE_BORDER) { >+ // top edge >+ gc.setForeground(borderTopColor); >+ gc.drawLine(rect.x, rect.y, rect.x+rect.width, rect.y); >+ // bottom edge >+ gc.setForeground(borderBottomColor); >+ gc.drawLine(rect.x, rect.y+rect.height-1, rect.x+rect.width, rect.y+rect.height-1); >+ } >+ >+ } >+ >+ public void drawBackground(GC gc, Rectangle rect, SWidget widget, int type, int state) { >+ if (type == Skin.TYPE_BACKGROUND) { >+ if (widget instanceof Control) { >+ gc.setForeground(backgroundTopColor); >+ gc.setBackground(backgroundBottomColor); >+ gc.fillGradientRectangle(rect.x, rect.y-1, rect.width, rect.height+1, true); >+ } >+ } >+ } >+ >+ public void drawDecoration(GC gc, Rectangle rect, SWidget widget, int type, int state) { >+ if (type == Skin.TYPE_DECORATION_ARROW) { >+ int delta = 0; >+ if ((state & Skin.PRESSED) == Skin.PRESSED) { >+ delta = 1; >+ } >+ gc.setForeground(arrowColor); >+ gc.drawLine(rect.x + delta, rect.y + delta, rect.x + rect.width -1 + delta, rect.y + delta); >+ gc.drawLine(rect.x + 1 + delta, rect.y + 1 + delta, rect.x + rect.width -2 + delta, rect.y + 1 + delta); >+ gc.drawPoint(rect.x + 2 + delta, rect.y + 2 + delta); >+ } else if (type == Skin.TYPE_DECORATION_SEPARATOR) { >+ gc.setForeground(separatorColor); >+ gc.drawLine(rect.x + (rect.width /2), rect.y, rect.x + (rect.width /2), rect.y + rect.height - 1); >+ } >+ } >+ >+ public void drawText(GC gc, Rectangle rect, String text, int style, SWidget widget, int type, int state) { >+ int delta = 0; >+ if ((state & Skin.PRESSED) == Skin.PRESSED) { >+ delta = 1; >+ gc.setForeground(textColor); >+ } >+ else if ((state & Skin.HOVER) == Skin.HOVER) { >+ gc.setForeground(textColor); >+ } >+ else { >+ gc.setForeground(textColor); >+ } >+ gc.drawText(text, rect.x + delta, rect.y + delta, style); >+ } >+ >+ public void drawImage(GC gc, Rectangle rect, Image image, SWidget widget, int type, int state) { >+ Rectangle imageRect = image.getBounds(); >+ gc.drawImage(image, imageRect.x, imageRect.y, imageRect.width, imageRect.height, >+ rect.x, rect.y, rect.width, rect.height); >+ } >+ >+ public void dispose() { >+ if (borderColor != null && borderColor.isDisposed() == false) >+ borderColor.dispose(); >+ if (itemBorderColor != null && itemBorderColor.isDisposed() == false) >+ itemBorderColor.dispose(); >+ >+ if (shadowColor != null && shadowColor.isDisposed() == false) >+ shadowColor.dispose(); >+ if (backgroundTopColor != null && backgroundTopColor.isDisposed() == false) >+ backgroundTopColor.dispose(); >+ if (backgroundBottomColor != null && backgroundBottomColor.isDisposed() == false) >+ backgroundBottomColor.dispose(); >+ if (hoverItemBackgroundColor != null && hoverItemBackgroundColor.isDisposed() == false) >+ hoverItemBackgroundColor.dispose(); >+ if (pressedItemBackgroundColor != null && pressedItemBackgroundColor.isDisposed() == false) >+ pressedItemBackgroundColor.dispose(); >+ >+ if (arrowColor != null && arrowColor.isDisposed() == false) >+ arrowColor.dispose(); >+ if (textColor != null && textColor.isDisposed() == false) >+ textColor.dispose(); >+ if (hoverTextColor != null && hoverTextColor.isDisposed() == false) >+ hoverTextColor.dispose(); >+ >+ if (separatorColor != null) { >+ separatorColor.dispose(); >+ } >+ >+ if (textFont != null) { >+ textFont.dispose(); >+ textFont = null; >+ } >+ } >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/CustomToolBarPresentation.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/CustomToolBarPresentation.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/CustomToolBarPresentation.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/CustomToolBarPresentation.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,48 @@ >+package org.eclipse.ui.examples.presentation.customtoolbar; >+ >+import org.eclipse.jface.action.ICoolBarManager; >+import org.eclipse.jface.action.IToolBarManager; >+import org.eclipse.jface.action.IToolBarManager2; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Control; >+import org.eclipse.ui.examples.presentation.customtoolbar.widgets.SCoolBarManager; >+import org.eclipse.ui.examples.presentation.customtoolbar.widgets.SToolBarManager2; >+import org.eclipse.ui.presentations.ActionBarPresentation; >+ >+/** >+ * Demonstrates how to customize the tool bars in an RCP application. >+ * >+ */ >+class CustomToolBarPresentation extends ActionBarPresentation { >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.presentations.ToolBarPresentation#createCoolBarManager(int) >+ */ >+ public ICoolBarManager createCoolBarManager(int style) { >+ SCoolBarManager coolBarManager = new SCoolBarManager(style); >+ return coolBarManager; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.presentations.ToolBarPresentation#createToolBarManager(int) >+ */ >+ public IToolBarManager createToolBarManager(int style) { >+ SToolBarManager2 toolBarManager = new SToolBarManager2(style); >+ return toolBarManager; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.presentations.ToolBarPresentation#createCoolBarControl(org.eclipse.jface.action.ICoolBarManager, org.eclipse.swt.widgets.Composite) >+ */ >+ public Control createCoolBarControl(ICoolBarManager coolBarManager, Composite parent) { >+ return super.createCoolBarControl(coolBarManager, parent); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.presentations.ToolBarPresentation#createToolBarControl(org.eclipse.jface.action.IToolBarManager2, org.eclipse.swt.widgets.Composite) >+ */ >+ public Control createToolBarControl(IToolBarManager2 toolBarManager, Composite parent) { >+ return super.createToolBarControl(toolBarManager, parent); >+ } >+ >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolBarManager2.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolBarManager2.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolBarManager2.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SToolBarManager2.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,411 @@ >+/** >+ * >+ */ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import java.util.ArrayList; >+import java.util.Iterator; >+ >+import org.eclipse.jface.action.ActionContributionItem; >+import org.eclipse.jface.action.ContributionManager; >+import org.eclipse.jface.action.IAction; >+import org.eclipse.jface.action.IContributionItem; >+import org.eclipse.jface.action.IToolBarManager; >+import org.eclipse.jface.action.MenuManager; >+import org.eclipse.jface.action.Separator; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.accessibility.ACC; >+import org.eclipse.swt.accessibility.AccessibleAdapter; >+import org.eclipse.swt.accessibility.AccessibleEvent; >+import org.eclipse.swt.accessibility.AccessibleListener; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Control; >+import org.eclipse.swt.widgets.Menu; >+import org.eclipse.swt.widgets.ToolBar; >+import org.eclipse.swt.widgets.ToolItem; >+ >+public class SToolBarManager2 extends ContributionManager implements IToolBarManager { >+ >+ /** >+ * The tool bar items style; <code>SWT.NONE</code> by default. >+ */ >+ private int itemStyle = SWT.NONE; >+ >+ /** >+ * The tool bat control; <code>null</code> before creation and after >+ * disposal. >+ */ >+ private Control toolBar = null; >+ >+ /** >+ * The menu manager to the context menu associated with the toolbar. >+ * >+ * @since 3.0 >+ */ >+ private MenuManager contextMenuManager = null; >+ >+ /** >+ * Creates a new tool bar manager with the default SWT button style. Use the >+ * <code>createControl</code> method to create the tool bar control. >+ */ >+ public SToolBarManager2() { >+ //Do nothing if there are no parameters >+ } >+ >+ /** >+ * Creates a tool bar manager with the given SWT button style. Use the >+ * <code>createControl</code> method to create the tool bar control. >+ * >+ * @param style >+ * the tool bar item style >+ * @see org.eclipse.swt.widgets.ToolBar for valid style bits >+ */ >+ public SToolBarManager2(int style) { >+ itemStyle = style; >+ } >+ >+ /** >+ * Creates a tool bar manager for an existing tool bar control. This manager >+ * becomes responsible for the control, and will dispose of it when the >+ * manager is disposed. >+ * >+ * @param toolbar >+ * the tool bar control >+ */ >+ public SToolBarManager2(ToolBar toolbar) { >+ this(); >+ this.toolBar = toolbar; >+ } >+ >+ public void add(IAction action) { >+ super.add(new SActionContributionItem(action)); >+ } >+ >+ public void add(IContributionItem item) { >+ if (item instanceof Separator) { >+ Separator separator = (Separator)item; >+ String label = separator.getGroupName(); >+ SActionContributionItem sItem = new SActionContributionItem(); >+ IAction action = sItem.getAction(); >+ action.setText(label); >+ item = sItem; >+ } >+ if (item instanceof ActionContributionItem) { >+ SActionContributionItem proxyItem = >+ new SActionContributionItem(((ActionContributionItem)item).getAction()); >+ item = proxyItem; >+ } >+ super.add(item); >+ } >+ >+ /** >+ * Creates and returns this manager's tool bar control. Does not create a >+ * new control if one already exists. >+ * >+ * @param parent >+ * the parent control >+ * @return the tool bar control >+ */ >+ public Control createControl(Composite parent) { >+ if (!toolBarExist() && parent != null) { >+ toolBar = new SToolBar(parent, itemStyle); >+ ((SToolBar)toolBar).setSkin(new HannoverGlobalToolBarSkin()); >+ toolBar.setMenu(getContextMenuControl()); >+ update(false); >+ >+ toolBar.getAccessible().addAccessibleListener(getAccessibleListener()); >+ } >+ return toolBar; >+ } >+ >+ /** >+ * Get the accessible listener for the tool bar. >+ * >+ * @return AccessibleListener >+ * >+ * @since 3.1 >+ */ >+ private AccessibleListener getAccessibleListener() { >+ return new AccessibleAdapter() { >+ public void getName(AccessibleEvent e) { >+ if (e.childID != ACC.CHILDID_SELF) { >+ SToolItem item = ((SToolBar)toolBar).getItem(e.childID); >+ if (item != null) { >+ String toolTip = item.getToolTipText(); >+ if (toolTip != null) { >+ e.result = toolTip; >+ } >+ } >+ } >+ } >+ }; >+ >+ } >+ >+ /** >+ * Disposes of this tool bar manager and frees all allocated SWT resources. >+ * Notifies all contribution items of the dispose. Note that this method >+ * does not clean up references between this tool bar manager and its >+ * associated contribution items. Use <code>removeAll</code> for that >+ * purpose. >+ */ >+ public void dispose() { >+ >+ if (toolBarExist()) { >+ toolBar.dispose(); >+ } >+ toolBar = null; >+ >+ IContributionItem[] items = getItems(); >+ for (int i = 0; i < items.length; i++) { >+ items[i].dispose(); >+ } >+ >+ if (getContextMenuManager() != null) { >+ getContextMenuManager().dispose(); >+ setContextMenuManager(null); >+ } >+ } >+ >+ /** >+ * Returns the tool bar control for this manager. >+ * >+ * @return the tool bar control, or <code>null</code> if none (before >+ * creating or after disposal) >+ */ >+ public Control getControl() { >+ return toolBar; >+ } >+ >+ /** >+ * Re-lays out the tool bar. >+ * <p> >+ * The default implementation of this framework method re-lays out the >+ * parent when the number of items crosses the zero threshold. Subclasses >+ * should override this method to implement their own re-layout strategy >+ * >+ * @param layoutBar >+ * the tool bar control >+ * @param oldCount >+ * the old number of items >+ * @param newCount >+ * the new number of items >+ */ >+ protected void relayout(SToolBar layoutBar, int oldCount, int newCount) { >+ if ((oldCount == 0) != (newCount == 0)) >+ layoutBar.getParent().layout(); >+ } >+ >+ /** >+ * Returns whether the tool bar control is created and not disposed. >+ * >+ * @return <code>true</code> if the control is created and not disposed, >+ * <code>false</code> otherwise >+ */ >+ private boolean toolBarExist() { >+ return toolBar != null && !toolBar.isDisposed(); >+ } >+ >+ /* >+ * (non-Javadoc) Method declared on IContributionManager. >+ */ >+ public void update(boolean force) { >+ >+ // long startTime= 0; >+ // if (DEBUG) { >+ // dumpStatistics(); >+ // startTime= (new Date()).getTime(); >+ // } >+ >+ if (isDirty() || force) { >+ >+ if (toolBarExist()) { >+ >+ int oldCount = ((SToolBar)toolBar).getItemCount(); >+ >+ // clean contains all active items without double separators >+ IContributionItem[] items = getItems(); >+ ArrayList clean = new ArrayList(items.length); >+ IContributionItem separator = null; >+ // long cleanStartTime= 0; >+ // if (DEBUG) { >+ // cleanStartTime= (new Date()).getTime(); >+ // } >+ for (int i = 0; i < items.length; ++i) { >+ IContributionItem ci = items[i]; >+ if (!ci.isVisible()) >+ continue; >+ if (ci.isSeparator()) { >+ // delay creation until necessary >+ // (handles both adjacent separators, and separator at >+ // end) >+ separator = ci; >+ } else { >+ if (separator != null) { >+ if (clean.size() > 0) // no separator if first item >+ clean.add(separator); >+ separator = null; >+ } >+ clean.add(ci); >+ } >+ } >+ // if (DEBUG) { >+ // System.out.println(" Time needed to build clean vector: " + >+ // ((new Date()).getTime() - cleanStartTime)); >+ // } >+ >+ // determine obsolete items (removed or non active) >+ SToolItem[] mi = ((SToolBar)toolBar).getItems(); >+ ArrayList toRemove = new ArrayList(mi.length); >+ for (int i = 0; i < mi.length; i++) { >+ Object data = mi[i].getData(); >+ if (data == null >+ || !clean.contains(data) >+ || (data instanceof IContributionItem && ((IContributionItem) data) >+ .isDynamic())) { >+ toRemove.add(mi[i]); >+ } >+ } >+ >+ // Turn redraw off if the number of items to be added >+ // is above a certain threshold, to minimize flicker, >+ // otherwise the toolbar can be seen to redraw after each item. >+ // Do this before any modifications are made. >+ // We assume each contribution item will contribute at least one >+ // toolbar item. >+ boolean useRedraw = (clean.size() - (mi.length - toRemove >+ .size())) >= 3; >+ try { >+ if (useRedraw) { >+ toolBar.setRedraw(false); >+ } >+ >+ // remove obsolete items >+ for (int i = toRemove.size(); --i >= 0;) { >+ ToolItem item = (ToolItem) toRemove.get(i); >+ if (!item.isDisposed()) { >+ Control ctrl = item.getControl(); >+ if (ctrl != null) { >+ item.setControl(null); >+ ctrl.dispose(); >+ } >+ item.dispose(); >+ } >+ } >+ >+ // add new items >+ IContributionItem src, dest; >+ mi = ((SToolBar)toolBar).getItems(); >+ int srcIx = 0; >+ int destIx = 0; >+ for (Iterator e = clean.iterator(); e.hasNext();) { >+ src = (IContributionItem) e.next(); >+ >+ // get corresponding item in SWT widget >+ if (srcIx < mi.length) >+ dest = (IContributionItem) mi[srcIx].getData(); >+ else >+ dest = null; >+ >+ if (dest != null && src.equals(dest)) { >+ srcIx++; >+ destIx++; >+ continue; >+ } >+ >+ if (dest != null && dest.isSeparator() >+ && src.isSeparator()) { >+ mi[srcIx].setData(src); >+ srcIx++; >+ destIx++; >+ continue; >+ } >+ >+ int start = ((SToolBar)toolBar).getItemCount(); >+ ((ISContributionItem)src).fill(((SToolBar)toolBar), destIx); >+ int newItems = ((SToolBar)toolBar).getItemCount() - start; >+ for (int i = 0; i < newItems; i++) { >+ SToolItem item = ((SToolBar)toolBar).getItem(destIx++); >+ item.setData(src); >+ } >+ } >+ >+ // remove any old tool items not accounted for >+ for (int i = mi.length; --i >= srcIx;) { >+ SToolItem item = mi[i]; >+ if (!item.isDisposed()) { >+ Control ctrl = item.getControl(); >+ if (ctrl != null) { >+ item.setControl(null); >+ ctrl.dispose(); >+ } >+ item.dispose(); >+ } >+ } >+ >+ setDirty(false); >+ >+ // turn redraw back on if we turned it off above >+ } finally { >+ if (useRedraw) { >+ toolBar.setRedraw(true); >+ } >+ } >+ >+ int newCount = ((SToolBar)toolBar).getItemCount(); >+ relayout((SToolBar)toolBar, oldCount, newCount); >+ } >+ >+ } >+ >+ // if (DEBUG) { >+ // System.out.println(" Time needed for update: " + ((new >+ // Date()).getTime() - startTime)); >+ // System.out.println(); >+ // } >+ } >+ >+ /** >+ * Returns the control of the Menu Manager. If the menu manager does not >+ * have a control then one is created. >+ * >+ * @return menu widget associated with manager >+ */ >+ private Menu getContextMenuControl() { >+ if ((contextMenuManager != null) && (toolBar != null)) { >+ Menu menuWidget = contextMenuManager.getMenu(); >+ if ((menuWidget == null) || (menuWidget.isDisposed())) { >+ menuWidget = contextMenuManager.createContextMenu(toolBar); >+ } >+ return menuWidget; >+ } >+ return null; >+ } >+ >+ /** >+ * Returns the context menu manager for this tool bar manager. >+ * >+ * @return the context menu manager, or <code>null</code> if none >+ * @since 3.0 >+ */ >+ public MenuManager getContextMenuManager() { >+ return contextMenuManager; >+ } >+ >+ /** >+ * Sets the context menu manager for this tool bar manager to the given menu >+ * manager. If the tool bar control exists, it also adds the menu control to >+ * the tool bar. >+ * >+ * @param contextMenuManager >+ * the context menu manager, or <code>null</code> if none >+ * @since 3.0 >+ */ >+ public void setContextMenuManager(MenuManager contextMenuManager) { >+ this.contextMenuManager = contextMenuManager; >+ if (toolBar != null) { >+ toolBar.setMenu(getContextMenuControl()); >+ } >+ } >+ >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SActionContributionItem.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SActionContributionItem.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SActionContributionItem.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SActionContributionItem.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,627 @@ >+/******************************************************************************* >+ * Copyright (c) 2000, 2005 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import org.eclipse.jface.action.Action; >+import org.eclipse.jface.action.ActionContributionItem; >+import org.eclipse.jface.action.ExternalActionManager; >+import org.eclipse.jface.action.IAction; >+import org.eclipse.jface.action.IContributionManagerOverrides; >+import org.eclipse.jface.action.IMenuCreator; >+import org.eclipse.jface.resource.ImageDescriptor; >+import org.eclipse.jface.resource.JFaceResources; >+import org.eclipse.jface.resource.LocalResourceManager; >+import org.eclipse.jface.resource.ResourceManager; >+import org.eclipse.jface.util.IPropertyChangeListener; >+import org.eclipse.jface.util.Policy; >+import org.eclipse.jface.util.PropertyChangeEvent; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.graphics.Point; >+import org.eclipse.swt.graphics.Rectangle; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.CoolBar; >+import org.eclipse.swt.widgets.Display; >+import org.eclipse.swt.widgets.Event; >+import org.eclipse.swt.widgets.Item; >+import org.eclipse.swt.widgets.Listener; >+import org.eclipse.swt.widgets.Menu; >+import org.eclipse.swt.widgets.ToolBar; >+import org.eclipse.swt.widgets.Widget; >+ >+/** >+ * A contribution item which delegates to an action. >+ * <p> >+ * This class may be instantiated; it is not intended to be subclassed. >+ * </p> >+ */ >+public class SActionContributionItem extends ActionContributionItem implements ISContributionItem { >+ >+ ActionContributionItem actionContributionItem = null; >+ >+ private boolean isSeparator; >+ >+ /** >+ * The listener for changes to the text of the action contributed by an >+ * external source. >+ */ >+ private final IPropertyChangeListener actionTextListener = new IPropertyChangeListener() { >+ >+ /** >+ * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent) >+ */ >+ public void propertyChange(PropertyChangeEvent event) { >+ update(event.getProperty()); >+ } >+ }; >+ >+ /** >+ * Remembers all images in use by this contribution item >+ */ >+ private LocalResourceManager imageManager; >+ >+ private boolean isWidgetSet = false; >+ >+ /** >+ * Listener for SWT tool item widget events. >+ */ >+ private Listener toolItemListener; >+ >+ /** >+ * The widget created for this item; <code>null</code> >+ * before creation and after disposal. >+ */ >+ private Widget widget = null; >+ >+ /** >+ * Listener for action property change notifications. >+ */ >+ private final IPropertyChangeListener propertyListener = new IPropertyChangeListener() { >+ public void propertyChange(PropertyChangeEvent event) { >+ actionPropertyChange(event); >+ } >+ }; >+ >+ public SActionContributionItem() { >+ this(new ActionContributionItem(new Action() {public void run() {}})); >+ this.isSeparator = true; >+ } >+ >+ /** >+ * @param action >+ */ >+ public SActionContributionItem(IAction action) { >+ this(new ActionContributionItem(action)); >+ } >+ >+ /** >+ * >+ * @param action >+ */ >+ public SActionContributionItem(ActionContributionItem actionContributionItem) { >+ super(actionContributionItem.getAction()); >+ this.actionContributionItem = actionContributionItem; >+ } >+ >+ public boolean isSeparator() { >+ return isSeparator; >+ } >+ >+ /** >+ * Handles a property change event on the action (forwarded by nested listener). >+ */ >+ private void actionPropertyChange(final PropertyChangeEvent e) { >+ // This code should be removed. Avoid using free asyncExec >+ >+ if (isVisible() && widget != null) { >+ Display display = widget.getDisplay(); >+ if (display.getThread() == Thread.currentThread()) { >+ update(e.getProperty()); >+ } else { >+ display.asyncExec(new Runnable() { >+ public void run() { >+ update(e.getProperty()); >+ } >+ }); >+ } >+ >+ } >+ } >+ >+ /** >+ * Dispose any images allocated for this contribution item >+ */ >+ private void disposeOldImages() { >+ if (imageManager != null) { >+ imageManager.dispose(); >+ imageManager = null; >+ } >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ActionContributionItem#fill(org.eclipse.swt.widgets.Composite) >+ */ >+ public void fill(Composite parent) { >+ isWidgetSet = true; >+ actionContributionItem.fill(parent); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.CoolBar, int) >+ */ >+ public void fill(CoolBar parent, int index) { >+ if (widget instanceof SToolItem) >+ return; >+ isWidgetSet = true; >+ actionContributionItem.fill(parent, index); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ActionContributionItem#fill(org.eclipse.swt.widgets.Menu, int) >+ */ >+ public void fill(Menu parent, int index) { >+ if (widget instanceof SToolItem) >+ return; >+ isWidgetSet = true; >+ actionContributionItem.fill(parent, index); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ActionContributionItem#fill(org.eclipse.swt.widgets.ToolBar, int) >+ */ >+ public void fill(ToolBar parent, int index) { >+ if (widget instanceof SToolItem) >+ return; >+ isWidgetSet = true; >+ actionContributionItem.fill(parent, index); >+ } >+ >+ public void fill(SCoolBar parent, int index) { >+ // do nothing >+ } >+ >+ public void fill(SToolBar parent, int index) { >+ if (isWidgetSet == false && widget == null && parent != null) { >+ int flags = SWT.PUSH; >+ IAction action = getAction(); >+ if (action != null) { >+ if (isSeparator) { >+ flags = SWT.SEPARATOR; >+ } else { >+ int style = action.getStyle(); >+ if (style == IAction.AS_CHECK_BOX) >+ flags = SWT.CHECK; >+ else if (style == IAction.AS_RADIO_BUTTON) >+ flags = SWT.RADIO; >+ else if (style == IAction.AS_DROP_DOWN_MENU) >+ flags = SWT.DROP_DOWN; >+ } >+ } >+ >+ Item ti = null; >+ if (index >= 0) >+ ti = new SToolItem(parent, flags, index); >+ else >+ ti = new SToolItem(parent, flags); >+ ti.setData(this); >+ ti.addListener(SWT.Selection, getToolItemListener()); >+ ti.addListener(SWT.Dispose, getToolItemListener()); >+ >+ widget = ti; >+ >+ update(null); >+ >+ // Attach some extra listeners. >+ action.addPropertyChangeListener(propertyListener); >+ if (action != null) { >+ String commandId = action.getActionDefinitionId(); >+ ExternalActionManager.ICallback callback = ExternalActionManager >+ .getInstance().getCallback(); >+ >+ if ((callback != null) && (commandId != null)) { >+ callback.addPropertyChangeListener(commandId, >+ actionTextListener); >+ } >+ } >+ } >+ } >+ >+ private Listener getToolItemListener() { >+ if (toolItemListener == null) { >+ toolItemListener = new Listener() { >+ public void handleEvent(Event event) { >+ switch (event.type) { >+ case SWT.Dispose: >+ handleWidgetDispose(event); >+ break; >+ case SWT.Selection: >+ Widget ew = event.widget; >+ if (ew != null) { >+ handleWidgetSelection(event, ((SToolItem) ew) >+ .getSelection()); >+ } >+ break; >+ } >+ } >+ }; >+ } >+ return toolItemListener; >+ } >+ >+ /** >+ * Handles a widget dispose event for the widget corresponding to this item. >+ */ >+ private void handleWidgetDispose(Event e) { >+ // Check if our widget is the one being disposed. >+ if (e.widget == widget) { >+ IAction action = getAction(); >+ // Dispose of the menu creator. >+ if (action.getStyle() == IAction.AS_DROP_DOWN_MENU) { >+ IMenuCreator mc = action.getMenuCreator(); >+ if (mc != null) { >+ mc.dispose(); >+ } >+ } >+ >+ // Unhook all of the listeners. >+ action.removePropertyChangeListener(propertyListener); >+ if (action != null) { >+ String commandId = action.getActionDefinitionId(); >+ ExternalActionManager.ICallback callback = ExternalActionManager >+ .getInstance().getCallback(); >+ >+ if ((callback != null) && (commandId != null)) { >+ callback.removePropertyChangeListener(commandId, >+ actionTextListener); >+ } >+ } >+ >+ // Clear the widget field. >+ widget = null; >+ >+ disposeOldImages(); >+ } >+ } >+ >+ /** >+ * Handles a widget selection event. >+ */ >+ private void handleWidgetSelection(Event e, boolean selection) { >+ >+ Widget item = e.widget; >+ if (item != null) { >+ int style = item.getStyle(); >+ IAction action = getAction(); >+ >+ if ((style & (SWT.TOGGLE | SWT.CHECK)) != 0) { >+ if (action.getStyle() == IAction.AS_CHECK_BOX) { >+ action.setChecked(selection); >+ } >+ } else if ((style & SWT.RADIO) != 0) { >+ if (action.getStyle() == IAction.AS_RADIO_BUTTON) { >+ action.setChecked(selection); >+ } >+ } else if ((style & SWT.DROP_DOWN) != 0) { >+ if (e.detail == 4) { // on drop-down button >+ if (action.getStyle() == IAction.AS_DROP_DOWN_MENU) { >+ IMenuCreator mc = action.getMenuCreator(); >+ SToolItem ti = (SToolItem) item; >+ // we create the menu as a sub-menu of "dummy" so that we can use >+ // it in a cascading menu too. >+ // If created on a SWT control we would get an SWT error... >+ //Menu dummy= new Menu(ti.getParent()); >+ //Menu m= mc.getMenu(dummy); >+ //dummy.dispose(); >+ if (mc != null) { >+ Menu m = mc.getMenu(ti.getParent()); >+ if (m != null) { >+ // position the menu below the drop down item >+ Rectangle b = ti.getBounds(); >+ Point p = ti.getParent().toDisplay( >+ new Point(b.x, b.y + b.height)); >+ m.setLocation(p.x, p.y); // waiting for SWT 0.42 >+ m.setVisible(true); >+ return; // we don't fire the action >+ } >+ } >+ } >+ } >+ } >+ // Ensure action is enabled first. >+ // See 1GAN3M6: ITPUI:WINNT - Any IAction in the workbench can be executed while disabled. >+ if (action.isEnabled()) { >+ boolean trace = Policy.TRACE_ACTIONS; >+ >+ long ms = System.currentTimeMillis(); >+ if (trace) >+ System.out.println("Running action: " + action.getText()); //$NON-NLS-1$ >+ >+ action.runWithEvent(e); >+ >+ if (trace) >+ System.out.println((System.currentTimeMillis() - ms) >+ + " ms to run action: " + action.getText()); //$NON-NLS-1$ >+ } >+ } >+ } >+ >+ /** >+ * Returns whether the given action has any images. >+ * >+ * @param actionToCheck the action >+ * @return <code>true</code> if the action has any images, <code>false</code> if not >+ */ >+ private boolean hasImages(IAction actionToCheck) { >+ return actionToCheck.getImageDescriptor() != null >+ || actionToCheck.getHoverImageDescriptor() != null >+ || actionToCheck.getDisabledImageDescriptor() != null; >+ } >+ >+ /** >+ * Synchronizes the UI with the given property. >+ * >+ * @param propertyName the name of the property, or <code>null</code> meaning all applicable >+ * properties >+ */ >+ public void update(String propertyName) { >+ >+ if (widget instanceof SToolItem) { >+ >+ // determine what to do >+ IAction action = getAction(); >+ boolean textChanged = propertyName == null >+ || propertyName.equals(IAction.TEXT); >+ boolean imageChanged = propertyName == null >+ || propertyName.equals(IAction.IMAGE); >+ boolean tooltipTextChanged = propertyName == null >+ || propertyName.equals(IAction.TOOL_TIP_TEXT); >+ boolean enableStateChanged = propertyName == null >+ || propertyName.equals(IAction.ENABLED) >+ || propertyName >+ .equals(IContributionManagerOverrides.P_ENABLED); >+ boolean checkChanged = (action.getStyle() == IAction.AS_CHECK_BOX || action >+ .getStyle() == IAction.AS_RADIO_BUTTON) >+ && (propertyName == null || propertyName >+ .equals(IAction.CHECKED)); >+ >+ if (widget instanceof SToolItem) { >+ SToolItem ti = (SToolItem) widget; >+ String text = action.getText(); >+ // the set text is shown only if there is no image or if forced by MODE_FORCE_TEXT >+ boolean showText = text != null >+ && ((getMode() & MODE_FORCE_TEXT) != 0 || !hasImages(action)); >+ >+ // only do the trimming if the text will be used >+ if (showText && text != null) { >+ text = Action.removeAcceleratorText(text); >+ text = Action.removeMnemonics(text); >+ } >+ >+ if (textChanged) { >+ String textToSet = showText ? text : ""; //$NON-NLS-1$ >+ boolean rightStyle = (ti.getParent().getStyle() & SWT.RIGHT) != 0; >+ if (rightStyle || !ti.getText().equals(textToSet)) { >+ // In addition to being required to update the text if it >+ // gets nulled out in the action, this is also a workaround >+ // for bug 50151: Using SWT.RIGHT on a ToolBar leaves blank space >+ ti.setText(textToSet); >+ } >+ } >+ >+ if (imageChanged) { >+ // only substitute a missing image if it has no text >+ updateImages(!showText); >+ } >+ >+ if (tooltipTextChanged || textChanged) { >+ String toolTip = action.getToolTipText(); >+ // if the text is showing, then only set the tooltip if different >+ if (!showText || toolTip != null && !toolTip.equals(text)) { >+ ti.setToolTipText(toolTip); >+ } else { >+ ti.setToolTipText(null); >+ } >+ } >+ >+/* FIXME TFS -- Need to keep the buttons enabled for now so they actually look like they work. >+ * [Terry Smith 12/9/2005]. >+ * if (enableStateChanged) { >+ boolean shouldBeEnabled = action.isEnabled() >+ && isEnabledAllowed(); >+ >+ if (ti.getEnabled() != shouldBeEnabled) >+ ti.setEnabled(shouldBeEnabled); >+ } >+*/ >+ if (checkChanged) { >+ boolean bv = action.isChecked(); >+ >+ if (ti.getSelection() != bv) >+ ti.setSelection(bv); >+ } >+ return; >+ } >+ } else { >+ actionContributionItem.update(propertyName); >+ } >+ } >+ >+ /** >+ * Updates the images for this action. >+ * >+ * @param forceImage <code>true</code> if some form of image is compulsory, >+ * and <code>false</code> if it is acceptable for this item to have no image >+ * @return <code>true</code> if there are images for this action, <code>false</code> if not >+ */ >+ private boolean updateImages(boolean forceImage) { >+ >+ ResourceManager parentResourceManager = JFaceResources.getResources(); >+ >+ if (widget instanceof SToolItem) { >+ IAction action = getAction(); >+ if (getUseColorIconsInToolbars()) { >+ ImageDescriptor imageDesc = action.getImageDescriptor(); >+ ImageDescriptor hoverImageDesc = action.getHoverImageDescriptor(); >+ ImageDescriptor disabledImageDesc = action.getDisabledImageDescriptor(); >+ ImageDescriptor pressedImageDesc = null; >+ >+ if (imageDesc == null && forceImage) { >+ imageDesc = ImageDescriptor.getMissingImageDescriptor(); >+ } >+ >+// if (disabledImageDesc == null && imageDesc != null) { >+// disabledImageDesc = ImageDescriptor.createWithFlags(imageDesc, SWT.IMAGE_GRAY); >+// } >+ >+ LocalResourceManager localManager = new LocalResourceManager(parentResourceManager); >+ >+ // performance: more efficient in SWT to set disabled and hot image before regular image >+ ((SToolItem) widget).setDisabledImage(disabledImageDesc != null ? localManager.createImageWithDefault(disabledImageDesc) : null); >+ ((SToolItem) widget).setHotImage(hoverImageDesc != null ? localManager.createImageWithDefault(hoverImageDesc) : null); >+ ((SToolItem) widget).setImage(imageDesc != null ? localManager.createImageWithDefault(imageDesc) : null); >+ disposeOldImages(); >+ imageManager = localManager; >+ >+ return imageDesc != null; >+/* TODO TFS Old Crap >+ ImageDescriptor image = action.getHoverImageDescriptor(); >+ if (image == null) { >+ image = action.getImageDescriptor(); >+ } >+ ImageDescriptor disabledImage = action.getDisabledImageDescriptor(); >+ >+ // Make sure there is a valid image. >+ if (image == null && forceImage) { >+ image = ImageDescriptor.getMissingImageDescriptor(); >+ } >+ >+ LocalResourceManager localManager = new LocalResourceManager(parentResourceManager); >+ >+ // performance: more efficient in SWT to set disabled and hot image before regular image >+ ((SToolItem) widget).setDisabledImage(disabledImage == null ? null : localManager.createImageWithDefault(disabledImage)); >+ ((SToolItem) widget).setImage(image == null ? null : localManager.createImageWithDefault(image)); >+ >+ disposeOldImages(); >+ imageManager = localManager; >+ >+ return image != null; >+*/ >+ } >+ ImageDescriptor image = action.getImageDescriptor(); >+ ImageDescriptor hoverImage = action >+ .getHoverImageDescriptor(); >+ ImageDescriptor disabledImage = action >+ .getDisabledImageDescriptor(); >+ >+ // If there is no regular image, but there is a hover image, >+ // convert the hover image to gray and use it as the regular image. >+ if (image == null && hoverImage != null) { >+ image = ImageDescriptor.createWithFlags(action.getHoverImageDescriptor(), SWT.IMAGE_GRAY); >+ } else { >+ // If there is no hover image, use the regular image as the hover image, >+ // and convert the regular image to gray >+ if (hoverImage == null && image != null) { >+ hoverImage = image; >+ image = ImageDescriptor.createWithFlags(action.getImageDescriptor(), SWT.IMAGE_GRAY); >+ } >+ } >+ >+ // Make sure there is a valid image. >+ if (hoverImage == null && image == null && forceImage) { >+ image = ImageDescriptor.getMissingImageDescriptor(); >+ } >+ >+ // Create a local resource manager to remember the images we've allocated for this tool item >+ LocalResourceManager localManager = new LocalResourceManager(parentResourceManager); >+ >+ // performance: more efficient in SWT to set disabled and hot image before regular image >+ ((SToolItem) widget).setDisabledImage(disabledImage == null? null : localManager.createImageWithDefault(disabledImage)); >+ ((SToolItem) widget).setHotImage(hoverImage == null? null : localManager.createImageWithDefault(hoverImage)); >+ ((SToolItem) widget).setImage(image == null? null : localManager.createImageWithDefault(image)); >+ >+ // Now that we're no longer referencing the old images, clear them out. >+ disposeOldImages(); >+ imageManager = localManager; >+ >+ return image != null; >+ } >+ return false; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ActionContributionItem#equals(java.lang.Object) >+ */ >+ public boolean equals(Object o) { >+ >+ if (!(o instanceof ActionContributionItem)) { >+ return false; >+ } >+ return actionContributionItem.getAction().equals(((ActionContributionItem) o).getAction()); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ActionContributionItem#getAction() >+ */ >+ public IAction getAction() { >+ return actionContributionItem.getAction(); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ActionContributionItem#getMode() >+ */ >+ public int getMode() { >+ return actionContributionItem.getMode(); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ActionContributionItem#hashCode() >+ */ >+ public int hashCode() { >+ return actionContributionItem.hashCode(); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ActionContributionItem#isDynamic() >+ */ >+ public boolean isDynamic() { >+ return actionContributionItem.isDynamic(); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ActionContributionItem#isEnabled() >+ */ >+ public boolean isEnabled() { >+ return actionContributionItem.isEnabled(); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ActionContributionItem#isEnabledAllowed() >+ */ >+ protected boolean isEnabledAllowed() { >+ if (actionContributionItem.getParent() == null) >+ return true; >+ Boolean value = actionContributionItem.getParent().getOverrides().getEnabled(this); >+ return (value == null) ? true : value.booleanValue(); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ActionContributionItem#isVisible() >+ */ >+ public boolean isVisible() { >+ return actionContributionItem.isVisible(); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.action.ActionContributionItem#setMode(int) >+ */ >+ public void setMode(int mode) { >+ actionContributionItem.setMode(mode); >+ } >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/ISContributionItem.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/ISContributionItem.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/ISContributionItem.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/ISContributionItem.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,8 @@ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import org.eclipse.jface.action.IContributionItem; >+ >+public interface ISContributionItem extends IContributionItem { >+ public void fill(SCoolBar parent, int index); >+ public void fill(SToolBar parent, int index); >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SCoolItem.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SCoolItem.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SCoolItem.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/widgets/SCoolItem.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,322 @@ >+package org.eclipse.ui.examples.presentation.customtoolbar.widgets; >+ >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.events.SelectionAdapter; >+import org.eclipse.swt.graphics.GC; >+import org.eclipse.swt.graphics.Image; >+import org.eclipse.swt.graphics.Point; >+import org.eclipse.swt.graphics.Rectangle; >+import org.eclipse.swt.widgets.Control; >+import org.eclipse.swt.widgets.Item; >+ >+public class SCoolItem extends Item { >+ Control control; >+ int preferredHeight = -1; >+ int preferredWidth = -1; >+ int width = -1; >+ int height = -1; >+ //int hIndent = 8; >+ int hIndent = 0; >+ //int vIndent = 8; >+ int vIndent = 0; >+ Rectangle itemBounds = new Rectangle(0, 0, 0, 0); >+ boolean wrap = false; // true if the item should be moved onto the next row. >+ boolean alignment = false; // true if the item and (the items following the items) should be right-aligned. >+ Image arrowImage = null; >+ private SCoolBar parent = null; >+ >+ public SCoolItem(SCoolBar parent, int style) { >+ this(parent, style, parent.getItemCount()); >+ } >+ >+ public SCoolItem(SCoolBar parent, int style, int index) { >+ super(parent, style); >+ this.parent = parent; >+ parent.createItem (this, index); >+ } >+ >+ public void addSelectionListener(SelectionAdapter adapter) { >+ // TODO Auto-generated method stub >+ } >+ >+ public Point computeSize(int wHint, int hHint) { >+ return computeSize(wHint, hHint, true); >+ } >+ >+ public Point computeSize(int wHint, int hHint, boolean changed) { >+ checkWidget(); >+ >+ Skin skin = parent.getSkin(); >+ int width = wHint; >+ int height = hHint; >+ >+ if (control != null && (width == SWT.DEFAULT || height == SWT.DEFAULT)) { >+ Point size = control.computeSize(width, height); >+ if (width == SWT.DEFAULT) { >+ width = size.x; >+ } >+ if (height == SWT.DEFAULT) { >+ height = size.y; >+ } >+ >+ size = null; >+ if (skin != null) { >+ size = skin.getSize(Skin.TYPE_DECORATION_GRIPPER, Skin.NORMAL); >+ } >+ if (size == null) { >+ size = new Point(0, 0); >+ } >+ if ((parent.getStyle() & SWT.VERTICAL) == SWT.VERTICAL) { >+ height += size.x; >+ if (size.y != SWT.DEFAULT) { >+ width = Math.max(width, size.y); >+ } >+ } else { >+ width += size.x; >+ if (size.y != SWT.DEFAULT) { >+ height = Math.max(height, size.y); >+ } >+ } >+ } else { >+ Point minMax = null; >+ if (skin != null) { >+ minMax = skin.getSize(Skin.TYPE_ITEM_MIN_MAX_HEIGHT, Skin.NORMAL); >+ } >+ if (minMax == null) { >+ minMax = new Point(SWT.DEFAULT, SWT.DEFAULT); >+ } >+ >+ if (height == SWT.DEFAULT) { >+ if (minMax.x != SWT.DEFAULT) { >+ height = minMax.x; >+ } else { >+ height = 0; >+ } >+ } >+ >+ minMax = null; >+ if (skin != null) { >+ minMax = skin.getSize(Skin.TYPE_ITEM_MIN_MAX_HEIGHT, Skin.NORMAL); >+ } >+ if (minMax == null) { >+ minMax = new Point(SWT.DEFAULT, SWT.DEFAULT); >+ } >+ if (width == SWT.DEFAULT) { >+ if (minMax.x != SWT.DEFAULT) { >+ width = minMax.x; >+ } else { >+ width = 0; >+ } >+ } >+ } >+ >+ Rectangle borders = null; >+ if (skin != null) { >+ borders = skin.getRect(Skin.TYPE_ITEM_BORDER, Skin.NORMAL); >+ } >+ if (borders == null) { >+ borders = new Rectangle(0, 0, 0, 0); >+ } >+ >+ width += borders.x + borders.width; >+ height += borders.y + borders.height; >+ >+ return new Point(width, height); >+ } >+ >+ public Rectangle getBounds() { >+ return itemBounds; >+ } >+ >+ public Control getControl() { >+ return control; >+ } >+ >+ public Control getParent() { >+ return this.parent; >+ } >+ >+ public Point getPreferredSize() { >+ checkWidget(); >+ int width = preferredWidth; >+ int height = preferredHeight; >+ if (control != null && (width == SWT.DEFAULT || height == SWT.DEFAULT)) { >+ Point size = control.computeSize(width, height); >+ if (width == SWT.DEFAULT) { >+ width = size.x; >+ } >+ if (height == SWT.DEFAULT) { >+ height = size.y; >+ } >+ >+ Skin skin = parent.getSkin(); >+ size = null; >+ if (skin != null) { >+ size = skin.getSize(Skin.TYPE_DECORATION_GRIPPER, Skin.NORMAL); >+ } >+ if (size == null) { >+ size = new Point(0, 0); >+ } >+ if ((parent.getStyle() & SWT.VERTICAL) == SWT.VERTICAL) { >+ height += size.x; >+ if (size.y != SWT.DEFAULT) { >+ width = Math.max(width, size.y); >+ } >+ } else { >+ width += size.x; >+ if (size.y != SWT.DEFAULT) { >+ height = Math.max(height, size.y); >+ } >+ } >+ >+ Rectangle borders = null; >+ if (skin != null) { >+ borders = skin.getRect(Skin.TYPE_ITEM_BORDER, Skin.NORMAL); >+ } >+ if (borders == null) { >+ borders = new Rectangle(0, 0, 0, 0); >+ } >+ >+ width += borders.x + borders.width; >+ height += borders.y + borders.height; >+ } >+ >+ return new Point(width, height); >+ } >+ >+ public Point getSize() { >+ checkWidget(); >+ if (itemBounds == null) >+ itemBounds = new Rectangle(0,0,0,0); >+ return new Point(itemBounds.width, itemBounds.height); >+ } >+ >+ public void setBounds(int x, int y, int width, int height) { >+ itemBounds.x = x; >+ itemBounds.y = y; >+ itemBounds.width = width; >+ itemBounds.height = height; >+ >+ if (control != null) { >+ Skin skin = parent.getSkin(); >+ Point size = null; >+ if (skin != null) { >+ size = skin.getSize(Skin.TYPE_DECORATION_GRIPPER, Skin.NORMAL); >+ } >+ if (size == null) { >+ size = new Point(0, 0); >+ } >+ >+ if (width > size.x) { >+ width -= size.x; >+ } else { >+ width = size.x; >+ } >+ >+ Rectangle borders = null; >+ if (skin != null) { >+ borders = skin.getRect(Skin.TYPE_ITEM_BORDER, Skin.NORMAL); >+ } >+ if (borders == null) { >+ borders = new Rectangle(0, 0, 0, 0); >+ } >+ >+ width -= (borders.x + borders.width); >+ height -= (borders.y + borders.height); >+ >+ Rectangle controlRect = new Rectangle(itemBounds.x + size.x, y, width - size.x, height); >+ control.setBounds(controlRect); >+ } >+ } >+ >+ public void setControl(Control control) { >+ if (control != null) { >+ if (control.isDisposed()) >+ SWT.error (SWT.ERROR_INVALID_ARGUMENT); >+ if (control.getParent() != parent) >+ SWT.error (SWT.ERROR_INVALID_PARENT); >+ } >+ this.control = control; >+ >+ parent.layout(); >+ } >+ >+ public void setMinimumSize(int width, int height) { >+ // TODO Auto-generated method stub >+ } >+ >+ public void setPreferredSize(Point preferredSize) { >+ setPreferredSize(preferredSize.x, preferredSize.y); >+ } >+ >+ public void setPreferredSize (int width, int height) { >+ preferredWidth = width; >+ preferredHeight = height; >+ } >+ >+ public void setSize(int width, int height) { >+ checkWidget(); >+ itemBounds.width = width; >+ itemBounds.height = height; >+ >+ if (preferredWidth == -1) >+ preferredWidth = width; >+ if (preferredHeight == -1) >+ preferredHeight = height; >+ >+// if (control != null) { >+// control.setSize(width - SCoolBar.GRIPPER_AREA_WIDTH, height); >+// } >+// TFS - parent.layout()? >+ } >+ >+ public void setSize(Point pt) { >+ checkWidget(); >+ if (pt == null) { >+ SWT.error (SWT.ERROR_NULL_ARGUMENT); >+ } >+ setSize(pt.x, pt.y); >+ } >+ >+ public void paint(GC gc, int state) { >+ Skin skin = parent.getSkin(); >+ >+ if (skin == null) { >+ return; >+ } >+ >+ skin.drawBackground(gc, itemBounds, parent, Skin.TYPE_ITEM_BACKGROUND, state); >+ skin.drawBorder(gc, itemBounds, parent, Skin.TYPE_ITEM_BORDER, state); >+ >+ Point size = skin.getSize(Skin.TYPE_DECORATION_GRIPPER, state); >+ if (size == null) { >+ return; >+ } >+ >+ if ((parent.getStyle() & SWT.VERTICAL) == SWT.VERTICAL) { >+ if (size.y == SWT.DEFAULT) { >+ size.y = 0; >+ } >+ if (size.x == SWT.DEFAULT) { >+ size.x = itemBounds.width; >+ } >+ >+ } else { >+ if (size.x == SWT.DEFAULT) { >+ size.x = 0; >+ } >+ if (size.y == SWT.DEFAULT) { >+ size.y = itemBounds.height; >+ } >+ } >+ >+ if (size.x == 0 || size.y == 0) { >+ return; >+ } >+ >+ Rectangle gripperRect = new Rectangle(itemBounds.x, itemBounds.y, size.x, size.y); >+ skin.drawDecoration(gc, gripperRect, parent, Skin.TYPE_DECORATION_GRIPPER, state); >+ >+ } >+ >+} >Index: src/org/eclipse/ui/examples/presentation/customtoolbar/CustomToolBarPresentationFactory.java >=================================================================== >RCS file: src/org/eclipse/ui/examples/presentation/customtoolbar/CustomToolBarPresentationFactory.java >diff -N src/org/eclipse/ui/examples/presentation/customtoolbar/CustomToolBarPresentationFactory.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/examples/presentation/customtoolbar/CustomToolBarPresentationFactory.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,13 @@ >+package org.eclipse.ui.examples.presentation.customtoolbar; >+ >+import org.eclipse.ui.IWorkbenchWindow; >+import org.eclipse.ui.presentations.ActionBarPresentation; >+import org.eclipse.ui.presentations.WorkbenchPresentationFactory; >+ >+public class CustomToolBarPresentationFactory extends WorkbenchPresentationFactory { >+ >+ public ActionBarPresentation createToolBarPresentation(IWorkbenchWindow window) { >+ return new CustomToolBarPresentation(); >+ } >+ >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 123257
:
33169
|
33171
|
33172
|
33173
|
34107
|
34108
|
34109
|
34110
|
34111
|
34239
|
34308
|
34367
|
34373
|
34437