KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.IMenuListener;
15 import org.eclipse.jface.action.IMenuManager;
16 import org.eclipse.jface.action.MenuManager;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.widgets.Menu;
21 import org.eclipse.swt.widgets.MenuItem;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.IWorkbenchWindow;
25
26 /**
27  * A dynamic menu item to switch to other opened workbench windows.
28  */

29 public class SwitchToWindowMenu extends ContributionItem {
30     private static final int MAX_TEXT_LENGTH = 40;
31
32     private IWorkbenchWindow workbenchWindow;
33
34     private boolean showSeparator;
35
36     private boolean dirty = true;
37
38     private IMenuListener menuListener = new IMenuListener() {
39         public void menuAboutToShow(IMenuManager manager) {
40             manager.markDirty();
41             dirty = true;
42         }
43     };
44
45     /**
46      * Creates a new instance of this class.
47      *
48      * @param window the workbench window this action applies to
49      * @param showSeparator whether to add a separator in the menu
50      */

51     public SwitchToWindowMenu(IWorkbenchWindow window, String JavaDoc id,
52             boolean showSeparator) {
53         super(id);
54         this.workbenchWindow = window;
55         this.showSeparator = showSeparator;
56     }
57
58     /**
59      * Returns the text for a window. This may be truncated to fit
60      * within the MAX_TEXT_LENGTH.
61      */

62     private String JavaDoc calcText(int number, IWorkbenchWindow window) {
63         String JavaDoc suffix = window.getShell().getText();
64         if (suffix == null) {
65             return null;
66         }
67
68         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
69         if (number < 10) {
70             sb.append('&');
71         }
72         sb.append(number);
73         sb.append(' ');
74         if (suffix.length() <= MAX_TEXT_LENGTH) {
75             sb.append(suffix);
76         } else {
77             sb.append(suffix.substring(0, MAX_TEXT_LENGTH));
78             sb.append("..."); //$NON-NLS-1$
79
}
80         return sb.toString();
81     }
82
83     /**
84      * Fills the given menu with menu items for all
85      * opened workbench windows.
86      */

87     public void fill(Menu menu, int index) {
88
89         // Get workbench windows.
90
IWorkbench workbench = workbenchWindow.getWorkbench();
91         IWorkbenchWindow[] array = workbench.getWorkbenchWindows();
92         // avoid showing the separator and list for 0 or 1 items
93
if (array.length < 2) {
94             return;
95         }
96
97         if (getParent() instanceof MenuManager) {
98             ((MenuManager) getParent()).addMenuListener(menuListener);
99         }
100
101         if (!dirty) {
102             return;
103         }
104
105         // Add separator.
106
if (showSeparator) {
107             new MenuItem(menu, SWT.SEPARATOR, index);
108             ++index;
109         }
110
111         // Add one item for each window.
112
int count = 1;
113         for (int i = 0; i < array.length; i++) {
114             final IWorkbenchWindow window = array[i];
115             // can encounter disposed shells if this update is in response to a shell closing
116
if (!window.getShell().isDisposed()) {
117                 String JavaDoc name = calcText(count, window);
118                 if (name != null) {
119                     MenuItem mi = new MenuItem(menu, SWT.RADIO, index);
120                     index++;
121                     count++;
122                     mi.setText(name);
123                     mi.addSelectionListener(new SelectionAdapter() {
124                         public void widgetSelected(SelectionEvent e) {
125                             Shell windowShell = window.getShell();
126                             if (windowShell.getMinimized()) {
127                                 windowShell.setMinimized(false);
128                             }
129                             windowShell.setActive();
130                             windowShell.moveAbove(null);
131                         }
132                     });
133                     mi.setSelection(window == workbenchWindow);
134                 }
135             }
136         }
137         dirty = false;
138     }
139
140     /**
141      * Overridden to always return true and force dynamic menu building.
142      */

143     public boolean isDirty() {
144         return dirty;
145     }
146
147     /**
148      * Overridden to always return true and force dynamic menu building.
149      */

150     public boolean isDynamic() {
151         return true;
152     }
153 }
154
Popular Tags