1 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 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 36 public OpenedPerspectivesMenu(IWorkbenchWindow window, String id, boolean showSeparator) { 37 super(id); 38 this.window = window; 39 this.showSeparator = showSeparator; 40 } 41 42 46 private String calcText(int number, IPerspectiveDescriptor persp) { 47 StringBuffer sb = new StringBuffer (); 48 if (number < 10) 49 sb.append('&'); 50 sb.append(number); 51 sb.append(' '); 52 String 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("..."); sb.append(suffix.substring(suffix.length() - MAX_TEXT_LENGTH / 2)); 59 } 60 return sb.toString(); 61 } 62 63 66 public void fill(Menu menu, int index) { 67 final IWorkbenchPage page = window.getActivePage(); 68 if (page == null) 69 return; 70 71 if (showSeparator) { 73 new MenuItem(menu, SWT.SEPARATOR, index); 74 ++index; 75 } 76 77 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 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 104 public boolean isDynamic() { 105 return true; 106 } 107 } 108 | Popular Tags |