Skip to main content
summaryrefslogtreecommitdiffstats
blob: 827b977a044e60a6e217d9d15989769d37613cb0 (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
151
152
153
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.sync.actions;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.team.internal.ui.sync.sets.SubscriberInput;
import org.eclipse.team.internal.ui.sync.views.SynchronizeView;
import org.eclipse.ui.actions.ActionContext;

/**
 * SyncViewerSubscriberListActions
 */
public class SyncViewerSubscriberListActions extends SyncViewerActionGroup {

	// {QualifiedName:subscriber id -> SubscriberInput}
	private Map actions = new HashMap();
	private SubscriberInput activeInput = null;
	private CancelSubscription cancelAction;

	/**
	 * Action for filtering by change type.
	 */
	class SwitchSubscriberAction extends Action {
		private SubscriberInput input;
		public SwitchSubscriberAction(SubscriberInput input) {
			super(input.getSubscriber().getName(), Action.AS_RADIO_BUTTON);
			this.input = input;
		}
		public void run() {
			// Uncheck and let the activate check once the activate succeeds
			setChecked(false);
			SyncViewerSubscriberListActions.this.activate(this);
		}
		public SubscriberInput getSubscriberInput() {
			return input;
		}
	}

	public SyncViewerSubscriberListActions(SynchronizeView syncView) {
		super(syncView);
		setContext(null);
	}

	public void activate(SwitchSubscriberAction activatedAction) {
		if(activeInput == null || ! activatedAction.getSubscriberInput().getSubscriber().getId().equals(activeInput.getSubscriber().getId())) {
			getSyncView().initializeSubscriberInput(activatedAction.getSubscriberInput());
			// The action check state will be updated when the view invokes
			// setContext which then invokes initializeActions
		} else {
			activatedAction.setChecked(true);
		}
	}

	/*
	 * Called when a context is enabled for the view.
	 *  (non-Javadoc)
	 * @see SyncViewerActionGroup#initializeActions()
	 */
	public void initializeActions() {
		SubscriberInput input = getSubscriberContext();
		if (input != null) {
			for (Iterator it = actions.values().iterator(); it.hasNext();) {
				SwitchSubscriberAction action =
					(SwitchSubscriberAction) it.next();
				boolean checked = action.getSubscriberInput().getSubscriber().getId().equals(
														input.getSubscriber().getId());											
				action.setChecked(checked);
				if(checked) {
					activeInput = input;
				}
			}
			cancelAction.setEnabled(input.getSubscriber().isCancellable());
			cancelAction.setSubscriber(input.getSubscriber());
		} else {
			if(cancelAction != null)
				cancelAction.setEnabled(false);
		}
	}

   /* 
    * Checking of the currently active subscriber input is done when the context is set
    * in the initializeActions method. 
	* (non-Javadoc)
	* @see fillContextMenu(org.eclipse.jface.action.IMenuManager)
	*/
	public void fillContextMenu(IMenuManager menu) {
		super.fillContextMenu(menu);
		if (! actions.isEmpty()) {
			for (Iterator it = actions.values().iterator(); it.hasNext();) {
				SwitchSubscriberAction action = (SwitchSubscriberAction) it.next();
				menu.add(action);				
			}
			if(cancelAction != null) {
				menu.add(new Separator());
				menu.add(cancelAction);
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ui.sync.actions.SyncViewerActionGroup#addContext(org.eclipse.ui.actions.ActionContext)
	 */
	public void addContext(ActionContext context) {
		boolean enableFirstContext = actions.isEmpty();
		SubscriberInput input = (SubscriberInput)context.getInput();
		SwitchSubscriberAction action =  new SwitchSubscriberAction(input);
		actions.put(input.getSubscriber().getId(), action);		
		if(cancelAction == null) {
			cancelAction = new CancelSubscription(getSyncView(), input.getSubscriber());
		}
		if(enableFirstContext) {
			activate(action);
		}	
	}
	
	/* 
	 * Method to add menu items to a toolbar drop down action
	 */
	public void fillMenu(SyncViewerToolbarDropDownAction dropDown) {
		super.fillMenu(dropDown);
		if (! actions.isEmpty()) {
			for (Iterator it = actions.values().iterator(); it.hasNext();) {
				SwitchSubscriberAction action = (SwitchSubscriberAction) it.next();
				dropDown.add(action);				
			}
			if(cancelAction != null) {
				dropDown.add(new Separator());
				dropDown.add(cancelAction);
			}
		}
	 }
	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ui.sync.actions.SyncViewerActionGroup#removeContext(org.eclipse.ui.actions.ActionContext)
	 */
	public void removeContext(ActionContext context) {
		SubscriberInput input = (SubscriberInput)context.getInput();
		actions.remove(input.getSubscriber().getId());
	}
}

Back to the top