Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b43702fde2a97c73ec3bd69559dd0b4a7fae7182 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.synchronize.actions;

import org.eclipse.jface.action.*;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.*;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ui.*;
import org.eclipse.team.internal.ui.synchronize.SynchronizeView;
import org.eclipse.team.internal.ui.wizards.GlobalSynchronizeWizard;
import org.eclipse.team.ui.TeamImages;
import org.eclipse.team.ui.TeamUI;
import org.eclipse.team.ui.synchronize.*;

public class SynchronizePageDropDownAction extends Action implements IMenuCreator, ISynchronizeParticipantListener {

	private ISynchronizeView fView;
	private Action synchronizeAction;
	private MenuManager menuManager;

	public SynchronizePageDropDownAction(ISynchronizeView view) {
		fView= view;
		Utils.initAction(this, "action.refreshSubscriber."); //$NON-NLS-1$

		synchronizeAction = new Action(TeamUIMessages.GlobalRefreshAction_4) {
			@Override
			public void run() {
				IWizard wizard = new GlobalSynchronizeWizard();
				WizardDialog dialog = new WizardDialog(fView.getViewSite().getShell(), wizard);
				dialog.open();
			}
		};
		synchronizeAction.setImageDescriptor(TeamImages.getImageDescriptor(ITeamUIImages.IMG_SYNC_VIEW));
		synchronizeAction.setActionDefinitionId("org.eclipse.team.ui.synchronizeAll"); //$NON-NLS-1$
		setMenuCreator(this);
		TeamUI.getSynchronizeManager().addSynchronizeParticipantListener(this);
		update();
		fView.getSite().getKeyBindingService().registerAction(synchronizeAction);
		setActionDefinitionId("org.eclipse.team.ui.synchronizeLast"); //$NON-NLS-1$
		fView.getSite().getKeyBindingService().registerAction(this);
	}

	@Override
	public void dispose() {
		if(menuManager != null) {
			menuManager.dispose();
			menuManager = null;
		}
		TeamUI.getSynchronizeManager().removeSynchronizeParticipantListener(this);
	}

	@Override
	public Menu getMenu(Menu parent) {
		return null;
	}

	@Override
	public Menu getMenu(Control parent) {
		Menu fMenu = null;
		if (menuManager == null) {
			menuManager = new MenuManager();
			fMenu = menuManager.createContextMenu(parent);
			final ISynchronizeParticipantReference[] participants = TeamUI.getSynchronizeManager().getSynchronizeParticipants();
			addParticipantsToMenu(participants);
			if (participants.length > 0)
				menuManager.add(new Separator());
			menuManager.add(synchronizeAction);
			menuManager.update(true);
		} else {
			fMenu = menuManager.getMenu();
		}
		return fMenu;
	}

	protected void addParticipantsToMenu(ISynchronizeParticipantReference[] refs) {
		ISynchronizeParticipant current = fView.getParticipant();
		for (int i = 0; i < refs.length; i++) {
			ISynchronizeParticipantReference page = refs[i];
			Action action = new ShowSynchronizeParticipantAction(fView, page);
			try {
				boolean isCurrent = page.getParticipant().equals(current);
				action.setChecked(isCurrent);
			} catch (TeamException e) {
				continue;
			}
			menuManager.add(action);
		}
	}

	@Override
	public void run() {
		ISynchronizeParticipant current = fView.getParticipant();
		if(current != null) {
			current.run(fView);
		} else {
			synchronizeAction.run();
		}
		update();
	}

	@Override
	public void participantsAdded(ISynchronizeParticipant[] consoles) {
		Display display = TeamUIPlugin.getStandardDisplay();
		display.asyncExec(() -> {
			if(menuManager != null) {
				menuManager.dispose();
				menuManager = null;
			}
			update();
		});
	}

	@Override
	public void participantsRemoved(ISynchronizeParticipant[] consoles) {
		Display display = TeamUIPlugin.getStandardDisplay();
		display.asyncExec(() -> {
			if(menuManager != null) {
				menuManager.dispose();
				menuManager = null;
			}
			update();
		});
	}

	public void update() {
		ISynchronizeParticipant current = fView.getParticipant();
		ISynchronizeParticipantReference[] refs = TeamUI.getSynchronizeManager().getSynchronizeParticipants();
		String text = null;
		if(current != null && refs.length > 0) {
			text = NLS.bind(TeamUIMessages.GlobalRefreshAction_5, new String[] { Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, current.getName()) });
			setToolTipText(text);
			setText(text);
		} else {
			text = TeamUIMessages.GlobalRefreshAction_4;
			setToolTipText(text);
			setText(text);
		}
	}
}

Back to the top