KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > HistoryTableProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui;
12
13 import com.ibm.icu.text.DateFormat;
14 import java.util.Date JavaDoc;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.jface.viewers.*;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.*;
22 import org.eclipse.swt.graphics.*;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.widgets.*;
25 import org.eclipse.team.internal.ccvs.core.*;
26 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
27
28 /**
29  * This class provides the table and it's required components for a file's revision
30  * history
31  */

32 public class HistoryTableProvider {
33
34     private ICVSFile currentFile;
35     private String JavaDoc currentRevision;
36     private TableViewer viewer;
37     private Font currentRevisionFont;
38     
39     /**
40      * Constructor for HistoryTableProvider.
41      */

42     public HistoryTableProvider() {
43         super();
44     }
45
46     //column constants
47
private static final int COL_REVISION = 0;
48     private static final int COL_TAGS = 1;
49     private static final int COL_DATE = 2;
50     private static final int COL_AUTHOR = 3;
51     private static final int COL_COMMENT = 4;
52
53     /**
54      * The history label provider.
55      */

56     class HistoryLabelProvider extends LabelProvider implements ITableLabelProvider, IColorProvider, IFontProvider {
57         private DateFormat dateFormat;
58         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
59             return null;
60         }
61         public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
62             ILogEntry entry = adaptToLogEntry(element);
63             if (entry == null) return ""; //$NON-NLS-1$
64
switch (columnIndex) {
65                 case COL_REVISION:
66                     String JavaDoc revision = entry.getRevision();
67                     String JavaDoc currentRevision = getCurrentRevision();
68                     if (currentRevision != null && currentRevision.equals(revision)) {
69                         revision = NLS.bind(CVSUIMessages.currentRevision, new String JavaDoc[] { revision });
70                     }
71                     return revision;
72                 case COL_TAGS:
73                     CVSTag[] tags = entry.getTags();
74                     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
75                     for (int i = 0; i < tags.length; i++) {
76                         result.append(tags[i].getName());
77                         if (i < tags.length - 1) {
78                             result.append(", "); //$NON-NLS-1$
79
}
80                     }
81                     return result.toString();
82                 case COL_DATE:
83                     Date JavaDoc date = entry.getDate();
84                     if (date == null) return CVSUIMessages.notAvailable;
85                     return getDateFormat().format(date);
86                 case COL_AUTHOR:
87                     return entry.getAuthor();
88                 case COL_COMMENT:
89                     String JavaDoc comment = entry.getComment();
90                     int index = comment.indexOf("\n"); //$NON-NLS-1$
91
switch (index) {
92                         case -1:
93                             return comment;
94                         case 0:
95                             return CVSUIMessages.HistoryView_______4;
96                         default:
97                             return NLS.bind(CVSUIMessages.CVSCompareRevisionsInput_truncate, new String JavaDoc[] { comment.substring(0, index) });
98                     }
99             }
100             return ""; //$NON-NLS-1$
101
}
102         
103         private synchronized DateFormat getDateFormat() {
104             if (dateFormat == null)
105                 dateFormat = DateFormat.getInstance();
106             return dateFormat;
107         }
108         
109         /* (non-Javadoc)
110          * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
111          */

112         public Color getForeground(Object JavaDoc element) {
113             ILogEntry entry = adaptToLogEntry(element);
114             if (entry.isDeletion()) {
115                 return Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
116             } else {
117                 return null;
118             }
119         }
120         /* (non-Javadoc)
121          * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
122          */

123         public Color getBackground(Object JavaDoc element) {
124             return null;
125         }
126         /*
127          * (non-Javadoc)
128          * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
129          */

130         public Font getFont(Object JavaDoc element) {
131             ILogEntry entry = adaptToLogEntry(element);
132             if (entry == null)
133                 return null;
134             String JavaDoc revision = entry.getRevision();
135             String JavaDoc currentRevision = getCurrentRevision();
136             if (currentRevision != null && currentRevision.equals(revision)) {
137                 if (currentRevisionFont == null) {
138                     Font defaultFont = JFaceResources.getDefaultFont();
139                     FontData[] data = defaultFont.getFontData();
140                     for (int i = 0; i < data.length; i++) {
141                         data[i].setStyle(SWT.BOLD);
142                     }
143                     currentRevisionFont = new Font(viewer.getTable().getDisplay(), data);
144                 }
145                 return currentRevisionFont;
146             }
147             return null;
148         }
149     }
150
151     /**
152      * The history sorter
153      */

154     class HistoryComparator extends ViewerComparator {
155         private boolean reversed = false;
156         private int columnNumber;
157         
158         private VersionCollator versionCollator = new VersionCollator();
159         
160         // column headings: "Revision" "Tags" "Date" "Author" "Comment"
161
private int[][] SORT_ORDERS_BY_COLUMN = {
162             {COL_REVISION, COL_DATE, COL_AUTHOR, COL_COMMENT, COL_TAGS}, /* revision */
163             {COL_TAGS, COL_REVISION, COL_DATE, COL_AUTHOR, COL_COMMENT}, /* tags */
164             {COL_DATE, COL_REVISION, COL_AUTHOR, COL_COMMENT, COL_TAGS}, /* date */
165             {COL_AUTHOR, COL_REVISION, COL_DATE, COL_COMMENT, COL_TAGS}, /* author */
166             {COL_COMMENT, COL_REVISION, COL_DATE, COL_AUTHOR, COL_TAGS} /* comment */
167         };
168         
169         /**
170          * The constructor.
171          */

172         public HistoryComparator(int columnNumber) {
173             this.columnNumber = columnNumber;
174         }
175         /**
176          * Compares two log entries, sorting first by the main column of this sorter,
177          * then by subsequent columns, depending on the column sort order.
178          */

179         public int compare(Viewer viewer, Object JavaDoc o1, Object JavaDoc o2) {
180             ILogEntry e1 = adaptToLogEntry(o1);
181             ILogEntry e2 = adaptToLogEntry(o2);
182             int result = 0;
183             if (e1 == null || e2 == null) {
184                 result = super.compare(viewer, o1, o2);
185             } else {
186                 int[] columnSortOrder = SORT_ORDERS_BY_COLUMN[columnNumber];
187                 for (int i = 0; i < columnSortOrder.length; ++i) {
188                     result = compareColumnValue(columnSortOrder[i], e1, e2);
189                     if (result != 0)
190                         break;
191                 }
192             }
193             if (reversed)
194                 result = -result;
195             return result;
196         }
197         /**
198          * Compares two markers, based only on the value of the specified column.
199          */

200         int compareColumnValue(int columnNumber, ILogEntry e1, ILogEntry e2) {
201             switch (columnNumber) {
202                 case 0: /* revision */
203                     return versionCollator.compare(e1.getRevision(), e2.getRevision());
204                 case 1: /* tags */
205                     CVSTag[] tags1 = e1.getTags();
206                     CVSTag[] tags2 = e2.getTags();
207                     if (tags2.length == 0) {
208                         return -1;
209                     }
210                     if (tags1.length == 0) {
211                         return 1;
212                     }
213                     return getComparator().compare(tags1[0].getName(), tags2[0].getName());
214                 case 2: /* date */
215                     Date JavaDoc date1 = e1.getDate();
216                     Date JavaDoc date2 = e2.getDate();
217                     return date1.compareTo(date2);
218                 case 3: /* author */
219                     return getComparator().compare(e1.getAuthor(), e2.getAuthor());
220                 case 4: /* comment */
221                     return getComparator().compare(e1.getComment(), e2.getComment());
222                 default:
223                     return 0;
224             }
225         }
226         /**
227          * Returns the number of the column by which this is sorting.
228          */

229         public int getColumnNumber() {
230             return columnNumber;
231         }
232         /**
233          * Returns true for descending, or false
234          * for ascending sorting order.
235          */

236         public boolean isReversed() {
237             return reversed;
238         }
239         /**
240          * Sets the sorting order.
241          */

242         public void setReversed(boolean newReversed) {
243             reversed = newReversed;
244         }
245     }
246
247     protected ILogEntry adaptToLogEntry(Object JavaDoc element) {
248         // Get the log entry for the provided object
249
ILogEntry entry = null;
250         if (element instanceof ILogEntry) {
251             entry = (ILogEntry) element;
252         } else if (element instanceof IAdaptable) {
253             entry = (ILogEntry)((IAdaptable)element).getAdapter(ILogEntry.class);
254         }
255         return entry;
256     }
257     
258     /**
259      * Create a TableViewer that can be used to display a list of ILogEntry instances.
260      * Ths method provides the labels and sorter but does not provide a content provider
261      *
262      * @param parent
263      * @return TableViewer
264      */

265     public TableViewer createTable(Composite parent) {
266         Table table = new Table(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
267         table.setHeaderVisible(true);
268         table.setLinesVisible(true);
269         GridData data = new GridData(GridData.FILL_BOTH);
270         table.setLayoutData(data);
271     
272         TableLayout layout = new TableLayout();
273         table.setLayout(layout);
274         
275         TableViewer viewer = new TableViewer(table);
276         
277         createColumns(table, layout, viewer);
278
279         viewer.setLabelProvider(new HistoryLabelProvider());
280         
281         // By default, reverse sort by revision.
282
HistoryComparator sorter = new HistoryComparator(COL_REVISION);
283         sorter.setReversed(true);
284         viewer.setComparator(sorter);
285         
286         table.addDisposeListener(new DisposeListener() {
287             public void widgetDisposed(DisposeEvent e) {
288                 if(currentRevisionFont != null) {
289                     currentRevisionFont.dispose();
290                 }
291             }
292         });
293         
294         this.viewer = viewer;
295         return viewer;
296     }
297
298     /**
299      * Create a CheckBoxTableViewer that can be used to display a list of ILogEntry instances.
300      * Ths method provides the labels and sorter but does not provide a content provider
301      *
302      * @param parent
303      * @return TableViewer
304      */

305     public CheckboxTableViewer createCheckBoxTable(Composite parent) {
306         Table table = new Table(parent, SWT.CHECK | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
307         table.setHeaderVisible(true);
308         table.setLinesVisible(true);
309         GridData data = new GridData(GridData.FILL_BOTH);
310         table.setLayoutData(data);
311     
312         TableLayout layout = new TableLayout();
313         table.setLayout(layout);
314         
315         CheckboxTableViewer viewer = new CheckboxTableViewer(table);
316         
317         createColumns(table, layout, viewer);
318
319         viewer.setLabelProvider(new HistoryLabelProvider());
320         
321         // By default, reverse sort by revision.
322
HistoryComparator sorter = new HistoryComparator(COL_REVISION);
323         sorter.setReversed(true);
324         viewer.setComparator(sorter);
325         
326         table.addDisposeListener(new DisposeListener() {
327             public void widgetDisposed(DisposeEvent e) {
328                 if(currentRevisionFont != null) {
329                     currentRevisionFont.dispose();
330                 }
331             }
332         });
333         
334         this.viewer = viewer;
335         return viewer;
336     }
337     
338     /**
339      * Creates the columns for the history table.
340      */

341     private void createColumns(Table table, TableLayout layout, TableViewer viewer) {
342         SelectionListener headerListener = getColumnListener(viewer);
343         // revision
344
TableColumn col = new TableColumn(table, SWT.NONE);
345         col.setResizable(true);
346         col.setText(CVSUIMessages.HistoryView_revision);
347         col.addSelectionListener(headerListener);
348         layout.addColumnData(new ColumnWeightData(20, true));
349     
350         // tags
351
col = new TableColumn(table, SWT.NONE);
352         col.setResizable(true);
353         col.setText(CVSUIMessages.HistoryView_tags);
354         col.addSelectionListener(headerListener);
355         layout.addColumnData(new ColumnWeightData(20, true));
356     
357         // creation date
358
col = new TableColumn(table, SWT.NONE);
359         col.setResizable(true);
360         col.setText(CVSUIMessages.HistoryView_date);
361         col.addSelectionListener(headerListener);
362         layout.addColumnData(new ColumnWeightData(20, true));
363     
364         // author
365
col = new TableColumn(table, SWT.NONE);
366         col.setResizable(true);
367         col.setText(CVSUIMessages.HistoryView_author);
368         col.addSelectionListener(headerListener);
369         layout.addColumnData(new ColumnWeightData(20, true));
370     
371         //comment
372
col = new TableColumn(table, SWT.NONE);
373         col.setResizable(true);
374         col.setText(CVSUIMessages.HistoryView_comment);
375         col.addSelectionListener(headerListener);
376         layout.addColumnData(new ColumnWeightData(50, true));
377     }
378
379     /**
380      * Adds the listener that sets the sorter.
381      */

382     private SelectionListener getColumnListener(final TableViewer tableViewer) {
383         /**
384          * This class handles selections of the column headers.
385          * Selection of the column header will cause resorting
386          * of the shown tasks using that column's sorter.
387          * Repeated selection of the header will toggle
388          * sorting order (ascending versus descending).
389          */

390         return new SelectionAdapter() {
391             /**
392              * Handles the case of user selecting the
393              * header area.
394              * <p>If the column has not been selected previously,
395              * it will set the sorter of that column to be
396              * the current tasklist sorter. Repeated
397              * presses on the same column header will
398              * toggle sorting order (ascending/descending).
399              */

400             public void widgetSelected(SelectionEvent e) {
401                 // column selected - need to sort
402
int column = tableViewer.getTable().indexOf((TableColumn) e.widget);
403                 HistoryComparator oldSorter = (HistoryComparator)tableViewer.getComparator();
404                 if (oldSorter != null && column == oldSorter.getColumnNumber()) {
405                     oldSorter.setReversed(!oldSorter.isReversed());
406                     tableViewer.refresh();
407                 } else {
408                     tableViewer.setComparator(new HistoryComparator(column));
409                 }
410             }
411         };
412     }
413     
414     public String JavaDoc getCurrentRevision() {
415         return currentRevision;
416     }
417         
418     /**
419      * Method getRevision.
420      * @param currentEdition
421      */

422     private String JavaDoc getRevision(ICVSFile currentEdition) throws CVSException {
423         if (currentEdition == null) return ""; //$NON-NLS-1$
424
ResourceSyncInfo info = currentEdition.getSyncInfo();
425         if (info == null) return ""; //$NON-NLS-1$
426
return info.getRevision();
427     }
428     
429     public void setFile(ICVSFile file) throws CVSException {
430         this.currentFile = file;
431         this.currentRevision = getRevision(this.currentFile);
432     }
433
434     public ICVSFile getICVSFile() {
435         return this.currentFile;
436     }
437 }
438
Popular Tags