PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/com.atlassian.connector.eclipse.crucible.ui/src/com/atlassian/connector/eclipse/internal/crucible/ui/annotations/CrucibleInformationControl.java

https://github.com/spingel/mylyn-reviews
Java | 264 lines | 202 code | 43 blank | 19 comment | 40 complexity | 34d6cacd3bcd00d949f02e731038a345 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.annotations;
  12. import com.atlassian.theplugin.commons.crucible.api.model.Comment;
  13. import com.atlassian.theplugin.commons.crucible.api.model.Comment.ReadState;
  14. import com.atlassian.theplugin.commons.util.MiscUtil;
  15. import org.eclipse.core.runtime.jobs.Job;
  16. import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
  17. import org.eclipse.jface.text.DefaultInformationControl;
  18. import org.eclipse.jface.text.IInformationControlCreator;
  19. import org.eclipse.jface.text.IInformationControlExtension2;
  20. import org.eclipse.jface.text.information.InformationPresenter;
  21. import org.eclipse.swt.SWT;
  22. import org.eclipse.swt.events.DisposeListener;
  23. import org.eclipse.swt.events.FocusListener;
  24. import org.eclipse.swt.graphics.Color;
  25. import org.eclipse.swt.graphics.Point;
  26. import org.eclipse.swt.graphics.Rectangle;
  27. import org.eclipse.swt.widgets.Shell;
  28. import java.util.Collection;
  29. import java.util.List;
  30. import java.util.Set;
  31. /**
  32. * A custom control to display on hover, or delegates to the default control to display if we aren't dealing with a
  33. * CrucibleCommentAnnotation.
  34. *
  35. * @author sminto
  36. */
  37. public class CrucibleInformationControl extends DefaultInformationControl implements IInformationControlExtension2 {
  38. private Object input;
  39. private CrucibleCommentPopupDialog commentPopupDialog;
  40. private final CrucibleInformationControlCreator informationControlCreator;
  41. private Job markAsReadJob;
  42. @SuppressWarnings("restriction")
  43. public CrucibleInformationControl(Shell parent, CrucibleInformationControlCreator crucibleInformationControlCreator) {
  44. super(parent, new HTMLTextPresenter(true));
  45. this.informationControlCreator = crucibleInformationControlCreator;
  46. commentPopupDialog = new CrucibleCommentPopupDialog(parent, SWT.NO_FOCUS | SWT.ON_TOP);
  47. // Force create early so that listeners can be added at all times with API.
  48. commentPopupDialog.create();
  49. commentPopupDialog.setInformationControl(this);
  50. }
  51. public InformationPresenter getInformationPresenter() {
  52. return new InformationPresenter(informationControlCreator);
  53. }
  54. @Override
  55. public void setInformation(String content) {
  56. this.input = content;
  57. commentPopupDialog.setInput(input);
  58. super.setInformation(content);
  59. }
  60. public void setInput(Object input) {
  61. this.input = input;
  62. commentPopupDialog.setInput(input);
  63. }
  64. @Override
  65. public boolean hasContents() {
  66. return input != null || super.hasContents();
  67. }
  68. private void runMarkCommentAsReadJob(CrucibleAnnotationHoverInput input) {
  69. List<CrucibleCommentAnnotation> annotations = input.getCrucibleAnnotations();
  70. if (annotations == null || annotations.size() == 0) {
  71. return;
  72. }
  73. Set<Comment> comments = MiscUtil.buildHashSet();
  74. for (CrucibleCommentAnnotation annotation : annotations) {
  75. comments.addAll(getUnreadComments(annotation.getVersionedComment()));
  76. }
  77. if (comments.size() > 0) {
  78. // markAsReadJob = new MarkCommentsReadJob(comments.iterator().next().getReview(), comments, true);
  79. // markAsReadJob.schedule(MarkCommentsReadJob.DEFAULT_DELAY_INTERVAL);
  80. }
  81. }
  82. private Collection<? extends Comment> getUnreadComments(Comment comment) {
  83. Set<Comment> result = MiscUtil.buildHashSet();
  84. if (comment.getReadState().equals(ReadState.UNREAD)) {
  85. result.add(comment);
  86. }
  87. for (Comment reply : comment.getReplies()) {
  88. result.addAll(getUnreadComments(reply));
  89. }
  90. return result;
  91. }
  92. private void cancelMarkCommentAsReadJob() {
  93. if (markAsReadJob != null) {
  94. markAsReadJob.cancel();
  95. markAsReadJob = null;
  96. }
  97. }
  98. @Override
  99. public void setVisible(boolean visible) {
  100. cancelMarkCommentAsReadJob();
  101. if (input instanceof String) {
  102. setInformation((String) input);
  103. super.setVisible(visible);
  104. } else if (input instanceof CrucibleAnnotationHoverInput) {
  105. if (visible) {
  106. commentPopupDialog.open();
  107. runMarkCommentAsReadJob((CrucibleAnnotationHoverInput) input);
  108. } else {
  109. commentPopupDialog.getShell().setVisible(false);
  110. }
  111. } else {
  112. super.setVisible(visible);
  113. }
  114. }
  115. @Override
  116. public void dispose() {
  117. cancelMarkCommentAsReadJob();
  118. commentPopupDialog.dispose();
  119. commentPopupDialog = null;
  120. }
  121. @Override
  122. public void setSize(int width, int height) {
  123. super.setSize(width, height);
  124. commentPopupDialog.setSize(width, height);
  125. }
  126. @Override
  127. public void setLocation(Point location) {
  128. super.setLocation(location);
  129. commentPopupDialog.setLocation(location);
  130. }
  131. @Override
  132. public void setSizeConstraints(int maxWidth, int maxHeight) {
  133. super.setSizeConstraints(maxWidth, maxHeight);
  134. commentPopupDialog.setSizeConstraints(maxWidth, maxHeight);
  135. }
  136. @Override
  137. public Rectangle computeTrim() {
  138. if (input instanceof String) {
  139. return super.computeTrim();
  140. } else if (input instanceof CrucibleAnnotationHoverInput) {
  141. return commentPopupDialog.computeTrim();
  142. } else {
  143. return super.computeTrim();
  144. }
  145. }
  146. @Override
  147. public Rectangle getBounds() {
  148. if (input instanceof String) {
  149. return super.getBounds();
  150. } else if (input instanceof CrucibleAnnotationHoverInput) {
  151. return commentPopupDialog.getBounds();
  152. } else {
  153. return super.getBounds();
  154. }
  155. }
  156. @Override
  157. public void addDisposeListener(DisposeListener listener) {
  158. super.addDisposeListener(listener);
  159. if (commentPopupDialog != null) {
  160. commentPopupDialog.addDisposeListener(listener);
  161. }
  162. }
  163. @Override
  164. public void removeDisposeListener(DisposeListener listener) {
  165. super.removeDisposeListener(listener);
  166. commentPopupDialog.removeDisposeListener(listener);
  167. }
  168. @Override
  169. public void setForegroundColor(Color foreground) {
  170. super.setForegroundColor(foreground);
  171. }
  172. @Override
  173. public void setBackgroundColor(Color background) {
  174. super.setBackgroundColor(background);
  175. }
  176. @Override
  177. public boolean isFocusControl() {
  178. if (input instanceof String) {
  179. return super.isFocusControl();
  180. } else if (input instanceof CrucibleAnnotationHoverInput) {
  181. return commentPopupDialog.isFocusControl();
  182. } else {
  183. return super.isFocusControl();
  184. }
  185. }
  186. @Override
  187. public void setFocus() {
  188. if (input instanceof String) {
  189. super.setFocus();
  190. } else if (input instanceof CrucibleAnnotationHoverInput) {
  191. commentPopupDialog.setFocus();
  192. } else {
  193. super.setFocus();
  194. }
  195. }
  196. @Override
  197. public void addFocusListener(FocusListener listener) {
  198. super.addFocusListener(listener);
  199. commentPopupDialog.addFocusListener(listener);
  200. }
  201. @Override
  202. public void removeFocusListener(FocusListener listener) {
  203. super.removeFocusListener(listener);
  204. commentPopupDialog.removeFocusListener(listener);
  205. }
  206. @Override
  207. public Point computeSizeHint() {
  208. if (input instanceof String) {
  209. setInformation((String) input);
  210. return super.computeSizeHint();
  211. } else if (input instanceof CrucibleAnnotationHoverInput) {
  212. return commentPopupDialog.computeSizeHint();
  213. } else {
  214. return super.computeSizeHint();
  215. }
  216. }
  217. @Override
  218. public IInformationControlCreator getInformationPresenterControlCreator() {
  219. return new CrucibleInformationControlCreator();
  220. }
  221. }