1 11 package org.eclipse.ui.actions; 12 13 import org.eclipse.jface.action.ContributionItem; 14 import org.eclipse.jface.action.IContributionItem; 15 import org.eclipse.jface.action.IContributionManager; 16 import org.eclipse.jface.action.IMenuListener; 17 import org.eclipse.jface.action.IMenuManager; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.widgets.Menu; 20 import org.eclipse.swt.widgets.MenuItem; 21 22 28 public abstract class CompoundContributionItem extends ContributionItem { 29 30 private boolean dirty = true; 31 32 private IMenuListener menuListener = new IMenuListener() { 33 public void menuAboutToShow(IMenuManager manager) { 34 manager.markDirty(); 35 dirty = true; 36 } 37 }; 38 39 private IContributionItem[] oldItems; 40 41 44 protected CompoundContributionItem() { 45 super(); 46 } 47 48 53 protected CompoundContributionItem(String id) { 54 super(id); 55 } 56 57 60 public void fill(Menu menu, int index) { 61 if (index == -1) { 62 index = menu.getItemCount(); 63 } 64 if (!dirty && menu.getParentItem() != null) { 65 new MenuItem(menu, SWT.NONE, index); 67 return; 68 } 69 70 IContributionItem[] items = getContributionItemsToFill(); 71 for (int i = 0; i < items.length; i++) { 72 IContributionItem item = items[i]; 73 int oldItemCount = menu.getItemCount(); 74 if (item.isVisible()) { 75 item.fill(menu, index); 76 } 77 int newItemCount = menu.getItemCount(); 78 int numAdded = newItemCount - oldItemCount; 79 index += numAdded; 80 } 81 dirty = false; 82 } 83 84 91 protected abstract IContributionItem[] getContributionItems(); 92 93 private IContributionItem[] getContributionItemsToFill() { 94 if (oldItems != null) { 95 for (int i = 0; i < oldItems.length; i++) { 96 IContributionItem oldItem = oldItems[i]; 97 oldItem.dispose(); 98 } 99 oldItems = null; 100 } 101 oldItems = getContributionItems(); 102 return oldItems; 103 } 104 105 108 public boolean isDirty() { 109 return dirty; 110 } 111 112 115 public boolean isDynamic() { 116 return true; 117 } 118 119 120 123 public void setParent(IContributionManager parent) { 124 if (getParent() instanceof IMenuManager) { 125 IMenuManager menuMgr = (IMenuManager) getParent(); 126 menuMgr.removeMenuListener(menuListener); 127 } 128 if (parent instanceof IMenuManager) { 129 IMenuManager menuMgr = (IMenuManager) parent; 130 menuMgr.addMenuListener(menuListener); 131 } 132 super.setParent(parent); 133 } 134 } 135 | Popular Tags |