KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > mapping > SynchronizationActionProvider


1 /*******************************************************************************
2  * Copyright (c) 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 package org.eclipse.team.ui.mapping;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.commands.IHandler;
18 import org.eclipse.jface.action.IContributionItem;
19 import org.eclipse.jface.action.IMenuManager;
20 import org.eclipse.team.core.mapping.ISynchronizationContext;
21 import org.eclipse.team.internal.ui.mapping.CommonMenuManager;
22 import org.eclipse.team.internal.ui.synchronize.actions.OpenWithActionGroup;
23 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
24 import org.eclipse.ui.*;
25 import org.eclipse.ui.actions.ActionContext;
26 import org.eclipse.ui.navigator.*;
27
28 /**
29  * An action group that can be used by models to contribute actions
30  * to a team synchronization viewer. Subclasses should override the
31  * {@link #initialize()} method in order to register handlers for the
32  * following merge actions if they want custom merge behavior:
33  * <ul>
34  * <li>{@link #MERGE_ACTION_ID}
35  * <li>{@link #OVERWRITE_ACTION_ID}
36  * <li>{@link #OVERWRITE_ACTION_ID}
37  * </ul>
38  * They may also add other actions to the context menu or register action handlers
39  * in the {@link #fillActionBars(IActionBars)} method.
40  * <p>
41  * This class may be subclasses by clients.
42  *
43  * @see MergeActionHandler
44  * @since 3.2
45  */

46 public class SynchronizationActionProvider extends CommonActionProvider {
47     
48     /**
49      * Action id constant for the merge action.
50      * @see #registerHandler(String, IHandler)
51      */

52     public static final String JavaDoc MERGE_ACTION_ID = "org.eclipse.team.ui.mergeAction"; //$NON-NLS-1$
53

54     /**
55      * Action id constant for the merge action.
56      * @see #registerHandler(String, IHandler)
57      */

58     public static final String JavaDoc OVERWRITE_ACTION_ID = "org.eclipse.team.ui.overwriteAction"; //$NON-NLS-1$
59

60     /**
61      * Action id constant for the mark-as-merge action.
62      * @see #registerHandler(String, IHandler)
63      */

64     public static final String JavaDoc MARK_AS_MERGE_ACTION_ID = "org.eclipse.team.ui.markAsMergeAction"; //$NON-NLS-1$
65

66     private Map JavaDoc handlers = new HashMap JavaDoc();
67     private OpenWithActionGroup openWithActions;
68
69     public void init(ICommonActionExtensionSite site) {
70         super.init(site);
71         initialize();
72     }
73
74     /**
75      * Method called during action provider initialization.
76      * It is invoked from the {@link #init(ICommonActionExtensionSite)}
77      * after after the configuration has been recorded. Subclasses
78      * may override. Subclasses that want to provide there own merge actions
79      * handlers can register them in this method.
80      * @see #registerHandler(String, IHandler)
81      * @see MergeActionHandler
82      */

83     protected void initialize() {
84         initializeOpenActions();
85     }
86
87     /**
88      * Method called from {@link #initialize()} to initialize the Open/Open With
89      * actions. This method will add an Open item and Open With menu for single
90      * selections that adapt to IResource. Subclasses may override. They may
91      * still call this method, in which case they only need to handle providing
92      * open for non-files. Otherwise, if they do not call this method, they must
93      * provide all non-compare related open items.
94      *
95      */

96     protected void initializeOpenActions() {
97         ICommonViewerSite cvs = getActionSite().getViewSite();
98         ISynchronizePageConfiguration configuration = getSynchronizePageConfiguration();
99         if (cvs instanceof ICommonViewerWorkbenchSite && configuration != null) {
100             ICommonViewerWorkbenchSite cvws = (ICommonViewerWorkbenchSite) cvs;
101             final IWorkbenchPartSite wps = cvws.getSite();
102             if (wps instanceof IViewSite) {
103                 openWithActions = new OpenWithActionGroup(configuration, false);
104             }
105         }
106     }
107     
108     /**
109      * Return the configuration from the synchronize page that contains
110      * the common viewer.
111      * @return the configuration from the synchronize page that contains
112      * the common viewer
113      */

114     protected final ISynchronizePageConfiguration getSynchronizePageConfiguration() {
115         return (ISynchronizePageConfiguration)getExtensionStateModel().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_PAGE_CONFIGURATION);
116     }
117
118     /**
119      * Return the extension state model for the content provider associated with
120      * action provider.
121      * @return the extension state model for the content provider associated with
122      * action provider
123      */

124     protected final IExtensionStateModel getExtensionStateModel() {
125         return getActionSite().getExtensionStateModel();
126     }
127     
128     /**
129      * Return the synchronization context to which the actions of this provider
130      * apply.
131      * @return the synchronization context to which the actions of this provider
132      * apply
133      */

134     protected final ISynchronizationContext getSynchronizationContext() {
135         return (ISynchronizationContext)getExtensionStateModel().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT);
136     }
137     
138     /**
139      * Register the handler as the handler for the given action id when
140      * a merge action is performed on elements that match this groups
141      * enablement.
142      * @param actionId the id of the merge action
143      * @param handler the handler for elements of the model that provided this group
144      */

145     protected void registerHandler(String JavaDoc actionId, IHandler handler) {
146         handlers.put(actionId, handler);
147     }
148     
149     /* (non-Javadoc)
150      * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
151      */

152     public void fillContextMenu(IMenuManager menu) {
153         super.fillContextMenu(menu);
154         if (menu instanceof CommonMenuManager) {
155             CommonMenuManager manager = (CommonMenuManager) menu;
156             for (Iterator JavaDoc iter = handlers.keySet().iterator(); iter.hasNext();) {
157                 String JavaDoc actionId = (String JavaDoc) iter.next();
158                 manager.registerHandler(actionId, (IHandler)handlers.get(actionId));
159             }
160         }
161         final IContributionItem fileGroup = menu.find(ISynchronizePageConfiguration.FILE_GROUP);
162         if (openWithActions != null && fileGroup != null) {
163             openWithActions.fillContextMenu(menu, fileGroup.getId());
164         }
165     }
166     
167     /* (non-Javadoc)
168      * @see org.eclipse.ui.actions.ActionGroup#fillActionBars(org.eclipse.ui.IActionBars)
169      */

170     public void fillActionBars(IActionBars actionBars) {
171         super.fillActionBars(actionBars);
172         if (openWithActions != null) openWithActions.fillActionBars(actionBars);
173     }
174     
175     public void updateActionBars() {
176         super.updateActionBars();
177         if (openWithActions != null) openWithActions.updateActionBars();
178     }
179     
180     public void setContext(ActionContext context) {
181         super.setContext(context);
182         if (openWithActions != null) openWithActions.setContext(context);
183     }
184     
185     /* (non-Javadoc)
186      * @see org.eclipse.ui.actions.ActionGroup#dispose()
187      */

188     public void dispose() {
189         super.dispose();
190         if (openWithActions != null) openWithActions.dispose();
191         for (Iterator JavaDoc iter = handlers.values().iterator(); iter.hasNext();) {
192             IHandler handler = (IHandler) iter.next();
193             if (handler instanceof MergeActionHandler) {
194                 MergeActionHandler mah = (MergeActionHandler) handler;
195                 mah.dispose();
196             }
197         }
198     }
199
200     /**
201      * Return the extension site for this action provider.
202      * This method just calls {@link #getActionSite()}.
203      * @return the extension site for this action provider
204      */

205     public ICommonActionExtensionSite getExtensionSite() {
206         return getActionSite();
207     }
208
209 }
210
Popular Tags