PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/com.atlassian.connector.eclipse.crucible.ui/src/com/atlassian/connector/eclipse/internal/crucible/ui/util/CommentUiUtil.java

https://github.com/spingel/mylyn-reviews
Java | 190 lines | 146 code | 29 blank | 15 comment | 28 complexity | da83316399e556d57f73ed2cadb77178 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2009 Atlassian 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. * Atlassian - initial API and implementation
  10. ******************************************************************************/
  11. package com.atlassian.connector.eclipse.internal.crucible.ui.util;
  12. import com.atlassian.connector.commons.misc.IntRanges;
  13. import com.atlassian.connector.eclipse.internal.crucible.core.CrucibleConstants;
  14. import com.atlassian.connector.eclipse.internal.crucible.ui.CrucibleUiUtil;
  15. import com.atlassian.theplugin.commons.crucible.api.model.Comment;
  16. import com.atlassian.theplugin.commons.crucible.api.model.CustomField;
  17. import org.eclipse.mylyn.internal.tasks.ui.editors.RichTextEditor;
  18. import org.eclipse.mylyn.internal.wikitext.tasks.ui.editor.ConfluenceMarkupTaskEditorExtension;
  19. import org.eclipse.mylyn.tasks.core.ITask;
  20. import org.eclipse.mylyn.tasks.core.TaskRepository;
  21. import org.eclipse.mylyn.tasks.ui.TasksUi;
  22. import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorExtension;
  23. import org.eclipse.swt.SWT;
  24. import org.eclipse.swt.events.FocusEvent;
  25. import org.eclipse.swt.events.FocusListener;
  26. import org.eclipse.swt.widgets.Composite;
  27. import org.eclipse.ui.PlatformUI;
  28. import org.eclipse.ui.contexts.IContextService;
  29. import org.eclipse.ui.forms.widgets.FormToolkit;
  30. import java.text.DateFormat;
  31. import java.util.Iterator;
  32. import java.util.Map;
  33. import java.util.Map.Entry;
  34. /**
  35. *
  36. * @author Wojciech Seliga
  37. */
  38. public final class CommentUiUtil {
  39. private CommentUiUtil() {
  40. }
  41. public static String getCommentInfoHeaderText(Comment comment) {
  42. StringBuilder headerText = new StringBuilder();
  43. headerText.append(comment.getAuthor().getDisplayName());
  44. headerText.append("\n");
  45. headerText.append(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM)
  46. .format(comment.getCreateDate()));
  47. if (comment.getReadState() != null) {
  48. if (comment.getReadState().equals(Comment.ReadState.READ)) {
  49. headerText.append(", Read");
  50. } else if (comment.getReadState().equals(Comment.ReadState.UNREAD)
  51. || comment.getReadState().equals(Comment.ReadState.LEAVE_UNREAD)) {
  52. headerText.append(", Unread");
  53. }
  54. }
  55. if (comment.isDraft()) {
  56. headerText.append(", ");
  57. headerText.append("Draft");
  58. }
  59. if (comment.isDefectRaised()) {
  60. headerText.append(", ");
  61. headerText.append("Defect");
  62. Map<String, CustomField> fields = comment.getCustomFields();
  63. if (fields != null) {
  64. boolean shouldCloseBracket = false;
  65. if (fields.containsKey(CrucibleConstants.RANK_CUSTOM_FIELD_KEY)) {
  66. headerText.append(" (");
  67. shouldCloseBracket = true;
  68. headerText.append(fields.get(CrucibleConstants.RANK_CUSTOM_FIELD_KEY).getValue());
  69. }
  70. if (fields.containsKey(CrucibleConstants.CLASSIFICATION_CUSTOM_FIELD_KEY)) {
  71. if (shouldCloseBracket) {
  72. headerText.append(",");
  73. }
  74. headerText.append(" ");
  75. headerText.append(fields.get(CrucibleConstants.CLASSIFICATION_CUSTOM_FIELD_KEY)
  76. .getValue());
  77. }
  78. if (shouldCloseBracket) {
  79. headerText.append(")");
  80. }
  81. }
  82. }
  83. return headerText.toString();
  84. }
  85. public static boolean isSimpleInfoEnough(Map<String, IntRanges> ranges) {
  86. if (ranges.size() <= 1) {
  87. return true;
  88. }
  89. final Iterator<Entry<String, IntRanges>> it = ranges.entrySet().iterator();
  90. final IntRanges lines = it.next().getValue();
  91. while (it.hasNext()) {
  92. if (!lines.equals(it.next().getValue())) {
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. public static String getCompactedLineInfoText(Map<String, IntRanges> ranges) {
  99. if (isSimpleInfoEnough(ranges)) {
  100. final StringBuilder infoText = new StringBuilder("File comment for ");
  101. final Iterator<Entry<String, IntRanges>> it = ranges.entrySet().iterator();
  102. Entry<String, IntRanges> curEntry = it.next();
  103. IntRanges lines = curEntry.getValue();
  104. infoText.append(getLineInfo(lines));
  105. if (it.hasNext()) {
  106. infoText.append(" in revisions: ");
  107. } else {
  108. infoText.append(" in revision: ");
  109. }
  110. do {
  111. infoText.append(curEntry.getKey());
  112. if (it.hasNext()) {
  113. infoText.append(", ");
  114. } else {
  115. break;
  116. }
  117. curEntry = it.next();
  118. } while (true);
  119. return infoText.toString();
  120. } else {
  121. final StringBuilder infoText = new StringBuilder("File comment for:\n");
  122. for (Map.Entry<String, IntRanges> range : ranges.entrySet()) {
  123. infoText.append("- ");
  124. infoText.append(getLineInfo(range.getValue()));
  125. infoText.append(" in revision: ");
  126. infoText.append(range.getKey());
  127. infoText.append("\n");
  128. }
  129. return infoText.toString();
  130. }
  131. }
  132. public static String getLineInfo(IntRanges intRanges) {
  133. if (intRanges.getTotalMin() == intRanges.getTotalMax()) {
  134. return "line " + intRanges.getTotalMin();
  135. } else {
  136. return "lines " + intRanges.toNiceString();
  137. }
  138. }
  139. @SuppressWarnings("restriction")
  140. public static RichTextEditor createWikiTextControl(FormToolkit toolkit, Composite parent, Comment comment) {
  141. int style = SWT.FLAT | SWT.READ_ONLY | SWT.MULTI | SWT.WRAP;
  142. ITask task = CrucibleUiUtil.getCrucibleTask(comment.getReview());
  143. TaskRepository repository = TasksUi.getRepositoryManager().getRepository(task.getConnectorKind(),
  144. task.getRepositoryUrl());
  145. final AbstractTaskEditorExtension extension = new ConfluenceMarkupTaskEditorExtension();
  146. IContextService contextService = (IContextService) PlatformUI.getWorkbench().getService(IContextService.class);
  147. final RichTextEditor editor = new RichTextEditor(repository, style, contextService, extension);
  148. editor.setReadOnly(true);
  149. editor.setText(comment.getMessage());
  150. editor.createControl(parent, toolkit);
  151. // HACK: this is to make sure that we can't have multiple things highlighted
  152. editor.getViewer().getTextWidget().addFocusListener(new FocusListener() {
  153. public void focusGained(FocusEvent e) {
  154. }
  155. public void focusLost(FocusEvent e) {
  156. editor.getViewer().getTextWidget().setSelection(0);
  157. }
  158. });
  159. return editor;
  160. }
  161. }