KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > services > MenuSourceProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.services;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.ui.AbstractSourceProvider;
21 import org.eclipse.ui.ISources;
22
23 /**
24  * <p>
25  * A listener to changes in the showing menus. This is used to activate handlers
26  * just for the span of a menu being shown. This is needed for full support for
27  * legacy action-based extension points.
28  * </p>
29  * <p>
30  * This class is only intended for internal use within
31  * <code>org.eclipse.ui.workbench</code>.
32  * </p>
33  *
34  * @since 3.2
35  */

36 public final class MenuSourceProvider extends AbstractSourceProvider {
37
38     /**
39      * The names of the sources supported by this source provider.
40      */

41     private static final String JavaDoc[] PROVIDED_SOURCE_NAMES = new String JavaDoc[] {
42             ISources.ACTIVE_MENU_NAME, ISources.ACTIVE_MENU_SELECTION_NAME,
43             ISources.ACTIVE_MENU_EDITOR_INPUT_NAME };
44
45     /**
46      * The menu ids that are currently showing, as known by this source
47      * provider. This value may be <code>null</code>.
48      */

49     private Set JavaDoc menuIds = new HashSet JavaDoc();
50
51     /**
52      * Adds all of the given menu identifiers as being shown.
53      *
54      * @param menuIds
55      * The ids of the menu that is now showing; must not be
56      * <code>null</code>.
57      */

58     public final void addShowingMenus(final Set JavaDoc menuIds,
59             final ISelection localSelection, final ISelection localEditorInput) {
60         this.menuIds.addAll(menuIds);
61         if (DEBUG) {
62             logDebuggingInfo("Menu ids changed to " + this.menuIds); //$NON-NLS-1$
63
}
64         Map JavaDoc m = new HashMap JavaDoc();
65         m.put(ISources.ACTIVE_MENU_NAME, this.menuIds);
66         if (selection != localSelection) {
67             selection = localSelection;
68             m.put(ISources.ACTIVE_MENU_SELECTION_NAME, selection);
69         }
70         if (input != localEditorInput) {
71             input = localEditorInput;
72             m.put(ISources.ACTIVE_MENU_EDITOR_INPUT_NAME, input);
73         }
74
75         fireSourceChanged(ISources.ACTIVE_MENU, m);
76     }
77
78     public final void dispose() {
79         menuIds.clear();
80         selection = null;
81         input = null;
82     }
83
84     public final Map JavaDoc getCurrentState() {
85         final Map JavaDoc state = new HashMap JavaDoc();
86         state.put(ISources.ACTIVE_MENU_NAME, menuIds);
87         state.put(ISources.ACTIVE_MENU_SELECTION_NAME, selection);
88         state.put(ISources.ACTIVE_MENU_EDITOR_INPUT_NAME, input);
89         return state;
90     }
91
92     public final String JavaDoc[] getProvidedSourceNames() {
93         return PROVIDED_SOURCE_NAMES;
94     }
95
96     /**
97      * Removes all of the given menu identifiers as being shown.
98      *
99      * @param menuIds
100      * The ids of the menu that is no longer shown; must not be
101      * <code>null</code>.
102      */

103     public final void removeShowingMenus(final Set JavaDoc menuIds,
104             final ISelection localSelection, final ISelection localEditorInput) {
105         this.menuIds.removeAll(menuIds);
106         if (DEBUG) {
107             logDebuggingInfo("Menu ids changed to " + this.menuIds); //$NON-NLS-1$
108
}
109         Map JavaDoc m = new HashMap JavaDoc();
110         m.put(ISources.ACTIVE_MENU_NAME, this.menuIds);
111         if (selection != localSelection) {
112             selection = localSelection;
113             m.put(ISources.ACTIVE_MENU_SELECTION_NAME, selection);
114         }
115         if (input != localEditorInput) {
116             input = localEditorInput;
117             m.put(ISources.ACTIVE_MENU_EDITOR_INPUT_NAME, input);
118         }
119         fireSourceChanged(ISources.ACTIVE_MENU, m);
120     }
121
122     private ISelection selection = null;
123     private ISelection input = null;
124 }
125
Popular Tags