1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.jface.action.ContributionItem; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.events.DisposeEvent; 20 import org.eclipse.swt.events.DisposeListener; 21 import org.eclipse.swt.events.SelectionAdapter; 22 import org.eclipse.swt.events.SelectionEvent; 23 import org.eclipse.swt.widgets.ToolBar; 24 import org.eclipse.swt.widgets.ToolItem; 25 import org.eclipse.ui.IPropertyListener; 26 import org.eclipse.ui.IViewReference; 27 import org.eclipse.ui.IWorkbenchPartConstants; 28 import org.eclipse.ui.IWorkbenchPartReference; 29 import org.eclipse.ui.IWorkbenchWindow; 30 import org.eclipse.ui.internal.util.Util; 31 32 35 public class ShowFastViewContribution extends ContributionItem { 36 public static final String FAST_VIEW = "FastView"; 38 private IWorkbenchWindow window; 39 private String fvbId; 40 41 44 public ShowFastViewContribution(IWorkbenchWindow window, String id) { 45 super("showFastViewContr"); this.window = window; 47 this.fvbId = id; 48 } 49 50 54 public ShowFastViewContribution(IWorkbenchWindow window) { 55 this(window, FastViewBar.FASTVIEWBAR_ID); 56 } 57 58 private void updateItem(ToolItem item, IViewReference ref) { 59 if (item.getImage() != ref.getTitleImage()) { 60 item.setImage(ref.getTitleImage()); 61 } 62 63 if (!Util.equals(item.getToolTipText(), ref.getTitle())) { 64 item.setToolTipText(ref.getTitle()); 65 } 66 } 67 68 public static ToolItem getItem(ToolBar toSearch, IWorkbenchPartReference ref) { 69 ToolItem[] items = toSearch.getItems(); 70 71 for (int i = 0; i < items.length; i++) { 72 ToolItem item = items[i]; 73 74 if (item.getData(FAST_VIEW) == ref) { 75 return item; 76 } 77 } 78 79 return null; 80 } 81 82 86 public void fill(ToolBar parent, int index) { 87 WorkbenchPage page = (WorkbenchPage) window.getActivePage(); 89 if (page == null) { 90 return; 91 } 92 93 List fvs = new ArrayList (); 95 Perspective perspective = page.getActivePerspective(); 96 if (perspective != null) 97 fvs = perspective.getFastViewManager().getFastViews(fvbId); 98 99 for (Iterator fvIter = fvs.iterator(); fvIter.hasNext();) { 101 final IViewReference ref = (IViewReference) fvIter.next(); 102 final ToolItem item = new ToolItem(parent, SWT.CHECK, index); 103 updateItem(item, ref); 104 item.setData(FAST_VIEW, ref); 105 106 final IPropertyListener propertyListener = new IPropertyListener() { 107 108 public void propertyChanged(Object source, int propId) { 109 if (propId == IWorkbenchPartConstants.PROP_TITLE) { 110 if (!item.isDisposed()) { 111 updateItem(item, ref); 112 } 113 } 114 } 115 116 }; 117 118 ref.addPropertyListener(propertyListener); 119 120 item.addDisposeListener(new DisposeListener() { 121 124 public void widgetDisposed(DisposeEvent e) { 125 ref.removePropertyListener(propertyListener); 126 } 127 }); 128 129 if (ref == page.getActiveFastView()) { 131 item.setSelection(true); 132 } else { 133 item.setSelection(false); 134 } 135 136 item.addSelectionListener(new SelectionAdapter() { 137 public void widgetSelected(SelectionEvent e) { 138 showView(ref); 139 } 140 }); 141 index++; 142 } 143 } 144 145 148 public boolean isDynamic() { 149 return true; 150 } 151 152 155 private void showView(IViewReference ref) { 156 WorkbenchPage page = (WorkbenchPage) ref.getPage(); 157 page.toggleFastView(ref); 158 } 159 } 160 | Popular Tags |