Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7cdd343a6c7ff22e8ad2006039b6ef789bc0e05a (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
/*******************************************************************************
 * 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.synchronize.actions;

import org.eclipse.jface.action.*;
import org.eclipse.jface.util.*;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.ui.synchronize.ISynchronizeView;
import org.eclipse.team.ui.synchronize.TeamSubscriberParticipant;
import org.eclipse.ui.*;
import org.eclipse.ui.actions.ActionGroup;

/**
 * Adds working set filter actions (set / clear / edit)
 * 
 * @since 2.1  
 */
public class WorkingSetFilterActionGroup extends ActionGroup {
	public static final String CHANGE_WORKING_SET = "changeWorkingSet"; //$NON-NLS-1$
	
	private IWorkingSet workingSet = null;
	
	private ClearWorkingSetAction clearWorkingSetAction;
	private SelectWorkingSetAction selectWorkingSetAction;
	private EditWorkingSetAction editWorkingSetAction;
	private IPropertyChangeListener workingSetUpdater;
	private IActionBars bars;
	private IContributionItem item;
	private String id;

	/**
	 * Creates a new instance of the receiver
	 * 
	 * @param shell shell to open dialogs and wizards on
	 * @param workingSetUpdater property change listener notified when a 
	 * 	working set is set
	 */
	public WorkingSetFilterActionGroup(Shell shell, IPropertyChangeListener workingSetUpdater, ISynchronizeView view, TeamSubscriberParticipant participant) {
		Assert.isNotNull(shell);
		this.id = participant.toString();
		this.workingSetUpdater = workingSetUpdater;
		this.workingSet = participant.getWorkingSet();
		clearWorkingSetAction = new ClearWorkingSetAction(this);
		selectWorkingSetAction = new SelectWorkingSetAction(this, shell);
		editWorkingSetAction = new EditWorkingSetAction(this, shell);
	}

	/**
	 * Adds working set actions to the specified action bar.
	 * 
	 * @param actionBars action bar to add working set actions to.
	 * @see ActionGroup#fillActionBars(IActionBars)
	 */
	public void fillActionBars(IActionBars actionBars) {
		bars = actionBars;
		IMenuManager menuManager = actionBars.getMenuManager();
		menuManager.add(selectWorkingSetAction);
		menuManager.add(clearWorkingSetAction);
		menuManager.add(editWorkingSetAction);
		menuManager.add(new Separator(id));
		updateMruContribution(menuManager);
	};
	
	private void updateMruContribution(IMenuManager menuManager) {
		IWorkingSet[] sets = PlatformUI.getWorkbench().getWorkingSetManager().getRecentWorkingSets();
		if(sets.length > 0) {
			if(item == null) {
				item = new WorkingSetMenuContributionItem(id, this);
				menuManager.prependToGroup(id, item);
			}
		} else {
			if(item != null) {
				menuManager.remove(item);
			}
			item = null;
		}
	}

	/**
	 * Returns the working set which is currently selected.
	 * 
	 * @return the working set which is currently selected.
	 */
	public IWorkingSet getWorkingSet() {
		return workingSet;
	}
	
	/**
	 * Sets the current working set.
	 * 
	 * @param newWorkingSet the new working set
	 */
	public void setWorkingSet(IWorkingSet newWorkingSet) {
		IWorkingSet oldWorkingSet = workingSet;
		
		workingSet = newWorkingSet;
		// Update action
		clearWorkingSetAction.setEnabled(newWorkingSet != null);
		editWorkingSetAction.setEnabled(newWorkingSet != null);
		
		if(workingSet != null)
			PlatformUI.getWorkbench().getWorkingSetManager().addRecentWorkingSet(newWorkingSet);
		// Update viewer
		if (workingSetUpdater != null) {
			workingSetUpdater.propertyChange(
				new PropertyChangeEvent(
					this, 
					WorkingSetFilterActionGroup.CHANGE_WORKING_SET, 
					oldWorkingSet, 
					newWorkingSet));
		}
		
		// Trick to get dynamic menu contribution for most-recent list to
		// be updated. These are action contributions and must be added/removed
		// before the menu is shown.
		updateMruContribution(bars.getMenuManager());
		//bars.updateActionBars();
	}	
}

Back to the top