Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9c3b37b5b7014913550d411879df77a9b5ddccef (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
/*******************************************************************************
 * Copyright (c) 2000, 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.synchronize;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.team.internal.ui.TeamUIMessages;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.internal.ui.synchronize.actions.StatusLineContributionGroup;
import org.eclipse.team.internal.ui.synchronize.actions.SyncInfoSetStatusLineContributionGroup;
import org.eclipse.team.ui.synchronize.*;
import org.eclipse.ui.*;
import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer;
import org.eclipse.ui.model.BaseWorkbenchContentProvider;
import org.eclipse.ui.part.ResourceTransfer;

/**
 * A <code>TreeViewerAdvisor</code> that works with TreeViewers. Two default
 * tree viewers are provided that support navigation: <code>NavigableTreeViewer</code>
 * and <code>NavigableCheckboxTreeViewer</code>.
 * <p>
 * Note that this advisor can be used with any tree viewer. By default it provides an
 * expand all action, double click behavior on containers, and navigation support for
 * tree viewers.
 * </p><p>
 * By default this advisor supports hierarchical models and honour the compressed
 * folder Team preference for showing the sync set as compressed folders. Subclasses
 * can provide their own presentation models.
 * <p>
 * @since 3.0
 */
public class TreeViewerAdvisor extends AbstractTreeViewerAdvisor {

	// Special actions that could not be contributed using an ActionGroup
	private StatusLineContributionGroup statusLine;

	/**
	 * Style bit that indicates that a checkbox viewer is desired.
	 */
	public static final int CHECKBOX = 1;

	private SynchronizeModelManager modelManager;

	/**
	 * A navigable checkbox tree viewer that will work with the <code>navigate</code> method of
	 * this advisor.
	 */
	public static class NavigableCheckboxTreeViewer extends ContainerCheckedTreeViewer implements ITreeViewerAccessor {
		public NavigableCheckboxTreeViewer(Composite parent, int style) {
			super(parent, style);
			setUseHashlookup(true);
		}

		@Override
		public void createChildren(TreeItem item) {
			super.createChildren(item);
		}

		@Override
		public void openSelection() {
			fireOpen(new OpenEvent(this, getSelection()));
		}
	}

	/**
	 * A navigable tree viewer that will work with the <code>navigate</code> method of
	 * this advisor.
	 */
	public static class NavigableTreeViewer extends TreeViewer implements ITreeViewerAccessor {
		public NavigableTreeViewer(Composite parent, int style) {
			super(parent, style);
			setUseHashlookup(true);
		}

		@Override
		public void createChildren(TreeItem item) {
			super.createChildren(item);
		}

		@Override
		public void openSelection() {
			fireOpen(new OpenEvent(this, getSelection()));
		}
	}

	public static StructuredViewer createViewer(Composite parent, ISynchronizePageConfiguration configuration) {
		int style = ((SynchronizePageConfiguration)configuration).getViewerStyle();
		if ((style & CHECKBOX) > 0) {
			NavigableCheckboxTreeViewer v = new TreeViewerAdvisor.NavigableCheckboxTreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
			configuration.getSite().setSelectionProvider(v);
			return v;
		} else {
			NavigableTreeViewer v = new TreeViewerAdvisor.NavigableTreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
			configuration.getSite().setSelectionProvider(v);
			return v;
		}
	}

	/**
	 * Create an advisor that will allow viewer contributions with the given <code>targetID</code>. This
	 * advisor will provide a presentation model based on the given sync info set. Note that it's important
	 * to call {@link #dispose()} when finished with an advisor.
	 *
	 * @param parent
	 * @param configuration
	 */
	public TreeViewerAdvisor(Composite parent, ISynchronizePageConfiguration configuration) {
		super(configuration);

		// Allow the configuration to provide it's own model manager but if one isn't initialized, then
		// simply use the default provided by the advisor.
		modelManager = (SynchronizeModelManager)configuration.getProperty(SynchronizePageConfiguration.P_MODEL_MANAGER);
		if(modelManager == null) {
			modelManager = createModelManager(configuration);
			configuration.setProperty(SynchronizePageConfiguration.P_MODEL_MANAGER, modelManager);
		}
		Assert.isNotNull(modelManager, "model manager must be set"); //$NON-NLS-1$
		modelManager.setViewerAdvisor(this);

		StructuredViewer viewer = TreeViewerAdvisor.createViewer(parent, configuration);
		GridData data = new GridData(GridData.FILL_BOTH);
		viewer.getControl().setLayoutData(data);
		initializeViewer(viewer);
	}

	@Override
	public void setInitialInput() {
		// The input will be set later
	}

	/**
	 * Create the model manager to be used by this advisor
	 * @param configuration
	 */
	protected SynchronizeModelManager createModelManager(ISynchronizePageConfiguration configuration) {
        ISynchronizeParticipant participant = configuration.getParticipant();
        if (participant instanceof IChangeSetProvider) {
            IChangeSetProvider provider = (IChangeSetProvider) participant;
    	    ChangeSetCapability changeSetCapability = provider.getChangeSetCapability();
            if (changeSetCapability != null) {
    	        if (changeSetCapability.supportsActiveChangeSets() || changeSetCapability.supportsCheckedInChangeSets()) {
    	            return new ChangeSetModelManager(configuration);
    	        }
    	    }
        }
		return new HierarchicalModelManager(configuration);
	}

	/*
	 * For use by test cases only
	 * @return Returns the modelManager.
	 */
	public SynchronizeModelManager getModelManager() {
		return modelManager;
	}

	@Override
	public boolean validateViewer(StructuredViewer viewer) {
		return viewer instanceof AbstractTreeViewer;
	}

	@Override
	protected void initializeListeners(final StructuredViewer viewer) {
		super.initializeListeners(viewer);
		viewer.addSelectionChangedListener(event -> updateStatusLine(event.getStructuredSelection()));
	}

	/* private */ void updateStatusLine(IStructuredSelection selection) {
		IWorkbenchSite ws = getConfiguration().getSite().getWorkbenchSite();
		if (ws != null && ws instanceof IViewSite) {
			String msg = getStatusLineMessage(selection);
			((IViewSite)ws).getActionBars().getStatusLineManager().setMessage(msg);
		}
	}

	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() + TeamUIMessages.SynchronizeView_13;
		}
		return ""; //$NON-NLS-1$
	}

	/**
	 * Called to set the input to a viewer. The input to a viewer is always the model created
	 * by the model provider.
	 *
	 * @param modelProvider
	 */
	public final void setInput(final ISynchronizeModelProvider modelProvider) {
		final ISynchronizeModelElement modelRoot = modelProvider.getModelRoot();
		getActionGroup().modelChanged(modelRoot);
		modelRoot.addCompareInputChangeListener(source -> getActionGroup().modelChanged(modelRoot));
		final StructuredViewer viewer = getViewer();
		if (viewer != null) {
			viewer.setSorter(modelProvider.getViewerSorter());
			viewer.setInput(modelRoot);
			modelProvider.addPropertyChangeListener(event -> {
			    if (event.getProperty() == ISynchronizeModelProvider.P_VIEWER_SORTER) {
			        if (viewer != null && !viewer.getControl().isDisposed()) {
			            viewer.getControl().getDisplay().syncExec(() -> {
						    if (viewer != null && !viewer.getControl().isDisposed()) {
						        ViewerSorter newSorter = modelProvider.getViewerSorter();
						        ViewerSorter oldSorter = viewer.getSorter();
						        if (newSorter == oldSorter) {
						            viewer.refresh();
						        } else {
						            viewer.setSorter(newSorter);
						        }
						    }
						});
			        }
			    }
			});
		}
	}

	/**
	 * Install a viewer to be configured with this advisor. An advisor can only be installed with
	 * one viewer at a time. When this method completes the viewer is considered initialized and
	 * can be shown to the user.
	 * @param viewer the viewer being installed
	 */
	@Override
	public final void initializeViewer(final StructuredViewer viewer) {
		super.initializeViewer(viewer);

		final DragSourceListener listener = new DragSourceListener() {

            @Override
			public void dragStart(DragSourceEvent event) {
				final IStructuredSelection selection = viewer.getStructuredSelection();
                final Object [] array= selection.toArray();
                event.doit= Utils.getResources(array).length > 0;
			}

            @Override
			public void dragSetData(DragSourceEvent event) {

                if (ResourceTransfer.getInstance().isSupportedType(event.dataType)) {
                    final IStructuredSelection selection= viewer.getStructuredSelection();
                    final Object [] array= selection.toArray();
                    event.data= Utils.getResources(array);
                }
            }

            @Override
			public void dragFinished(DragSourceEvent event) {}
		};

		final int ops = DND.DROP_COPY | DND.DROP_LINK;
		viewer.addDragSupport(ops, new Transfer[] { ResourceTransfer.getInstance() }, listener);

		viewer.setLabelProvider(getLabelProvider());
		viewer.setContentProvider(getContentProvider());
	}

	/**
	 * Returns the content provider for the viewer.
	 *
	 * @return the content provider for the viewer.
	 */
	protected IStructuredContentProvider getContentProvider() {
		return new BaseWorkbenchContentProvider();
	}

	/**
	 * Get the label provider that will be assigned to the viewer initialized
	 * by this configuration. Subclass may override but should either wrap the
	 * default one provided by this method or subclass <code>TeamSubscriberParticipantLabelProvider</code>.
	 * In the later case, the logical label provider should still be assigned
	 * to the subclass of <code>TeamSubscriberParticipantLabelProvider</code>.
	 * @return a label provider
	 * @see SynchronizeModelElementLabelProvider
	 */
	protected ILabelProvider getLabelProvider() {
		ILabelProvider provider = new SynchronizeModelElementLabelProvider();
		ILabelDecorator[] decorators = (ILabelDecorator[])getConfiguration().getProperty(ISynchronizePageConfiguration.P_LABEL_DECORATORS);
		if (decorators == null) {
			return provider;
		}
		return new DecoratingColorLabelProvider(provider, decorators);
	}

	@Override
	public void dispose() {
		if (statusLine != null) {
			statusLine.dispose();
		}
		super.dispose();
	}

	@Override
	protected void initializeStatusLine(IActionBars actionBars) {
		statusLine = new SyncInfoSetStatusLineContributionGroup(
				getConfiguration().getSite().getShell(),
				getConfiguration());
		IStatusLineManager statusLineMgr = actionBars.getStatusLineManager();
		if (statusLineMgr != null && statusLine != null) {
			statusLine.fillActionBars(actionBars);
		}
	}
}

Back to the top