1 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 42 public class GlobalRefreshAction extends Action implements IMenuCreator, IWorkbenchWindowPulldownDelegate, ISynchronizeParticipantListener { 43 44 public final static String NO_DEFAULT_PARTICPANT = "none"; 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())); this.participant = participant; 65 setImageDescriptor(participant.getDescriptor().getImageDescriptor()); 66 } 67 } 68 69 public GlobalRefreshAction() { 70 } 72 73 77 public void dispose() { 78 if(menuManager != null) { 79 menuManager.dispose(); 80 } 81 82 TeamUI.getSynchronizeManager().removeSynchronizeParticipantListener(this); 84 85 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 101 public Menu getMenu(Menu parent) { 102 return null; 103 } 104 105 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 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"); 147 IHandlerService hs = (IHandlerService)window.getService(IHandlerService.class); 148 if (hs != null) { 149 IHandler handler = new AbstractHandler() { 151 public Object execute(ExecutionEvent event) 152 throws ExecutionException { 153 synchronizeAction.run(); 154 return null; 155 } 156 }; 157 syncAll = hs.activateHandler("org.eclipse.team.ui.synchronizeAll", handler); 159 handler = new AbstractHandler() { 160 public Object execute(ExecutionEvent event) 161 throws ExecutionException { 162 run(); 163 return null; 164 } 165 }; 166 syncLatest = hs.activateHandler("org.eclipse.team.ui.synchronizeLast", handler); } 168 setMenuCreator(this); 169 TeamUI.getSynchronizeManager().addSynchronizeParticipantListener(this); 170 } 171 172 public void run() { 173 String id = TeamUIPlugin.getPlugin().getPreferenceStore().getString(IPreferenceIds.SYNCHRONIZING_DEFAULT_PARTICIPANT); 174 String 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 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 ); 198 updateTooltipText(); 199 } catch (TeamException e) { 200 Utils.handle(e); 201 } 202 } 203 204 205 208 public void participantsAdded(ISynchronizeParticipant[] consoles) { 209 Display display = TeamUIPlugin.getStandardDisplay(); 210 display.asyncExec(new Runnable () { 211 public void run() { 212 if(menuManager != null) { 213 menuManager.dispose(); 214 menuManager = null; 215 } 216 updateTooltipText(); 217 } 218 }); 219 } 220 221 224 public void participantsRemoved(ISynchronizeParticipant[] consoles) { 225 Display display = TeamUIPlugin.getStandardDisplay(); 226 display.asyncExec(new Runnable () { 227 public void run() { 228 if(menuManager != null) { 229 menuManager.dispose(); 230 menuManager = null; 231 } 232 updateTooltipText(); 233 } 234 }); 235 } 236 237 242 public void selectionChanged(IAction action, ISelection selection) { 243 actionProxy = action; 244 } 245 246 protected void updateTooltipText() { 247 if (actionProxy != null) { 248 String id = TeamUIPlugin.getPlugin().getPreferenceStore().getString(IPreferenceIds.SYNCHRONIZING_DEFAULT_PARTICIPANT); 249 String 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 [] { ref.getDisplayName() })); 254 return; 255 } 256 } 257 actionProxy.setToolTipText(TeamUIMessages.GlobalRefreshAction_4); 258 } 259 } 260 } 261 | Popular Tags |