View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.atlassian.theplugin.idea.action.bamboo.changes;
18  
19  import com.atlassian.theplugin.idea.IdeaHelper;
20  import com.atlassian.theplugin.idea.VcsIdeaHelper;
21  import com.atlassian.theplugin.idea.ui.tree.file.BambooFileNode;
22  import com.intellij.openapi.actionSystem.AnActionEvent;
23  import com.intellij.openapi.diff.*;
24  import com.intellij.openapi.editor.Document;
25  import com.intellij.openapi.fileEditor.OpenFileDescriptor;
26  import com.intellij.openapi.project.Project;
27  import com.intellij.openapi.ui.Messages;
28  import com.intellij.openapi.vcs.VcsBundle;
29  import com.intellij.openapi.vcs.history.VcsRevisionNumber;
30  import com.intellij.openapi.vfs.VirtualFile;
31  import com.intellij.psi.PsiFile;
32  
33  public class ShowDiffAction extends AbstractBambooFileActions {
34  
35  	public void showRevisionDiff(final Project project, final BambooFileNode bambooFileNode) {
36  		final PsiFile psiFile = bambooFileNode.getPsiFile();
37  		if (psiFile == null) {
38  			Messages.showErrorDialog(project, "Cannot find corresponding file in the project.", "Problem");
39  			return;
40  		}
41  		final VirtualFile virtualFile = psiFile.getVirtualFile();
42  		if (virtualFile == null) {
43  			Messages.showErrorDialog(project, "PsiFile has not corresponding VirtualFile.", "Problem");
44  			return;
45  		}
46  		final VcsRevisionNumber currentRevisionNumber = VcsIdeaHelper.getVcsRevisionNumber(project, virtualFile);
47  		if (currentRevisionNumber == null) {
48  			Messages.showErrorDialog(project, "Cannot determine current version of file ["
49  					+ psiFile.getName() + "] in the project.", "Problem");
50  			return;
51  		}
52  
53  		VcsIdeaHelper.openFile(project, virtualFile, bambooFileNode.getRevision(), 1, 1,
54  				new VcsIdeaHelper.OpenFileDescriptorAction() {
55  
56  					public boolean shouldNavigate() {
57  						return false;
58  					}
59  
60  					public void run(OpenFileDescriptor ofd) {
61  
62  						final Document displayDocument = new FileContent(project, ofd.getFile()).getDocument();
63  						final Document referenceDocument = new FileContent(project, virtualFile).getDocument();
64  
65  						DiffRequest request = new DiffRequest(project) {
66  
67  							@Override
68  							public DiffContent[] getContents() {
69  								return (new DiffContent[]{
70  										new DocumentContent(project, displayDocument),
71  										new DocumentContent(project, referenceDocument),
72  								});
73  							}
74  
75  							@Override
76  							public String[] getContentTitles() {
77  								return (new String[]{
78  										VcsBundle.message("diff.content.title.repository.version",
79  												bambooFileNode.getRevision()), "Your version"
80  								});
81  							}
82  
83  							@Override
84  							public String getWindowTitle() {
85  								return "Diff between revisions " + bambooFileNode.getRevision() + " and "
86  										+ currentRevisionNumber.asString() + " of file " + psiFile.getName();
87  							}
88  						};
89  						DiffManager.getInstance().getDiffTool().show(request);
90  					}
91  				});
92  	}
93  
94  
95  	public void actionPerformed(AnActionEvent event) {
96  		showRevisionDiff(IdeaHelper.getCurrentProject(event), getBambooFileNode(event));
97  
98  	}
99  
100 }