1 11 package org.eclipse.jface.action; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.jface.util.Policy; 18 19 38 public abstract class ContributionManager implements IContributionManager { 39 40 43 46 private List contributions = new ArrayList (); 47 48 51 private boolean isDirty = true; 52 53 56 private int dynamicItems = 0; 57 58 61 private IContributionManagerOverrides overrides; 62 63 66 protected ContributionManager() { 67 } 69 70 73 public void add(IAction action) { 74 add(new ActionContributionItem(action)); 75 } 76 77 80 public void add(IContributionItem item) { 81 if (allowItem(item)) { 82 contributions.add(item); 83 itemAdded(item); 84 } 85 } 86 87 101 private void addToGroup(String groupName, IContributionItem item, 102 boolean append) { 103 int i; 104 Iterator items = contributions.iterator(); 105 for (i = 0; items.hasNext(); i++) { 106 IContributionItem o = (IContributionItem) items.next(); 107 if (o.isGroupMarker()) { 108 String id = o.getId(); 109 if (id != null && id.equalsIgnoreCase(groupName)) { 110 i++; 111 if (append) { 112 for (; items.hasNext(); i++) { 113 IContributionItem ci = (IContributionItem) items 114 .next(); 115 if (ci.isGroupMarker()) { 116 break; 117 } 118 } 119 } 120 if (allowItem(item)) { 121 contributions.add(i, item); 122 itemAdded(item); 123 } 124 return; 125 } 126 } 127 } 128 throw new IllegalArgumentException ("Group not found: " + groupName); } 130 131 134 public void appendToGroup(String groupName, IAction action) { 135 addToGroup(groupName, new ActionContributionItem(action), true); 136 } 137 138 141 public void appendToGroup(String groupName, IContributionItem item) { 142 addToGroup(groupName, item, true); 143 } 144 145 159 protected boolean allowItem(IContributionItem itemToAdd) { 160 return true; 161 } 162 163 167 protected void dumpStatistics() { 168 int size = 0; 169 if (contributions != null) { 170 size = contributions.size(); 171 } 172 173 System.out.println(this.toString()); 174 System.out.println(" Number of elements: " + size); int sum = 0; 176 for (int i = 0; i < size; i++) { 177 if (((IContributionItem) contributions.get(i)).isVisible()) { 178 sum++; 179 } 180 } 181 System.out.println(" Number of visible elements: " + sum); System.out.println(" Is dirty: " + isDirty()); } 184 185 188 public IContributionItem find(String id) { 189 Iterator e = contributions.iterator(); 190 while (e.hasNext()) { 191 IContributionItem item = (IContributionItem) e.next(); 192 String itemId = item.getId(); 193 if (itemId != null && itemId.equalsIgnoreCase(id)) { 194 return item; 195 } 196 } 197 return null; 198 } 199 200 203 public IContributionItem[] getItems() { 204 IContributionItem[] items = new IContributionItem[contributions.size()]; 205 contributions.toArray(items); 206 return items; 207 } 208 209 215 public int getSize() { 216 return contributions.size(); 217 } 218 219 227 public IContributionManagerOverrides getOverrides() { 228 if (overrides == null) { 229 overrides = new IContributionManagerOverrides() { 230 public Boolean getEnabled(IContributionItem item) { 231 return null; 232 } 233 234 public Integer getAccelerator(IContributionItem item) { 235 return null; 236 } 237 238 public String getAcceleratorText(IContributionItem item) { 239 return null; 240 } 241 242 public String getText(IContributionItem item) { 243 return null; 244 } 245 }; 246 } 247 return overrides; 248 } 249 250 258 protected boolean hasDynamicItems() { 259 return (dynamicItems > 0); 260 } 261 262 270 public int indexOf(String id) { 271 for (int i = 0; i < contributions.size(); i++) { 272 IContributionItem item = (IContributionItem) contributions.get(i); 273 String itemId = item.getId(); 274 if (itemId != null && itemId.equalsIgnoreCase(id)) { 275 return i; 276 } 277 } 278 return -1; 279 } 280 281 291 protected int indexOf(IContributionItem item) { 292 return contributions.indexOf(item); 293 } 294 295 303 public void insert(int index, IContributionItem item) { 304 if (index > contributions.size()) { 305 throw new IndexOutOfBoundsException ( 306 "inserting " + item.getId() + " at " + index); } 308 if (allowItem(item)) { 309 contributions.add(index, item); 310 itemAdded(item); 311 } 312 } 313 314 317 public void insertAfter(String ID, IAction action) { 318 insertAfter(ID, new ActionContributionItem(action)); 319 } 320 321 324 public void insertAfter(String ID, IContributionItem item) { 325 IContributionItem ci = find(ID); 326 if (ci == null) { 327 throw new IllegalArgumentException ("can't find ID" + ID); } 329 int ix = contributions.indexOf(ci); 330 if (ix >= 0) { 331 if (allowItem(item)) { 333 contributions.add(ix + 1, item); 334 itemAdded(item); 335 } 336 } 337 } 338 339 342 public void insertBefore(String ID, IAction action) { 343 insertBefore(ID, new ActionContributionItem(action)); 344 } 345 346 349 public void insertBefore(String ID, IContributionItem item) { 350 IContributionItem ci = find(ID); 351 if (ci == null) { 352 throw new IllegalArgumentException ("can't find ID " + ID); } 354 int ix = contributions.indexOf(ci); 355 if (ix >= 0) { 356 if (allowItem(item)) { 358 contributions.add(ix, item); 359 itemAdded(item); 360 } 361 } 362 } 363 364 367 public boolean isDirty() { 368 if (isDirty) { 369 return true; 370 } 371 if (hasDynamicItems()) { 372 for (Iterator iter = contributions.iterator(); iter.hasNext();) { 373 IContributionItem item = (IContributionItem) iter.next(); 374 if (item.isDirty()) { 375 return true; 376 } 377 } 378 } 379 return false; 380 } 381 382 385 public boolean isEmpty() { 386 return contributions.isEmpty(); 387 } 388 389 397 protected void itemAdded(IContributionItem item) { 398 item.setParent(this); 399 markDirty(); 400 if (item.isDynamic()) { 401 dynamicItems++; 402 } 403 } 404 405 412 protected void itemRemoved(IContributionItem item) { 413 item.setParent(null); 414 markDirty(); 415 if (item.isDynamic()) { 416 dynamicItems--; 417 } 418 } 419 420 423 public void markDirty() { 424 setDirty(true); 425 } 426 427 430 public void prependToGroup(String groupName, IAction action) { 431 addToGroup(groupName, new ActionContributionItem(action), false); 432 } 433 434 437 public void prependToGroup(String groupName, IContributionItem item) { 438 addToGroup(groupName, item, false); 439 } 440 441 444 public IContributionItem remove(String ID) { 445 IContributionItem ci = find(ID); 446 if (ci == null) { 447 return null; 448 } 449 return remove(ci); 450 } 451 452 455 public IContributionItem remove(IContributionItem item) { 456 if (contributions.remove(item)) { 457 itemRemoved(item); 458 return item; 459 } 460 return null; 461 } 462 463 466 public void removeAll() { 467 IContributionItem[] items = getItems(); 468 contributions.clear(); 469 for (int i = 0; i < items.length; i++) { 470 IContributionItem item = items[i]; 471 itemRemoved(item); 472 } 473 dynamicItems = 0; 474 markDirty(); 475 } 476 477 496 public boolean replaceItem(final String identifier, 497 final IContributionItem replacementItem) { 498 if (identifier == null) { 499 return false; 500 } 501 502 final int index = indexOf(identifier); 503 if (index < 0) { 504 return false; } 506 507 final IContributionItem oldItem = (IContributionItem) contributions 509 .get(index); 510 itemRemoved(oldItem); 511 512 contributions.set(index, replacementItem); 514 itemAdded(replacementItem); 516 for (int i = contributions.size() - 1; i > index; i--) { 518 IContributionItem item = (IContributionItem) contributions.get(i); 519 if ((item != null) && (identifier.equals(item.getId()))) { 520 if (Policy.TRACE_TOOLBAR) { 521 System.out 522 .println("Removing duplicate on replace: " + identifier); } 524 contributions.remove(i); 525 itemRemoved(item); 526 } 527 } 528 529 return true; } 531 532 540 protected void setDirty(boolean dirty) { 541 isDirty = dirty; 542 } 543 544 551 public void setOverrides(IContributionManagerOverrides newOverrides) { 552 overrides = newOverrides; 553 } 554 555 562 protected void internalSetItems(IContributionItem[] items) { 563 contributions.clear(); 564 for (int i = 0; i < items.length; i++) { 565 if (allowItem(items[i])) { 566 contributions.add(items[i]); 567 } 568 } 569 } 570 } 571 | Popular Tags |