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