Skip to main content
summaryrefslogtreecommitdiffstats
blob: fb19f71625a4a07e97d148acb8502d8cda79abd6 (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, 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.ui.synchronize.subscribers;

import org.eclipse.compare.structuremergeviewer.DiffNode;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.action.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.team.internal.ui.Policy;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement;
import org.eclipse.team.internal.ui.synchronize.actions.OpenWithActionGroup;
import org.eclipse.team.internal.ui.synchronize.actions.RefactorActionGroup;
import org.eclipse.team.ui.synchronize.ISynchronizeView;
import org.eclipse.team.ui.synchronize.TreeViewerAdvisor;

/**
 * Overrides the SyncInfoDiffViewerConfiguration to configure the diff viewer
 * for the synchroniza view
 */
public class SynchronizeViewerAdvisor extends TreeViewerAdvisor {

	private ISynchronizeView view;
	private SubscriberParticipant participant;
	private OpenWithActionGroup openWithActions;
	private RefactorActionGroup refactorActions;
	private Action refreshSelectionAction;

	public SynchronizeViewerAdvisor(ISynchronizeView view, SubscriberParticipant participant) {
		super(participant.getId(), view.getViewSite(), participant.getSubscriberSyncInfoCollector().getSyncInfoTree());
		this.view = view;
		this.participant = participant;
	}

	protected SubscriberParticipant getParticipant() {
		return participant;
	}

	protected void initializeActions(StructuredViewer treeViewer) {
		super.initializeActions(treeViewer);
		openWithActions = new OpenWithActionGroup(view, participant.getName());
		refactorActions = new RefactorActionGroup(view);
		refreshSelectionAction = new Action() {
			public void run() {
				StructuredViewer viewer = getViewer();
				if(viewer != null && ! viewer.getControl().isDisposed()) {
					IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
					IResource[] resources = Utils.getResources(selection.toArray());
					participant.refresh(resources, participant.getRefreshListeners().createSynchronizeViewListener(participant), Policy.bind("Participant.synchronizing"), view.getSite()); //$NON-NLS-1$
				}
			}
		};
		Utils.initAction(refreshSelectionAction, "action.refreshWithRemote."); //$NON-NLS-1$
	}

	protected ISynchronizeView getSynchronizeView() {
		return view;
	}
	
	protected void fillContextMenu(StructuredViewer viewer, IMenuManager manager) {
		openWithActions.fillContextMenu(manager);
		refactorActions.fillContextMenu(manager);
		manager.add(refreshSelectionAction);
		manager.add(new Separator());
		super.fillContextMenu(viewer, manager);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.team.ui.synchronize.SyncInfoDiffTreeViewer#handleDoubleClick(org.eclipse.jface.viewers.DoubleClickEvent)
	 */
	protected void handleDoubleClick(StructuredViewer viewer, DoubleClickEvent event) {
		IStructuredSelection selection = (IStructuredSelection) event.getSelection();
		DiffNode node = (DiffNode) selection.getFirstElement();
		if (node != null && node instanceof SyncInfoModelElement) {
			SyncInfoModelElement syncNode = (SyncInfoModelElement) node;
			IResource resource = syncNode.getResource();
			if (syncNode != null && resource != null && resource.getType() == IResource.FILE) {
				openWithActions.openInCompareEditor();
				return;
			}
		}
		// Double-clicking should expand/collapse containers
		super.handleDoubleClick(viewer, event);
	}

	protected void initializeListeners(StructuredViewer viewer) {
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {

		public void selectionChanged(SelectionChangedEvent event) {
				updateStatusLine((IStructuredSelection) event.getSelection());
			}
		});
		viewer.addOpenListener(new IOpenListener() {

		public void open(OpenEvent event) {
				handleOpen();
			}
		});
		super.initializeListeners(viewer);
	}

	protected void handleOpen() {
		openWithActions.openInCompareEditor();
	}

	/**
	 * Updates the message shown in the status line.
	 * @param selection
	 *            the current selection
	 */
	private void updateStatusLine(IStructuredSelection selection) {
		String msg = getStatusLineMessage(selection);
		view.getViewSite().getActionBars().getStatusLineManager().setMessage(msg);
	}

	/**
	 * Returns the message to show in the status line.
	 * @param selection
	 *            the current selection
	 * @return the status line message
	 * @since 2.0
	 */
	private String getStatusLineMessage(IStructuredSelection selection) {
		if (selection.size() == 1) {
			Object first = selection.getFirstElement();
			if (first instanceof SyncInfoModelElement) {
				SyncInfoModelElement node = (SyncInfoModelElement) first;
				IResource resource = node.getResource();
				if (resource == null) {
					return node.getName();
				} else {
					return resource.getFullPath().makeRelative().toString();
				}
			}
		}
		if (selection.size() > 1) {
			return selection.size() + Policy.bind("SynchronizeView.13"); //$NON-NLS-1$
		}
		return ""; //$NON-NLS-1$
	}
}

Back to the top