KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > contexts > ActiveContextSourceProvider


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.contexts;
13
14 import java.util.Collection JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.TreeMap JavaDoc;
17
18 import org.eclipse.core.commands.contexts.ContextManagerEvent;
19 import org.eclipse.core.commands.contexts.IContextManagerListener;
20 import org.eclipse.ui.AbstractSourceProvider;
21 import org.eclipse.ui.ISources;
22 import org.eclipse.ui.contexts.IContextService;
23
24 /**
25  * <p>
26  * This listens to changes to the list of active contexts, and propagates them
27  * through the <code>ISourceProvider</code> framework (a common language in
28  * which events are communicated to services).
29  * </p>
30  *
31  * @since 3.2
32  */

33 public final class ActiveContextSourceProvider extends AbstractSourceProvider
34         implements IContextManagerListener {
35
36     /**
37      * The names of the sources supported by this source provider.
38      */

39     private static final String JavaDoc[] PROVIDED_SOURCE_NAMES = new String JavaDoc[] { ISources.ACTIVE_CONTEXT_NAME };
40
41     /**
42      * The context service with which this source provider should communicate.
43      * This value is never <code>null</code>.
44      */

45     private final IContextService contextService;
46
47     /**
48      * Constructs a new instance of <code>ActiveContextSourceProvider</code>.
49      *
50      * @param contextService
51      * The context service to use; must not be <code>null</code>.
52      */

53     public ActiveContextSourceProvider(final IContextService contextService) {
54         this.contextService = contextService;
55         contextService.addContextManagerListener(this);
56     }
57
58     public final void contextManagerChanged(final ContextManagerEvent event) {
59         if (event.isActiveContextsChanged()) {
60             final Map JavaDoc currentState = getCurrentState();
61
62             if (DEBUG) {
63                 logDebuggingInfo("Contexts changed to " //$NON-NLS-1$
64
+ currentState.get(ISources.ACTIVE_CONTEXT_NAME));
65             }
66
67             fireSourceChanged(ISources.ACTIVE_CONTEXT, currentState);
68         }
69     }
70
71     public final void dispose() {
72         contextService.removeContextManagerListener(this);
73     }
74
75     public final Map JavaDoc getCurrentState() {
76         final Map JavaDoc currentState = new TreeMap JavaDoc();
77         final Collection JavaDoc activeContextIds = contextService
78                 .getActiveContextIds();
79         currentState.put(ISources.ACTIVE_CONTEXT_NAME, activeContextIds);
80         return currentState;
81     }
82
83     public final String JavaDoc[] getProvidedSourceNames() {
84         return PROVIDED_SOURCE_NAMES;
85     }
86
87 }
88
Popular Tags