Speed up the file history browser.
[egit.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / GitHistoryPage.java
blobdf3b109a0dda86760b6dc535ac6964775636301e
1 /*
2 * Copyright (C) 2006 Robin Rosenberg
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License, version 2.1, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.egit.ui;
19 import java.util.Date;
21 import org.eclipse.compare.CompareConfiguration;
22 import org.eclipse.compare.structuremergeviewer.ICompareInput;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.IResourceChangeEvent;
25 import org.eclipse.core.resources.IResourceChangeListener;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.core.runtime.IAdaptable;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.jface.action.IMenuListener;
30 import org.eclipse.jface.action.IMenuManager;
31 import org.eclipse.jface.action.MenuManager;
32 import org.eclipse.jface.viewers.ColumnWeightData;
33 import org.eclipse.jface.viewers.ILazyTreeContentProvider;
34 import org.eclipse.jface.viewers.ITableLabelProvider;
35 import org.eclipse.jface.viewers.ITreeContentProvider;
36 import org.eclipse.jface.viewers.LabelProvider;
37 import org.eclipse.jface.viewers.StructuredSelection;
38 import org.eclipse.jface.viewers.TableLayout;
39 import org.eclipse.jface.viewers.TreeViewer;
40 import org.eclipse.jface.viewers.Viewer;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.events.SelectionAdapter;
43 import org.eclipse.swt.events.SelectionEvent;
44 import org.eclipse.swt.graphics.Image;
45 import org.eclipse.swt.layout.GridData;
46 import org.eclipse.swt.layout.GridLayout;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.widgets.Control;
49 import org.eclipse.swt.widgets.Event;
50 import org.eclipse.swt.widgets.Listener;
51 import org.eclipse.swt.widgets.Menu;
52 import org.eclipse.swt.widgets.Tree;
53 import org.eclipse.swt.widgets.TreeColumn;
54 import org.eclipse.swt.widgets.TreeItem;
55 import org.eclipse.team.core.RepositoryProvider;
56 import org.eclipse.team.core.history.IFileHistory;
57 import org.eclipse.team.core.history.IFileHistoryProvider;
58 import org.eclipse.team.core.history.IFileRevision;
59 import org.eclipse.team.internal.ui.TeamUIMessages;
60 import org.eclipse.team.internal.ui.actions.CompareRevisionAction;
61 import org.eclipse.team.internal.ui.history.DialogHistoryPageSite;
62 import org.eclipse.team.ui.history.HistoryPage;
63 import org.eclipse.team.ui.history.IHistoryCompareAdapter;
64 import org.eclipse.team.ui.history.IHistoryPageSite;
66 public class GitHistoryPage extends HistoryPage implements IAdaptable,
67 IHistoryCompareAdapter {
69 private Composite localComposite;
71 private TreeViewer viewer;
73 private Tree tree;
75 private IFileRevision[] fileRevisions;
77 public GitHistoryPage(Object object) {
78 setInput(object);
81 public boolean inputSet() {
82 if (viewer != null)
83 viewer.setInput(getInput());
84 // TODO Auto-generated method stub
85 return true;
88 public void createControl(Composite parent) {
89 localComposite = new Composite(parent, SWT.NONE);
90 GridLayout layout = new GridLayout();
91 layout.marginHeight = 0;
92 layout.marginWidth = 0;
93 localComposite.setLayout(layout);
94 GridData data = new GridData(GridData.FILL_BOTH);
95 data.grabExcessVerticalSpace = true;
96 localComposite.setLayoutData(data);
98 createTree(localComposite);
100 IHistoryPageSite parentSite = getHistoryPageSite();
101 if (parentSite != null && parentSite instanceof DialogHistoryPageSite)
102 parentSite.setSelectionProvider(viewer);
104 final CompareRevisionAction compareAction = new CompareRevisionAction(
105 "Compare");
106 tree.addSelectionListener(new SelectionAdapter() {
107 public void widgetSelected(SelectionEvent e) {
108 // update the current
109 TreeItem[] selection = tree.getSelection();
110 IFileRevision[] selection2 = new IFileRevision[selection.length];
111 for (int i = 0; i < selection.length; ++i) {
112 selection2[i] = (IFileRevision) selection[i].getData();
115 compareAction.setCurrentFileRevision(fileRevisions[0]);
116 compareAction.selectionChanged(new StructuredSelection(
117 selection2));
120 compareAction.setPage(this);
121 MenuManager menuMgr = new MenuManager();
122 Menu menu = menuMgr.createContextMenu(tree);
123 menuMgr.addMenuListener(new IMenuListener() {
124 public void menuAboutToShow(IMenuManager menuMgr) {
125 menuMgr.add(compareAction);
128 menuMgr.setRemoveAllWhenShown(true);
129 tree.setMenu(menu);
131 GitHistoryResourceListener resourceListener = new GitHistoryResourceListener();
132 ResourcesPlugin.getWorkspace().addResourceChangeListener(
133 resourceListener, IResourceChangeEvent.POST_CHANGE);
137 class GitHistoryResourceListener implements IResourceChangeListener {
139 public void resourceChanged(IResourceChangeEvent event) {
140 System.out.println("resourceChanged(" + event + ")");
145 class GitHistoryLabelProvider extends LabelProvider implements
146 ITableLabelProvider {
148 public Image getColumnImage(Object element, int columnIndex) {
149 return null;
152 public String getColumnText(Object element, int columnIndex) {
153 if (columnIndex == 0) {
154 String id = ((IFileRevision) element).getContentIdentifier();
155 if (id != null)
156 return id.substring(0, 7) + "..";
157 else
158 return id;
161 if (columnIndex == 1)
162 return ""; // TAGS
164 if (columnIndex == 2)
165 return new Date(((IFileRevision) element).getTimestamp())
166 .toString();
168 if (columnIndex == 3)
169 return ((IFileRevision) element).getAuthor();
171 if (columnIndex == 4) {
172 String comment = ((IFileRevision) element).getComment();
173 if (comment == null)
174 return null;
175 int p = comment.indexOf('\n');
176 if (p >= 0)
177 return comment.substring(0, p);
178 else
179 return comment;
181 return Integer.toString(columnIndex);
186 private void createTree(Composite composite) {
187 tree = new Tree(composite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI
188 | SWT.FULL_SELECTION | SWT.VIRTUAL);
189 tree.setHeaderVisible(true);
190 tree.setLinesVisible(true);
192 GridData data = new GridData(GridData.FILL_BOTH);
193 tree.setLayoutData(data);
194 tree.setData("HEAD");
195 tree.addListener(SWT.SetData, new Listener() {
196 public void handleEvent(Event event) {
197 TreeItem item = (TreeItem) event.item;
198 Tree parent = item.getParent();
199 if (parent == null) {
200 item.setText(new String[] { "hej", "san" });
201 item.setData("");
202 } else {
203 ITableLabelProvider p = (ITableLabelProvider) viewer
204 .getLabelProvider();
205 for (int i = 0; i < 5; ++i) {
206 String text = p.getColumnText(fileRevisions[event.index], i);
207 if (text!=null)
208 item.setText(i, text);
209 else
210 item.setText("");
212 item.setData(fileRevisions[event.index]);
216 TableLayout layout = new TableLayout();
217 tree.setLayout(layout);
219 viewer = new TreeViewer(tree, SWT.VIRTUAL | SWT.FULL_SELECTION);
221 createColumns(viewer, tree, layout);
223 viewer.setLabelProvider(new GitHistoryLabelProvider());
225 viewer.setContentProvider(new GitHistoryContentProvider());
227 viewer.setInput(getInput());
230 class GitHistoryContentProvider implements ITreeContentProvider,
231 ILazyTreeContentProvider {
233 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
234 System.out.println("inputChanged(" + viewer + "," + oldInput + ","
235 + newInput);
236 RepositoryProvider provider = RepositoryProvider
237 .getProvider(((IResource) getInput()).getProject());
238 IFileHistoryProvider fileHistoryProvider = provider
239 .getFileHistoryProvider();
240 IFileHistory fileHistoryFor = fileHistoryProvider
241 .getFileHistoryFor((IResource) getInput(),
242 IFileHistoryProvider.SINGLE_LINE_OF_DESCENT, null/* monitor */);
243 fileRevisions = fileHistoryFor.getFileRevisions();
244 tree.setData(fileRevisions);
245 tree.setItemCount(fileRevisions.length);
246 viewer.refresh();
249 public void dispose() {
250 // TODO Auto-generated method stub
253 public Object[] getElements(Object inputElement) {
254 System.out.println("getElements(" + inputElement + ")");
255 return fileRevisions;
258 public boolean hasChildren(Object element) {
259 // System.out.println("hasChildren(" + element.getClass() +
260 // ")");
261 // TODO Auto-generated method stub
262 return false;
265 public Object getParent(Object element) {
266 // System.out.println("getParent(" + element + ")");
267 return null;
270 public Object[] getChildren(Object parentElement) {
271 // System.out.println("getElements(" + parentElement + ")");
272 return fileRevisions;
275 public void updateChildCount(Object element, int currentChildCount) {
276 viewer.setChildCount(element, fileRevisions.length);
279 public void updateElement(Object parent, int index) {
280 viewer.replace(parent, index, fileRevisions[index]);
284 private void createColumns(TreeViewer viewer, Tree tree, TableLayout layout) {
285 // X SelectionListener headerListener = getColumnListener(viewer);
286 // revision
287 TreeColumn col = new TreeColumn(tree, SWT.NONE);
288 col.setResizable(true);
289 col.setText(TeamUIMessages.GenericHistoryTableProvider_Revision);
290 // X col.addSelectionListener(headerListener);
291 layout.addColumnData(new ColumnWeightData(20, true));
293 // tags
294 col = new TreeColumn(tree, SWT.NONE);
295 col.setResizable(true);
296 // X col.setText(CVSUIMessages.HistoryView_tags);
297 // X col.addSelectionListener(headerListener);
298 layout.addColumnData(new ColumnWeightData(20, true));
299 // creation date
300 col = new TreeColumn(tree, SWT.NONE);
301 col.setResizable(true);
302 col.setText(TeamUIMessages.GenericHistoryTableProvider_RevisionTime);
303 // X col.addSelectionListener(headerListener);
304 layout.addColumnData(new ColumnWeightData(20, true));
306 // author
307 col = new TreeColumn(tree, SWT.NONE);
308 col.setResizable(true);
309 col.setText(TeamUIMessages.GenericHistoryTableProvider_Author);
310 // X col.addSelectionListener(headerListener);
311 layout.addColumnData(new ColumnWeightData(20, true));
313 // comment
314 col = new TreeColumn(tree, SWT.NONE);
315 col.setResizable(true);
316 col.setText(TeamUIMessages.GenericHistoryTableProvider_Comment);
317 // X col.addSelectionListener(headerListener);
318 layout.addColumnData(new ColumnWeightData(50, true));
321 public Control getControl() {
322 return localComposite;
325 public void setFocus() {
326 localComposite.setFocus();
329 public String getDescription() {
330 return "GIT History viewer";
333 public String getName() {
334 return getInput().toString();
337 public boolean isValidInput(Object object) {
338 // TODO Auto-generated method stub
339 return true;
342 public void refresh() {
343 // TODO Auto-generated method stub
347 public Object getAdapter(Class adapter) {
348 if (adapter == IHistoryCompareAdapter.class) {
349 return this;
351 return null;
354 public ICompareInput getCompareInput(Object object) {
355 // TODO Auto-generated method stub
356 return null;
359 public void prepareInput(ICompareInput input,
360 CompareConfiguration configuration, IProgressMonitor monitor) {
361 System.out.println("prepareInput()");
362 // TODO Auto-generated method stub