Search in sources :

Example 1 with IToolBarManager2

use of org.eclipse.jface.internal.provisional.action.IToolBarManager2 in project eclipse.platform.ui by eclipse-platform.

the class CoolBarManager method update.

/**
 * Subclasses may extend this <code>IContributionManager</code> method, but must
 * call <code>super.update</code>.
 *
 * @see org.eclipse.jface.action.IContributionManager#update(boolean)
 */
@Override
public void update(boolean force) {
    if ((!isDirty() && !force) || (!coolBarExist())) {
        return;
    }
    boolean relock = false;
    boolean changed = false;
    try {
        coolBar.setRedraw(false);
        // Refresh the widget data with the internal data structure.
        refresh();
        if (coolBar.getLocked()) {
            coolBar.setLocked(false);
            relock = true;
        }
        /*
			 * Make a list of items including only those items that are visible. Separators
			 * should stay because they mark line breaks in a cool bar.
			 */
        final IContributionItem[] items = getItems();
        final List<IContributionItem> visibleItems = new ArrayList<>(items.length);
        for (final IContributionItem item : items) {
            if (isChildVisible(item)) {
                visibleItems.add(item);
            }
        }
        /*
			 * Make a list of CoolItem widgets in the cool bar for which there is no current
			 * visible contribution item. These are the widgets to be disposed. Dynamic
			 * items are also removed.
			 */
        CoolItem[] coolItems = coolBar.getItems();
        final ArrayList<CoolItem> coolItemsToRemove = new ArrayList<>(coolItems.length);
        for (CoolItem coolItem : coolItems) {
            final Object data = coolItem.getData();
            if ((data == null) || (!visibleItems.contains(data)) || ((data instanceof IContributionItem) && ((IContributionItem) data).isDynamic())) {
                coolItemsToRemove.add(coolItem);
            }
        }
        // Dispose of any items in the list to be removed.
        for (int i = coolItemsToRemove.size() - 1; i >= 0; i--) {
            CoolItem coolItem = coolItemsToRemove.get(i);
            if (!coolItem.isDisposed()) {
                Control control = coolItem.getControl();
                if (control != null) {
                    coolItem.setControl(null);
                    control.dispose();
                }
                coolItem.dispose();
            }
        }
        // Add any new items by telling them to fill.
        coolItems = coolBar.getItems();
        IContributionItem sourceItem;
        IContributionItem destinationItem;
        int sourceIndex = 0;
        int destinationIndex = 0;
        final Iterator<IContributionItem> visibleItemItr = visibleItems.iterator();
        while (visibleItemItr.hasNext()) {
            sourceItem = visibleItemItr.next();
            // data.
            if (sourceIndex < coolItems.length) {
                destinationItem = (IContributionItem) coolItems[sourceIndex].getData();
            } else {
                destinationItem = null;
            }
            // The items match is they are equal or both separators.
            if (destinationItem != null) {
                if (sourceItem.equals(destinationItem)) {
                    sourceIndex++;
                    destinationIndex++;
                    sourceItem.update();
                    continue;
                } else if ((destinationItem.isSeparator()) && (sourceItem.isSeparator())) {
                    coolItems[sourceIndex].setData(sourceItem);
                    sourceIndex++;
                    destinationIndex++;
                    sourceItem.update();
                    continue;
                }
            }
            // Otherwise, a new item has to be added.
            final int start = coolBar.getItemCount();
            if (sourceItem instanceof ToolBarContributionItem) {
                IToolBarManager manager = ((ToolBarContributionItem) sourceItem).getToolBarManager();
                if (manager instanceof IToolBarManager2) {
                    ((IToolBarManager2) manager).setOverrides(getOverrides());
                }
            }
            sourceItem.fill(coolBar, destinationIndex);
            final int newItems = coolBar.getItemCount() - start;
            for (int i = 0; i < newItems; i++) {
                coolBar.getItem(destinationIndex++).setData(sourceItem);
            }
            changed = true;
        }
        // Remove any old widgets not accounted for.
        for (int i = coolItems.length - 1; i >= sourceIndex; i--) {
            final CoolItem item = coolItems[i];
            if (!item.isDisposed()) {
                Control control = item.getControl();
                if (control != null) {
                    item.setControl(null);
                    control.dispose();
                }
                item.dispose();
                changed = true;
            }
        }
        // Update wrap indices.
        updateWrapIndices();
        // Update the sizes.
        for (IContributionItem item : items) {
            item.update(SIZE);
        }
        // if the coolBar was previously locked then lock it
        if (relock) {
            coolBar.setLocked(true);
        }
        if (changed) {
            updateTabOrder();
        }
        // We are no longer dirty.
        setDirty(false);
    } finally {
        coolBar.setRedraw(true);
    }
}
Also used : ArrayList(java.util.ArrayList) IToolBarManager2(org.eclipse.jface.internal.provisional.action.IToolBarManager2) Control(org.eclipse.swt.widgets.Control) CoolItem(org.eclipse.swt.widgets.CoolItem)

Aggregations

ArrayList (java.util.ArrayList)1 IToolBarManager2 (org.eclipse.jface.internal.provisional.action.IToolBarManager2)1 Control (org.eclipse.swt.widgets.Control)1 CoolItem (org.eclipse.swt.widgets.CoolItem)1