Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cd6aa3317eff27ff01461c9f0e6eaaca175fd349 (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
/*******************************************************************************
 * Copyright (c) 2010, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.wizards;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.compare.internal.core.patch.DiffProject;
import org.eclipse.compare.internal.patch.WorkspacePatcher;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.*;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.team.internal.ui.TeamUIMessages;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.views.navigator.ResourceComparator;

public class PatchInaccessibleProjectsPage extends WizardPage {

	private CheckboxTableViewer checkList;
	private Button checkAllButton;
	private Button uncheckAllButton;

	private WorkspacePatcher fPatcher;

	public final static String PATCH_INACCESSIBLE_PROJECTS_NAME = "PatchInaccessibleProjectsPage"; //$NON-NLS-1$

	public PatchInaccessibleProjectsPage(WorkspacePatcher patcher) {
		super(PATCH_INACCESSIBLE_PROJECTS_NAME,
				TeamUIMessages.PatchInaccessibleProjectsPage_title, null);
		setMessage(TeamUIMessages.PatchInaccessibleProjectsPage_message);
		fPatcher = patcher;
	}

	@Override
	public void createControl(Composite parent) {
		initializeDialogUnits(parent);

		Composite composite = new Composite(parent, SWT.NULL);
		composite.setLayout(new GridLayout(3, false));
		composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL
				| GridData.HORIZONTAL_ALIGN_FILL));
		setControl(composite);
		Font parentFont = composite.getFont();

		checkList = CheckboxTableViewer.newCheckList(composite, SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.BORDER);
		checkList.setContentProvider(new ArrayContentProvider());
		checkList.setLabelProvider(new WorkbenchLabelProvider() {
			@Override
			public Color getForeground(Object element) {
				if (element instanceof IProject
						&& !((IProject) element).exists())
					return Display.getCurrent().getSystemColor(
							SWT.COLOR_WIDGET_NORMAL_SHADOW);
				return super.getForeground(element);
			}

			@Override
			protected String decorateText(String input, Object element) {
				if (element instanceof IProject
						&& !((IProject) element).exists())
					return input
							+ NLS.bind(
									TeamUIMessages.PatchInaccessibleProjectsPage_projectDoesNotExistInWorkspace,
									""); //$NON-NLS-1$
				return input;
			}
		});
		checkList.addCheckStateListener(event -> {
			IProject project = (IProject) event.getElement();
			if (event.getChecked() && !project.exists())
				checkList.setChecked(project, false);
		});
		checkList
				.setComparator(new ResourceComparator(ResourceComparator.NAME));

		Table table = checkList.getTable();
		GridData data = new GridData(GridData.VERTICAL_ALIGN_FILL
				| GridData.HORIZONTAL_ALIGN_FILL);
		data.horizontalSpan = 3;
		data.grabExcessHorizontalSpace = true;
		data.grabExcessVerticalSpace = true;
		table.setLayoutData(data);

		checkAllButton = new Button(composite, SWT.NONE);
		checkAllButton
				.setText(TeamUIMessages.PatchInaccessibleProjectsPage_selectExisting);
		checkAllButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setAllChecked(true);
			}
		});
		checkAllButton.setFont(parentFont);
		setButtonLayoutData(checkAllButton);

		uncheckAllButton = new Button(composite, SWT.NONE);
		uncheckAllButton
				.setText(TeamUIMessages.PatchInaccessibleProjectsPage_deselectAll);
		uncheckAllButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setAllChecked(false);
			}
		});
		uncheckAllButton.setFont(parentFont);
		setButtonLayoutData(uncheckAllButton);

		updateControls();
	}

	private void updateControls() {
		DiffProject[] diffProjects = fPatcher.getDiffProjects();
		List<IProject> projects = new ArrayList<>();
		if (diffProjects != null) {
			for (int i = 0; i < diffProjects.length; i++) {
				IProject project = ResourcesPlugin.getWorkspace().getRoot()
						.getProject(diffProjects[i].getName());
				if (!project.isAccessible())
					projects.add(project);
			}
		}
		checkList.setInput(projects.toArray(new IProject[] {}));
	}

	@Override
	public void setVisible(boolean visible) {
		super.setVisible(visible);
		if (visible)
			updateControls();
	}

	@Override
	public IWizardPage getNextPage() {
		// Skipping the patch parsed page in case this one is displayed
		Control control = getControl();
		if (control != null && control.isVisible())
			return null;
		return super.getNextPage();
	}

	public IProject[] getSelectedProjects() {
		Object elements[] = checkList.getCheckedElements();
		List<IProject> projects = new ArrayList<>();
		for (int i = 0; i < elements.length; i++)
			projects.add((IProject) elements[i]);
		return projects.toArray(new IProject[] {});
	}

	private void setAllChecked(boolean checked) {
		int count = checkList.getTable().getItemCount();
		for (int i = 0; i < count; i++) {
			IProject project = (IProject) checkList.getElementAt(i);
			if (project.exists())
				checkList.setChecked(project, checked);
		}
	}

}

Back to the top