PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/com.atlassian.connector.eclipse.crucible.ui/src/com/atlassian/connector/eclipse/internal/crucible/ui/editor/ruler/CommentAnnotationRulerColumn.java

https://github.com/spingel/mylyn-reviews
Java | 274 lines | 209 code | 47 blank | 18 comment | 27 complexity | ea20d58f480b2999095ee2fbd67b1b27 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.editor.ruler;
  12. import com.atlassian.connector.eclipse.internal.crucible.ui.ActiveReviewManager.IReviewActivationListener;
  13. import com.atlassian.connector.eclipse.internal.crucible.ui.CrucibleUiPlugin;
  14. import com.atlassian.connector.eclipse.internal.crucible.ui.CrucibleUiUtil;
  15. import com.atlassian.connector.eclipse.internal.crucible.ui.ICrucibleFileProvider;
  16. import com.atlassian.connector.eclipse.internal.crucible.ui.annotations.CrucibleAnnotationModel;
  17. import com.atlassian.connector.eclipse.internal.crucible.ui.annotations.CrucibleCommentAnnotation;
  18. import com.atlassian.connector.eclipse.team.ui.CrucibleFile;
  19. import com.atlassian.theplugin.commons.crucible.api.model.CrucibleFileInfo;
  20. import com.atlassian.theplugin.commons.crucible.api.model.Review;
  21. import com.atlassian.theplugin.commons.crucible.api.model.notification.CrucibleNotification;
  22. import org.eclipse.core.runtime.Assert;
  23. import org.eclipse.jface.preference.IPreferenceStore;
  24. import org.eclipse.jface.preference.PreferenceConverter;
  25. import org.eclipse.jface.text.BadLocationException;
  26. import org.eclipse.jface.text.ITextViewer;
  27. import org.eclipse.jface.text.source.AbstractRulerColumn;
  28. import org.eclipse.jface.text.source.AnnotationModel;
  29. import org.eclipse.jface.text.source.CompositeRuler;
  30. import org.eclipse.jface.text.source.IAnnotationModel;
  31. import org.eclipse.jface.text.source.IAnnotationModelListener;
  32. import org.eclipse.jface.text.source.ISharedTextColors;
  33. import org.eclipse.jface.text.source.ISourceViewer;
  34. import org.eclipse.jface.util.IPropertyChangeListener;
  35. import org.eclipse.jface.util.PropertyChangeEvent;
  36. import org.eclipse.mylyn.tasks.core.ITask;
  37. import org.eclipse.swt.SWT;
  38. import org.eclipse.swt.graphics.Color;
  39. import org.eclipse.swt.graphics.GC;
  40. import org.eclipse.swt.graphics.RGB;
  41. import org.eclipse.swt.widgets.Composite;
  42. import org.eclipse.swt.widgets.Control;
  43. import org.eclipse.swt.widgets.Display;
  44. import org.eclipse.ui.IFileEditorInput;
  45. import org.eclipse.ui.editors.text.EditorsUI;
  46. import org.eclipse.ui.internal.texteditor.PropertyEventDispatcher;
  47. import org.eclipse.ui.texteditor.AnnotationPreference;
  48. import org.eclipse.ui.texteditor.AnnotationPreferenceLookup;
  49. import org.eclipse.ui.texteditor.IDocumentProvider;
  50. import org.eclipse.ui.texteditor.ITextEditor;
  51. import org.eclipse.ui.texteditor.rulers.IContributedRulerColumn;
  52. import org.eclipse.ui.texteditor.rulers.RulerColumnDescriptor;
  53. import java.util.Collection;
  54. import java.util.List;
  55. public class CommentAnnotationRulerColumn extends AbstractRulerColumn implements IContributedRulerColumn,
  56. IReviewActivationListener {
  57. /** The contribution descriptor. */
  58. private RulerColumnDescriptor fDescriptor;
  59. private CrucibleAnnotationModel annotationModel;
  60. private ITextEditor fEditor;
  61. private Color colorCommented;
  62. private ISourceViewer fViewer;
  63. private PropertyEventDispatcher fDispatcher;
  64. private IDocumentProvider fDocumentProvider;
  65. public CommentAnnotationRulerColumn() {
  66. setTextInset(10);
  67. setHover(new CommentAnnotationRulerHover(this));
  68. }
  69. @Override
  70. public void dispose() {
  71. colorCommented.dispose();
  72. super.dispose();
  73. }
  74. public RulerColumnDescriptor getDescriptor() {
  75. return fDescriptor;
  76. }
  77. public void setDescriptor(RulerColumnDescriptor descriptor) {
  78. fDescriptor = descriptor;
  79. }
  80. public void setEditor(ITextEditor editor) {
  81. fEditor = editor;
  82. fDocumentProvider = fEditor.getDocumentProvider();
  83. }
  84. public ITextEditor getEditor() {
  85. return fEditor;
  86. }
  87. public void columnCreated() {
  88. }
  89. public void columnRemoved() {
  90. }
  91. protected Color computeLeftBackground(int line) {
  92. List<CrucibleCommentAnnotation> annotations = getAnnotations(line);
  93. if (annotations == null || annotations.size() == 0) {
  94. return super.computeBackground(line);
  95. } else {
  96. return colorCommented;
  97. }
  98. }
  99. @Override
  100. protected Color computeForeground(int line) {
  101. return Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
  102. }
  103. @Override
  104. protected void paintLine(GC gc, int modelLine, int widgetLine, int linePixel, int lineHeight) {
  105. gc.setBackground(computeLeftBackground(modelLine));
  106. gc.fillRectangle(0, linePixel, getWidth(), lineHeight);
  107. }
  108. public List<CrucibleCommentAnnotation> getAnnotations(int startLine) {
  109. try {
  110. int offset = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()).getLineOffset(startLine);
  111. return annotationModel == null ? null : annotationModel.getAnnotationsForOffset(offset);
  112. } catch (BadLocationException e) {
  113. }
  114. return null;
  115. }
  116. private ISharedTextColors getSharedColors() {
  117. return EditorsUI.getSharedTextColors();
  118. }
  119. public static RGB getColorFromAnnotationPreference(IPreferenceStore store, AnnotationPreference pref) {
  120. String key = pref.getColorPreferenceKey();
  121. RGB rgb = null;
  122. if (store.contains(key)) {
  123. if (store.isDefault(key)) {
  124. rgb = pref.getColorPreferenceValue();
  125. } else {
  126. rgb = PreferenceConverter.getColor(store, key);
  127. }
  128. }
  129. if (rgb == null) {
  130. rgb = pref.getColorPreferenceValue();
  131. }
  132. return rgb;
  133. }
  134. private void updateCommentedColor(AnnotationPreference pref, IPreferenceStore store) {
  135. if (pref != null) {
  136. RGB rgb = getColorFromAnnotationPreference(store, pref);
  137. colorCommented = getSharedColors().getColor(rgb);
  138. }
  139. }
  140. /**
  141. * Initializes the given line number ruler column from the preference store.
  142. */
  143. private void initialize() {
  144. final IPreferenceStore store = EditorsUI.getPreferenceStore();
  145. if (store == null) {
  146. return;
  147. }
  148. AnnotationPreferenceLookup lookup = EditorsUI.getAnnotationPreferenceLookup();
  149. final AnnotationPreference commentedPref = lookup.getAnnotationPreference(CrucibleCommentAnnotation.COMMENT_ANNOTATION_ID);
  150. updateCommentedColor(commentedPref, store);
  151. redraw();
  152. // listen to changes
  153. fDispatcher = new PropertyEventDispatcher(store);
  154. if (commentedPref != null) {
  155. fDispatcher.addPropertyChangeListener(commentedPref.getColorPreferenceKey(), new IPropertyChangeListener() {
  156. public void propertyChange(PropertyChangeEvent event) {
  157. updateCommentedColor(commentedPref, store);
  158. redraw();
  159. }
  160. });
  161. }
  162. }
  163. @Override
  164. public Control createControl(CompositeRuler parentRuler, Composite parentControl) {
  165. ITextViewer viewer = parentRuler.getTextViewer();
  166. Assert.isLegal(viewer instanceof ISourceViewer);
  167. fViewer = (ISourceViewer) viewer;
  168. fViewer.showAnnotations(true);
  169. IAnnotationModel model = fViewer.getAnnotationModel();
  170. if (model == null) {
  171. fViewer.setDocument(fViewer.getDocument(), new AnnotationModel());
  172. }
  173. fViewer.getAnnotationModel().addAnnotationModelListener(new IAnnotationModelListener() {
  174. public void modelChanged(IAnnotationModel model) {
  175. if (CrucibleUiPlugin.getDefault().getActiveReviewManager().isReviewActive()) {
  176. reviewActivated(CrucibleUiPlugin.getDefault().getActiveReviewManager().getActiveTask(),
  177. CrucibleUiPlugin.getDefault().getActiveReviewManager().getActiveReview());
  178. } else {
  179. reviewDeactivated(null, null);
  180. }
  181. }
  182. });
  183. initialize();
  184. CrucibleUiPlugin.getDefault().getActiveReviewManager().addReviewActivationListener(this);
  185. return super.createControl(parentRuler, parentControl);
  186. }
  187. public void reviewActivated(ITask task, Review review) {
  188. annotationModel = null;
  189. CrucibleFileInfo currentFileInfo = null;
  190. CrucibleFile file = null;
  191. if (fEditor.getEditorInput() instanceof ICrucibleFileProvider) {
  192. ICrucibleFileProvider fileProvider = (ICrucibleFileProvider) fEditor.getEditorInput();
  193. file = fileProvider.getCrucibleFile();
  194. }
  195. if (fEditor.getEditorInput() instanceof IFileEditorInput) {
  196. IFileEditorInput input = (IFileEditorInput) fEditor.getEditorInput();
  197. file = CrucibleUiUtil.getCruciblePostCommitFile(input.getFile(), review);
  198. }
  199. if (file != null) {
  200. currentFileInfo = review.getFileByPermId(file.getCrucibleFileInfo().getPermId());
  201. if (currentFileInfo != null) {
  202. annotationModel = new CrucibleAnnotationModel(fEditor, fEditor.getEditorInput(),
  203. fDocumentProvider.getDocument(fEditor.getEditorInput()), new CrucibleFile(currentFileInfo,
  204. file.isOldFile()), review);
  205. }
  206. }
  207. Display.getDefault().asyncExec(new Runnable() {
  208. public void run() {
  209. redraw();
  210. }
  211. });
  212. }
  213. /**
  214. * task and review might be null when called internally
  215. */
  216. public void reviewDeactivated(ITask task, Review review) {
  217. annotationModel = null;
  218. Display.getDefault().asyncExec(new Runnable() {
  219. public void run() {
  220. redraw();
  221. }
  222. });
  223. }
  224. public void reviewUpdated(ITask task, Review review, Collection<CrucibleNotification> differences) {
  225. reviewActivated(task, review);
  226. }
  227. }