Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 62e1c6a01d4ba4dfcbf4fcc1b924e8023db276e1 (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
/*******************************************************************************
 * Copyright (C) 2012, Markus Duft <markus.duft@salomon.at>
 *
 * 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
 *******************************************************************************/

package org.eclipse.egit.ui.internal.clean;

import java.lang.reflect.InvocationTargetException;
import java.util.Set;
import java.util.TreeSet;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.egit.core.internal.util.ProjectUtil;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.UIText;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.jgit.api.CleanCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

/**
 * A page for the Clean wizard presenting all things to be cleaned to the user.
 */
public class CleanRepositoryPage extends WizardPage {

	private Repository repository;
	private CheckboxTableViewer cleanTable;
	private boolean cleanDirectories;
	private boolean includeIgnored;

	/**
	 * Creates a new page for the given repository.
	 * @param repository repository to clean.
	 */
	public CleanRepositoryPage(Repository repository) {
		super(UIText.CleanRepositoryPage_title);
		this.repository = repository;

		setTitle(UIText.CleanRepositoryPage_title);
		setMessage(UIText.CleanRepositoryPage_message);
	}

	public void createControl(Composite parent) {
		Composite main = new Composite(parent, SWT.NONE);
		GridDataFactory.fillDefaults().grab(true, true).applyTo(main);
		main.setLayout(new GridLayout());

		final Button radioCleanFiles = new Button(main, SWT.RADIO);
		radioCleanFiles.setText(UIText.CleanRepositoryPage_cleanFiles);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(radioCleanFiles);

		final Button radioCleanDirs = new Button(main, SWT.RADIO);
		radioCleanDirs.setText(UIText.CleanRepositoryPage_cleanDirs);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(radioCleanDirs);

		SelectionAdapter listener = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				cleanDirectories = radioCleanDirs.getSelection();
				updateCleanItems();
			}
		};

		radioCleanFiles.addSelectionListener(listener);
		radioCleanDirs.addSelectionListener(listener);

		radioCleanFiles.setSelection(true);

		final Image fileImage = PlatformUI.getWorkbench().getSharedImages()
				.getImage(ISharedImages.IMG_OBJ_FILE);
		final Image dirImage = PlatformUI.getWorkbench().getSharedImages()
				.getImage(ISharedImages.IMG_OBJ_FOLDER);

		cleanTable = CheckboxTableViewer.newCheckList(main, SWT.BORDER);
		cleanTable.setContentProvider(new ArrayContentProvider());
		cleanTable.setLabelProvider(new LabelProvider() {
			@Override
			public Image getImage(Object element) {
				if(!(element instanceof String))
					return null;

				if(((String)element).endsWith("/")) //$NON-NLS-1$
					return dirImage;
				else
					return fileImage;
			}
		});

		GridDataFactory.fillDefaults().grab(true, true).applyTo(cleanTable.getControl());

		Composite lowerComp = new Composite(main, SWT.NONE);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(lowerComp);
		lowerComp.setLayout(new GridLayout(3, false));

		final Button checkIncludeIgnored = new Button(lowerComp, SWT.CHECK);
		checkIncludeIgnored.setText(UIText.CleanRepositoryPage_includeIgnored);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(checkIncludeIgnored);
		checkIncludeIgnored.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				includeIgnored = checkIncludeIgnored.getSelection();
				updateCleanItems();
			}
		});

		Button selAll = new Button(lowerComp, SWT.PUSH);
		selAll.setText(UIText.WizardProjectsImportPage_selectAll);
		GridDataFactory.defaultsFor(selAll).applyTo(selAll);

		Button selNone = new Button(lowerComp, SWT.PUSH);
		selNone.setText(UIText.WizardProjectsImportPage_deselectAll);
		GridDataFactory.defaultsFor(selNone).applyTo(selNone);

		selAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (cleanTable.getInput() instanceof Set<?>) {
					Set<?> input = (Set<?>) cleanTable.getInput();
					cleanTable.setCheckedElements(input.toArray());
				}
			}
		});

		selNone.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				cleanTable.setCheckedElements(new Object[0]);
			}
		});

		setControl(main);
	}

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

		if(visible)
			getShell().getDisplay().asyncExec(new Runnable() {
				public void run() {
					updateCleanItems();
				}
			});
	}

	private void updateCleanItems() {
		try {
			getContainer().run(true, false, new IRunnableWithProgress() {
				public void run(IProgressMonitor monitor) throws InvocationTargetException,
						InterruptedException {
					monitor.beginTask(UIText.CleanRepositoryPage_findingItems, IProgressMonitor.UNKNOWN);

					Git git = Git.wrap(repository);
					CleanCommand command = git.clean().setDryRun(true);
					command.setCleanDirectories(cleanDirectories);
					command.setIgnore(!includeIgnored);
					try {
						final Set<String> paths = command.call();

						getShell().getDisplay().syncExec(new Runnable() {
							public void run() {
								cleanTable.setInput(paths);
							}
						});
					} catch (GitAPIException ex) {
						Activator.logError("cannot call clean command!", ex); //$NON-NLS-1$
					}

					monitor.done();
				}
			});
		} catch (InvocationTargetException e) {
			Activator.logError("Unexpected exception while finding items to clean", e); //$NON-NLS-1$
			clearPage();
		} catch (InterruptedException e) {
			clearPage();
		}
	}

	private void clearPage() {
		cleanTable.setInput(null);
	}

	/**
	 * Retrieves the items that the user chose to clean.
	 * @return the items to clean.
	 */
	public Set<String> getItemsToClean() {
		Set<String> result = new TreeSet<String>();
		for(Object ele : cleanTable.getCheckedElements()) {
			String str = ele.toString();

			if(str.endsWith("/")) //$NON-NLS-1$
				result.add(str.substring(0, str.length() - 1));
			else
				result.add(str);
		}

		return result;
	}

	/**
	 * Do the cleaning with the selected values.
	 */
	public void finish() {
		try {
			final Set<String> itemsToClean = getItemsToClean();

			getContainer().run(true, false, new IRunnableWithProgress() {
				public void run(IProgressMonitor monitor) throws InvocationTargetException,
						InterruptedException {
					monitor.beginTask(UIText.CleanRepositoryPage_cleaningItems, IProgressMonitor.UNKNOWN);

					Git git = Git.wrap(repository);
					CleanCommand command = git.clean().setDryRun(false);
					command.setCleanDirectories(cleanDirectories);
					command.setIgnore(!includeIgnored);
					command.setPaths(itemsToClean);
					try {
						command.call();
					} catch (GitAPIException ex) {
						Activator.logError("cannot call clean command!", ex); //$NON-NLS-1$
					}

					try {
						IProject[] projects = ProjectUtil.getProjectsContaining(repository, itemsToClean);
						ProjectUtil.refreshResources(projects, new SubProgressMonitor(monitor, 1));
					} catch (CoreException e) {
						// could not refresh... not a "real" problem
					}

					monitor.done();
				}
			});
		} catch (Exception e) {
			Activator.logError("Unexpected exception while cleaning", e); //$NON-NLS-1$
		}
	}

}

Back to the top