1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 import com.ibm.icu.text.DateFormat; 14 import java.util.Date ; 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 32 public class HistoryTableProvider { 33 34 private ICVSFile currentFile; 35 private String currentRevision; 36 private TableViewer viewer; 37 private Font currentRevisionFont; 38 39 42 public HistoryTableProvider() { 43 super(); 44 } 45 46 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 56 class HistoryLabelProvider extends LabelProvider implements ITableLabelProvider, IColorProvider, IFontProvider { 57 private DateFormat dateFormat; 58 public Image getColumnImage(Object element, int columnIndex) { 59 return null; 60 } 61 public String getColumnText(Object element, int columnIndex) { 62 ILogEntry entry = adaptToLogEntry(element); 63 if (entry == null) return ""; switch (columnIndex) { 65 case COL_REVISION: 66 String revision = entry.getRevision(); 67 String currentRevision = getCurrentRevision(); 68 if (currentRevision != null && currentRevision.equals(revision)) { 69 revision = NLS.bind(CVSUIMessages.currentRevision, new String [] { revision }); 70 } 71 return revision; 72 case COL_TAGS: 73 CVSTag[] tags = entry.getTags(); 74 StringBuffer result = new StringBuffer (); 75 for (int i = 0; i < tags.length; i++) { 76 result.append(tags[i].getName()); 77 if (i < tags.length - 1) { 78 result.append(", "); } 80 } 81 return result.toString(); 82 case COL_DATE: 83 Date 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 comment = entry.getComment(); 90 int index = comment.indexOf("\n"); 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 [] { comment.substring(0, index) }); 98 } 99 } 100 return ""; } 102 103 private synchronized DateFormat getDateFormat() { 104 if (dateFormat == null) 105 dateFormat = DateFormat.getInstance(); 106 return dateFormat; 107 } 108 109 112 public Color getForeground(Object 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 123 public Color getBackground(Object element) { 124 return null; 125 } 126 130 public Font getFont(Object element) { 131 ILogEntry entry = adaptToLogEntry(element); 132 if (entry == null) 133 return null; 134 String revision = entry.getRevision(); 135 String 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 154 class HistoryComparator extends ViewerComparator { 155 private boolean reversed = false; 156 private int columnNumber; 157 158 private VersionCollator versionCollator = new VersionCollator(); 159 160 private int[][] SORT_ORDERS_BY_COLUMN = { 162 {COL_REVISION, COL_DATE, COL_AUTHOR, COL_COMMENT, COL_TAGS}, 163 {COL_TAGS, COL_REVISION, COL_DATE, COL_AUTHOR, COL_COMMENT}, 164 {COL_DATE, COL_REVISION, COL_AUTHOR, COL_COMMENT, COL_TAGS}, 165 {COL_AUTHOR, COL_REVISION, COL_DATE, COL_COMMENT, COL_TAGS}, 166 {COL_COMMENT, COL_REVISION, COL_DATE, COL_AUTHOR, COL_TAGS} 167 }; 168 169 172 public HistoryComparator(int columnNumber) { 173 this.columnNumber = columnNumber; 174 } 175 179 public int compare(Viewer viewer, Object o1, Object 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 200 int compareColumnValue(int columnNumber, ILogEntry e1, ILogEntry e2) { 201 switch (columnNumber) { 202 case 0: 203 return versionCollator.compare(e1.getRevision(), e2.getRevision()); 204 case 1: 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: 215 Date date1 = e1.getDate(); 216 Date date2 = e2.getDate(); 217 return date1.compareTo(date2); 218 case 3: 219 return getComparator().compare(e1.getAuthor(), e2.getAuthor()); 220 case 4: 221 return getComparator().compare(e1.getComment(), e2.getComment()); 222 default: 223 return 0; 224 } 225 } 226 229 public int getColumnNumber() { 230 return columnNumber; 231 } 232 236 public boolean isReversed() { 237 return reversed; 238 } 239 242 public void setReversed(boolean newReversed) { 243 reversed = newReversed; 244 } 245 } 246 247 protected ILogEntry adaptToLogEntry(Object element) { 248 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 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 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 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 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 341 private void createColumns(Table table, TableLayout layout, TableViewer viewer) { 342 SelectionListener headerListener = getColumnListener(viewer); 343 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 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 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 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 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 382 private SelectionListener getColumnListener(final TableViewer tableViewer) { 383 390 return new SelectionAdapter() { 391 400 public void widgetSelected(SelectionEvent e) { 401 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 getCurrentRevision() { 415 return currentRevision; 416 } 417 418 422 private String getRevision(ICVSFile currentEdition) throws CVSException { 423 if (currentEdition == null) return ""; ResourceSyncInfo info = currentEdition.getSyncInfo(); 425 if (info == null) return ""; 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 |