Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9d0a744ae00e8bb3653f5b49a918fc2a84dc2864 (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
/*******************************************************************************
 * Copyright (c) 2006 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.examples.filesystem.ui;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.history.IFileHistory;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.team.examples.filesystem.FileSystemProvider;
import org.eclipse.team.examples.filesystem.history.FileSystemHistory;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.ui.history.HistoryPage;
import org.eclipse.team.ui.history.IHistoryPageSite;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;

public class FileSystemHistoryPage extends HistoryPage {

	/* private */IFile file;
	/* private */FileSystemHistory fileSystemHistory;
	/* private */IFileRevision[] entries;
	/* private */IFileRevision currentSelection;

	private FileSystemTableProvider fileSystemTableProvider;
	/* private */TableViewer tableViewer;
	private Composite localComposite;

	/* private */OpenFileSystemRevisionAction openAction;

	boolean shutdown = false;

	private RefreshFileHistory refreshFileHistoryJob;

	private class RefreshFileHistory extends Job {
		/* private */FileSystemHistory fileHistory;

		public RefreshFileHistory() {
			super("Fetching FileSystem revisions...");  //$NON-NLS-1$
		}

		public void setFileHistory(FileSystemHistory fileHistory) {
			this.fileHistory = fileHistory;
		}

		public IStatus run(IProgressMonitor monitor) {

			IStatus status = Status.OK_STATUS;

			if (fileHistory != null && !shutdown) {
				fileHistory.refresh(monitor);
				//Internal code used for convenience - you can use 
				//your own here
				Utils.asyncExec(new Runnable() {
					public void run() {
						tableViewer.setInput(fileHistory);
					}
				}, tableViewer);
			}

			return status;
		}
	}

	public boolean inputSet() {
		IFile tempFile = getFile();
		this.file = tempFile;
		if (tempFile == null)
			return false;

		//blank current input only after we're sure that we have a file
		//to fetch history for
		this.tableViewer.setInput(null);

		fileSystemHistory = new FileSystemHistory(file);

		refreshHistory();
		return true;
	}

	private IWorkbenchPartSite getWorkbenchSite(IHistoryPageSite parentSite) {
		IWorkbenchPart part = parentSite.getPart();
		if (part != null)
			return part.getSite();
		return null;
	}

	private IFile getFile() {
		Object obj = getInput();
		if (obj instanceof IFile)
			return (IFile) obj;

		return null;
	}

	public void createControl(Composite parent) {

		localComposite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		localComposite.setLayout(layout);
		GridData data = new GridData(GridData.FILL_BOTH);
		data.grabExcessVerticalSpace = true;
		localComposite.setLayoutData(data);

		tableViewer = createTable(localComposite);

		contributeActions();

	}

	private void contributeActions() {
		openAction = new OpenFileSystemRevisionAction("Open");  //$NON-NLS-1$
		tableViewer.getTable().addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				openAction.selectionChanged((IStructuredSelection) tableViewer.getSelection());
			}
		});
		openAction.setPage(this);
		//Contribute actions to popup menu
		MenuManager menuMgr = new MenuManager();
		Menu menu = menuMgr.createContextMenu(tableViewer.getTable());
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager menuMgr) {
				menuMgr.add(new Separator(IWorkbenchActionConstants.GROUP_FILE));
				menuMgr.add(openAction);
			}
		});
		menuMgr.setRemoveAllWhenShown(true);
		tableViewer.getTable().setMenu(menu);
	}

	private TableViewer createTable(Composite parent) {
		fileSystemTableProvider = new FileSystemTableProvider();
		TableViewer viewer = fileSystemTableProvider.createTable(parent);
		viewer.setContentProvider(new IStructuredContentProvider() {

			public Object[] getElements(Object inputElement) {
				// The entries of already been fetch so return them
				if (entries != null)
					return entries;

				final IFileHistory fileHistory = (IFileHistory) inputElement;
				entries = fileHistory.getFileRevisions();

				return entries;
			}

			public void dispose() {
				// TODO Auto-generated method stub

			}

			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
				entries = null;
			}

		});
		return viewer;
	}

	public Control getControl() {
		return localComposite;
	}

	public void setFocus() {
		localComposite.setFocus();
	}

	public String getDescription() {
		if (file != null)
			return file.getFullPath().toString();

		return null;
	}

	public String getName() {
		if (file != null)
			return file.getName();

		return ""; //$NON-NLS-1$
	}

	public boolean isValidInput(Object object) {

		if (object instanceof IResource && ((IResource) object).getType() == IResource.FILE) {
			RepositoryProvider provider = RepositoryProvider.getProvider(((IFile) object).getProject());
			if (provider != null && provider instanceof FileSystemProvider)
				return true;
		}

		return false;
	}

	public void refresh() {
		refreshHistory();
	}

	private void refreshHistory() {
		if (refreshFileHistoryJob == null)
			refreshFileHistoryJob = new RefreshFileHistory();

		if (refreshFileHistoryJob.getState() != Job.NONE) {
			refreshFileHistoryJob.cancel();
		}
		refreshFileHistoryJob.setFileHistory(fileSystemHistory);
		IHistoryPageSite parentSite = getHistoryPageSite();
		//Internal code used for convenience - you can use your own here
		Utils.schedule(refreshFileHistoryJob, getWorkbenchSite(parentSite));
	}

	public Object getAdapter(Class adapter) {
		return null;
	}

}

Back to the top