1 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 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 51 public SwitchToWindowMenu(IWorkbenchWindow window, String id, 52 boolean showSeparator) { 53 super(id); 54 this.workbenchWindow = window; 55 this.showSeparator = showSeparator; 56 } 57 58 62 private String calcText(int number, IWorkbenchWindow window) { 63 String suffix = window.getShell().getText(); 64 if (suffix == null) { 65 return null; 66 } 67 68 StringBuffer sb = new StringBuffer (); 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("..."); } 80 return sb.toString(); 81 } 82 83 87 public void fill(Menu menu, int index) { 88 89 IWorkbench workbench = workbenchWindow.getWorkbench(); 91 IWorkbenchWindow[] array = workbench.getWorkbenchWindows(); 92 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 if (showSeparator) { 107 new MenuItem(menu, SWT.SEPARATOR, index); 108 ++index; 109 } 110 111 int count = 1; 113 for (int i = 0; i < array.length; i++) { 114 final IWorkbenchWindow window = array[i]; 115 if (!window.getShell().isDisposed()) { 117 String 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 143 public boolean isDirty() { 144 return dirty; 145 } 146 147 150 public boolean isDynamic() { 151 return true; 152 } 153 } 154 | Popular Tags |