View Javadoc

1   package com.atlassian.theplugin.idea.crucible.editor;
2   
3   import com.atlassian.theplugin.commons.crucible.ValueNotYetInitialized;
4   import com.atlassian.theplugin.commons.crucible.api.model.*;
5   import com.atlassian.theplugin.idea.crucible.CommentDateUtil;
6   import com.atlassian.theplugin.idea.crucible.CommentTooltipPanel;
7   import com.atlassian.theplugin.idea.crucible.CommentTooltipPanelWithRunners;
8   import com.atlassian.theplugin.idea.Constants;
9   import com.intellij.openapi.actionSystem.AnAction;
10  import com.intellij.openapi.actionSystem.AnActionEvent;
11  import com.intellij.openapi.editor.markup.GutterIconRenderer;
12  import com.intellij.openapi.editor.Editor;
13  import org.jetbrains.annotations.NotNull;
14  
15  import javax.swing.*;
16  
17  public class CrucibleGutterIconRenderer extends GutterIconRenderer {
18      private Editor editor;
19      private final ReviewAdapter review;
20  	private final CrucibleFileInfo fileInfo;
21  	private final VersionedComment comment;
22  
23  	public CrucibleGutterIconRenderer(Editor editor, ReviewAdapter review,
24                                        CrucibleFileInfo fileInfo, VersionedComment comment) {
25          this.editor = editor;
26          this.review = review;
27  		this.fileInfo = fileInfo;
28  		this.comment = comment;
29  	}
30  
31  	@NotNull
32  	public Icon getIcon() {
33  		return Constants.CRUCIBLE_REVIEW_PANEL_ICON;
34  	}
35  
36  	@Override
37  	public String getTooltipText() {
38  		StringBuilder s = new StringBuilder();
39  		s.append("<html><b>")
40  				.append(comment.getAuthor().getDisplayName())
41  				.append("</b> said <i>on ")
42  				.append(CommentDateUtil.getDateText(comment.getCreateDate()))
43  				.append("</i>:<br>")
44  				.append(comment.getMessage().replace("\n", "<br>"));
45  		for (Comment versionedComment : comment.getReplies()) {
46  			s.append("<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>")
47  					.append(versionedComment.getAuthor().getDisplayName())
48  					.append("</b> replied <i> on ")
49  					.append(CommentDateUtil.getDateText(versionedComment.getCreateDate()))
50  					.append("</i>:<br>&nbsp;&nbsp;&nbsp;&nbsp;")
51  					.append(versionedComment.getMessage().replace("\n", "<br>"));
52  		}
53  		s.append("</html>");
54  		return s.toString();
55  	}
56  
57  /*	@Override
58  	public ActionGroup getPopupMenuActions() {
59  		final ActionManager actionManager = ActionManager.getInstance();
60  		DefaultActionGroup defaultactiongroup = new DefaultActionGroup();
61  
62  		if (checkIfAuthorized()) {
63  			ReplyAction reply = (ReplyAction) actionManager.getAction("ThePlugin.Crucible.Comment.Gutter.Reply");
64  			reply.setReview(review);
65  			reply.setFile(fileInfo);
66  			reply.setComment(comment);
67  			defaultactiongroup.add(reply);
68  		}
69  
70  		if (checkIfUserAnAuthor()) {
71  			EditAction edit = (EditAction) actionManager.getAction("ThePlugin.Crucible.Comment.Gutter.Edit");
72  			edit.setReview(review);
73  			edit.setFile(fileInfo);
74  			edit.setComment(comment);
75  			defaultactiongroup.add(edit);
76  		}
77  
78  		if (checkIfUserAnAuthor()) {
79  			RemoveAction removeAction = (RemoveAction) actionManager.getAction("ThePlugin.Crucible.Comment.Gutter.Remove");
80  			removeAction.setReview(review);
81  			removeAction.setFile(fileInfo);
82  			removeAction.setComment(comment);
83  			defaultactiongroup.add(removeAction);
84  		}
85  
86  		if (checkIfDraftAndAuthor()) {
87  			PublishAction publishAction = (PublishAction) actionManager.getAction("ThePlugin.Crucible.Comment.Gutter.Publish");
88  			publishAction.setReview(review);
89  			publishAction.setFile(fileInfo);
90  			publishAction.setComment(comment);
91  			defaultactiongroup.add(publishAction);
92  		}
93  		return defaultactiongroup;
94  	}*/
95  
96  	@Override
97  	public AnAction getClickAction() {
98  		return new ClickAction();
99  	}
100 
101 	public boolean isNavigateAction() {
102 		return true;
103 	}
104 
105 	private class ClickAction extends AnAction {
106 		public void actionPerformed(final AnActionEvent e) {
107 			CommentTooltipPanel lctp = new CommentTooltipPanelWithRunners(e, review, fileInfo, comment, null);
108             e.getPresentation().putClientProperty(CommentTooltipPanel.JBPOPUP_PARENT_COMPONENT, editor.getComponent());
109 
110             CommentTooltipPanel.showCommentTooltipPopup(e, lctp, lctp, null);
111         }
112 	}
113 
114 	protected boolean checkIfDraftAndAuthor() {
115 		return checkIfUserAnAuthor() && comment.isDraft();
116 	}
117 
118 	protected boolean checkIfUserAnAuthor() {
119 		return review.getServerData().getUserName().equals(comment.getAuthor().getUserName());
120 	}
121 
122 	protected boolean checkIfAuthorized() {
123 		if (review == null) {
124 			return false;
125 		}
126 		try {
127 			if (!review.getActions().contains(CrucibleAction.COMMENT)) {
128 				return false;
129 			}
130 		} catch (ValueNotYetInitialized valueNotYetInitialized) {
131 			return false;
132 		}
133 		return true;
134 	}
135 }
136