Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e9fb11749e30f71c5367e794f2c744d6c15be463 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package org.eclipse.team.internal.ui.sync;

/*
 * (c) Copyright IBM Corp. 2000, 2002.
 * All Rights Reserved.
 */
 
import java.lang.reflect.InvocationTargetException;

import org.eclipse.compare.structuremergeviewer.DiffNode;
import org.eclipse.compare.structuremergeviewer.IDiffElement;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ui.Policy;
import org.eclipse.team.internal.ui.TeamUIPlugin;
import org.eclipse.team.internal.ui.UIConstants;
import org.eclipse.team.ui.TeamImages;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

/**
 * <b>Note:</b> This class/interface is part of an interim API that is still under 
 * development and expected to change significantly before reaching stability. 
 * It is being made available at this early stage to solicit feedback from pioneering 
 * adopters on the understanding that any code that uses this API will almost 
 * certainly be broken (repeatedly) as the API evolves.
 * 
 * This class provides a view for performing synchronizations
 * between the local workspace and a repository.
 */
public class SyncView extends ViewPart {
	public static final String VIEW_ID = "org.eclipse.team.ui.sync.SyncView"; //$NON-NLS-1$
	private SyncCompareInput input;
	private TreeViewer viewer;
	private Composite top;
	
	// The possible sync modes
	public static final int SYNC_NONE = 0;
	public static final int SYNC_INCOMING = 1;
	public static final int SYNC_OUTGOING = 2;
	public static final int SYNC_BOTH = 3;
	public static final int SYNC_MERGE = 4;
	public static final int SYNC_COMPARE = 5;
	
	// Titles cached for efficiency
	private final String CATCHUP_TITLE = Policy.bind("SyncView.incomingModeTitle"); //$NON-NLS-1$
	private final String RELEASE_TITLE = Policy.bind("SyncView.outgoingModeTitle"); //$NON-NLS-1$
	private final String FREE_TITLE = Policy.bind("SyncView.freeModeTitle"); //$NON-NLS-1$
	
	private int currentSyncMode = SYNC_NONE;
	
	/**
	 * Action for toggling the sync mode.
	 */
	class SyncModeAction extends Action {
		// The sync mode that this action enables
		private int syncMode;
		public SyncModeAction(String title, ImageDescriptor image, int mode) {
			super(title, image);
			this.syncMode = mode;
		}
		public void run() {
			SyncView.this.setSyncMode(syncMode);
		}
	}
	
	private SyncModeAction incomingMode;
	private SyncModeAction outgoingMode;
	private SyncModeAction freeMode;

	private class PartListener implements IPartListener {
		public void partActivated(IWorkbenchPart part) {
		}
		public void partBroughtToTop(IWorkbenchPart part) {
		}
		public void partClosed(IWorkbenchPart part) {
		}
		public void partDeactivated(IWorkbenchPart part) {
			if (part == SyncView.this && input != null) {
				input.saveIfNecessary();
			}
		}
		public void partOpened(IWorkbenchPart part) {
		}
	}
	
	private IPartListener partListener;
	
	/**
	 * Creates a new view.
	 */
	public SyncView() {
		super();
	}

	/*
	 * @see IWorkbenchPart#createPartControl
	 */
	public void createPartControl(Composite parent) {
		top = new Composite(parent, SWT.NONE);
		
		//XXX Set the control data to be this part, so the compare 
		//frames that will eventually live in this widget hierarchy
		//have some way to access the action bars for hooking global
		//actions.  See corresponding XXX comment in CompareEditor#findActionBars
		top.setData(this);
		
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		top.setLayout(layout);
		top.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		showDefaultContents();
		
		initializeSyncModes();
		// add part listener
		partListener = new PartListener();
		getViewSite().getWorkbenchWindow().getPartService().addPartListener(partListener);
	}
	
	public void dispose() {
		// remove part listener
		if (partListener != null) {
			getViewSite().getWorkbenchWindow().getPartService().removePartListener(partListener);
			partListener = null;
		}
		super.dispose();
	}

	/**
	 * Makes the sync view visible in the active perspective. If there
	 * isn't a sync view registered <code>null</code> is returned.
	 * Otherwise the opened view part is returned.
	 */
	public static SyncView findInActivePerspective() {
		try {
			IViewPart part = TeamUIPlugin.getActivePage().findView(VIEW_ID);
			if (part == null) {
				part = TeamUIPlugin.getActivePage().showView(VIEW_ID);
			}
			return (SyncView)part;
		} catch (PartInitException pe) {
			return null;
		}
	}
	
	/**
	 * Sets up the sync modes and the actions for switching between them.
	 */
	private void initializeSyncModes() {
		// Create the actions
		incomingMode = new SyncModeAction(
			Policy.bind("SyncView.incomingModeAction"), //$NON-NLS-1$
			TeamImages.getImageDescriptor(UIConstants.IMG_SYNC_MODE_CATCHUP),
			SYNC_INCOMING);
		incomingMode.setToolTipText(Policy.bind("SyncView.incomingModeToolTip")); //$NON-NLS-1$
		incomingMode.setChecked(false);
		
		outgoingMode = new SyncModeAction(
			Policy.bind("SyncView.outgoingModeAction"), //$NON-NLS-1$
			TeamImages.getImageDescriptor(UIConstants.IMG_SYNC_MODE_RELEASE),
			SYNC_OUTGOING);
		outgoingMode.setToolTipText(Policy.bind("SyncView.outgoingModeToolTip")); //$NON-NLS-1$
		outgoingMode.setChecked(false);
		
		freeMode = new SyncModeAction(
			Policy.bind("SyncView.freeModeAction"), //$NON-NLS-1$
			TeamImages.getImageDescriptor(UIConstants.IMG_SYNC_MODE_FREE),
			SYNC_BOTH);
		freeMode.setToolTipText(Policy.bind("SyncView.freeModeToolTip")); //$NON-NLS-1$
		freeMode.setChecked(false);
	}
	
	private boolean isEmpty(DiffNode node) {
		if (node.getKind() != 0) return false;
		IDiffElement[] children = node.getChildren();
		for (int i = 0; i < children.length; i++) {
			if (!isEmpty(children[i])) return false;
		}
		return true;
	}
	private boolean isEmpty(IDiffElement element) {
		if (element.getKind() != 0) return false;
		if (element instanceof DiffNode) {
			IDiffElement[] children = ((DiffNode)element).getChildren();
			for (int i = 0; i < children.length; i++) {
				if (!isEmpty(children[i])) return false;
			}
		}
		return true;
	}
	
	/**
	 * Runs an operation and handles progress and exceptions.  Returns true
	 * if the operation was successful, and false if there were errors or
	 * the user canceled.
	 */
	private boolean run(IRunnableWithProgress op) {
		ProgressMonitorDialog dialog = new ProgressMonitorDialog(getSite().getShell());
		try {
			dialog.run(true, true, op);
			return true;
		} catch (InvocationTargetException e) {
			Throwable throwable = e.getTargetException();
			IStatus error = null;
			if (throwable instanceof TeamException) {
				error = ((TeamException)throwable).getStatus();
			} else if (throwable instanceof CoreException) {
				error = ((CoreException)throwable).getStatus();
			} else {
				error = new Status(IStatus.ERROR, TeamUIPlugin.ID, 1, Policy.bind("simpleInternal"), throwable); //$NON-NLS-1$
			}
			ErrorDialog.openError(getSite().getShell(), Policy.bind("SyncView.unableSynchronize"), null, error); //$NON-NLS-1$
			TeamUIPlugin.log(error);
		} catch (InterruptedException e) {
		}
		return false;
	}
	
	/**
	 * Asks the part to take focus within the workbench.
	 */
	public void setFocus() {
		if (top != null && !top.isDisposed()) {
			top.setFocus();
		}
	}

	/**
	 * Activates the given sync mode.
	 */
	void setSyncMode(int mode) {
		// Implement radio button behaviour
		switch (mode) {
			case SYNC_INCOMING:
				incomingMode.setChecked(true);
				outgoingMode.setChecked(false);
				freeMode.setChecked(false);
				setTitle(CATCHUP_TITLE);
				break;
			case SYNC_OUTGOING:
				outgoingMode.setChecked(true);
				incomingMode.setChecked(false);
				freeMode.setChecked(false);
				setTitle(RELEASE_TITLE);
				break;
			case SYNC_BOTH:
				freeMode.setChecked(true);
				outgoingMode.setChecked(false);
				incomingMode.setChecked(false);
				setTitle(FREE_TITLE);
				break;
		}
		// Only update actions if there is valid input
		if (input != null && input.getDiffRoot() != null && mode != currentSyncMode) {
			currentSyncMode = mode;
			input.getViewer().syncModeChanged(mode);
			updateActions();
		}
	}
	
	/**
	 * Shows default contents for the view if there is nothing to synchronize.
	 */
	private void showDefaultContents() {
		Label label = new Label(top, SWT.WRAP);
		label.setLayoutData(new GridData(GridData.FILL_BOTH));
		label.setText(Policy.bind("SyncView.text")); //$NON-NLS-1$
	}
	
	/**
	 * Shows synchronization information for the given resources in the sync view.
	 */
	public void showSync(SyncCompareInput input) {
		input.setViewSite(getViewSite());
		this.input = input;
		currentSyncMode = SYNC_NONE;
		
		// Remove old viewer
		Control[] oldChildren = top.getChildren();
		if (oldChildren != null) {
			for (int i = 0; i < oldChildren.length; i++) {
				oldChildren[i].dispose();
			}
		}
		// Remove actions from toolbar
		IActionBars bars = getViewSite().getActionBars();
		bars.getToolBarManager().removeAll();
		bars.getToolBarManager().update(false);
		bars.getMenuManager().removeAll();
		bars.getMenuManager().update();
		bars.updateActionBars();
		
		// Display the default contents while running the diff
		showDefaultContents();
		top.layout();
		
		// Run the diff and stop if cancel or error occurred.
		if (!run(input)) return;
		
		// Check for problem message
		if (input.getMessage() != null) {
			MessageDialog.openInformation(getSite().getShell(), Policy.bind("SyncView.unableSynchronize"), input.getMessage()); //$NON-NLS-1$
			return;
		}
		
		// Check for empty comparison
		if (isEmpty(input.getDiffRoot())) {
			MessageDialog.openInformation(getSite().getShell(), Policy.bind("nothingToSynchronize"), Policy.bind("SyncView.same")); //$NON-NLS-1$ //$NON-NLS-2$
			return;
		}
	
		// Remove the default contents
		oldChildren = top.getChildren();
		if (oldChildren != null) {
			for (int i = 0; i < oldChildren.length; i++) {
				oldChildren[i].dispose();
			}
		}

		// Show the result
		Control control = input.createContents(top);
		control.setLayoutData(new GridData(GridData.FILL_BOTH));
		/*TreeViewer viewer = input.getViewer();
		if (viewer != null) {
			Control viewerControl = viewer.getControl();
			if (viewerControl != null && !viewerControl.isDisposed()) {
				WorkbenchHelp.setHelp(viewerControl, new ViewContextComputer(this, ITeamHelpContextIds.SYNC_VIEW));
			}
		}*/
		
		top.layout();
		
		// Set the sync mode depending on user preference
		if (TeamUIPlugin.getPlugin().getPreferenceStore().getBoolean(UIConstants.PREF_ALWAYS_IN_INCOMING_OUTGOING)) {
			freeMode.run();
		} else {
			if (input.hasIncomingChanges()) {
				incomingMode.run();
			} else {
				outgoingMode.run();
			}
		}
		// Reveal if fast view
		try {
			TeamUIPlugin.getActivePage().showView(VIEW_ID);
		} catch (PartInitException e) {
			TeamUIPlugin.log(e.getStatus());
		}
	}
	
	/**
	 * Updates the actions for this view's action bar.
	 */
	private void updateActions() {
		IActionBars bars = getViewSite().getActionBars();
		IToolBarManager toolBar = bars.getToolBarManager();
		IMenuManager menu = bars.getMenuManager();
		toolBar.removeAll();
		menu.removeAll();
		
		toolBar.add(incomingMode);
		toolBar.add(outgoingMode);
		toolBar.add(freeMode);
		input.getViewer().contributeToActionBars(bars);
		
		toolBar.update(false);
		menu.update(false);
		bars.updateActionBars();
	}
}

Back to the top