KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > history > LocalHistoryTableProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.ui.history;
12
13 import java.util.Date JavaDoc;
14
15 import org.eclipse.compare.IModificationDate;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IFileState;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.jface.util.IPropertyChangeListener;
21 import org.eclipse.jface.util.PropertyChangeEvent;
22 import org.eclipse.jface.viewers.*;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.*;
25 import org.eclipse.swt.graphics.*;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.widgets.*;
28 import org.eclipse.team.internal.ui.*;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.themes.ITheme;
31
32 import com.ibm.icu.text.DateFormat;
33
34 public class LocalHistoryTableProvider {
35     
36     /* private */ static final int COL_DATE = 0;
37     
38     /* private */ TreeViewer viewer;
39     
40     private Image localRevImage = null;
41     private DateFormat dateFormat;
42     
43     /**
44      * The Local history label provider.
45      */

46     private class LocalHistoryLabelProvider extends LabelProvider implements ITableLabelProvider, IColorProvider, IFontProvider {
47         
48         private Image dateImage = null;
49         private Font currentRevisionFont = null;
50         
51         private IPropertyChangeListener themeListener = new IPropertyChangeListener() {
52             public void propertyChange(PropertyChangeEvent event) {
53                 LocalHistoryTableProvider.this.viewer.refresh();
54             }
55         };
56         
57         public LocalHistoryLabelProvider(){
58                 PlatformUI.getWorkbench().getThemeManager().addPropertyChangeListener(themeListener);
59         }
60         
61         public void dispose() {
62             if (dateImage != null){
63                 dateImage.dispose();
64                 dateImage = null;
65             }
66             
67             if (localRevImage != null) {
68                 localRevImage.dispose();
69                 localRevImage = null;
70             }
71                     
72             if (themeListener != null){
73                 PlatformUI.getWorkbench().getThemeManager().removePropertyChangeListener(themeListener);
74             }
75             
76             if (currentRevisionFont != null) {
77                 currentRevisionFont.dispose();
78             }
79         }
80         
81         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
82             if (columnIndex == COL_DATE) {
83                 if (element instanceof DateHistoryCategory) {
84                     if (dateImage == null) {
85                         ImageDescriptor dateDesc = TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_DATES_CATEGORY);
86                         dateImage = dateDesc.createImage();
87                     }
88                     return dateImage;
89                 }
90     
91                 if (getModificationDate(element) != -1) {
92                     return getRevisionImage();
93                 }
94             }
95             
96             return null;
97         }
98
99         public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
100             if (columnIndex == COL_DATE) {
101                 if (element instanceof AbstractHistoryCategory){
102                     return ((AbstractHistoryCategory) element).getName();
103                 }
104                 
105                 long date = getModificationDate(element);
106                 if (date != -1) {
107                     Date JavaDoc dateFromLong = new Date JavaDoc(date);
108                     return getDateFormat().format(dateFromLong);
109                 }
110             }
111             return ""; //$NON-NLS-1$
112
}
113
114         /* (non-Javadoc)
115          * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
116          */

117         public Color getForeground(Object JavaDoc element) {
118             if (element instanceof AbstractHistoryCategory){
119                 // TODO: We should have a Team theme for this
120
ITheme current = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme();
121                 return current.getColorRegistry().get("org.eclipse.team.cvs.ui.fontsandcolors.cvshistorypagecategories"); //$NON-NLS-1$
122
}
123             
124             if (isDeletedEdition(element)) {
125                 return Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
126             }
127
128             return null;
129         }
130
131         /* (non-Javadoc)
132          * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
133          */

134         public Color getBackground(Object JavaDoc element) {
135             return null;
136         }
137
138         /*
139          * (non-Javadoc)
140          * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
141          */

142         public Font getFont(Object JavaDoc element) {
143             if (element instanceof AbstractHistoryCategory) {
144                 return getCurrentRevisionFont();
145             }
146             if (isCurrentEdition(element)) {
147                 return getCurrentRevisionFont();
148             }
149             return null;
150         }
151
152         private Font getCurrentRevisionFont() {
153             if (currentRevisionFont == null) {
154                 Font defaultFont = JFaceResources.getDefaultFont();
155                 FontData[] data = defaultFont.getFontData();
156                 for (int i = 0; i < data.length; i++) {
157                     data[i].setStyle(SWT.BOLD);
158                 }
159                 currentRevisionFont = new Font(viewer.getTree().getDisplay(), data);
160             }
161             return currentRevisionFont;
162         }
163     }
164
165     /**
166      * The history sorter
167      */

168     private class HistoryComparator extends ViewerComparator {
169         private boolean reversed = false;
170         private int columnNumber;
171         
172         // column headings: "Revision" "Tags" "Date" "Author" "Comment"
173
private int[][] SORT_ORDERS_BY_COLUMN = {
174                 {COL_DATE}, /* date */
175         };
176
177         /**
178          * The constructor.
179          * @param columnNumber
180          */

181         public HistoryComparator(int columnNumber) {
182             this.columnNumber = columnNumber;
183         }
184
185         /**
186          * Compares two log entries, sorting first by the main column of this sorter,
187          * then by subsequent columns, depending on the column sort order.
188          */

189         public int compare(Viewer compareViewer, Object JavaDoc o1, Object JavaDoc o2) {
190             /*if (o1 instanceof AbstractCVSHistoryCategory || o2 instanceof AbstractCVSHistoryCategory)
191                 return 0;*/

192             
193             long date1 = getModificationDate(o1);
194             long date2 = getModificationDate(o2);
195             int result = 0;
196             if (date1 == -1 || date2 == -1) {
197                 result = super.compare(compareViewer, o1, o2);
198             } else {
199                 int[] columnSortOrder = SORT_ORDERS_BY_COLUMN[columnNumber];
200                 for (int i = 0; i < columnSortOrder.length; ++i) {
201                     result = compareColumnValue(columnSortOrder[i], date1, date2);
202                     if (result != 0)
203                         break;
204                 }
205             }
206             if (reversed)
207                 result = -result;
208             return result;
209         }
210
211         /**
212          * Compares two markers, based only on the value of the specified column.
213          */

214         int compareColumnValue(int columnNumber, long date1, long date2) {
215             switch (columnNumber) {
216                 case 0 : /* date */
217                     if (date1 == date2)
218                         return 0;
219
220                     return date1 > date2 ? -1 : 1;
221                     
222                 default :
223                     return 0;
224             }
225         }
226
227         /**
228          * Returns the number of the column by which this is sorting.
229          * @return the column number
230          */

231         public int getColumnNumber() {
232             return columnNumber;
233         }
234
235         /**
236          * Returns true for descending, or false
237          * for ascending sorting order.
238          * @return returns true if reversed
239          */

240         public boolean isReversed() {
241             return reversed;
242         }
243
244         /**
245          * Sets the sorting order.
246          * @param newReversed
247          */

248         public void setReversed(boolean newReversed) {
249             reversed = newReversed;
250         }
251     }
252
253     /**
254      * Creates the columns for the history table.
255      */

256     private void createColumns(Tree tree, TableLayout layout) {
257         SelectionListener headerListener = new SelectionAdapter() {
258             public void widgetSelected(SelectionEvent e) {
259                 // column selected - need to sort
260
int column = viewer.getTree().indexOf((TreeColumn) e.widget);
261                 HistoryComparator oldSorter = (HistoryComparator) viewer.getComparator();
262                 if (oldSorter != null && column == oldSorter.getColumnNumber()) {
263                     oldSorter.setReversed(!oldSorter.isReversed());
264                     viewer.refresh();
265                 } else {
266                     viewer.setComparator(new HistoryComparator(column));
267                 }
268             }
269         };
270         // creation date
271
TreeColumn col = new TreeColumn(tree, SWT.NONE);
272         col.setResizable(true);
273         col.setText(TeamUIMessages.GenericHistoryTableProvider_RevisionTime);
274         col.addSelectionListener(headerListener);
275         layout.addColumnData(new ColumnWeightData(20, true));
276     }
277     
278     /**
279      * Create a TreeViewer that can be used to display a list of IFile instances.
280      * This method provides the labels and sorter but does not provide a content provider
281      *
282      * @param parent
283      * @return TableViewer
284      */

285     public TreeViewer createTree(Composite parent) {
286         Tree tree = new Tree(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
287         tree.setHeaderVisible(true);
288         tree.setLinesVisible(false);
289         
290         GridData data = new GridData(GridData.FILL_BOTH);
291         tree.setLayoutData(data);
292
293         TableLayout layout = new TableLayout();
294         tree.setLayout(layout);
295
296         this.viewer = new TreeViewer(tree);
297         
298         createColumns(tree, layout);
299
300         viewer.setLabelProvider(new LocalHistoryLabelProvider());
301
302         // By default, reverse sort by revision.
303
// If local filter is on sort by date
304
HistoryComparator sorter = new HistoryComparator(COL_DATE);
305         sorter.setReversed(false);
306         viewer.setComparator(sorter);
307         
308         return viewer;
309     }
310     
311     protected long getModificationDate(Object JavaDoc element) {
312         IModificationDate md = (IModificationDate)Utils.getAdapter(element, IModificationDate.class);
313         if (md != null)
314             return md.getModificationDate();
315         if (element instanceof IFileState) {
316             IFileState fs = (IFileState) element;
317             return fs.getModificationTime();
318         }
319         if (element instanceof IFile) {
320             IFile f = (IFile) element;
321             return f.getLocalTimeStamp();
322         }
323         return -1;
324     }
325     
326     protected boolean isCurrentEdition(Object JavaDoc element) {
327         if (element instanceof IFile) {
328             return true;
329         }
330         if (element instanceof IFileState) {
331             return false;
332         }
333         return false;
334     }
335     
336     protected boolean isDeletedEdition(Object JavaDoc element) {
337         if (element instanceof IFile) {
338             IFile f = (IFile) element;
339             return !f.exists();
340         }
341         return false;
342     }
343
344     public Image getRevisionImage() {
345         if (localRevImage == null) {
346             ImageDescriptor localRevDesc = TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_LOCALREVISION_TABLE);
347             localRevImage = localRevDesc.createImage();
348         }
349         return localRevImage;
350     }
351     
352     public synchronized DateFormat getDateFormat() {
353         if (dateFormat == null)
354             dateFormat = DateFormat.getInstance();
355         return dateFormat;
356     }
357 }
358
Popular Tags