PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.team.cvs.ui.source_3.3.400.I20110510-0800/org/eclipse/team/internal/ccvs/ui/EditorsView.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 303 lines | 200 code | 38 blank | 65 comment | 33 complexity | 77390dcc695e88c8aabfa327c54ab102 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2010 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. * CSC - Initial implementation
  10. * IBM Corporation - ongoing maintenance
  11. * Brian Mauter <brianmauter@gmail.com> - bug 248333
  12. *******************************************************************************/
  13. package org.eclipse.team.internal.ccvs.ui;
  14. import java.text.ParseException;
  15. import java.util.Locale;
  16. import org.eclipse.jface.viewers.*;
  17. import org.eclipse.swt.SWT;
  18. import org.eclipse.swt.events.*;
  19. import org.eclipse.swt.graphics.Image;
  20. import org.eclipse.swt.layout.GridData;
  21. import org.eclipse.swt.widgets.*;
  22. import org.eclipse.team.internal.ccvs.core.EditorsInfo;
  23. import org.eclipse.ui.PlatformUI;
  24. import org.eclipse.ui.part.ViewPart;
  25. import com.ibm.icu.text.DateFormat;
  26. import com.ibm.icu.text.SimpleDateFormat;
  27. /**
  28. *
  29. * The <code>EditorsView</code> shows the result of cvs editors command
  30. *
  31. * @author <a href="mailto:gregor.kohlwes@csc.com,kohlwes@gmx.net">Gregor Kohlwes</a>
  32. * @see org.eclipse.team.internal.ccvs.ui.actions.ShowEditorsAction
  33. */
  34. public class EditorsView extends ViewPart {
  35. public static final String VIEW_ID = "org.eclipse.team.ccvs.ui.EditorsView"; //$NON-NLS-1$
  36. private Table table;
  37. private TableViewer tableViewer;
  38. class EditorsLabelProvider implements ITableLabelProvider {
  39. /**
  40. * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
  41. */
  42. public Image getColumnImage(Object element, int columnIndex) {
  43. return null;
  44. }
  45. /**
  46. * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
  47. */
  48. public String getColumnText(Object element, int columnIndex) {
  49. if (element == null)
  50. return ""; //$NON-NLS-1$
  51. EditorsInfo info = (EditorsInfo) element;
  52. String result = null;
  53. switch (columnIndex) {
  54. case 0 :
  55. result = info.getFileName();
  56. break;
  57. case 1 :
  58. result = info.getUserName();
  59. break;
  60. case 2 :
  61. result = info.getDateString();
  62. break;
  63. case 3 :
  64. result = info.getComputerName();
  65. break;
  66. }
  67. // This method must not return null
  68. if (result == null) result = ""; //$NON-NLS-1$
  69. return result;
  70. }
  71. /**
  72. * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
  73. */
  74. public void addListener(ILabelProviderListener listener) {
  75. }
  76. /**
  77. * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
  78. */
  79. public void dispose() {
  80. }
  81. /**
  82. * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
  83. */
  84. public boolean isLabelProperty(Object element, String property) {
  85. return false;
  86. }
  87. /**
  88. * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
  89. */
  90. public void removeListener(ILabelProviderListener listener) {
  91. }
  92. }
  93. class EditorsComparator extends ViewerComparator {
  94. private int column;
  95. private boolean reversed;
  96. // we recognize the following date/time formats
  97. private DateFormat[] dateFormats = new DateFormat[] {
  98. new SimpleDateFormat("dd MMM yyyy HH:mm:ss z", Locale.US), //$NON-NLS-1$
  99. new SimpleDateFormat("dd MMM yyyy HH:mm:ss z"), //$NON-NLS-1$
  100. new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy z") //$NON-NLS-1$
  101. };
  102. public EditorsComparator(int column) {
  103. this.column = column;
  104. }
  105. public int getColumnNumber() {
  106. return column;
  107. }
  108. public boolean isReversed() {
  109. return reversed;
  110. }
  111. public void setReversed(boolean reversed) {
  112. this.reversed = reversed;
  113. }
  114. public int compare(Viewer compareViewer, Object o1, Object o2) {
  115. int result = 0;
  116. if ((o1 instanceof EditorsInfo) && (o2 instanceof EditorsInfo)) {
  117. EditorsInfo ei1 = (EditorsInfo) o1;
  118. EditorsInfo ei2 = (EditorsInfo) o2;
  119. switch (column) {
  120. case 0 : // file name
  121. result = compareStrings(ei1.getFileName(), ei2.getFileName());
  122. break;
  123. case 1 : // user name
  124. result = compareStrings(ei1.getUserName(), ei2.getUserName());
  125. break;
  126. case 2 : // date
  127. result = compareDates(ei1.getDateString(), ei2.getDateString());
  128. break;
  129. case 3 : // computer name
  130. result = compareStrings(ei1.getComputerName(), ei2.getComputerName());
  131. }
  132. if (reversed)
  133. result = -result;
  134. }
  135. return result;
  136. }
  137. private int compareStrings(String s1, String s2) {
  138. if (s1 == null && s2 == null)
  139. return 0;
  140. if (s1 == null)
  141. return -1;
  142. if (s2 == null)
  143. return 1;
  144. return getComparator().compare(s1, s2);
  145. }
  146. private int compareDates(String s1, String s2) {
  147. long date1 = extractDate(s1);
  148. long date2 = extractDate(s2);
  149. if (date1 == date2)
  150. return 0;
  151. if (date1 == -1)
  152. return -1;
  153. if (date2 == -1)
  154. return 1;
  155. return date1 > date2 ? 1 : -1;
  156. }
  157. /**
  158. * @param dateString Date to parse
  159. * @return the parsed time/date in milliseconds or -1 on error
  160. */
  161. private long extractDate(String dateString) {
  162. if (dateString != null) {
  163. for (int i = 0; i < dateFormats.length; i++) {
  164. dateFormats[i].setLenient(true);
  165. try {
  166. return dateFormats[i].parse(dateString).getTime();
  167. } catch (ParseException ex) {
  168. // silently ignored
  169. }
  170. }
  171. }
  172. return -1;
  173. }
  174. }
  175. /**
  176. * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
  177. */
  178. public void createPartControl(Composite parent) {
  179. table = new Table(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
  180. table.setHeaderVisible(true);
  181. table.setLinesVisible(true);
  182. GridData gridData = new GridData(GridData.FILL_BOTH);
  183. gridData.widthHint=500;
  184. gridData.heightHint=100;
  185. table.setLayoutData(gridData);
  186. TableLayout layout = new TableLayout();
  187. table.setLayout(layout);
  188. tableViewer = new TableViewer(table);
  189. createColumns(table, layout);
  190. tableViewer.setContentProvider(ArrayContentProvider.getInstance());
  191. tableViewer.setLabelProvider(new EditorsLabelProvider());
  192. // set F1 help
  193. PlatformUI.getWorkbench().getHelpSystem().setHelp(tableViewer.getControl(), IHelpContextIds.CVS_EDITORS_VIEW);
  194. }
  195. public void setInput(EditorsInfo[] infos) {
  196. tableViewer.setInput(infos);
  197. }
  198. /**
  199. * Method createColumns.
  200. * @param table
  201. * @param layout
  202. * @param viewer
  203. */
  204. private void createColumns(Table table, TableLayout layout) {
  205. SelectionListener headerListener = getColumnListener(tableViewer);
  206. TableColumn col;
  207. // file name
  208. col = new TableColumn(table, SWT.NONE);
  209. col.setResizable(true);
  210. col.setText(CVSUIMessages.EditorsView_file);
  211. layout.addColumnData(new ColumnWeightData(30, true));
  212. col.addSelectionListener(headerListener);
  213. // user name
  214. col = new TableColumn(table, SWT.NONE);
  215. col.setResizable(true);
  216. col.setText(CVSUIMessages.EditorsView_user);
  217. layout.addColumnData(new ColumnWeightData(20, true));
  218. col.addSelectionListener(headerListener);
  219. // creation date
  220. col = new TableColumn(table, SWT.NONE);
  221. col.setResizable(true);
  222. col.setText(CVSUIMessages.EditorsView_date);
  223. layout.addColumnData(new ColumnWeightData(30, true));
  224. col.addSelectionListener(headerListener);
  225. // computer name
  226. col = new TableColumn(table, SWT.NONE);
  227. col.setResizable(true);
  228. col.setText(CVSUIMessages.EditorsView_computer);
  229. layout.addColumnData(new ColumnWeightData(20, true));
  230. col.addSelectionListener(headerListener);
  231. }
  232. private SelectionListener getColumnListener(final TableViewer tableViewer) {
  233. return new SelectionAdapter() {
  234. public void widgetSelected(SelectionEvent e) {
  235. // column selected - need to sort
  236. TableColumn tableColumn = (TableColumn) e.widget;
  237. int column = tableViewer.getTable().indexOf(tableColumn);
  238. EditorsComparator oldSorter = (EditorsComparator) tableViewer.getComparator();
  239. if (oldSorter != null && column == oldSorter.getColumnNumber()) {
  240. oldSorter.setReversed(!oldSorter.isReversed());
  241. tableViewer.getTable().setSortColumn(tableColumn);
  242. tableViewer.getTable().setSortDirection(oldSorter.isReversed() ? SWT.DOWN : SWT.UP);
  243. tableViewer.refresh();
  244. } else {
  245. tableViewer.getTable().setSortColumn(tableColumn);
  246. tableViewer.getTable().setSortDirection(SWT.UP);
  247. tableViewer.setComparator(new EditorsComparator(column));
  248. }
  249. }
  250. };
  251. }
  252. /**
  253. * @see org.eclipse.ui.IWorkbenchPart#setFocus()
  254. */
  255. public void setFocus() {
  256. if (table != null)
  257. table.setFocus();
  258. }
  259. /**
  260. * Method getTable.
  261. */
  262. public Table getTable() {
  263. return table;
  264. }
  265. }