Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3af7efa9b81718fe8d83982dbee38841451efd95 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*******************************************************************************
 * 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 java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IContributionManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.team.internal.ui.Policy;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
import org.eclipse.team.ui.synchronize.SubscriberParticipant;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.actions.ActionGroup;

/**
 * This action group provides radio buttons for each possible direction of synchronization information. The
 * modes created by this action group can be configured. The actions directly set the mode of a
 * {@link SubscriberParticipant}.
 *
 * @see SubscriberParticipant
 * @since 3.0 
 */
public class DirectionFilterActionGroup extends ActionGroup implements IPropertyChangeListener {
	
	// The list of created actions	
	private List actions = new ArrayList(3);
	
	// The modes
	private DirectionFilterAction incomingMode;					
	private DirectionFilterAction outgoingMode;
	private DirectionFilterAction bothMode;
	private DirectionFilterAction conflictsMode;
	
	private ISynchronizePageConfiguration configuration;
	
	/**
	 * An action filter for a specific mode. 
	 */
	class DirectionFilterAction extends Action {
		private int modeId;
		
		public DirectionFilterAction(String prefix,String commandId, int modeId) {
			super("", AS_RADIO_BUTTON); //$NON-NLS-1$
			this.modeId = modeId;
			Utils.initAction(this, prefix, Policy.getBundle());
		}
		public void run() {
			if(isChecked()) {
				configuration.setMode(modeId);
			}
		}
		public int getModeId() {
			return modeId;
		}
	}
	
	/**
	 * Creates a direction filter group with the given supported modes. The
	 * possible values for modes are defined by the {@link SubscriberParticipant}
	 * class.
	 * 
	 * @see SubscriberParticipant#BOTH_MODE
	 * @see SubscriberParticipant#OUTGOING_MODE
	 * @see SubscriberParticipant#INCOMING_MODE
	 * @see SubscriberParticipant#CONFLICTING_MODE
	 * @see SubscriberParticipant#ALL_MODES
	 * 
	 * @param participant the participant showing this group 
	 * @param supportedModes the modes to be shown
	 */
	public DirectionFilterActionGroup(ISynchronizePageConfiguration configuration) {		
		this.configuration = configuration;
		createActions();
		configuration.addPropertyChangeListener(this);
		checkMode(configuration.getMode());
	}
	
	/**
	 * Sets up the sync modes and the actions for switching between them.
	 */
	private void createActions() {
		// Create the actions
		int supportedModes = configuration.getSupportedModes();
		if (supportedModes == 0) return;
		int currentMode = configuration.getMode();
		if ((supportedModes & currentMode) == 0) {
			currentMode = getSupportedMode(supportedModes);
			if (currentMode == 0) return;
			configuration.setMode(currentMode);
		}
		if((supportedModes & ISynchronizePageConfiguration.INCOMING_MODE) != 0) {
			incomingMode = new DirectionFilterAction("action.directionFilterIncoming.", "org.eclipse.team.ui.syncview.incomingFilter",  ISynchronizePageConfiguration.INCOMING_MODE); //$NON-NLS-1$ //$NON-NLS-2$
			actions.add(incomingMode);
			incomingMode.setChecked(currentMode == ISynchronizePageConfiguration.INCOMING_MODE);
		}
		
		if((supportedModes & ISynchronizePageConfiguration.OUTGOING_MODE) != 0) {
			outgoingMode = new DirectionFilterAction("action.directionFilterOutgoing.", "org.eclipse.team.ui.syncview.outgoingFilter",  ISynchronizePageConfiguration.OUTGOING_MODE); //$NON-NLS-1$ //$NON-NLS-2$
			actions.add(outgoingMode);
			outgoingMode.setChecked(currentMode == ISynchronizePageConfiguration.OUTGOING_MODE);
		}
		
		if((supportedModes & ISynchronizePageConfiguration.BOTH_MODE) != 0) {
			bothMode = new DirectionFilterAction("action.directionFilterBoth.", "org.eclipse.team.ui.syncview.bothFilter", ISynchronizePageConfiguration.BOTH_MODE); //$NON-NLS-1$ //$NON-NLS-2$
			actions.add(bothMode);
			bothMode.setChecked(currentMode == ISynchronizePageConfiguration.BOTH_MODE);
		}
		
		if((supportedModes & ISynchronizePageConfiguration.CONFLICTING_MODE) != 0) {
			conflictsMode = new DirectionFilterAction("action.directionFilterConflicts.", "org.eclipse.team.ui.syncview.conflictsFilter", ISynchronizePageConfiguration.CONFLICTING_MODE); //$NON-NLS-1$ //$NON-NLS-2$
			actions.add(conflictsMode);
			conflictsMode.setChecked(currentMode == ISynchronizePageConfiguration.CONFLICTING_MODE);
		}
	}
	
	/**
	 * @param supportedModes
	 * @return
	 */
	private int getSupportedMode(int supportedModes) {
		if((supportedModes & ISynchronizePageConfiguration.INCOMING_MODE) != 0) {
			return ISynchronizePageConfiguration.INCOMING_MODE;
		}
		if((supportedModes & ISynchronizePageConfiguration.OUTGOING_MODE) != 0) {
			return ISynchronizePageConfiguration.OUTGOING_MODE;
		}
		if((supportedModes & ISynchronizePageConfiguration.BOTH_MODE) != 0) {
			return ISynchronizePageConfiguration.BOTH_MODE;
		}
		if((supportedModes & ISynchronizePageConfiguration.CONFLICTING_MODE) != 0) {
			return ISynchronizePageConfiguration.CONFLICTING_MODE;
		}
		return 0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.actions.ActionGroup#fillActionBars(org.eclipse.ui.IActionBars)
	 */
	public void fillActionBars(IActionBars actionBars, String group) {
		super.fillActionBars(actionBars);
		IToolBarManager toolBar = actionBars.getToolBarManager();
		for (Iterator it = actions.iterator(); it.hasNext();) {
			DirectionFilterAction action = (DirectionFilterAction) it.next();
			if(group != null) {
				toolBar.appendToGroup(group, action);
			} else {
				toolBar.add(action);
			}
		}
	}
	
	public void fillToolBar(String groupId, IToolBarManager toolBar) {
		for (Iterator it = actions.iterator(); it.hasNext();) {
			DirectionFilterAction action = (DirectionFilterAction) it.next();
				toolBar.appendToGroup(groupId, action);
		}
	}
	
	public void fillMenu(IContributionManager manager) {
		for (Iterator it = actions.iterator(); it.hasNext();) {
			DirectionFilterAction action = (DirectionFilterAction) it.next();
				manager.add(action);
		}
	}
	
	private void checkMode(int mode) {
		for (Iterator it = actions.iterator(); it.hasNext();) {
			DirectionFilterAction action = (DirectionFilterAction)it.next();
			if(action.getModeId() == mode) {
				action.setChecked(true);
			} else {
				action.setChecked(false);
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
	 */
	public void propertyChange(PropertyChangeEvent event) {
		if(event.getProperty().equals(ISynchronizePageConfiguration.P_MODE)) {
			Integer mode = (Integer)event.getNewValue();
			checkMode(mode.intValue());
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.actions.ActionGroup#dispose()
	 */
	public void dispose() {
		super.dispose();
	}
}

Back to the top