001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.menus;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.Iterator;
015: import java.util.List;
016:
017: import org.eclipse.core.expressions.Expression;
018: import org.eclipse.jface.action.IContributionItem;
019: import org.eclipse.ui.internal.expressions.AlwaysEnabledExpression;
020: import org.eclipse.ui.menus.IContributionRoot;
021:
022: /**
023: * Default implementation.
024: *
025: * @since 3.3
026: */
027: final class ContributionRoot implements IContributionRoot {
028:
029: private List topLevelItems = new ArrayList();
030: private List itemsToExpressions = new ArrayList();
031: private InternalMenuService menuService;
032: private Expression restriction;
033: private String namespace;
034:
035: public ContributionRoot(InternalMenuService menuService,
036: Expression restriction, String namespace) {
037: this .menuService = menuService;
038: this .restriction = restriction;
039: this .namespace = namespace;
040: }
041:
042: /* (non-Javadoc)
043: * @see org.eclipse.ui.menus.IContributionRoot#addContributionItem(org.eclipse.jface.action.IContributionItem, org.eclipse.core.expressions.Expression, org.eclipse.core.expressions.Expression)
044: */
045: public void addContributionItem(IContributionItem item,
046: Expression visibleWhen) {
047: if (item == null)
048: throw new IllegalArgumentException();
049: topLevelItems.add(item);
050: if (visibleWhen == null)
051: visibleWhen = AlwaysEnabledExpression.INSTANCE;
052:
053: menuService.registerVisibleWhen(item, visibleWhen, restriction,
054: createIdentifierId(item));
055: itemsToExpressions.add(item);
056: }
057:
058: /**
059: * Create the activity identifier for this contribution item.
060: *
061: * @param item the item
062: * @return the identifier
063: */
064: private String createIdentifierId(IContributionItem item) {
065: String identifierID = namespace != null ? namespace + '/'
066: + item.getId() : null; // create the activity identifier ID. If
067: // this factory doesn't have a namespace
068: // it will be null.
069: return identifierID;
070: }
071:
072: public Collection getItems() {
073: return topLevelItems;
074: }
075:
076: /**
077: * Unregister all visible when expressions from the menu service.
078: */
079: public void release() {
080: for (Iterator itemIter = itemsToExpressions.iterator(); itemIter
081: .hasNext();) {
082: IContributionItem item = (IContributionItem) itemIter
083: .next();
084: menuService.unregisterVisibleWhen(item);
085: }
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see org.eclipse.ui.menus.IContributionRoot#registerVisibilityForChild(org.eclipse.jface.action.IContributionItem,
092: * org.eclipse.core.expressions.Expression,
093: * org.eclipse.core.expressions.Expression)
094: */
095: public void registerVisibilityForChild(IContributionItem item,
096: Expression visibleWhen) {
097: if (item == null)
098: throw new IllegalArgumentException();
099: if (visibleWhen == null)
100: visibleWhen = AlwaysEnabledExpression.INSTANCE;
101: menuService.registerVisibleWhen(item, visibleWhen, restriction,
102: createIdentifierId(item));
103: itemsToExpressions.add(item);
104: }
105: }
|