1 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 ; 41 import java.util.*; 42 43 47 public class BlameAction extends ContextAction { 48 49 protected String getBaseName(Node [] activatedNodes) { 50 if (visible(activatedNodes)) { 51 return "CTL_MenuItem_HideAnnotations"; } else { 53 return "CTL_MenuItem_ShowAnnotations"; } 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 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")); 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")); } 105 } 106 107 private void computeAnnotations(File 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")); 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")); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 123 return; 124 } 125 if (progress.isCanceled()) { 126 ab.setAnnotationMessage(NbBundle.getMessage(BlameAction.class, "CTL_AnnotationFailed")); return; 128 } 129 AnnotateLine [] lines = toAnnotateLines(annotations); 130 ab.annotationLines(file, Arrays.asList(lines)); 131 132 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 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 185 public boolean visible(Node[] nodes) { 186 JEditorPane currentPane = activatedEditorPane(nodes); 187 return AnnotationBarManager.annotationBarVisible(currentPane); 188 } 189 190 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 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 |