KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > blame > BlameAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.subversion.ui.blame;
21
22 import org.netbeans.modules.subversion.ui.actions.ContextAction;
23 import org.netbeans.modules.subversion.FileInformation;
24 import org.netbeans.modules.subversion.Subversion;
25 import org.netbeans.modules.subversion.util.SvnUtils;
26 import org.netbeans.modules.subversion.client.SvnClient;
27 import org.netbeans.modules.subversion.client.SvnProgressSupport;
28 import org.openide.nodes.Node;
29 import org.openide.cookies.EditorCookie;
30 import org.openide.util.NbBundle;
31 import org.openide.util.RequestProcessor;
32 import org.openide.ErrorManager;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.loaders.DataObject;
36 import org.openide.windows.WindowManager;
37 import org.tigris.subversion.svnclientadapter.*;
38
39 import javax.swing.*;
40 import java.io.File JavaDoc;
41 import java.util.*;
42
43 /**
44  *
45  * @author Maros Sandor
46  */

47 public class BlameAction extends ContextAction {
48     
49     protected String JavaDoc getBaseName(Node [] activatedNodes) {
50         if (visible(activatedNodes)) {
51             return "CTL_MenuItem_HideAnnotations"; // NOI18N
52
} else {
53             return "CTL_MenuItem_ShowAnnotations"; // NOI18N
54
}
55     }
56
57     public boolean enable(Node[] nodes) {
58         return super.enable(nodes) && activatedEditorCookie(nodes) != null;
59     }
60
61     protected int getFileEnabledStatus() {
62         return FileInformation.STATUS_IN_REPOSITORY;
63     }
64
65     protected int getDirectoryEnabledStatus() {
66         return 0;
67     }
68
69     protected boolean asynchronous() {
70         return false;
71     }
72
73     protected void performContextAction(Node[] nodes) {
74         if (visible(nodes)) {
75             JEditorPane pane = activatedEditorPane(nodes);
76             AnnotationBarManager.hideAnnotationBar(pane);
77         } else {
78             EditorCookie ec = activatedEditorCookie(nodes);
79             if (ec == null) return;
80             
81             final File JavaDoc file = activatedFile(nodes);
82
83             JEditorPane[] panes = ec.getOpenedPanes();
84             if (panes == null) {
85                 ec.open();
86             }
87             panes = ec.getOpenedPanes();
88             if (panes == null) {
89                 return;
90             }
91             final JEditorPane currentPane = panes[0];
92             
93             final AnnotationBar ab = AnnotationBarManager.showAnnotationBar(currentPane);
94             ab.setAnnotationMessage(NbBundle.getMessage(BlameAction.class, "CTL_AnnotationSubstitute")); // NOI18N;
95

96             SVNUrl repository = SvnUtils.getRepositoryRootUrl(file);
97             RequestProcessor rp = Subversion.getInstance().getRequestProcessor(repository);
98             SvnProgressSupport support = new SvnProgressSupport() {
99                 public void perform() {
100                     computeAnnotations(file, this, ab);
101                 }
102             };
103             support.start(rp, repository, NbBundle.getMessage(BlameAction.class, "MSG_Annotation_Progress")); // NOI18N
104
}
105     }
106
107     private void computeAnnotations(File JavaDoc file, SvnProgressSupport progress, AnnotationBar ab) {
108         SvnClient client;
109         try {
110             client = Subversion.getInstance().getClient(file, progress);
111         } catch (SVNClientException ex) {
112             ab.setAnnotationMessage(NbBundle.getMessage(BlameAction.class, "CTL_AnnotationFailed")); // NOI18N;
113
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
114             return;
115         }
116
117         ISVNAnnotations annotations;
118         try {
119             annotations = client.annotate(file, new SVNRevision.Number(1), SVNRevision.BASE);
120         } catch (SVNClientException e) {
121             ab.setAnnotationMessage(NbBundle.getMessage(BlameAction.class, "CTL_AnnotationFailed")); // NOI18N;
122
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
123             return;
124         }
125         if (progress.isCanceled()) {
126             ab.setAnnotationMessage(NbBundle.getMessage(BlameAction.class, "CTL_AnnotationFailed")); // NOI18N;
127
return;
128         }
129         AnnotateLine [] lines = toAnnotateLines(annotations);
130         ab.annotationLines(file, Arrays.asList(lines));
131         
132         // fetch log messages
133
ISVNLogMessage [] logs;
134         try {
135             logs = client.getLogMessages(file, new SVNRevision.Number(1), SVNRevision.BASE, false, false);
136         } catch (SVNClientException e) {
137             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
138             return;
139         }
140         if (progress.isCanceled()) {
141             return;
142         }
143         fillCommitMessages(lines, logs);
144     }
145
146     private static void fillCommitMessages(AnnotateLine [] annotations, ISVNLogMessage[] logs) {
147         long lowestRevisionNumber = Long.MAX_VALUE;
148         for (int i = 0; i < annotations.length; i++) {
149             AnnotateLine annotation = annotations[i];
150             for (int j = 0; j < logs.length; j++) {
151                 ISVNLogMessage log = logs[j];
152                 if (log.getRevision().getNumber() < lowestRevisionNumber) {
153                     lowestRevisionNumber = log.getRevision().getNumber();
154                 }
155                 if (annotation.getRevision().equals(log.getRevision().toString())) {
156                     annotation.setDate(log.getDate());
157                     annotation.setCommitMessage(log.getMessage());
158                 }
159             }
160         }
161         String JavaDoc lowestRev = Long.toString(lowestRevisionNumber);
162         for (int i = 0; i < annotations.length; i++) {
163             AnnotateLine annotation = annotations[i];
164             annotation.setCanBeRolledBack(!annotation.getRevision().equals(lowestRev));
165         }
166     }
167
168     private static AnnotateLine [] toAnnotateLines(ISVNAnnotations annotations) {
169         AnnotateLine [] lines = new AnnotateLine[annotations.numberOfLines()];
170         int n = annotations.numberOfLines();
171         for (int i = 0; i < n; i++) {
172             lines[i] = new AnnotateLine();
173             lines[i].setAuthor(annotations.getAuthor(i));
174             lines[i].setContent(annotations.getLine(i));
175             lines[i].setLineNum(i + 1);
176             lines[i].setRevision(Long.toString(annotations.getRevision(i)));
177             lines[i].setDate(annotations.getChanged(i));
178         }
179         return lines;
180     }
181
182     /**
183      * @param nodes or null (then taken from windowsystem, it may be wrong on editor tabs #66700).
184      */

185     public boolean visible(Node[] nodes) {
186         JEditorPane currentPane = activatedEditorPane(nodes);
187         return AnnotationBarManager.annotationBarVisible(currentPane);
188     }
189
190     /**
191      * @return active editor pane or null if selected node
192      * does not have any or more nodes selected.
193      */

194     private JEditorPane activatedEditorPane(Node[] nodes) {
195         EditorCookie ec = activatedEditorCookie(nodes);
196         if (ec != null) {
197             JEditorPane[] panes = ec.getOpenedPanes();
198             if (panes != null && panes.length > 0) {
199                 return panes[0];
200             }
201         }
202         return null;
203     }
204
205     private EditorCookie activatedEditorCookie(Node[] nodes) {
206         if (nodes == null) {
207             nodes = WindowManager.getDefault().getRegistry().getActivatedNodes();
208         }
209         if (nodes.length == 1) {
210             Node node = nodes[0];
211             return (EditorCookie) node.getCookie(EditorCookie.class);
212         }
213         return null;
214     }
215
216     private File JavaDoc activatedFile(Node[] nodes) {
217         if (nodes.length == 1) {
218             Node node = nodes[0];
219             DataObject dobj = (DataObject) node.getCookie(DataObject.class);
220             if (dobj != null) {
221                 FileObject fo = dobj.getPrimaryFile();
222                 return FileUtil.toFile(fo);
223             }
224         }
225         return null;
226     }
227     
228 }
229
Popular Tags