1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.atlassian.theplugin.idea.crucible;
18
19 import com.atlassian.theplugin.commons.crucible.CrucibleServerFacadeImpl;
20 import com.atlassian.theplugin.commons.crucible.ValueNotYetInitialized;
21 import com.atlassian.theplugin.commons.crucible.api.model.CommitType;
22 import com.atlassian.theplugin.commons.crucible.api.model.CrucibleFileInfo;
23 import com.atlassian.theplugin.commons.crucible.api.model.CustomFieldDef;
24 import com.atlassian.theplugin.commons.crucible.api.model.VersionedComment;
25 import com.atlassian.theplugin.commons.exception.ServerPasswordNotProvidedException;
26 import com.atlassian.theplugin.commons.remoteapi.RemoteApiException;
27 import com.atlassian.theplugin.idea.IdeaHelper;
28 import com.atlassian.theplugin.idea.VcsIdeaHelper;
29 import com.atlassian.theplugin.util.PluginUtil;
30 import com.intellij.openapi.diff.*;
31 import com.intellij.openapi.editor.Document;
32 import com.intellij.openapi.editor.Editor;
33 import com.intellij.openapi.editor.EditorFactory;
34 import com.intellij.openapi.fileEditor.FileEditorManager;
35 import com.intellij.openapi.fileEditor.OpenFileDescriptor;
36 import com.intellij.openapi.project.Project;
37 import com.intellij.openapi.vcs.VcsBundle;
38 import com.intellij.openapi.vfs.VirtualFile;
39 import org.jetbrains.annotations.NotNull;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44 public final class CrucibleHelper {
45
46
47 private CrucibleHelper() {
48 }
49
50
51
52
53
54
55
56
57
58
59
60 public static void showVirtualFileWithComments(final Project project,
61 final ReviewData review,
62 final CrucibleFileInfo reviewItem) {
63
64 int line = 1;
65
66 java.util.List<VersionedComment> fileComments;
67 try {
68 fileComments = reviewItem.getVersionedComments();
69 } catch (ValueNotYetInitialized valueNotYetInitialized) {
70 try {
71 fileComments = CrucibleServerFacadeImpl.getInstance()
72 .getVersionedComments(review.getServer(), review.getPermId(), reviewItem.getPermId());
73 } catch (RemoteApiException e) {
74 PluginUtil.getLogger().error(e.getMessage());
75 return;
76 } catch (ServerPasswordNotProvidedException e) {
77 PluginUtil.getLogger().error(e.getMessage());
78 return;
79 }
80 }
81
82 if (!fileComments.isEmpty()) {
83 line = fileComments.iterator().next().getFromStartLine();
84 }
85
86 VcsIdeaHelper.openFileWithDiffs(project
87 , reviewItem.getFileDescriptor().getAbsoluteUrl()
88 , reviewItem.getOldFileDescriptor().getRevision()
89 , reviewItem.getFileDescriptor().getRevision()
90 , reviewItem.getCommitType()
91 , line
92 , 1
93 , new VcsIdeaHelper.OpenDiffAction() {
94
95 public void run(OpenFileDescriptor displayFile, VirtualFile referenceFile, CommitType commitType) {
96 FileEditorManager fem = FileEditorManager.getInstance(project);
97 Editor editor = fem.openTextEditor(displayFile, true);
98 if (editor == null) {
99 return;
100 }
101
102 Document displayDocument = null;
103 Document referenceDocument = null;
104 switch (commitType) {
105 case Moved:
106 case Copied:
107 case Modified:
108 displayDocument = new FileContent(project, displayFile.getFile())
109 .getDocument();
110 referenceDocument = new FileContent(project, referenceFile).getDocument();
111 ChangeViewer.highlightChangesInEditor(project, editor, referenceDocument, displayDocument
112 , reviewItem.getOldFileDescriptor().getRevision()
113 , reviewItem.getFileDescriptor().getRevision());
114 break;
115 case Added:
116 break;
117 case Deleted:
118 break;
119 default:
120 break;
121 }
122 CommentHighlighter.highlightCommentsInEditor(project, editor, review, reviewItem);
123 }
124 });
125 }
126
127 public static void showRevisionDiff(final Project project, final CrucibleFileInfo reviewItem) {
128
129 VcsIdeaHelper.openFileWithDiffs(project
130 , reviewItem.getFileDescriptor().getAbsoluteUrl()
131 , reviewItem.getOldFileDescriptor().getRevision()
132 , reviewItem.getFileDescriptor().getRevision()
133 , reviewItem.getCommitType()
134 , 1
135 , 1
136 , new VcsIdeaHelper.OpenDiffAction() {
137
138 public void run(OpenFileDescriptor displayFile, VirtualFile referenceFile, CommitType commitType) {
139
140 final Document displayDocument = new FileContent(project, displayFile.getFile()).getDocument();
141 final Document referenceDocument = new FileContent(project, referenceFile).getDocument();
142
143 DiffRequest request = new DiffRequest(project) {
144
145 public DiffContent[] getContents() {
146 return (new DiffContent[]{
147 new DocumentContent(project, referenceDocument),
148 new DocumentContent(project, displayDocument),
149 });
150 }
151
152 public String[] getContentTitles() {
153 return (new String[]{
154 VcsBundle.message("diff.content.title.repository.version",
155 new Object[]{reviewItem.getOldFileDescriptor().getRevision()}),
156 VcsBundle.message("diff.content.title.repository.version",
157 new Object[]{reviewItem.getFileDescriptor().getRevision()})
158 });
159 }
160
161 public String getWindowTitle() {
162 return reviewItem.getFileDescriptor().getAbsoluteUrl();
163 }
164 };
165 DiffManager.getInstance().getDiffTool().show(request);
166 }
167 });
168 }
169
170 public static List<CustomFieldDef> getMetricsForReview(@NotNull final Project project, @NotNull final ReviewData review) {
171 java.util.List<CustomFieldDef> metrics = new ArrayList<CustomFieldDef>();
172 try {
173 metrics = CrucibleServerFacadeImpl.getInstance()
174 .getMetrics(review.getServer(), review.getMetricsVersion());
175 } catch (RemoteApiException e) {
176 IdeaHelper.handleRemoteApiException(project, e);
177 } catch (ServerPasswordNotProvidedException e) {
178 IdeaHelper.handleMissingPassword(e);
179 }
180 return metrics;
181 }
182
183 public static Editor getEditorForCrucibleFile(ReviewData review, CrucibleFileInfo file) {
184 Editor[] editors = EditorFactory.getInstance().getAllEditors();
185 for (Editor editor : editors) {
186 final ReviewData mr = editor.getUserData(CommentHighlighter.REVIEW_DATA_KEY);
187 final CrucibleFileInfo mf = editor.getUserData(CommentHighlighter.REVIEWITEM_DATA_KEY);
188 if (mr != null && mf != null) {
189 if (review.getPermId().equals(mr.getPermId()) && file.getPermId().equals(mf.getPermId())) {
190 return editor;
191 }
192 }
193 }
194 return null;
195 }
196
197 public static void openFileOnComment(final Project project, final ReviewData review, final CrucibleFileInfo file,
198 final VersionedComment comment) {
199 VcsIdeaHelper.openFileWithDiffs(project
200 , file.getFileDescriptor().getAbsoluteUrl()
201 , file.getOldFileDescriptor().getRevision()
202 , file.getFileDescriptor().getRevision()
203 , file.getCommitType()
204 , comment.getToStartLine() - 1
205 , 0
206 , new VcsIdeaHelper.OpenDiffAction() {
207
208 public void run(OpenFileDescriptor displayFile, VirtualFile referenceFile, CommitType commitType) {
209 FileEditorManager fem = FileEditorManager.getInstance(project);
210
211 if (displayFile != null) {
212 Editor editor = fem.openTextEditor(displayFile, false);
213 if (editor == null) {
214 return;
215 }
216 CommentHighlighter.highlightCommentsInEditor(project, editor, review, file);
217 }
218 }
219 });
220 }
221 }