1 11 package org.eclipse.jface.action; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.accessibility.ACC; 18 import org.eclipse.swt.accessibility.AccessibleAdapter; 19 import org.eclipse.swt.accessibility.AccessibleEvent; 20 import org.eclipse.swt.accessibility.AccessibleListener; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.Menu; 24 import org.eclipse.swt.widgets.ToolBar; 25 import org.eclipse.swt.widgets.ToolItem; 26 27 35 public class ToolBarManager extends ContributionManager implements 36 IToolBarManager { 37 38 41 private int itemStyle = SWT.NONE; 42 43 47 private ToolBar toolBar = null; 48 49 54 private MenuManager contextMenuManager = null; 55 56 61 public ToolBarManager() { 62 } 64 65 73 public ToolBarManager(int style) { 74 itemStyle = style; 75 } 76 77 90 public ToolBarManager(ToolBar toolbar) { 91 this(); 92 this.toolBar = toolbar; 93 } 94 95 104 public ToolBar createControl(Composite parent) { 105 if (!toolBarExist() && parent != null) { 106 toolBar = new ToolBar(parent, itemStyle); 107 toolBar.setMenu(getContextMenuControl()); 108 update(true); 109 110 toolBar.getAccessible().addAccessibleListener(getAccessibleListener()); 111 } 112 113 return toolBar; 114 } 115 116 123 private AccessibleListener getAccessibleListener() { 124 return new AccessibleAdapter() { 125 public void getName(AccessibleEvent e) { 126 if (e.childID != ACC.CHILDID_SELF) { 127 ToolItem item = toolBar.getItem(e.childID); 128 if (item != null) { 129 String toolTip = item.getToolTipText(); 130 if (toolTip != null) { 131 e.result = toolTip; 132 } 133 } 134 } 135 } 136 }; 137 138 } 139 140 147 public void dispose() { 148 149 if (toolBarExist()) { 150 toolBar.dispose(); 151 } 152 toolBar = null; 153 154 IContributionItem[] items = getItems(); 155 for (int i = 0; i < items.length; i++) { 156 items[i].dispose(); 157 } 158 159 if (getContextMenuManager() != null) { 160 getContextMenuManager().dispose(); 161 setContextMenuManager(null); 162 } 163 } 164 165 171 public ToolBar getControl() { 172 return toolBar; 173 } 174 175 189 protected void relayout(ToolBar layoutBar, int oldCount, int newCount) { 190 if ((oldCount == 0) != (newCount == 0)) { 191 layoutBar.getParent().layout(); 192 } 193 } 194 195 201 private boolean toolBarExist() { 202 return toolBar != null && !toolBar.isDisposed(); 203 } 204 205 208 public void update(boolean force) { 209 210 216 if (isDirty() || force) { 217 218 if (toolBarExist()) { 219 220 int oldCount = toolBar.getItemCount(); 221 222 IContributionItem[] items = getItems(); 224 ArrayList clean = new ArrayList (items.length); 225 IContributionItem separator = null; 226 for (int i = 0; i < items.length; ++i) { 231 IContributionItem ci = items[i]; 232 if (!ci.isVisible()) { 233 continue; 234 } 235 if (ci.isSeparator()) { 236 separator = ci; 240 } else { 241 if (separator != null) { 242 if (clean.size() > 0) { 243 clean.add(separator); 244 } 245 separator = null; 246 } 247 clean.add(ci); 248 } 249 } 250 255 ToolItem[] mi = toolBar.getItems(); 257 ArrayList toRemove = new ArrayList (mi.length); 258 for (int i = 0; i < mi.length; i++) { 259 Object data = mi[i].getData(); 260 if (data == null 261 || !clean.contains(data) 262 || (data instanceof IContributionItem && ((IContributionItem) data) 263 .isDynamic())) { 264 toRemove.add(mi[i]); 265 } 266 } 267 268 boolean useRedraw = (clean.size() - (mi.length - toRemove 275 .size())) >= 3; 276 try { 277 if (useRedraw) { 278 toolBar.setRedraw(false); 279 } 280 281 for (int i = toRemove.size(); --i >= 0;) { 283 ToolItem item = (ToolItem) toRemove.get(i); 284 if (!item.isDisposed()) { 285 Control ctrl = item.getControl(); 286 if (ctrl != null) { 287 item.setControl(null); 288 ctrl.dispose(); 289 } 290 item.dispose(); 291 } 292 } 293 294 IContributionItem src, dest; 296 mi = toolBar.getItems(); 297 int srcIx = 0; 298 int destIx = 0; 299 for (Iterator e = clean.iterator(); e.hasNext();) { 300 src = (IContributionItem) e.next(); 301 302 if (srcIx < mi.length) { 304 dest = (IContributionItem) mi[srcIx].getData(); 305 } else { 306 dest = null; 307 } 308 309 if (dest != null && src.equals(dest)) { 310 srcIx++; 311 destIx++; 312 continue; 313 } 314 315 if (dest != null && dest.isSeparator() 316 && src.isSeparator()) { 317 mi[srcIx].setData(src); 318 srcIx++; 319 destIx++; 320 continue; 321 } 322 323 int start = toolBar.getItemCount(); 324 src.fill(toolBar, destIx); 325 int newItems = toolBar.getItemCount() - start; 326 for (int i = 0; i < newItems; i++) { 327 ToolItem item = toolBar.getItem(destIx++); 328 item.setData(src); 329 } 330 } 331 332 for (int i = mi.length; --i >= srcIx;) { 334 ToolItem item = mi[i]; 335 if (!item.isDisposed()) { 336 Control ctrl = item.getControl(); 337 if (ctrl != null) { 338 item.setControl(null); 339 ctrl.dispose(); 340 } 341 item.dispose(); 342 } 343 } 344 345 setDirty(false); 346 347 } finally { 349 if (useRedraw) { 350 toolBar.setRedraw(true); 351 } 352 } 353 354 int newCount = toolBar.getItemCount(); 355 relayout(toolBar, oldCount, newCount); 356 } 357 358 } 359 360 } 366 367 373 private Menu getContextMenuControl() { 374 if ((contextMenuManager != null) && (toolBar != null)) { 375 Menu menuWidget = contextMenuManager.getMenu(); 376 if ((menuWidget == null) || (menuWidget.isDisposed())) { 377 menuWidget = contextMenuManager.createContextMenu(toolBar); 378 } 379 return menuWidget; 380 } 381 return null; 382 } 383 384 390 public MenuManager getContextMenuManager() { 391 return contextMenuManager; 392 } 393 394 403 public void setContextMenuManager(MenuManager contextMenuManager) { 404 this.contextMenuManager = contextMenuManager; 405 if (toolBar != null) { 406 toolBar.setMenu(getContextMenuControl()); 407 } 408 } 409 410 } 411 | Popular Tags |