001: /*******************************************************************************
002: * Copyright (c) 2006, 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.services;
011:
012: import java.util.HashMap;
013: import java.util.HashSet;
014: import java.util.Map;
015: import java.util.Set;
016:
017: import org.eclipse.jface.viewers.ISelection;
018: import org.eclipse.ui.AbstractSourceProvider;
019: import org.eclipse.ui.ISources;
020:
021: /**
022: * <p>
023: * A listener to changes in the showing menus. This is used to activate handlers
024: * just for the span of a menu being shown. This is needed for full support for
025: * legacy action-based extension points.
026: * </p>
027: * <p>
028: * This class is only intended for internal use within
029: * <code>org.eclipse.ui.workbench</code>.
030: * </p>
031: *
032: * @since 3.2
033: */
034: public final class MenuSourceProvider extends AbstractSourceProvider {
035:
036: /**
037: * The names of the sources supported by this source provider.
038: */
039: private static final String[] PROVIDED_SOURCE_NAMES = new String[] {
040: ISources.ACTIVE_MENU_NAME,
041: ISources.ACTIVE_MENU_SELECTION_NAME,
042: ISources.ACTIVE_MENU_EDITOR_INPUT_NAME };
043:
044: /**
045: * The menu ids that are currently showing, as known by this source
046: * provider. This value may be <code>null</code>.
047: */
048: private Set menuIds = new HashSet();
049:
050: /**
051: * Adds all of the given menu identifiers as being shown.
052: *
053: * @param menuIds
054: * The ids of the menu that is now showing; must not be
055: * <code>null</code>.
056: */
057: public final void addShowingMenus(final Set menuIds,
058: final ISelection localSelection,
059: final ISelection localEditorInput) {
060: this .menuIds.addAll(menuIds);
061: if (DEBUG) {
062: logDebuggingInfo("Menu ids changed to " + this .menuIds); //$NON-NLS-1$
063: }
064: Map m = new HashMap();
065: m.put(ISources.ACTIVE_MENU_NAME, this .menuIds);
066: if (selection != localSelection) {
067: selection = localSelection;
068: m.put(ISources.ACTIVE_MENU_SELECTION_NAME, selection);
069: }
070: if (input != localEditorInput) {
071: input = localEditorInput;
072: m.put(ISources.ACTIVE_MENU_EDITOR_INPUT_NAME, input);
073: }
074:
075: fireSourceChanged(ISources.ACTIVE_MENU, m);
076: }
077:
078: public final void dispose() {
079: menuIds.clear();
080: selection = null;
081: input = null;
082: }
083:
084: public final Map getCurrentState() {
085: final Map state = new HashMap();
086: state.put(ISources.ACTIVE_MENU_NAME, menuIds);
087: state.put(ISources.ACTIVE_MENU_SELECTION_NAME, selection);
088: state.put(ISources.ACTIVE_MENU_EDITOR_INPUT_NAME, input);
089: return state;
090: }
091:
092: public final String[] getProvidedSourceNames() {
093: return PROVIDED_SOURCE_NAMES;
094: }
095:
096: /**
097: * Removes all of the given menu identifiers as being shown.
098: *
099: * @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 menuIds,
104: final ISelection localSelection,
105: final ISelection localEditorInput) {
106: this .menuIds.removeAll(menuIds);
107: if (DEBUG) {
108: logDebuggingInfo("Menu ids changed to " + this .menuIds); //$NON-NLS-1$
109: }
110: Map m = new HashMap();
111: m.put(ISources.ACTIVE_MENU_NAME, this .menuIds);
112: if (selection != localSelection) {
113: selection = localSelection;
114: m.put(ISources.ACTIVE_MENU_SELECTION_NAME, selection);
115: }
116: if (input != localEditorInput) {
117: input = localEditorInput;
118: m.put(ISources.ACTIVE_MENU_EDITOR_INPUT_NAME, input);
119: }
120: fireSourceChanged(ISources.ACTIVE_MENU, m);
121: }
122:
123: private ISelection selection = null;
124: private ISelection input = null;
125: }
|