1 /*******************************************************************************
2  * Copyright (c) 2006, 2017 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.team.internal.ui.synchronize;
15 
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.team.internal.ui.TeamUIMessages;
24 import org.eclipse.team.internal.ui.TeamUIPlugin;
25 import org.eclipse.team.internal.ui.Utils;
26 import org.eclipse.team.ui.ISharedImages;
27 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
28 import org.eclipse.ui.forms.events.HyperlinkAdapter;
29 import org.eclipse.ui.forms.events.HyperlinkEvent;
30 import org.eclipse.ui.forms.widgets.Hyperlink;
31 
32 /**
33  * A changes section that points the user to a mode that has changes when the current mode
34  * is empty.
35  */
36 public abstract class ForwardingChangesSection extends ChangesSection {
37 
38 	/**
39 	 * Shows message to user is no changes are to be shown in the diff
40 	 * tree viewer.
41 	 */
42 	private Composite messageArea;
43 
ForwardingChangesSection(Composite parent, AbstractSynchronizePage page, ISynchronizePageConfiguration configuration)44 	public ForwardingChangesSection(Composite parent, AbstractSynchronizePage page, ISynchronizePageConfiguration configuration) {
45 		super(parent, page, configuration);
46 	}
47 
48 	@Override
initializeChangesViewer()49 	protected void initializeChangesViewer() {
50 		calculateDescription();
51 	}
52 
calculateDescription()53 	protected void calculateDescription() {
54 		if (getContainer().isDisposed())
55 			return;
56 		if(getVisibleChangesCount() == 0) {
57 			TeamUIPlugin.getStandardDisplay().asyncExec(() -> {
58 				if (!getContainer().isDisposed())
59 					updatePage(getEmptyChangesComposite(getContainer()));
60 			});
61 		} else {
62 			TeamUIPlugin.getStandardDisplay().asyncExec(() -> updatePage(null));
63 		}
64 	}
65 
updatePage(Composite message)66 	protected void updatePage(Composite message) {
67 		if (getContainer().isDisposed()) return;
68 		if(messageArea != null) {
69 			messageArea.dispose();
70 			messageArea = null;
71 		}
72 		messageArea = message;
73 		if (message == null) {
74 			Control control = getChangesViewer().getControl();
75 			if (!getContainer().isDisposed() && !control.isDisposed()) {
76 				getContainer().showPage(control);
77 			}
78 		} else {
79 			getContainer().showPage(messageArea);
80 		}
81 	}
82 
getEmptyChangesComposite(Composite parent)83 	protected Composite getEmptyChangesComposite(Composite parent) {
84 		Composite composite = new Composite(parent, SWT.NONE);
85 		composite.setBackground(getListBackgroundColor());
86 		GridLayout layout = new GridLayout();
87 		layout.numColumns = 2;
88 		composite.setLayout(layout);
89 		GridData data = new GridData(GridData.FILL_BOTH);
90 		data.grabExcessVerticalSpace = true;
91 		composite.setLayoutData(data);
92 
93 		if(! isThreeWay()) {
94 			createDescriptionLabel(composite,NLS.bind(TeamUIMessages.ChangesSection_noChanges, new String[] { Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, getConfiguration().getParticipant().getName()) }));
95 			return composite;
96 		}
97 
98 		int allChanges = getChangesCount();
99 		long visibleChanges = getVisibleChangesCount();
100 
101 		if(visibleChanges == 0 && allChanges != 0) {
102 			final int candidateMode = getCandidateMode();
103 			int currentMode = getConfiguration().getMode();
104 			if (candidateMode != currentMode) {
105 				long numChanges = getChangesInMode(candidateMode);
106 				if (numChanges > 0) {
107 					String message;
108 					if(numChanges > 1) {
109 						message = NLS.bind(TeamUIMessages.ChangesSection_filterHidesPlural, new String[] { Long.toString(numChanges), Utils.modeToString(candidateMode) });
110 					} else {
111 						message = NLS.bind(TeamUIMessages.ChangesSection_filterHidesSingular, new String[] { Long.toString(numChanges), Utils.modeToString(candidateMode) });
112 					}
113 					message = NLS.bind(TeamUIMessages.ChangesSection_filterHides, new String[] { Utils.modeToString(getConfiguration().getMode()), message });
114 
115 					Label warning = new Label(composite, SWT.NONE);
116 					warning.setImage(TeamUIPlugin.getPlugin().getImage(ISharedImages.IMG_WARNING_OVR));
117 
118 					Hyperlink link = getForms().createHyperlink(composite, NLS.bind(TeamUIMessages.ChangesSection_filterChange, new String[] { Utils.modeToString(candidateMode) }), SWT.WRAP);
119 					link.addHyperlinkListener(new HyperlinkAdapter() {
120 						@Override
121 						public void linkActivated(HyperlinkEvent e) {
122 							getConfiguration().setMode(candidateMode);
123 						}
124 					});
125 					getForms().getHyperlinkGroup().add(link);
126 					createDescriptionLabel(composite, message);
127 					return composite;
128 				}
129 			}
130 		}
131 		// There is no other mode that can be shown so just indicate that there are no changes
132 		createDescriptionLabel(composite,NLS.bind(TeamUIMessages.ChangesSection_noChanges, new String[] { Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, getConfiguration().getParticipant().getName()) }));	 //
133 		return composite;
134 	}
135 
createDescriptionLabel(Composite parent, String text)136 	protected Label createDescriptionLabel(Composite parent, String text) {
137 		Label description = new Label(parent, SWT.WRAP);
138 		GridData data = new GridData(GridData.FILL_HORIZONTAL);
139 		data.horizontalSpan = 2;
140 		data.widthHint = 100;
141 		description.setLayoutData(data);
142 		description.setText(text);
143 		description.setBackground(getListBackgroundColor());
144 		return description;
145 	}
146 
getChangesCount()147 	protected abstract int getChangesCount();
148 
getChangesInMode(int candidateMode)149 	protected abstract long getChangesInMode(int candidateMode);
150 
getVisibleChangesCount()151 	protected abstract long getVisibleChangesCount();
152 
getCandidateMode()153 	protected abstract int getCandidateMode();
154 }
155