KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.swt.SWT;
15 import org.eclipse.swt.events.SelectionAdapter;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.widgets.Menu;
18 import org.eclipse.swt.widgets.MenuItem;
19 import org.eclipse.ui.IPerspectiveDescriptor;
20 import org.eclipse.ui.IWorkbenchPage;
21 import org.eclipse.ui.IWorkbenchWindow;
22
23 /**
24  * A dynamic contribution item which shows all opened perspectives
25  * in the window's active page.
26  */

27 public class OpenedPerspectivesMenu extends ContributionItem {
28     private IWorkbenchWindow window;
29     private boolean showSeparator;
30
31     private static final int MAX_TEXT_LENGTH = 40;
32
33     /**
34      * Create a new instance.
35      */

36     public OpenedPerspectivesMenu(IWorkbenchWindow window, String JavaDoc id, boolean showSeparator) {
37         super(id);
38         this.window = window;
39         this.showSeparator = showSeparator;
40     }
41     
42     /**
43      * Returns the text for a perspective. This may be truncated to fit
44      * within the MAX_TEXT_LENGTH.
45      */

46     private String JavaDoc calcText(int number, IPerspectiveDescriptor persp) {
47         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
48         if (number < 10)
49             sb.append('&');
50         sb.append(number);
51         sb.append(' ');
52         String JavaDoc suffix = persp.getLabel();
53         if (suffix.length() <= MAX_TEXT_LENGTH) {
54             sb.append(suffix);
55         } else {
56             sb.append(suffix.substring(0, MAX_TEXT_LENGTH / 2));
57             sb.append("..."); //$NON-NLS-1$
58
sb.append(suffix.substring(suffix.length() - MAX_TEXT_LENGTH / 2));
59         }
60         return sb.toString();
61     }
62
63     /**
64      * Fills the given menu with menu items for all opened perspectives.
65      */

66     public void fill(Menu menu, int index) {
67         final IWorkbenchPage page = window.getActivePage();
68         if (page == null)
69             return;
70
71         // Add separator.
72
if (showSeparator) {
73             new MenuItem(menu, SWT.SEPARATOR, index);
74             ++index;
75         }
76
77         // Add one item for each opened perspective.
78
IPerspectiveDescriptor activePersp = page.getPerspective();
79         IPerspectiveDescriptor descriptors[] = ((WorkbenchPage) page).getOpenedPerspectives();
80         int count = 1;
81         for (int i = 0; i < descriptors.length; i++) {
82             final IPerspectiveDescriptor desc = (IPerspectiveDescriptor)descriptors[i];
83             MenuItem mi = new MenuItem(menu, SWT.RADIO, index);
84             mi.setSelection(desc == activePersp);
85             mi.setText(calcText(count, desc));
86             // avoid hanging onto page or perspective directly in menu
87
mi.addSelectionListener(new SelectionAdapter() {
88                 public void widgetSelected(SelectionEvent e) {
89                     IWorkbenchPage page = window.getActivePage();
90                     if (page != null) {
91                         page.setPerspective(desc);
92                     }
93                 }
94             });
95         
96             index++;
97             count++;
98         }
99     }
100     
101     /**
102      * Overridden to always return true and force dynamic menu building.
103      */

104     public boolean isDynamic() {
105         return true;
106     }
107 }
108
Popular Tags