KickJava   Java API By Example, From Geeks To Geeks.

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


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.jface.action.*;
14 import org.eclipse.jface.wizard.IWizard;
15 import org.eclipse.jface.wizard.WizardDialog;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.swt.widgets.*;
18 import org.eclipse.team.core.TeamException;
19 import org.eclipse.team.internal.ui.*;
20 import org.eclipse.team.internal.ui.synchronize.SynchronizeView;
21 import org.eclipse.team.internal.ui.wizards.GlobalSynchronizeWizard;
22 import org.eclipse.team.ui.TeamImages;
23 import org.eclipse.team.ui.TeamUI;
24 import org.eclipse.team.ui.synchronize.*;
25
26 public class SynchronizePageDropDownAction extends Action implements IMenuCreator, ISynchronizeParticipantListener {
27
28     private ISynchronizeView fView;
29     private Action synchronizeAction;
30     private MenuManager menuManager;
31
32     public SynchronizePageDropDownAction(ISynchronizeView view) {
33         fView= view;
34         Utils.initAction(this, "action.refreshSubscriber."); //$NON-NLS-1$
35

36         synchronizeAction = new Action(TeamUIMessages.GlobalRefreshAction_4) {
37             public void run() {
38                 IWizard wizard = new GlobalSynchronizeWizard();
39                 WizardDialog dialog = new WizardDialog(fView.getViewSite().getShell(), wizard);
40                 dialog.open();
41             }
42         };
43         synchronizeAction.setImageDescriptor(TeamImages.getImageDescriptor(ITeamUIImages.IMG_SYNC_VIEW));
44         synchronizeAction.setActionDefinitionId("org.eclipse.team.ui.synchronizeAll"); //$NON-NLS-1$
45
setMenuCreator(this);
46         TeamUI.getSynchronizeManager().addSynchronizeParticipantListener(this);
47         update();
48         fView.getSite().getKeyBindingService().registerAction(synchronizeAction);
49         setActionDefinitionId("org.eclipse.team.ui.synchronizeLast"); //$NON-NLS-1$
50
fView.getSite().getKeyBindingService().registerAction(this);
51     }
52
53     /* (non-Javadoc)
54      * @see org.eclipse.jface.action.IMenuCreator#dispose()
55      */

56     public void dispose() {
57         if(menuManager != null) {
58             menuManager.dispose();
59             menuManager = null;
60         }
61         TeamUI.getSynchronizeManager().removeSynchronizeParticipantListener(this);
62     }
63
64     /* (non-Javadoc)
65      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
66      */

67     public Menu getMenu(Menu parent) {
68         return null;
69     }
70
71     /* (non-Javadoc)
72      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
73      */

74     public Menu getMenu(Control parent) {
75         Menu fMenu = null;
76         if (menuManager == null) {
77             menuManager = new MenuManager();
78             fMenu = menuManager.createContextMenu(parent);
79             final ISynchronizeParticipantReference[] participants = TeamUI.getSynchronizeManager().getSynchronizeParticipants();
80             addParticipantsToMenu(participants);
81             if (participants.length > 0)
82                 menuManager.add(new Separator());
83             menuManager.add(synchronizeAction);
84             menuManager.update(true);
85         } else {
86             fMenu = menuManager.getMenu();
87         }
88         return fMenu;
89     }
90
91     protected void addParticipantsToMenu(ISynchronizeParticipantReference[] refs) {
92         ISynchronizeParticipant current = fView.getParticipant();
93         for (int i = 0; i < refs.length; i++) {
94             ISynchronizeParticipantReference page = refs[i];
95             Action action = new ShowSynchronizeParticipantAction(fView, page);
96             try {
97                 boolean isCurrent = page.getParticipant().equals(current);
98                 action.setChecked(isCurrent);
99             } catch (TeamException e) {
100                 continue;
101             }
102             menuManager.add(action);
103         }
104     }
105
106     /* (non-Javadoc)
107      * @see org.eclipse.jface.action.IAction#run()
108      */

109     public void run() {
110         ISynchronizeParticipant current = fView.getParticipant();
111         if(current != null) {
112             current.run(fView);
113         } else {
114             synchronizeAction.run();
115         }
116         update();
117     }
118
119     /* (non-Javadoc)
120      * @see org.eclipse.team.ui.sync.ISynchronizeParticipantListener#participantsAdded(org.eclipse.team.ui.sync.ISynchronizeParticipant[])
121      */

122     public void participantsAdded(ISynchronizeParticipant[] consoles) {
123         Display display = TeamUIPlugin.getStandardDisplay();
124         display.asyncExec(new Runnable JavaDoc() {
125             public void run() {
126                 if(menuManager != null) {
127                     menuManager.dispose();
128                     menuManager = null;
129                 }
130                 update();
131             }
132         });
133     }
134
135     /* (non-Javadoc)
136      * @see org.eclipse.team.ui.sync.ISynchronizeParticipantListener#participantsRemoved(org.eclipse.team.ui.sync.ISynchronizeParticipant[])
137      */

138     public void participantsRemoved(ISynchronizeParticipant[] consoles) {
139         Display display = TeamUIPlugin.getStandardDisplay();
140         display.asyncExec(new Runnable JavaDoc() {
141             public void run() {
142                 if(menuManager != null) {
143                     menuManager.dispose();
144                     menuManager = null;
145                 }
146                 update();
147             }
148         });
149     }
150     
151     public void update() {
152         ISynchronizeParticipant current = fView.getParticipant();
153         ISynchronizeParticipantReference[] refs = TeamUI.getSynchronizeManager().getSynchronizeParticipants();
154         String JavaDoc text = null;
155         if(current != null && refs.length > 0) {
156             text = NLS.bind(TeamUIMessages.GlobalRefreshAction_5, new String JavaDoc[] { Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, current.getName()) });
157             setToolTipText(text);
158             setText(text);
159         } else {
160             text = TeamUIMessages.GlobalRefreshAction_4;
161             setToolTipText(text);
162             setText(text);
163         }
164     }
165 }
166
Popular Tags