KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > PerspectiveBarNewContributionItem


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import org.eclipse.jface.action.ContributionItem;
14 import org.eclipse.jface.action.MenuManager;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.DisposeEvent;
17 import org.eclipse.swt.events.DisposeListener;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.widgets.Menu;
24 import org.eclipse.swt.widgets.ToolBar;
25 import org.eclipse.swt.widgets.ToolItem;
26 import org.eclipse.ui.IWorkbenchWindow;
27 import org.eclipse.ui.actions.ContributionItemFactory;
28
29 public class PerspectiveBarNewContributionItem extends ContributionItem {
30
31     private MenuManager menuManager = null;
32
33     private Image image;
34
35     private ToolItem toolItem = null;
36
37     public PerspectiveBarNewContributionItem(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.setToolTipText(WorkbenchMessages.PerspectiveBarNewContributionItem_toolTip);
75             toolItem.addSelectionListener(new SelectionAdapter() {
76
77                 public void widgetSelected(SelectionEvent event) {
78                     menuManager.update(true);
79                     Point point = new Point(event.x, event.y);
80                     if (event.widget instanceof ToolItem) {
81                         ToolItem toolItem = (ToolItem) event.widget;
82                         Rectangle rectangle = toolItem.getBounds();
83                         point = new Point(rectangle.x, rectangle.y
84                                 + rectangle.height);
85                     }
86                     Menu menu = menuManager.createContextMenu(parent);
87                     point = parent.toDisplay(point);
88                     menu.setLocation(point.x, point.y);
89                     menu.setVisible(true);
90                 }
91             });
92         }
93     }
94 }
95
Popular Tags