01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal;
11:
12: import org.eclipse.jface.action.ContributionItem;
13: import org.eclipse.jface.action.MenuManager;
14: import org.eclipse.swt.SWT;
15: import org.eclipse.swt.events.DisposeEvent;
16: import org.eclipse.swt.events.DisposeListener;
17: import org.eclipse.swt.events.SelectionAdapter;
18: import org.eclipse.swt.events.SelectionEvent;
19: import org.eclipse.swt.graphics.Image;
20: import org.eclipse.swt.graphics.Point;
21: import org.eclipse.swt.graphics.Rectangle;
22: import org.eclipse.swt.widgets.Menu;
23: import org.eclipse.swt.widgets.ToolBar;
24: import org.eclipse.swt.widgets.ToolItem;
25: import org.eclipse.ui.IWorkbenchWindow;
26: import org.eclipse.ui.actions.ContributionItemFactory;
27:
28: public class PerspectiveBarNewContributionItem extends ContributionItem {
29:
30: private MenuManager menuManager = null;
31:
32: private Image image;
33:
34: private ToolItem toolItem = null;
35:
36: public PerspectiveBarNewContributionItem(
37: IWorkbenchWindow workbenchWindow) {
38: super (PerspectiveBarNewContributionItem.class.getName());
39: menuManager = new MenuManager();
40: menuManager.add(ContributionItemFactory.PERSPECTIVES_SHORTLIST
41: .create(workbenchWindow));
42: }
43:
44: /* (non-Javadoc)
45: * @see org.eclipse.jface.action.ContributionItem#dispose()
46: */
47: public void dispose() {
48: super .dispose();
49: if (image != null && !image.isDisposed()) {
50: image.dispose();
51: image = null;
52: }
53: }
54:
55: public void fill(final ToolBar parent, int index) {
56: if (toolItem == null && parent != null) {
57: parent.addDisposeListener(new DisposeListener() {
58: public void widgetDisposed(DisposeEvent e) {
59: //toolItem.getImage().dispose();
60: toolItem.dispose();
61: toolItem = null;
62: }
63: });
64:
65: toolItem = new ToolItem(parent, SWT.PUSH);
66: if (image == null || image.isDisposed()) {
67: image = WorkbenchImages.getImageDescriptor(
68: IWorkbenchGraphicConstants.IMG_ETOOL_NEW_PAGE)
69: .createImage();
70: }
71: toolItem.setImage(image);
72:
73: toolItem.setText(""); //$NON-NLS-1$
74: toolItem
75: .setToolTipText(WorkbenchMessages.PerspectiveBarNewContributionItem_toolTip);
76: toolItem.addSelectionListener(new SelectionAdapter() {
77:
78: public void widgetSelected(SelectionEvent event) {
79: menuManager.update(true);
80: Point point = new Point(event.x, event.y);
81: if (event.widget instanceof ToolItem) {
82: ToolItem toolItem = (ToolItem) event.widget;
83: Rectangle rectangle = toolItem.getBounds();
84: point = new Point(rectangle.x, rectangle.y
85: + rectangle.height);
86: }
87: Menu menu = menuManager.createContextMenu(parent);
88: point = parent.toDisplay(point);
89: menu.setLocation(point.x, point.y);
90: menu.setVisible(true);
91: }
92: });
93: }
94: }
95: }
|