001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.jface.action.ContributionItem;
013: import org.eclipse.jface.action.IMenuListener;
014: import org.eclipse.jface.action.IMenuManager;
015: import org.eclipse.jface.action.MenuManager;
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.events.SelectionAdapter;
018: import org.eclipse.swt.events.SelectionEvent;
019: import org.eclipse.swt.widgets.Menu;
020: import org.eclipse.swt.widgets.MenuItem;
021: import org.eclipse.swt.widgets.Shell;
022: import org.eclipse.ui.IWorkbench;
023: import org.eclipse.ui.IWorkbenchWindow;
024:
025: /**
026: * A dynamic menu item to switch to other opened workbench windows.
027: */
028: public class SwitchToWindowMenu extends ContributionItem {
029: private static final int MAX_TEXT_LENGTH = 40;
030:
031: private IWorkbenchWindow workbenchWindow;
032:
033: private boolean showSeparator;
034:
035: private boolean dirty = true;
036:
037: private IMenuListener menuListener = new IMenuListener() {
038: public void menuAboutToShow(IMenuManager manager) {
039: manager.markDirty();
040: dirty = true;
041: }
042: };
043:
044: /**
045: * Creates a new instance of this class.
046: *
047: * @param window the workbench window this action applies to
048: * @param showSeparator whether to add a separator in the menu
049: */
050: public SwitchToWindowMenu(IWorkbenchWindow window, String id,
051: boolean showSeparator) {
052: super (id);
053: this .workbenchWindow = window;
054: this .showSeparator = showSeparator;
055: }
056:
057: /**
058: * Returns the text for a window. This may be truncated to fit
059: * within the MAX_TEXT_LENGTH.
060: */
061: private String calcText(int number, IWorkbenchWindow window) {
062: String suffix = window.getShell().getText();
063: if (suffix == null) {
064: return null;
065: }
066:
067: StringBuffer sb = new StringBuffer();
068: if (number < 10) {
069: sb.append('&');
070: }
071: sb.append(number);
072: sb.append(' ');
073: if (suffix.length() <= MAX_TEXT_LENGTH) {
074: sb.append(suffix);
075: } else {
076: sb.append(suffix.substring(0, MAX_TEXT_LENGTH));
077: sb.append("..."); //$NON-NLS-1$
078: }
079: return sb.toString();
080: }
081:
082: /**
083: * Fills the given menu with menu items for all
084: * opened workbench windows.
085: */
086: public void fill(Menu menu, int index) {
087:
088: // Get workbench windows.
089: IWorkbench workbench = workbenchWindow.getWorkbench();
090: IWorkbenchWindow[] array = workbench.getWorkbenchWindows();
091: // avoid showing the separator and list for 0 or 1 items
092: if (array.length < 2) {
093: return;
094: }
095:
096: if (getParent() instanceof MenuManager) {
097: ((MenuManager) getParent()).addMenuListener(menuListener);
098: }
099:
100: if (!dirty) {
101: return;
102: }
103:
104: // Add separator.
105: if (showSeparator) {
106: new MenuItem(menu, SWT.SEPARATOR, index);
107: ++index;
108: }
109:
110: // Add one item for each window.
111: int count = 1;
112: for (int i = 0; i < array.length; i++) {
113: final IWorkbenchWindow window = array[i];
114: // can encounter disposed shells if this update is in response to a shell closing
115: if (!window.getShell().isDisposed()) {
116: String name = calcText(count, window);
117: if (name != null) {
118: MenuItem mi = new MenuItem(menu, SWT.RADIO, index);
119: index++;
120: count++;
121: mi.setText(name);
122: mi.addSelectionListener(new SelectionAdapter() {
123: public void widgetSelected(SelectionEvent e) {
124: Shell windowShell = window.getShell();
125: if (windowShell.getMinimized()) {
126: windowShell.setMinimized(false);
127: }
128: windowShell.setActive();
129: windowShell.moveAbove(null);
130: }
131: });
132: mi.setSelection(window == workbenchWindow);
133: }
134: }
135: }
136: dirty = false;
137: }
138:
139: /**
140: * Overridden to always return true and force dynamic menu building.
141: */
142: public boolean isDirty() {
143: return dirty;
144: }
145:
146: /**
147: * Overridden to always return true and force dynamic menu building.
148: */
149: public boolean isDynamic() {
150: return true;
151: }
152: }
|