1 11 12 package org.eclipse.ui.internal.menus; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.expressions.Expression; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.jface.action.ContributionManager; 22 import org.eclipse.jface.action.IContributionItem; 23 import org.eclipse.jface.action.ToolBarContributionItem; 24 import org.eclipse.jface.action.ToolBarManager; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.ToolBar; 28 import org.eclipse.ui.internal.WorkbenchWindow; 29 import org.eclipse.ui.internal.expressions.WorkbenchWindowExpression; 30 import org.eclipse.ui.internal.layout.IWindowTrim; 31 import org.eclipse.ui.internal.layout.TrimLayout; 32 import org.eclipse.ui.internal.misc.StatusUtil; 33 import org.eclipse.ui.menus.AbstractContributionFactory; 34 import org.eclipse.ui.menus.IMenuService; 35 import org.eclipse.ui.menus.MenuUtil; 36 import org.eclipse.ui.statushandlers.StatusManager; 37 38 45 public class TrimContributionManager extends ContributionManager { 46 private class ToolBarTrimProxy implements IWindowTrim { 47 private String id; 48 private String uriSpec; 49 private WorkbenchMenuService menuService; 50 private WorkbenchWindow wbw; 51 private ToolBar tb = null; 52 private ToolBarManager tbm = null; 53 54 ToolBarTrimProxy(String id, WorkbenchWindow wbw) { 55 this.id = id; 56 uriSpec = "toolbar:" + id; this.wbw = wbw; 58 59 this.menuService = (WorkbenchMenuService) wbw.getWorkbench().getService( 60 IMenuService.class); 61 } 62 63 66 public void dock(int dropSide) { 67 dispose(); 68 69 int orientation = SWT.HORIZONTAL; 70 if (dropSide == SWT.LEFT || dropSide == SWT.RIGHT) 71 orientation = SWT.VERTICAL; 72 73 tbm = new ToolBarManager(SWT.FLAT | orientation); 75 menuService.populateContributionManager(tbm, uriSpec); 76 77 IContributionItem[] items = tbm.getItems(); 79 for (int i = 0; i < items.length; i++) { 80 if (items[i] instanceof InternalControlContribution) { 81 InternalControlContribution wbwcc = (InternalControlContribution) items[i]; 82 wbwcc.setWorkbenchWindow(wbw); 83 wbwcc.setCurSide(dropSide); 84 } 85 } 86 87 tb = tbm.createControl(wbw.getShell()); 89 } 90 91 94 public Control getControl() { 95 return tb; 96 } 97 98 101 public String getDisplayName() { 102 return getId(); 103 } 104 105 108 public int getHeightHint() { 109 return SWT.DEFAULT; 110 } 111 112 115 public String getId() { 116 return id; 117 } 118 119 122 public int getValidSides() { 123 return SWT.TOP | SWT.BOTTOM | SWT.LEFT | SWT.RIGHT; 124 } 125 126 129 public int getWidthHint() { 130 return SWT.DEFAULT; 131 } 132 133 136 public void handleClose() { 137 } 138 139 142 public boolean isCloseable() { 143 return false; 144 } 145 146 149 public boolean isResizeable() { 150 return false; 151 } 152 153 156 public void dispose() { 157 if (tbm != null) { 158 tbm.removeAll(); 159 tbm.dispose(); 160 } 161 } 162 } 163 164 167 private String [] trimAreaURIs = { 168 MenuUtil.TRIM_COMMAND1, 169 MenuUtil.TRIM_COMMAND2, 170 MenuUtil.TRIM_VERTICAL1, 171 MenuUtil.TRIM_VERTICAL2, 172 MenuUtil.TRIM_STATUS 173 }; 174 175 178 private int[] swtSides = { SWT.TOP, SWT.TOP, SWT.LEFT, SWT.RIGHT, SWT.BOTTOM }; 179 180 private WorkbenchWindow wbWindow; 181 TrimLayout layout; 182 private InternalMenuService menuService; 183 184 List contributedTrim = new ArrayList (); 185 186 List contributedLists = new ArrayList (); 187 188 private Expression restrictionExpression; 189 190 193 public TrimContributionManager(WorkbenchWindow window) { 194 wbWindow = window; 195 layout = (TrimLayout) wbWindow.getShell().getLayout(); 196 menuService = (InternalMenuService) window.getService( 197 IMenuService.class); 198 restrictionExpression = new WorkbenchWindowExpression(wbWindow); 199 } 200 201 204 public void update(boolean force) { 205 update(force, false); 206 } 207 208 public void update(boolean force, boolean hideTopTrim) { 209 teardown(); 211 212 for (int i = 0; i < trimAreaURIs.length; i++) { 214 if (hideTopTrim && swtSides[i] == SWT.TOP) 216 continue; 217 218 List contribs = menuService.getAdditionsForURI(new MenuLocationURI(trimAreaURIs[i])); 219 220 for (Iterator cacheIter = contribs.iterator(); cacheIter.hasNext();) { 221 AbstractContributionFactory cache = (AbstractContributionFactory) cacheIter.next(); 222 ContributionRoot ciList = new ContributionRoot(menuService, 223 restrictionExpression, cache.getNamespace()); 224 cache.createContributionItems(wbWindow, ciList); 225 contributedLists.add(ciList); 227 for (Iterator ciIter = ciList.getItems().iterator(); ciIter.hasNext();) { 228 IContributionItem ci = (IContributionItem) ciIter.next(); 229 if (ci instanceof ToolBarContributionItem) { 230 ToolBarTrimProxy tbProxy = new ToolBarTrimProxy(ci.getId(), wbWindow); 232 tbProxy.dock(swtSides[i]); 233 234 IWindowTrim insertBefore = null; 236 if (i == 0) { 237 insertBefore = layout.getTrim("org.eclipse.ui.internal.WorkbenchWindow.topBar"); } 239 layout.addTrim(swtSides[i], tbProxy, insertBefore); 240 contributedTrim.add(tbProxy); 241 } 242 } 243 } 244 } 245 } 246 247 private void teardown() { 248 for (Iterator iter = contributedTrim.iterator(); iter.hasNext();) { 250 ToolBarTrimProxy proxy = (ToolBarTrimProxy) iter.next(); 251 layout.removeTrim(proxy); 252 253 try { 254 proxy.dispose(); 255 } catch (Throwable e) { 256 IStatus status = null; 257 if (e instanceof CoreException) { 258 status = ((CoreException) e).getStatus(); 259 } else { 260 status = StatusUtil 261 .newStatus( 262 IStatus.ERROR, 263 "Internal plug-in widget delegate error on dispose.", e); } 265 StatusUtil 266 .handleStatus( 267 status, 268 "widget delegate failed on dispose: id = " + proxy.getId(), StatusManager.LOG); } 270 } 271 272 contributedTrim.clear(); 274 275 for (Iterator iter = contributedLists.iterator(); iter.hasNext();) { 277 ContributionRoot list = (ContributionRoot) iter.next(); 278 list.release(); 279 } 280 281 contributedLists.clear(); 282 } 283 284 287 public void dispose() { 288 teardown(); 289 } 290 291 294 public void updateLocations(List knownIds) { 295 297 } 298 } 299 | Popular Tags |