KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > menus > ContributionRoot


1 /*******************************************************************************
2  * Copyright (c) 2007 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  ******************************************************************************/

11
12 package org.eclipse.ui.internal.menus;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.expressions.Expression;
20 import org.eclipse.jface.action.IContributionItem;
21 import org.eclipse.ui.internal.expressions.AlwaysEnabledExpression;
22 import org.eclipse.ui.menus.IContributionRoot;
23
24 /**
25  * Default implementation.
26  *
27  * @since 3.3
28  */

29 final class ContributionRoot implements
30         IContributionRoot {
31
32     private List JavaDoc topLevelItems = new ArrayList JavaDoc();
33     private List JavaDoc itemsToExpressions = new ArrayList JavaDoc();
34     private InternalMenuService menuService;
35     private Expression restriction;
36     private String JavaDoc namespace;
37
38     public ContributionRoot(InternalMenuService menuService, Expression restriction, String JavaDoc namespace) {
39         this.menuService = menuService;
40         this.restriction = restriction;
41         this.namespace = namespace;
42     }
43
44     /* (non-Javadoc)
45      * @see org.eclipse.ui.menus.IContributionRoot#addContributionItem(org.eclipse.jface.action.IContributionItem, org.eclipse.core.expressions.Expression, org.eclipse.core.expressions.Expression)
46      */

47     public void addContributionItem(IContributionItem item,
48             Expression visibleWhen) {
49         if (item == null)
50             throw new IllegalArgumentException JavaDoc();
51         topLevelItems.add(item);
52         if (visibleWhen == null)
53             visibleWhen = AlwaysEnabledExpression.INSTANCE;
54         
55         menuService.registerVisibleWhen(item, visibleWhen, restriction,
56                 createIdentifierId(item));
57         itemsToExpressions.add(item);
58     }
59
60     /**
61      * Create the activity identifier for this contribution item.
62      *
63      * @param item the item
64      * @return the identifier
65      */

66     private String JavaDoc createIdentifierId(IContributionItem item) {
67         String JavaDoc identifierID = namespace != null ? namespace + '/'
68                 + item.getId() : null; // create the activity identifier ID. If
69
// this factory doesn't have a namespace
70
// it will be null.
71
return identifierID;
72     }
73
74     public Collection JavaDoc getItems() {
75         return topLevelItems;
76     }
77
78     /**
79      * Unregister all visible when expressions from the menu service.
80      */

81     public void release() {
82         for (Iterator JavaDoc itemIter = itemsToExpressions.iterator(); itemIter.hasNext();) {
83             IContributionItem item = (IContributionItem) itemIter.next();
84             menuService.unregisterVisibleWhen(item);
85         }
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see org.eclipse.ui.menus.IContributionRoot#registerVisibilityForChild(org.eclipse.jface.action.IContributionItem,
92      * org.eclipse.core.expressions.Expression,
93      * org.eclipse.core.expressions.Expression)
94      */

95     public void registerVisibilityForChild(IContributionItem item,
96             Expression visibleWhen) {
97         if (item == null)
98             throw new IllegalArgumentException JavaDoc();
99         if (visibleWhen == null)
100             visibleWhen = AlwaysEnabledExpression.INSTANCE;
101         menuService.registerVisibleWhen(item, visibleWhen, restriction,
102                 createIdentifierId(item));
103         itemsToExpressions.add(item);
104     }
105 }
106
Popular Tags