KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.IContributionItem;
14 import org.eclipse.jface.action.ToolBarManager;
15 import org.eclipse.jface.resource.JFaceResources;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.graphics.Rectangle;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.CoolBar;
25 import org.eclipse.swt.widgets.CoolItem;
26 import org.eclipse.swt.widgets.Menu;
27 import org.eclipse.swt.widgets.MenuItem;
28 import org.eclipse.swt.widgets.ToolBar;
29 import org.eclipse.swt.widgets.ToolItem;
30 import org.eclipse.ui.internal.layout.LayoutUtil;
31
32 public class PerspectiveBarManager extends ToolBarManager {
33
34     /**
35      * The symbolic font name for the small font (value <code>"org.eclipse.jface.smallfont"</code>).
36      */

37     public static final String JavaDoc SMALL_FONT = "org.eclipse.ui.smallFont"; //$NON-NLS-1$
38

39     public PerspectiveBarManager(int style) {
40         super(style);
41     }
42
43     public ToolBar createControl(Composite parent) {
44         ToolBar control = super.createControl(parent);
45
46         if (control != null && !control.isDisposed()) {
47             control.setFont(getFont());
48         }
49
50         return control;
51     }
52
53     // TODO begin refactor this out? it is not good that we know we are inside a
54
// CoolBar
55
private CoolBar coolBar;
56     private Menu chevronMenu = null;
57     
58     public void handleChevron(SelectionEvent event) {
59         CoolItem item = (CoolItem) event.widget;
60         //ToolBar toolbar = (ToolBar)getControl();
61
Control control = getControl();
62         if (!(control instanceof ToolBar)) {
63             return; // currently we only deal with toolbar items
64
}
65         /* Retrieve the current bounding rectangle for the selected cool item. */
66         Rectangle itemBounds = item.getBounds();
67         /* Convert to display coordinates (i.e. was relative to CoolBar). */
68         Point pt = coolBar.toDisplay(new Point(itemBounds.x, itemBounds.y));
69         itemBounds.x = pt.x;
70         itemBounds.y = pt.y;
71         /* Retrieve the total number of buttons in the toolbar. */
72         ToolBar toolBar = (ToolBar) control;
73         ToolItem[] tools = toolBar.getItems();
74         int toolCount = tools.length;
75         int i = 0;
76         while (i < toolCount) {
77             /*
78              * Starting from the leftmost tool, retrieve the tool's bounding
79              * rectangle.
80              */

81             Rectangle toolBounds = tools[i].getBounds();
82             /* Convert to display coordinates (i.e. was relative to ToolBar). */
83             pt = toolBar.toDisplay(new Point(toolBounds.x, toolBounds.y));
84             toolBounds.x = pt.x;
85             toolBounds.y = pt.y;
86             /*
87              * Figure out the visible portion of the tool by looking at the
88              * intersection of the tool bounds with the cool item bounds.
89              */

90             Rectangle intersection = itemBounds.intersection(toolBounds);
91             /*
92              * If the tool is not completely within the cool item bounds, then
93              * the tool is at least partially hidden, and all remaining tools
94              * are completely hidden.
95              */

96             if (!intersection.equals(toolBounds)) {
97                 break;
98             }
99             i++;
100         }
101         
102         /* Create a pop-up menu with items for each of the hidden buttons. */
103         // Out with the old...
104
if (chevronMenu != null && !chevronMenu.isDisposed())
105             chevronMenu.dispose();
106         
107         // ...In with the new
108
chevronMenu = new Menu(coolBar);
109
110         for (int j = i; j < toolCount; j++) {
111             ToolItem tool = tools[j];
112             MenuItem menuItem = new MenuItem(chevronMenu, SWT.NONE);
113             if (tool.getSelection()) {
114                 menuItem.setEnabled(false);
115             }
116             
117             // Show the full text of the perspective name in the chevron menu.
118
if (tool.getData() instanceof PerspectiveBarContributionItem) {
119                 menuItem.setText(((PerspectiveBarContributionItem) tool
120                         .getData()).getPerspective().getLabel());
121             } else {
122                 menuItem.setText(tool.getText());
123             }
124             menuItem.setImage(tool.getImage());
125
126             menuItem.setData("IContributionItem", tool.getData()); //$NON-NLS-1$
127

128             menuItem.addSelectionListener(new SelectionAdapter() {
129                 public void widgetSelected(SelectionEvent e) {
130                     //rotate the selected item in and the other items right
131
// don't touch the "Open" item
132
MenuItem menuItem = (MenuItem) e.widget;
133                     Object JavaDoc item = menuItem.getData("IContributionItem"); //$NON-NLS-1$
134
if (item instanceof PerspectiveBarContributionItem) {
135                         PerspectiveBarContributionItem contribItem = (PerspectiveBarContributionItem) item;
136                         update(false);
137                         contribItem.select();
138                     }
139                 }
140             });
141         }
142         /*
143          * Display the pop-up menu immediately below the chevron, with the left
144          * edges aligned. Need to convert the given point to display
145          * coordinates in order to pass them to Menu.setLocation (i.e. was
146          * relative to CoolBar).
147          */

148         pt = coolBar.toDisplay(new Point(event.x, event.y));
149         chevronMenu.setLocation(pt.x, pt.y);
150         chevronMenu.setVisible(true);
151     }
152
153     /* (non-Javadoc)
154      * @see org.eclipse.jface.action.ToolBarManager#relayout(org.eclipse.swt.widgets.ToolBar, int, int)
155      */

156     protected void relayout(ToolBar toolBar, int oldCount, int newCount) {
157         super.relayout(toolBar, oldCount, newCount);
158
159         if (getControl() != null) {
160             LayoutUtil.resize(getControl());
161         }
162     }
163
164     void setParent(CoolBar cool) {
165         this.coolBar = cool;
166     }
167
168     // TODO end refactor this out?
169

170     private Font getFont() {
171         return JFaceResources.getFont(SMALL_FONT);
172     }
173
174     /**
175      * Method to select a PerspectiveBarContributionItem and ensure
176      * that it is visible. It updates the MRU list.
177      * @param contribItem the PerspectiveBarContributionItem to select
178      */

179     void select(PerspectiveBarContributionItem contribItem) {
180         if (contribItem.getToolItem() == null) {
181             return;
182         }
183         // check if not visible and ensure visible
184
if (getControl().isVisible()
185                 && !isItemVisible(contribItem.getToolItem())) {
186             ensureVisible(contribItem);
187         }
188     }
189
190     /**
191      * Method that adds a PerspectiveBarContributionItem and
192      * ensures it is visible
193      * @param item the PerspectiveBarContributionItem to be added
194      */

195     public void addItem(PerspectiveBarContributionItem item) {
196         insert(1, item);
197         update(false);
198     }
199
200     /**
201      * Method to remove a PerspectiveBarContributionItem from the toolbar
202      * and from the MRU and sequence lists when necessary
203      * @param item the PerspectiveBarContributionItem to be removed
204      */

205     public void removeItem(PerspectiveBarContributionItem item) {
206         remove(item);
207     }
208
209     /**
210      * Re-insert the item at the beginning of the perspective bar,
211      * ensuring that it is visible to the user.
212      *
213      * @param contribItem
214      */

215     private void ensureVisible(PerspectiveBarContributionItem contribItem) {
216         relocate(contribItem, 1);
217     }
218     
219     void relocate(PerspectiveBarContributionItem contribItem, int index) {
220         PerspectiveBarContributionItem newItem = new PerspectiveBarContributionItem(
221                 contribItem.getPerspective(), contribItem.getPage());
222
223         removeItem(contribItem);
224         contribItem.dispose();
225         contribItem = null;
226
227         insert(index, newItem);
228         update(false);
229     }
230
231     /**
232      * @return true if the toolItem is visible on the screen, false otherwise.
233      */

234     private boolean isItemVisible(ToolItem toolItem) {
235         Rectangle barBounds = getControl().getBounds();
236         Rectangle itemBounds = toolItem.getBounds();
237         return (barBounds.intersection(itemBounds).equals(itemBounds));
238     }
239
240     /**
241      * This method ensures that the selected item in the toolbar is visible.
242      */

243     public void arrangeToolbar() {
244         // check if the tool bar is visible
245
if (!getControl().isVisible()) {
246             return;
247         }
248
249         // the tool bar should contain at least the new perspective button
250
// and 2 other buttons
251
if (getControl().getItemCount() < 3) {
252             return;
253         }
254
255         // Find the selected item and make sure it is visible.
256
IContributionItem[] items = getItems();
257         for (int i = 2; i < items.length; i++) {
258             PerspectiveBarContributionItem contribItem = (PerspectiveBarContributionItem) items[i];
259             if (contribItem.getToolItem().getSelection()) {
260                 if (!isItemVisible(contribItem.getToolItem())) {
261                     ensureVisible(contribItem);
262                     break;
263                 }
264             }
265         }
266     }
267 }
268
Popular Tags