|
Added
Link Here
|
| 1 |
package org.eclipse.ui.examples.presentation.customtoolbar.widgets; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.HashMap; |
| 5 |
import java.util.Iterator; |
| 6 |
import java.util.List; |
| 7 |
import java.util.ListIterator; |
| 8 |
|
| 9 |
import org.eclipse.jface.action.ContributionManager; |
| 10 |
import org.eclipse.jface.action.IContributionItem; |
| 11 |
import org.eclipse.jface.action.ICoolBarManager; |
| 12 |
import org.eclipse.jface.action.IMenuManager; |
| 13 |
import org.eclipse.jface.action.IToolBarManager; |
| 14 |
import org.eclipse.jface.action.MenuManager; |
| 15 |
import org.eclipse.jface.action.Separator; |
| 16 |
import org.eclipse.jface.action.ToolBarContributionItem; |
| 17 |
import org.eclipse.jface.util.Assert; |
| 18 |
import org.eclipse.jface.util.Policy; |
| 19 |
import org.eclipse.swt.SWT; |
| 20 |
import org.eclipse.swt.widgets.Composite; |
| 21 |
import org.eclipse.swt.widgets.Control; |
| 22 |
import org.eclipse.swt.widgets.CoolBar; |
| 23 |
import org.eclipse.swt.widgets.Menu; |
| 24 |
|
| 25 |
public class SCoolBarManager extends ContributionManager implements ICoolBarManager { |
| 26 |
|
| 27 |
/** |
| 28 |
* A separator created by the end user. |
| 29 |
*/ |
| 30 |
public final static String USER_SEPARATOR = "UserSeparator"; //$NON-NLS-1$ |
| 31 |
|
| 32 |
/** |
| 33 |
* The original creation order of the contribution items. |
| 34 |
*/ |
| 35 |
private ArrayList cbItemsCreationOrder = new ArrayList(); |
| 36 |
|
| 37 |
/** |
| 38 |
* MenuManager for cool bar pop-up menu, or null if none. |
| 39 |
*/ |
| 40 |
private MenuManager contextMenuManager = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* The cool bar control; <code>null</code> before creation and after |
| 44 |
* disposal. |
| 45 |
*/ |
| 46 |
private SCoolBar coolBar = null; |
| 47 |
|
| 48 |
/** |
| 49 |
* The cool bar items style; <code>SWT.NONE</code> by default. |
| 50 |
*/ |
| 51 |
private int itemStyle = SWT.NONE; |
| 52 |
|
| 53 |
/** |
| 54 |
* Creates a new cool bar manager with the default style. Equivalent to |
| 55 |
* <code>CoolBarManager(SWT.NONE)</code>. |
| 56 |
*/ |
| 57 |
public SCoolBarManager() { |
| 58 |
// do nothing |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Creates a cool bar manager for an existing cool bar control. This |
| 63 |
* manager becomes responsible for the control, and will dispose of it when |
| 64 |
* the manager is disposed. |
| 65 |
* |
| 66 |
* @param coolBar |
| 67 |
* the cool bar control |
| 68 |
*/ |
| 69 |
public SCoolBarManager(SCoolBar coolBar) { |
| 70 |
this(); |
| 71 |
Assert.isNotNull(coolBar); |
| 72 |
this.coolBar = coolBar; |
| 73 |
itemStyle = coolBar.getStyle(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Creates a cool bar manager with the given SWT style. Calling <code>createControl</code> |
| 78 |
* will create the cool bar control. |
| 79 |
* |
| 80 |
* @param style |
| 81 |
* the cool bar item style; see |
| 82 |
* {@link org.eclipse.swt.widgets.CoolBar CoolBar}for for valid |
| 83 |
* style bits |
| 84 |
*/ |
| 85 |
public SCoolBarManager(int style) { |
| 86 |
itemStyle = style; |
| 87 |
} |
| 88 |
|
| 89 |
/* |
| 90 |
* (non-Javadoc) |
| 91 |
* |
| 92 |
* @see org.eclipse.jface.action.ICoolBarManager#add(org.eclipse.jface.action.IToolBarManager) |
| 93 |
*/ |
| 94 |
public void add(IToolBarManager toolBarManager) { |
| 95 |
Assert.isNotNull(toolBarManager); |
| 96 |
SToolBarContributionItem2 toolBarContributionItem = null; |
| 97 |
if (toolBarContributionItem == null) |
| 98 |
toolBarContributionItem = new SToolBarContributionItem2(toolBarManager); |
| 99 |
super.add(toolBarContributionItem); |
| 100 |
} |
| 101 |
|
| 102 |
public void add(IContributionItem item) { |
| 103 |
super.add(item); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Collapses consecutive separators and removes a separator from the |
| 108 |
* beginning and end of the list. |
| 109 |
* |
| 110 |
* @param contributionList |
| 111 |
* the list of contributions; must not be <code>null</code>. |
| 112 |
* @return The contribution list provided with extraneous separators |
| 113 |
* removed; this value is never <code>null</code>, but may be |
| 114 |
* empty. |
| 115 |
*/ |
| 116 |
private ArrayList adjustContributionList(ArrayList contributionList) { |
| 117 |
IContributionItem item; |
| 118 |
// Fist remove a separator if it is the first element of the list |
| 119 |
if (contributionList.size() != 0) { |
| 120 |
item = (IContributionItem) contributionList.get(0); |
| 121 |
if (item.isSeparator()) { |
| 122 |
contributionList.remove(0); |
| 123 |
} |
| 124 |
|
| 125 |
ListIterator iterator = contributionList.listIterator(); |
| 126 |
// collapse consecutive separators |
| 127 |
while (iterator.hasNext()) { |
| 128 |
item = (IContributionItem) iterator.next(); |
| 129 |
if (item.isSeparator()) { |
| 130 |
while (iterator.hasNext()) { |
| 131 |
item = (IContributionItem) iterator.next(); |
| 132 |
if (item.isSeparator()) { |
| 133 |
iterator.remove(); |
| 134 |
} else { |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
} |
| 141 |
// Now check last element to see if there is a separator |
| 142 |
item = (IContributionItem) contributionList.get(contributionList |
| 143 |
.size() - 1); |
| 144 |
if (item.isSeparator()) { |
| 145 |
contributionList.remove(contributionList.size() - 1); |
| 146 |
} |
| 147 |
} |
| 148 |
return contributionList; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
/* (non-Javadoc) |
| 153 |
* @see org.eclipse.jface.action.ContributionManager#checkDuplication(org.eclipse.jface.action.IContributionItem) |
| 154 |
*/ |
| 155 |
protected boolean allowItem(IContributionItem itemToAdd) { |
| 156 |
/* We will allow as many null entries as they like, though there should |
| 157 |
* be none. |
| 158 |
*/ |
| 159 |
if (itemToAdd == null) { |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
/* Null identifiers can be expected in generic contribution items. |
| 164 |
*/ |
| 165 |
String firstId = itemToAdd.getId(); |
| 166 |
if (firstId == null) { |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
// Cycle through the current list looking for duplicates. |
| 171 |
IContributionItem[] currentItems = getItems(); |
| 172 |
for (int i = 0; i < currentItems.length; i++) { |
| 173 |
IContributionItem currentItem = currentItems[i]; |
| 174 |
|
| 175 |
// We ignore null entries. |
| 176 |
if (currentItem == null) { |
| 177 |
continue; |
| 178 |
} |
| 179 |
|
| 180 |
String secondId = currentItem.getId(); |
| 181 |
if (firstId.equals(secondId)) { |
| 182 |
if (Policy.TRACE_TOOLBAR) { //$NON-NLS-1$ |
| 183 |
System.out.println("Trying to add a duplicate item."); //$NON-NLS-1$ |
| 184 |
new Exception().printStackTrace(System.out); |
| 185 |
System.out.println("DONE --------------------------"); //$NON-NLS-1$ |
| 186 |
} |
| 187 |
return false; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Positions the list iterator to the end of all the separators. Calling |
| 196 |
* <code>next()</code> the iterator should return the immediate object |
| 197 |
* following the last separator. |
| 198 |
* |
| 199 |
* @param iterator |
| 200 |
* the list iterator. |
| 201 |
*/ |
| 202 |
private void collapseSeparators(ListIterator iterator) { |
| 203 |
|
| 204 |
while (iterator.hasNext()) { |
| 205 |
IContributionItem item = (IContributionItem) iterator.next(); |
| 206 |
if (!item.isSeparator()) { |
| 207 |
iterator.previous(); |
| 208 |
return; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Returns whether the cool bar control has been created and not yet |
| 215 |
* disposed. |
| 216 |
* |
| 217 |
* @return <code>true</code> if the control has been created and not yet |
| 218 |
* disposed, <code>false</code> otherwise |
| 219 |
*/ |
| 220 |
private boolean coolBarExist() { |
| 221 |
return coolBar != null && !coolBar.isDisposed(); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Creates and returns this manager's cool bar control. Does not create a |
| 226 |
* new control if one already exists. |
| 227 |
* |
| 228 |
* @param parent |
| 229 |
* the parent control |
| 230 |
* @return the cool bar control |
| 231 |
*/ |
| 232 |
public Control createControl2(Composite parent) { |
| 233 |
Assert.isNotNull(parent); |
| 234 |
if (!coolBarExist()) { |
| 235 |
coolBar = new SCoolBar(parent, itemStyle); |
| 236 |
((SCoolBar)coolBar).setSkin(new HannoverGlobalCoolBarSkin()); |
| 237 |
coolBar.setMenu(getContextMenuControl()); |
| 238 |
coolBar.setLocked(false); |
| 239 |
coolBar.setSkin(new HannoverGlobalCoolBarSkin()); |
| 240 |
update(false); |
| 241 |
} |
| 242 |
return coolBar; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Disposes of this cool bar manager and frees all allocated SWT resources. |
| 247 |
* Notifies all contribution items of the dispose. Note that this method |
| 248 |
* does not clean up references between this cool bar manager and its |
| 249 |
* associated contribution items. Use <code>removeAll</code> for that |
| 250 |
* purpose. |
| 251 |
*/ |
| 252 |
public void dispose() { |
| 253 |
if (coolBarExist()) { |
| 254 |
IContributionItem[] items = getItems(); |
| 255 |
for (int i = 0; i < items.length; i++) { |
| 256 |
// Disposes of the contribution item. |
| 257 |
// If Contribution Item is a toolbar then it will dispose of |
| 258 |
// all the nested |
| 259 |
// contribution items. |
| 260 |
items[i].dispose(); |
| 261 |
} |
| 262 |
coolBar.dispose(); |
| 263 |
coolBar = null; |
| 264 |
} |
| 265 |
// If a context menu existed then dispose of it. |
| 266 |
if (contextMenuManager != null) { |
| 267 |
contextMenuManager.dispose(); |
| 268 |
contextMenuManager = null; |
| 269 |
} |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Disposes the given cool item. |
| 275 |
* |
| 276 |
* @param item |
| 277 |
* the cool item to dispose |
| 278 |
*/ |
| 279 |
private void dispose(SCoolItem item) { |
| 280 |
if ((item != null) && !item.isDisposed()) { |
| 281 |
|
| 282 |
item.setData(null); |
| 283 |
Control control = item.getControl(); |
| 284 |
// if the control is already disposed, setting the coolitem |
| 285 |
// control to null will cause an SWT exception, workaround |
| 286 |
// for 19630 |
| 287 |
if ((control != null) && !control.isDisposed()) { |
| 288 |
item.setControl(null); |
| 289 |
} |
| 290 |
item.dispose(); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Finds the cool item associated with the given contribution item. |
| 296 |
* |
| 297 |
* @param item |
| 298 |
* the contribution item |
| 299 |
* @return the associated cool item, or <code>null</code> if not found |
| 300 |
*/ |
| 301 |
private SCoolItem findCoolItem(IContributionItem item) { |
| 302 |
if (coolBar == null) |
| 303 |
return null; |
| 304 |
SCoolItem[] items = coolBar.getItems(); |
| 305 |
for (int i = 0; i < items.length; i++) { |
| 306 |
SCoolItem coolItem = items[i]; |
| 307 |
IContributionItem data = (IContributionItem) coolItem.getData(); |
| 308 |
if (data != null && data.equals(item)) |
| 309 |
return coolItem; |
| 310 |
} |
| 311 |
return null; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Return a consistent set of wrap indices. The return value will always |
| 316 |
* include at least one entry and the first entry will always be zero. |
| 317 |
* CoolBar.getWrapIndices() is inconsistent in whether or not it returns an |
| 318 |
* index for the first row. |
| 319 |
* |
| 320 |
* @param wraps |
| 321 |
* the wrap indicies from the cool bar widget |
| 322 |
* @return the adjusted wrap indicies. |
| 323 |
*/ |
| 324 |
private int[] getAdjustedWrapIndices(int[] wraps) { |
| 325 |
int[] adjustedWrapIndices; |
| 326 |
if (wraps.length == 0) { |
| 327 |
adjustedWrapIndices = new int[] { 0 }; |
| 328 |
} else { |
| 329 |
if (wraps[0] != 0) { |
| 330 |
adjustedWrapIndices = new int[wraps.length + 1]; |
| 331 |
adjustedWrapIndices[0] = 0; |
| 332 |
for (int i = 0; i < wraps.length; i++) { |
| 333 |
adjustedWrapIndices[i + 1] = wraps[i]; |
| 334 |
} |
| 335 |
} else { |
| 336 |
adjustedWrapIndices = wraps; |
| 337 |
} |
| 338 |
} |
| 339 |
return adjustedWrapIndices; |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Returns the control of the Menu Manager. If the menu manager does not |
| 344 |
* have a control then one is created. |
| 345 |
* |
| 346 |
* @return menu control associated with manager, or null if none |
| 347 |
*/ |
| 348 |
private Menu getContextMenuControl() { |
| 349 |
if ((contextMenuManager != null) && (coolBar != null)) { |
| 350 |
Menu menuWidget = contextMenuManager.getMenu(); |
| 351 |
if ((menuWidget == null) || (menuWidget.isDisposed())) { |
| 352 |
menuWidget = contextMenuManager.createContextMenu(coolBar); |
| 353 |
} |
| 354 |
return menuWidget; |
| 355 |
} |
| 356 |
return null; |
| 357 |
} |
| 358 |
|
| 359 |
/* |
| 360 |
* (non-Javadoc) |
| 361 |
* |
| 362 |
* @see org.eclipse.jface.action.ICoolBarManager#isLayoutLocked() |
| 363 |
*/ |
| 364 |
public IMenuManager getContextMenuManager() { |
| 365 |
return contextMenuManager; |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Returns the cool bar control for this manager. |
| 370 |
* |
| 371 |
* @return the cool bar control, or <code>null</code> if none |
| 372 |
*/ |
| 373 |
public CoolBar getControl() { |
| 374 |
return null; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Returns an array list of all the contribution items in the manager. |
| 379 |
* |
| 380 |
* @return an array list of contribution items. |
| 381 |
*/ |
| 382 |
private ArrayList getItemList() { |
| 383 |
IContributionItem[] cbItems = getItems(); |
| 384 |
ArrayList list = new ArrayList(cbItems.length); |
| 385 |
for (int i = 0; i < cbItems.length; i++) { |
| 386 |
list.add(cbItems[i]); |
| 387 |
} |
| 388 |
return list; |
| 389 |
} |
| 390 |
|
| 391 |
/* |
| 392 |
* (non-Javadoc) |
| 393 |
* |
| 394 |
* @see org.eclipse.jface.action.ICoolBarManager#isLayoutLocked() |
| 395 |
*/ |
| 396 |
public boolean getLockLayout() { |
| 397 |
if (!coolBarExist()) { |
| 398 |
return false; |
| 399 |
} |
| 400 |
return coolBar.getLocked(); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Returns the number of rows that should be displayed visually. |
| 405 |
* |
| 406 |
* @param items |
| 407 |
* the array of contributin items |
| 408 |
* @return the number of rows |
| 409 |
*/ |
| 410 |
private int getNumRows(IContributionItem[] items) { |
| 411 |
int numRows = 1; |
| 412 |
boolean separatorFound = false; |
| 413 |
for (int i = 0; i < items.length; i++) { |
| 414 |
if (items[i].isSeparator()) { |
| 415 |
separatorFound = true; |
| 416 |
} |
| 417 |
if ((separatorFound) && (items[i].isVisible()) |
| 418 |
&& (!items[i].isGroupMarker()) && (!items[i].isSeparator())) { |
| 419 |
numRows++; |
| 420 |
separatorFound = false; |
| 421 |
} |
| 422 |
} |
| 423 |
return numRows; |
| 424 |
} |
| 425 |
|
| 426 |
/* |
| 427 |
* (non-Javadoc) |
| 428 |
* |
| 429 |
* @see org.eclipse.jface.action.ICoolBarManager#getStyle() |
| 430 |
*/ |
| 431 |
public int getStyle() { |
| 432 |
return itemStyle; |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Subclasses may extend this <code>ContributionManager</code> method, |
| 437 |
* but must call <code>super.itemAdded</code>. |
| 438 |
* |
| 439 |
* @see org.eclipse.jface.action.ContributionManager#itemAdded(org.eclipse.jface.action.IContributionItem) |
| 440 |
*/ |
| 441 |
protected void itemAdded(IContributionItem item) { |
| 442 |
Assert.isNotNull(item); |
| 443 |
super.itemAdded(item); |
| 444 |
int insertedAt = indexOf(item); |
| 445 |
boolean replaced = false; |
| 446 |
final int size = cbItemsCreationOrder.size(); |
| 447 |
for (int i = 0; i < size; i++) { |
| 448 |
IContributionItem created = (IContributionItem) cbItemsCreationOrder |
| 449 |
.get(i); |
| 450 |
if (created.getId() != null && created.getId().equals(item.getId())) { |
| 451 |
cbItemsCreationOrder.set(i, item); |
| 452 |
replaced = true; |
| 453 |
break; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
if (!replaced) { |
| 458 |
cbItemsCreationOrder.add(Math.min(Math.max(insertedAt, 0), |
| 459 |
cbItemsCreationOrder.size()), item); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Subclasses may extend this <code>ContributionManager</code> method, |
| 465 |
* but must call <code>super.itemRemoved</code>. |
| 466 |
* |
| 467 |
* @see org.eclipse.jface.action.ContributionManager#itemRemoved(org.eclipse.jface.action.IContributionItem) |
| 468 |
*/ |
| 469 |
protected void itemRemoved(IContributionItem item) { |
| 470 |
Assert.isNotNull(item); |
| 471 |
super.itemRemoved(item); |
| 472 |
SCoolItem coolItem = findCoolItem(item); |
| 473 |
if (coolItem != null) { |
| 474 |
coolItem.setData(null); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
/** |
| 479 |
* Positions the list iterator to the starting of the next row. By calling |
| 480 |
* next on the returned iterator, it will return the first element of the |
| 481 |
* next row. |
| 482 |
* |
| 483 |
* @param iterator |
| 484 |
* the list iterator of contribution items |
| 485 |
* @param ignoreCurrentItem |
| 486 |
* Whether the current item in the iterator should be considered |
| 487 |
* (as well as subsequent items). |
| 488 |
*/ |
| 489 |
private void nextRow(ListIterator iterator, boolean ignoreCurrentItem) { |
| 490 |
|
| 491 |
IContributionItem currentElement = null; |
| 492 |
if (!ignoreCurrentItem && iterator.hasPrevious()) { |
| 493 |
currentElement = (IContributionItem) iterator.previous(); |
| 494 |
iterator.next(); |
| 495 |
} |
| 496 |
|
| 497 |
if ((currentElement != null) && (currentElement.isSeparator())) { |
| 498 |
collapseSeparators(iterator); |
| 499 |
return; |
| 500 |
} else { |
| 501 |
//Find next separator |
| 502 |
while (iterator.hasNext()) { |
| 503 |
IContributionItem item = (IContributionItem) iterator.next(); |
| 504 |
if (item.isSeparator()) { |
| 505 |
// we we find a separator, collapse any consecutive |
| 506 |
// separators |
| 507 |
// and return |
| 508 |
collapseSeparators(iterator); |
| 509 |
return; |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
/* |
| 516 |
* Used for debuging. Prints all the items in the internal structures. |
| 517 |
*/ |
| 518 |
// private void printContributions(ArrayList contributionList) { |
| 519 |
// int index = 0; |
| 520 |
// System.out.println("----------------------------------\n"); //$NON-NLS-1$ |
| 521 |
// for (Iterator i = contributionList.iterator(); i.hasNext(); index++) { |
| 522 |
// IContributionItem item = (IContributionItem) i.next(); |
| 523 |
// if (item.isSeparator()) { |
| 524 |
// System.out.println("Separator"); //$NON-NLS-1$ |
| 525 |
// } else { |
| 526 |
// System.out.println(index + ". Item id: " + item.getId() //$NON-NLS-1$ |
| 527 |
// + " - is Visible: " //$NON-NLS-1$ |
| 528 |
// + item.isVisible()); |
| 529 |
// } |
| 530 |
// } |
| 531 |
// } |
| 532 |
/** |
| 533 |
* Synchronizes the visual order of the cool items in the control with this |
| 534 |
* manager's internal data structures. This method should be called before |
| 535 |
* requesting the order of the contribution items to ensure that the order |
| 536 |
* is accurate. |
| 537 |
* <p> |
| 538 |
* Note that <code>update()</code> and <code>refresh()</code> are |
| 539 |
* converses: <code>update()</code> changes the visual order to match the |
| 540 |
* internal structures, and <code>refresh</code> changes the internal |
| 541 |
* structures to match the visual order. |
| 542 |
* </p> |
| 543 |
*/ |
| 544 |
public void refresh() { |
| 545 |
if (!coolBarExist()) { |
| 546 |
return; |
| 547 |
} |
| 548 |
|
| 549 |
// Retreives the list of contribution items as an array list |
| 550 |
ArrayList contributionList = getItemList(); |
| 551 |
|
| 552 |
// Check the size of the list |
| 553 |
if (contributionList.size() == 0) |
| 554 |
return; |
| 555 |
|
| 556 |
// The list of all the cool items in their visual order |
| 557 |
SCoolItem[] coolItems = coolBar.getItems(); |
| 558 |
// The wrap indicies of the coolbar |
| 559 |
int[] wrapIndicies = getAdjustedWrapIndices(coolBar.getWrapIndices()); |
| 560 |
|
| 561 |
int row = 0; |
| 562 |
int coolItemIndex = 0; |
| 563 |
|
| 564 |
// Traverse through all cool items in the coolbar add them to a new |
| 565 |
// data structure |
| 566 |
// in the correct order |
| 567 |
ArrayList displayedItems = new ArrayList(coolBar.getItemCount()); |
| 568 |
for (int i = 0; i < coolItems.length; i++) { |
| 569 |
SCoolItem coolItem = coolItems[i]; |
| 570 |
if (coolItem.getData() instanceof IContributionItem) { |
| 571 |
IContributionItem cbItem = (IContributionItem) coolItem |
| 572 |
.getData(); |
| 573 |
displayedItems.add(Math.min(i, displayedItems.size()), cbItem); |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
// Add separators to the displayed Items data structure |
| 578 |
int offset = 0; |
| 579 |
for (int i = 1; i < wrapIndicies.length; i++) { |
| 580 |
int insertAt = wrapIndicies[i] + offset; |
| 581 |
displayedItems.add(insertAt, new Separator(USER_SEPARATOR)); |
| 582 |
offset++; |
| 583 |
} |
| 584 |
|
| 585 |
// Determine which rows are invisible |
| 586 |
ArrayList existingVisibleRows = new ArrayList(4); |
| 587 |
ListIterator rowIterator = contributionList.listIterator(); |
| 588 |
collapseSeparators(rowIterator); |
| 589 |
int numRow = 0; |
| 590 |
while (rowIterator.hasNext()) { |
| 591 |
// Scan row |
| 592 |
while (rowIterator.hasNext()) { |
| 593 |
IContributionItem cbItem = (IContributionItem) rowIterator |
| 594 |
.next(); |
| 595 |
if (displayedItems.contains(cbItem)) { |
| 596 |
existingVisibleRows.add(new Integer(numRow)); |
| 597 |
break; |
| 598 |
} |
| 599 |
if (cbItem.isSeparator()) { |
| 600 |
break; |
| 601 |
} |
| 602 |
} |
| 603 |
nextRow(rowIterator, false); |
| 604 |
numRow++; |
| 605 |
} |
| 606 |
|
| 607 |
Iterator existingRows = existingVisibleRows.iterator(); |
| 608 |
// Adjust row number to the first visible |
| 609 |
if (existingRows.hasNext()) { |
| 610 |
row = ((Integer) existingRows.next()).intValue(); |
| 611 |
} |
| 612 |
|
| 613 |
HashMap itemLocation = new HashMap(); |
| 614 |
for (ListIterator locationIterator = displayedItems.listIterator(); locationIterator |
| 615 |
.hasNext();) { |
| 616 |
IContributionItem item = (IContributionItem) locationIterator |
| 617 |
.next(); |
| 618 |
if (item.isSeparator()) { |
| 619 |
if (existingRows.hasNext()) { |
| 620 |
Integer value = (Integer) existingRows.next(); |
| 621 |
row = value.intValue(); |
| 622 |
} else { |
| 623 |
row++; |
| 624 |
} |
| 625 |
} else { |
| 626 |
itemLocation.put(item, new Integer(row)); |
| 627 |
} |
| 628 |
|
| 629 |
} |
| 630 |
|
| 631 |
// Insert the contribution items in their correct location |
| 632 |
for (ListIterator iterator = displayedItems.listIterator(); iterator |
| 633 |
.hasNext();) { |
| 634 |
IContributionItem cbItem = (IContributionItem) iterator.next(); |
| 635 |
if (cbItem.isSeparator()) { |
| 636 |
coolItemIndex = 0; |
| 637 |
} else { |
| 638 |
relocate(cbItem, coolItemIndex, contributionList, itemLocation); |
| 639 |
cbItem.saveWidgetState(); |
| 640 |
coolItemIndex++; |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
if (contributionList.size() != 0) { |
| 645 |
contributionList = adjustContributionList(contributionList); |
| 646 |
IContributionItem[] array = new IContributionItem[contributionList |
| 647 |
.size() - 1]; |
| 648 |
array = (IContributionItem[]) contributionList.toArray(array); |
| 649 |
internalSetItems(array); |
| 650 |
} |
| 651 |
|
| 652 |
} |
| 653 |
|
| 654 |
/** |
| 655 |
* Relocates the given contribution item to the specified index. |
| 656 |
* |
| 657 |
* @param cbItem |
| 658 |
* the conribution item to relocate |
| 659 |
* @param index |
| 660 |
* the index to locate this item |
| 661 |
* @param contributionList |
| 662 |
* the current list of conrtributions |
| 663 |
* @param itemLocation |
| 664 |
*/ |
| 665 |
private void relocate(IContributionItem cbItem, int index, |
| 666 |
ArrayList contributionList, HashMap itemLocation) { |
| 667 |
|
| 668 |
if (!(itemLocation.get(cbItem) instanceof Integer)) |
| 669 |
return; |
| 670 |
int targetRow = ((Integer) itemLocation.get(cbItem)).intValue(); |
| 671 |
|
| 672 |
int cbInternalIndex = contributionList.indexOf(cbItem); |
| 673 |
|
| 674 |
// by default add to end of list |
| 675 |
int insertAt = contributionList.size(); |
| 676 |
// Find the row to place this item in. |
| 677 |
ListIterator iterator = contributionList.listIterator(); |
| 678 |
// bypass any separators at the begining |
| 679 |
collapseSeparators(iterator); |
| 680 |
int currentRow = -1; |
| 681 |
while (iterator.hasNext()) { |
| 682 |
|
| 683 |
currentRow++; |
| 684 |
if (currentRow == targetRow) { |
| 685 |
// We found the row to insert the item |
| 686 |
int virtualIndex = 0; |
| 687 |
insertAt = iterator.nextIndex(); |
| 688 |
// first check the position of the current element (item) |
| 689 |
// then get the next element |
| 690 |
while (iterator.hasNext()) { |
| 691 |
IContributionItem item = (IContributionItem) iterator |
| 692 |
.next(); |
| 693 |
Integer itemRow = (Integer) itemLocation.get(item); |
| 694 |
if (item.isSeparator()) |
| 695 |
break; |
| 696 |
// if the item has an associate widget |
| 697 |
if ((itemRow != null) && (itemRow.intValue() == targetRow)) { |
| 698 |
// if the next element is the index we are looking for |
| 699 |
// then break |
| 700 |
if (virtualIndex >= index) |
| 701 |
break; |
| 702 |
virtualIndex++; |
| 703 |
|
| 704 |
} |
| 705 |
insertAt++; |
| 706 |
} |
| 707 |
// If we don't need to move it then we return |
| 708 |
if (cbInternalIndex == insertAt) |
| 709 |
return; |
| 710 |
break; |
| 711 |
} |
| 712 |
nextRow(iterator, true); |
| 713 |
} |
| 714 |
contributionList.remove(cbItem); |
| 715 |
|
| 716 |
// Adjust insertAt index |
| 717 |
if (cbInternalIndex < insertAt) { |
| 718 |
insertAt--; |
| 719 |
} |
| 720 |
|
| 721 |
// if we didn't find the row then add a new row |
| 722 |
if (currentRow != targetRow) { |
| 723 |
contributionList.add(new Separator(USER_SEPARATOR)); |
| 724 |
insertAt = contributionList.size(); |
| 725 |
} |
| 726 |
insertAt = Math.min(insertAt, contributionList.size()); |
| 727 |
contributionList.add(insertAt, cbItem); |
| 728 |
|
| 729 |
} |
| 730 |
|
| 731 |
/** |
| 732 |
* Restores the canonical order of this cool bar manager. The canonical |
| 733 |
* order is the order in which the contribution items where added. |
| 734 |
*/ |
| 735 |
public void resetItemOrder() { |
| 736 |
for (ListIterator iterator = cbItemsCreationOrder.listIterator(); iterator |
| 737 |
.hasNext();) { |
| 738 |
IContributionItem item = (IContributionItem) iterator.next(); |
| 739 |
// if its a user separator then do not include in original order. |
| 740 |
if ((item.getId() != null) && (item.getId().equals(USER_SEPARATOR))) { |
| 741 |
iterator.remove(); |
| 742 |
} |
| 743 |
} |
| 744 |
IContributionItem[] itemsToSet = new IContributionItem[cbItemsCreationOrder |
| 745 |
.size()]; |
| 746 |
cbItemsCreationOrder.toArray(itemsToSet); |
| 747 |
setItems(itemsToSet); |
| 748 |
} |
| 749 |
|
| 750 |
/* |
| 751 |
* (non-Javadoc) |
| 752 |
* |
| 753 |
* @see org.eclipse.jface.action.ICoolBarManager#setContextMenuManager(org.eclipse.jface.action.IMenuManager) |
| 754 |
*/ |
| 755 |
public void setContextMenuManager(IMenuManager contextMenuManager) { |
| 756 |
this.contextMenuManager = (MenuManager) contextMenuManager; |
| 757 |
if (coolBar != null) { |
| 758 |
coolBar.setMenu(getContextMenuControl()); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
/** |
| 763 |
* Replaces the current items with the given items. |
| 764 |
* Forces an update. |
| 765 |
* |
| 766 |
* @param newItems the items with which to replace the current items |
| 767 |
*/ |
| 768 |
public void setItems(IContributionItem[] newItems) { |
| 769 |
// dispose of all the cool items on the cool bar manager |
| 770 |
/* FIXME TFS |
| 771 |
* This is disposing of the coolItems but references are still hanging around. Might |
| 772 |
* be a problem with SCoolBar and or SCoolItem. [Terry Smith 12/9/2005] |
| 773 |
* |
| 774 |
if (coolBar != null) { |
| 775 |
SCoolItem[] coolItems = coolBar.getItems(); |
| 776 |
for (int i = 0; i < coolItems.length; i++) { |
| 777 |
dispose(coolItems[i]); |
| 778 |
} |
| 779 |
} |
| 780 |
// Set the internal structure to this order |
| 781 |
internalSetItems(newItems); |
| 782 |
*/ |
| 783 |
// Force and update |
| 784 |
update(true); |
| 785 |
} |
| 786 |
|
| 787 |
/* |
| 788 |
* (non-Javadoc) |
| 789 |
* |
| 790 |
* @see org.eclipse.jface.action.ICoolBarManager#lockLayout(boolean) |
| 791 |
*/ |
| 792 |
public void setLockLayout(boolean value) { |
| 793 |
if (!coolBarExist()) { |
| 794 |
return; |
| 795 |
} |
| 796 |
coolBar.setLocked(value); |
| 797 |
} |
| 798 |
|
| 799 |
/** |
| 800 |
* Subclasses may extend this <code>IContributionManager</code> method, |
| 801 |
* but must call <code>super.update</code>. |
| 802 |
* |
| 803 |
* @see org.eclipse.jface.action.IContributionManager#update(boolean) |
| 804 |
*/ |
| 805 |
public void update(boolean force) { |
| 806 |
if ((!isDirty() && !force) || (!coolBarExist())) { |
| 807 |
return; |
| 808 |
} |
| 809 |
|
| 810 |
boolean relock = false; |
| 811 |
boolean changed = false; |
| 812 |
|
| 813 |
try { |
| 814 |
coolBar.setRedraw(false); |
| 815 |
|
| 816 |
// Refresh the widget data with the internal data structure. |
| 817 |
refresh(); |
| 818 |
|
| 819 |
if (coolBar.getLocked()) { |
| 820 |
coolBar.setLocked(false); |
| 821 |
relock = true; |
| 822 |
} |
| 823 |
|
| 824 |
/* |
| 825 |
* Make a list of items including only those items that are |
| 826 |
* visible. Separators should stay because they mark line breaks in |
| 827 |
* a cool bar. |
| 828 |
*/ |
| 829 |
final IContributionItem[] items = getItems(); |
| 830 |
final List visibleItems = new ArrayList(items.length); |
| 831 |
for (int i = 0; i < items.length; i++) { |
| 832 |
final IContributionItem item = items[i]; |
| 833 |
if (item.isVisible()) { |
| 834 |
visibleItems.add(item); |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
/* |
| 839 |
* Make a list of CoolItem widgets in the cool bar for which there |
| 840 |
* is no current visible contribution item. These are the widgets |
| 841 |
* to be disposed. Dynamic items are also removed. |
| 842 |
*/ |
| 843 |
SCoolItem[] coolItems = coolBar.getItems(); |
| 844 |
final ArrayList coolItemsToRemove = new ArrayList(coolItems.length); |
| 845 |
for (int i = 0; i < coolItems.length; i++) { |
| 846 |
final Object data = coolItems[i].getData(); |
| 847 |
if ((data == null) |
| 848 |
|| (!visibleItems.contains(data)) |
| 849 |
|| ((data instanceof IContributionItem) && ((IContributionItem) data) |
| 850 |
.isDynamic())) { |
| 851 |
coolItemsToRemove.add(coolItems[i]); |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
// Dispose of any items in the list to be removed. |
| 856 |
for (int i = coolItemsToRemove.size() - 1; i >= 0; i--) { |
| 857 |
SCoolItem coolItem = (SCoolItem) coolItemsToRemove.get(i); |
| 858 |
if (!coolItem.isDisposed()) { |
| 859 |
Control control = coolItem.getControl(); |
| 860 |
if (control != null) { |
| 861 |
coolItem.setControl(null); |
| 862 |
control.dispose(); |
| 863 |
} |
| 864 |
coolItem.dispose(); |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
// Add any new items by telling them to fill. |
| 869 |
coolItems = coolBar.getItems(); |
| 870 |
IContributionItem sourceItem; |
| 871 |
IContributionItem destinationItem; |
| 872 |
int sourceIndex = 0; |
| 873 |
int destinationIndex = 0; |
| 874 |
final Iterator visibleItemItr = visibleItems.iterator(); |
| 875 |
while (visibleItemItr.hasNext()) { |
| 876 |
sourceItem = (IContributionItem) visibleItemItr.next(); |
| 877 |
|
| 878 |
// Retrieve the corresponding contribution item from SWT's |
| 879 |
// data. |
| 880 |
if (sourceIndex < coolItems.length) { |
| 881 |
destinationItem = (IContributionItem) coolItems[sourceIndex] |
| 882 |
.getData(); |
| 883 |
} else { |
| 884 |
destinationItem = null; |
| 885 |
} |
| 886 |
|
| 887 |
// The items match is they are equal or both separators. |
| 888 |
if (destinationItem != null) { |
| 889 |
if (sourceItem.equals(destinationItem)) { |
| 890 |
sourceIndex++; |
| 891 |
destinationIndex++; |
| 892 |
sourceItem.update(); |
| 893 |
continue; |
| 894 |
|
| 895 |
} else if ((destinationItem.isSeparator()) |
| 896 |
&& (sourceItem.isSeparator())) { |
| 897 |
coolItems[sourceIndex].setData(sourceItem); |
| 898 |
sourceIndex++; |
| 899 |
destinationIndex++; |
| 900 |
sourceItem.update(); |
| 901 |
continue; |
| 902 |
|
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
// Otherwise, a new item has to be added. |
| 907 |
final int start = coolBar.getItemCount(); |
| 908 |
if (sourceItem instanceof ISContributionItem) |
| 909 |
((ISContributionItem)sourceItem).fill(coolBar, destinationIndex); |
| 910 |
final int newItems = coolBar.getItemCount() - start; |
| 911 |
for (int i = 0; i < newItems; i++) { |
| 912 |
coolBar.getItem(destinationIndex++).setData(sourceItem); |
| 913 |
} |
| 914 |
changed = true; |
| 915 |
} |
| 916 |
|
| 917 |
// Remove any old widgets not accounted for. |
| 918 |
for (int i = coolItems.length - 1; i >= sourceIndex; i--) { |
| 919 |
final SCoolItem item = coolItems[i]; |
| 920 |
if (!item.isDisposed()) { |
| 921 |
Control control = item.getControl(); |
| 922 |
if (control != null) { |
| 923 |
item.setControl(null); |
| 924 |
control.dispose(); |
| 925 |
} |
| 926 |
item.dispose(); |
| 927 |
changed = true; |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
// Update wrap indices. |
| 932 |
updateWrapIndices(); |
| 933 |
|
| 934 |
// Update the sizes. |
| 935 |
for (int i = 0; i < items.length; i++) { |
| 936 |
IContributionItem item = items[i]; |
| 937 |
item.update(SIZE); |
| 938 |
} |
| 939 |
|
| 940 |
// if the coolBar was previously locked then lock it |
| 941 |
if (relock) { |
| 942 |
coolBar.setLocked(true); |
| 943 |
} |
| 944 |
|
| 945 |
if (changed) { |
| 946 |
updateTabOrder(); |
| 947 |
} |
| 948 |
|
| 949 |
// We are no longer dirty. |
| 950 |
setDirty(false); |
| 951 |
} finally { |
| 952 |
coolBar.setRedraw(true); |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
/** |
| 957 |
* Sets the tab order of the coolbar to the visual order of its items. |
| 958 |
*/ |
| 959 |
/* package */void updateTabOrder() { |
| 960 |
if (coolBar != null) { |
| 961 |
SCoolItem[] items = coolBar.getItems(); |
| 962 |
if (items != null) { |
| 963 |
ArrayList children = new ArrayList(items.length); |
| 964 |
for (int i = 0; i < items.length; i++) { |
| 965 |
if ((items[i].getControl() != null) |
| 966 |
&& (!items[i].getControl().isDisposed())) { |
| 967 |
children.add(items[i].getControl()); |
| 968 |
} |
| 969 |
} |
| 970 |
// Convert array |
| 971 |
Control[] childrenArray = new Control[0]; |
| 972 |
childrenArray = (Control[]) children.toArray(childrenArray); |
| 973 |
|
| 974 |
if (childrenArray != null) { |
| 975 |
coolBar.setTabList(childrenArray); |
| 976 |
} |
| 977 |
|
| 978 |
} |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
/** |
| 983 |
* Updates the indices at which the cool bar should wrap. |
| 984 |
*/ |
| 985 |
private void updateWrapIndices() { |
| 986 |
final IContributionItem[] items = getItems(); |
| 987 |
final int numRows = getNumRows(items) - 1; |
| 988 |
|
| 989 |
// Generate the list of wrap indices. |
| 990 |
final int[] wrapIndices = new int[numRows]; |
| 991 |
boolean foundSeparator = false; |
| 992 |
int j = 0; |
| 993 |
for (int i = 0; i < items.length; i++) { |
| 994 |
IContributionItem item = items[i]; |
| 995 |
SCoolItem coolItem = findCoolItem(item); |
| 996 |
if (item.isSeparator()) { |
| 997 |
foundSeparator = true; |
| 998 |
} |
| 999 |
if ((!item.isSeparator()) && (!item.isGroupMarker()) |
| 1000 |
&& (item.isVisible()) && (coolItem != null) |
| 1001 |
&& (foundSeparator)) { |
| 1002 |
wrapIndices[j] = coolBar.indexOf(coolItem); |
| 1003 |
j++; |
| 1004 |
foundSeparator = false; |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
/* |
| 1009 |
* Check to see if these new wrap indices are different than the old |
| 1010 |
* ones. |
| 1011 |
*/ |
| 1012 |
final int[] oldIndices = coolBar.getWrapIndices(); |
| 1013 |
boolean shouldUpdate = false; |
| 1014 |
if (oldIndices.length == wrapIndices.length) { |
| 1015 |
for (int i = 0; i < oldIndices.length; i++) { |
| 1016 |
if (oldIndices[i] != wrapIndices[i]) { |
| 1017 |
shouldUpdate = true; |
| 1018 |
break; |
| 1019 |
} |
| 1020 |
} |
| 1021 |
} else { |
| 1022 |
shouldUpdate = true; |
| 1023 |
} |
| 1024 |
|
| 1025 |
if (shouldUpdate) { |
| 1026 |
coolBar.setWrapIndices(wrapIndices); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
public CoolBar createControl(Composite parent) { |
| 1031 |
return null; |
| 1032 |
} |
| 1033 |
|
| 1034 |
public Control getControl2() { |
| 1035 |
return coolBar; |
| 1036 |
} |
| 1037 |
} |