1 11 package org.eclipse.team.internal.ui.history; 12 13 import java.util.Date ; 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 static final int COL_DATE = 0; 37 38 TreeViewer viewer; 39 40 private Image localRevImage = null; 41 private DateFormat dateFormat; 42 43 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 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 getColumnText(Object 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 dateFromLong = new Date (date); 108 return getDateFormat().format(dateFromLong); 109 } 110 } 111 return ""; } 113 114 117 public Color getForeground(Object element) { 118 if (element instanceof AbstractHistoryCategory){ 119 ITheme current = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme(); 121 return current.getColorRegistry().get("org.eclipse.team.cvs.ui.fontsandcolors.cvshistorypagecategories"); } 123 124 if (isDeletedEdition(element)) { 125 return Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW); 126 } 127 128 return null; 129 } 130 131 134 public Color getBackground(Object element) { 135 return null; 136 } 137 138 142 public Font getFont(Object 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 168 private class HistoryComparator extends ViewerComparator { 169 private boolean reversed = false; 170 private int columnNumber; 171 172 private int[][] SORT_ORDERS_BY_COLUMN = { 174 {COL_DATE}, 175 }; 176 177 181 public HistoryComparator(int columnNumber) { 182 this.columnNumber = columnNumber; 183 } 184 185 189 public int compare(Viewer compareViewer, Object o1, Object o2) { 190 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 214 int compareColumnValue(int columnNumber, long date1, long date2) { 215 switch (columnNumber) { 216 case 0 : 217 if (date1 == date2) 218 return 0; 219 220 return date1 > date2 ? -1 : 1; 221 222 default : 223 return 0; 224 } 225 } 226 227 231 public int getColumnNumber() { 232 return columnNumber; 233 } 234 235 240 public boolean isReversed() { 241 return reversed; 242 } 243 244 248 public void setReversed(boolean newReversed) { 249 reversed = newReversed; 250 } 251 } 252 253 256 private void createColumns(Tree tree, TableLayout layout) { 257 SelectionListener headerListener = new SelectionAdapter() { 258 public void widgetSelected(SelectionEvent e) { 259 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 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 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 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 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 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 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 |