KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > actions > GlobalRefreshAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.internal.ui.synchronize.actions;
12
13 import org.eclipse.core.commands.*;
14 import org.eclipse.jface.action.*;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.wizard.IWizard;
17 import org.eclipse.jface.wizard.WizardDialog;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.swt.widgets.*;
20 import org.eclipse.team.core.TeamException;
21 import org.eclipse.team.internal.ui.*;
22 import org.eclipse.team.internal.ui.synchronize.SynchronizeView;
23 import org.eclipse.team.internal.ui.wizards.GlobalSynchronizeWizard;
24 import org.eclipse.team.ui.TeamImages;
25 import org.eclipse.team.ui.TeamUI;
26 import org.eclipse.team.ui.synchronize.*;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate;
29 import org.eclipse.ui.handlers.IHandlerActivation;
30 import org.eclipse.ui.handlers.IHandlerService;
31
32 /**
33  * A global refresh action that allows the user to select the participant to refresh
34  * or the default action is to refresh the last selected participant. Participants are
35  * only listed if they support
36  * <p>
37  * This action is normally associated with the Team action set and is enabled by default
38  * in the Team Synchronizing perspective.
39  * </p>
40  * @since 3.0
41  */

42 public class GlobalRefreshAction extends Action implements IMenuCreator, IWorkbenchWindowPulldownDelegate, ISynchronizeParticipantListener {
43
44     public final static String JavaDoc NO_DEFAULT_PARTICPANT = "none"; //$NON-NLS-1$
45

46     private MenuManager menuManager;
47     private Action synchronizeAction;
48     private IWorkbenchWindow window;
49     private IAction actionProxy;
50     private IHandlerActivation syncAll;
51     private IHandlerActivation syncLatest;
52
53     class RefreshParticipantAction extends Action {
54         private ISynchronizeParticipantReference participant;
55
56         public void run() {
57             TeamUIPlugin.getPlugin().getPreferenceStore().setValue(IPreferenceIds.SYNCHRONIZING_DEFAULT_PARTICIPANT, participant.getId());
58             TeamUIPlugin.getPlugin().getPreferenceStore().setValue(IPreferenceIds.SYNCHRONIZING_DEFAULT_PARTICIPANT_SEC_ID, participant.getSecondaryId());
59             GlobalRefreshAction.this.run(participant);
60         }
61
62         public RefreshParticipantAction(int prefix, ISynchronizeParticipantReference participant) {
63             super("&" + prefix + " " + Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, participant.getDisplayName())); //$NON-NLS-1$ //$NON-NLS-2$
64
this.participant = participant;
65             setImageDescriptor(participant.getDescriptor().getImageDescriptor());
66         }
67     }
68
69     public GlobalRefreshAction() {
70         // Nothing to do
71
}
72
73     /*
74      * (non-Javadoc)
75      * @see org.eclipse.jface.action.IMenuCreator#dispose()
76      */

77     public void dispose() {
78         if(menuManager != null) {
79             menuManager.dispose();
80         }
81         
82         // participant listener
83
TeamUI.getSynchronizeManager().removeSynchronizeParticipantListener(this);
84         
85         // handlers
86
if (window != null) {
87             IHandlerService hs = (IHandlerService)window.getService(IHandlerService.class);
88             if (hs != null) {
89                 if (syncAll != null)
90                     hs.deactivateHandler(syncAll);
91                 if (syncLatest != null)
92                     hs.deactivateHandler(syncLatest);
93             }
94         }
95     }
96
97     /*
98      * (non-Javadoc)
99      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
100      */

101     public Menu getMenu(Menu parent) {
102         return null;
103     }
104
105     /*
106      * (non-Javadoc)
107      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
108      */

109     public Menu getMenu(Control parent) {
110         Menu fMenu = null;
111         if (menuManager == null) {
112             menuManager = new MenuManager();
113             fMenu = menuManager.createContextMenu(parent);
114             menuManager.removeAll();
115             ISynchronizeParticipantReference[] participants = TeamUI.getSynchronizeManager().getSynchronizeParticipants();
116             for (int i = 0; i < participants.length; i++) {
117                 ISynchronizeParticipantReference description = participants[i];
118                 Action action = new RefreshParticipantAction(i + 1, description);
119                 menuManager.add(action);
120             }
121             if (participants.length > 0)
122                 menuManager.add(new Separator());
123             menuManager.add(synchronizeAction);
124         } else {
125             fMenu = menuManager.getMenu();
126         }
127         return fMenu;
128     }
129
130     /*
131      * (non-Javadoc)
132      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
133      */

134     public void init(IWorkbenchWindow window) {
135         this.window = window;
136
137         synchronizeAction = new Action(TeamUIMessages.GlobalRefreshAction_4) {
138             public void run() {
139                 IWizard wizard = new GlobalSynchronizeWizard();
140                 WizardDialog dialog = new WizardDialog(GlobalRefreshAction.this.window.getShell(), wizard);
141                 dialog.open();
142             }
143         };
144         synchronizeAction.setImageDescriptor(TeamImages.getImageDescriptor(ITeamUIImages.IMG_SYNC_VIEW));
145         synchronizeAction.setActionDefinitionId("org.eclipse.team.ui.synchronizeAll"); //$NON-NLS-1$
146

147         IHandlerService hs = (IHandlerService)window.getService(IHandlerService.class);
148         if (hs != null) {
149             // hook up actions to the commands
150
IHandler handler = new AbstractHandler() {
151                 public Object JavaDoc execute(ExecutionEvent event)
152                         throws ExecutionException {
153                     synchronizeAction.run();
154                     return null;
155                 }
156             };
157             syncAll = hs.activateHandler("org.eclipse.team.ui.synchronizeAll", handler); //$NON-NLS-1$
158

159             handler = new AbstractHandler() {
160                 public Object JavaDoc execute(ExecutionEvent event)
161                         throws ExecutionException {
162                     run();
163                     return null;
164                 }
165             };
166             syncLatest = hs.activateHandler("org.eclipse.team.ui.synchronizeLast", handler); //$NON-NLS-1$
167
}
168         setMenuCreator(this);
169         TeamUI.getSynchronizeManager().addSynchronizeParticipantListener(this);
170     }
171
172     public void run() {
173         String JavaDoc id = TeamUIPlugin.getPlugin().getPreferenceStore().getString(IPreferenceIds.SYNCHRONIZING_DEFAULT_PARTICIPANT);
174         String JavaDoc secondaryId = TeamUIPlugin.getPlugin().getPreferenceStore().getString(IPreferenceIds.SYNCHRONIZING_DEFAULT_PARTICIPANT_SEC_ID);
175         ISynchronizeParticipantReference participant = TeamUI.getSynchronizeManager().get(id, secondaryId);
176         if (participant != null) {
177             run(participant);
178         } else {
179             synchronizeAction.run();
180         }
181     }
182     
183     /*
184      * (non-Javadoc)
185      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
186      */

187     public void run(IAction action) {
188         run();
189         actionProxy = action;
190         updateTooltipText();
191     }
192         
193     private void run(ISynchronizeParticipantReference participant) {
194         ISynchronizeParticipant p;
195         try {
196             p = participant.getParticipant();
197             p.run(null /* no workbench part */);
198             updateTooltipText();
199         } catch (TeamException e) {
200             Utils.handle(e);
201         }
202     }
203     
204     
205     /* (non-Javadoc)
206      * @see org.eclipse.team.ui.sync.ISynchronizeParticipantListener#participantsAdded(org.eclipse.team.ui.sync.ISynchronizeParticipant[])
207      */

208     public void participantsAdded(ISynchronizeParticipant[] consoles) {
209         Display display = TeamUIPlugin.getStandardDisplay();
210         display.asyncExec(new Runnable JavaDoc() {
211             public void run() {
212                 if(menuManager != null) {
213                     menuManager.dispose();
214                     menuManager = null;
215                 }
216                 updateTooltipText();
217             }
218         });
219     }
220
221     /* (non-Javadoc)
222      * @see org.eclipse.team.ui.sync.ISynchronizeParticipantListener#participantsRemoved(org.eclipse.team.ui.sync.ISynchronizeParticipant[])
223      */

224     public void participantsRemoved(ISynchronizeParticipant[] consoles) {
225         Display display = TeamUIPlugin.getStandardDisplay();
226         display.asyncExec(new Runnable JavaDoc() {
227             public void run() {
228                 if(menuManager != null) {
229                     menuManager.dispose();
230                     menuManager = null;
231                 }
232                 updateTooltipText();
233             }
234         });
235     }
236     
237     /*
238      * (non-Javadoc)
239      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
240      * org.eclipse.jface.viewers.ISelection)
241      */

242     public void selectionChanged(IAction action, ISelection selection) {
243         actionProxy = action;
244     }
245     
246     protected void updateTooltipText() {
247         if (actionProxy != null) {
248             String JavaDoc id = TeamUIPlugin.getPlugin().getPreferenceStore().getString(IPreferenceIds.SYNCHRONIZING_DEFAULT_PARTICIPANT);
249             String JavaDoc secondaryId = TeamUIPlugin.getPlugin().getPreferenceStore().getString(IPreferenceIds.SYNCHRONIZING_DEFAULT_PARTICIPANT_SEC_ID);
250             if (!id.equals(NO_DEFAULT_PARTICPANT)) {
251                 ISynchronizeParticipantReference ref = TeamUI.getSynchronizeManager().get(id, secondaryId);
252                 if (ref != null) {
253                     actionProxy.setToolTipText(NLS.bind(TeamUIMessages.GlobalRefreshAction_5, new String JavaDoc[] { ref.getDisplayName() }));
254                     return;
255                 }
256             }
257             actionProxy.setToolTipText(TeamUIMessages.GlobalRefreshAction_4);
258         }
259     }
260 }
261
Popular Tags