1 11 package org.eclipse.jface.action; 12 13 import java.util.Enumeration ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 23 public abstract class SubContributionManager implements IContributionManager { 24 27 private IContributionManager parentMgr; 28 29 33 private Map mapItemToWrapper = new HashMap (); 34 35 38 private boolean visible = false; 39 40 47 public SubContributionManager(IContributionManager mgr) { 48 super(); 49 parentMgr = mgr; 50 } 51 52 55 public void add(IAction action) { 56 add(new ActionContributionItem(action)); 57 } 58 59 62 public void add(IContributionItem item) { 63 item.setParent(this); 64 SubContributionItem wrap = wrap(item); 65 wrap.setVisible(visible); 66 parentMgr.add(wrap); 67 itemAdded(item, wrap); 68 } 69 70 73 public void appendToGroup(String groupName, IAction action) { 74 appendToGroup(groupName, new ActionContributionItem(action)); 75 } 76 77 80 public void appendToGroup(String groupName, IContributionItem item) { 81 item.setParent(this); 82 SubContributionItem wrap = wrap(item); 83 wrap.setVisible(visible); 84 parentMgr.appendToGroup(groupName, wrap); 85 itemAdded(item, wrap); 86 } 87 88 96 public void disposeManager() { 97 Iterator it = mapItemToWrapper.values().iterator(); 98 while (it.hasNext()) { 102 IContributionItem item = (IContributionItem) it.next(); 103 item.dispose(); 104 } 105 removeAll(); 106 } 107 108 113 public IContributionItem find(String id) { 114 IContributionItem item = parentMgr.find(id); 115 item = unwrap(item); 117 return item; 118 } 119 120 125 public IContributionItem[] getItems() { 126 IContributionItem[] result = new IContributionItem[mapItemToWrapper 127 .size()]; 128 mapItemToWrapper.keySet().toArray(result); 129 return result; 130 } 131 132 137 public IContributionManager getParent() { 138 return parentMgr; 139 } 140 141 144 public IContributionManagerOverrides getOverrides() { 145 return parentMgr.getOverrides(); 146 } 147 148 151 public void insertAfter(String id, IAction action) { 152 insertAfter(id, new ActionContributionItem(action)); 153 } 154 155 158 public void insertAfter(String id, IContributionItem item) { 159 item.setParent(this); 160 SubContributionItem wrap = wrap(item); 161 wrap.setVisible(visible); 162 parentMgr.insertAfter(id, wrap); 163 itemAdded(item, wrap); 164 } 165 166 169 public void insertBefore(String id, IAction action) { 170 insertBefore(id, new ActionContributionItem(action)); 171 } 172 173 176 public void insertBefore(String id, IContributionItem item) { 177 item.setParent(this); 178 SubContributionItem wrap = wrap(item); 179 wrap.setVisible(visible); 180 parentMgr.insertBefore(id, wrap); 181 itemAdded(item, wrap); 182 } 183 184 187 public boolean isDirty() { 188 return parentMgr.isDirty(); 189 } 190 191 194 public boolean isEmpty() { 195 return parentMgr.isEmpty(); 196 } 197 198 205 public boolean isVisible() { 206 return visible; 207 } 208 209 219 protected void itemAdded(IContributionItem item, SubContributionItem wrap) { 220 mapItemToWrapper.put(item, wrap); 221 } 222 223 231 protected void itemRemoved(IContributionItem item) { 232 mapItemToWrapper.remove(item); 233 } 234 235 239 public Enumeration items() { 240 final Iterator i = mapItemToWrapper.values().iterator(); 241 return new Enumeration () { 242 public boolean hasMoreElements() { 243 return i.hasNext(); 244 } 245 246 public Object nextElement() { 247 return i.next(); 248 } 249 }; 250 } 251 252 255 public void markDirty() { 256 parentMgr.markDirty(); 257 } 258 259 262 public void prependToGroup(String groupName, IAction action) { 263 prependToGroup(groupName, new ActionContributionItem(action)); 264 } 265 266 269 public void prependToGroup(String groupName, IContributionItem item) { 270 item.setParent(this); 271 SubContributionItem wrap = wrap(item); 272 wrap.setVisible(visible); 273 parentMgr.prependToGroup(groupName, wrap); 274 itemAdded(item, wrap); 275 } 276 277 280 public IContributionItem remove(String id) { 281 IContributionItem result = parentMgr.remove(id); 282 if (result != null) { 283 itemRemoved(result); 284 } 285 return result; 286 } 287 288 291 public IContributionItem remove(IContributionItem item) { 292 SubContributionItem wrap = (SubContributionItem) mapItemToWrapper 293 .get(item); 294 if (wrap == null) { 295 return null; 296 } 297 IContributionItem result = parentMgr.remove(wrap); 298 if (result == null) { 299 return null; 300 } 301 itemRemoved(item); 302 return item; 303 } 304 305 308 public void removeAll() { 309 Iterator it = mapItemToWrapper.values().iterator(); 310 while (it.hasNext()) { 311 IContributionItem item = (IContributionItem) it.next(); 312 parentMgr.remove(item); 313 } 314 mapItemToWrapper.clear(); 315 } 316 317 324 public void setVisible(boolean visible) { 325 this.visible = visible; 326 if (mapItemToWrapper.size() > 0) { 327 Iterator it = mapItemToWrapper.values().iterator(); 328 while (it.hasNext()) { 329 IContributionItem item = (IContributionItem) it.next(); 330 item.setVisible(visible); 331 } 332 parentMgr.markDirty(); 333 } 334 } 335 336 341 protected SubContributionItem wrap(IContributionItem item) { 342 return new SubContributionItem(item); 343 } 344 345 356 protected IContributionItem unwrap(IContributionItem item) { 357 if (item instanceof SubContributionItem) { 358 return ((SubContributionItem) item).getInnerItem(); 359 } 360 361 return item; 362 } 363 } 364 | Popular Tags |