KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > log > AnnotationsAction


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.versioning.system.cvss.ui.actions.log;
21
22 import org.openide.filesystems.FileObject;
23 import org.openide.filesystems.FileUtil;
24 import org.openide.loaders.DataObject;
25 import org.openide.ErrorManager;
26 import org.openide.util.Cancellable;
27 import org.openide.nodes.Node;
28 import org.openide.util.NbBundle;
29 import org.openide.windows.WindowManager;
30 import org.openide.cookies.EditorCookie;
31 import org.netbeans.modules.versioning.system.cvss.ui.actions.AbstractSystemAction;
32 import org.netbeans.modules.versioning.system.cvss.ui.actions.annotate.AnnotationBarManager;
33 import org.netbeans.modules.versioning.system.cvss.FileInformation;
34 import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem;
35 import org.netbeans.modules.versioning.system.cvss.ExecutorGroup;
36 import org.netbeans.lib.cvsclient.command.annotate.AnnotateCommand;
37 import org.netbeans.lib.cvsclient.command.log.LogCommand;
38 import org.netbeans.lib.cvsclient.admin.Entry;
39 import org.netbeans.lib.cvsclient.admin.AdminHandler;
40
41 import javax.swing.*;
42 import java.awt.event.ActionEvent JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 /**
47  * Show/Hide Annotations action. It's enabled for single
48  * node selections only.
49  *
50  * @author Petr Kuzel
51  */

52 public class AnnotationsAction extends AbstractSystemAction {
53
54     protected String JavaDoc getBaseName(Node [] activatedNodes) {
55         if (visible(activatedNodes)) {
56             return "CTL_MenuItem_HideAnnotations"; // NOI18N
57
} else {
58             return "CTL_MenuItem_Annotations"; // NOI18N
59
}
60     }
61
62     public boolean enable(Node[] nodes) {
63         return super.enable(nodes) && activatedEditorCookie(nodes) != null;
64     }
65
66     protected int getFileEnabledStatus() {
67         return FileInformation.STATUS_IN_REPOSITORY;
68     }
69
70     protected int getDirectoryEnabledStatus() {
71         return 0;
72     }
73
74     protected boolean asynchronous() {
75         return false;
76     }
77
78     public void performCvsAction(Node[] nodes) {
79         if (visible(nodes)) {
80             JEditorPane pane = activatedEditorPane(nodes);
81             AnnotationBarManager.hideAnnotationBar(pane);
82         } else {
83             EditorCookie ec = activatedEditorCookie(nodes);
84             if (ec != null) {
85                 File JavaDoc file = activatedFile(nodes);
86                 CvsVersioningSystem cvss = CvsVersioningSystem.getInstance();
87                 AdminHandler entries = cvss.getAdminHandler();
88
89                 JEditorPane[] panes = ec.getOpenedPanes();
90                 if (panes == null) {
91                     ec.open();
92                 }
93                 panes = ec.getOpenedPanes();
94                 if (panes == null) {
95                     return;
96                 }
97                 final JEditorPane currentPane = panes[0];
98                 LogOutputListener ab = AnnotationBarManager.showAnnotationBar(currentPane);
99
100                 AnnotateCommand annotate = new AnnotateCommand();
101
102                 try {
103                     Entry entry = entries.getEntry(file);
104                     if (entry == null) {
105                         return;
106                     }
107                     String JavaDoc revision = entry.getRevision();
108                     annotate.setAnnotateByRevision(revision);
109                     File JavaDoc[] cmdFiles = new File JavaDoc[] {file};
110                     annotate.setFiles(cmdFiles);
111
112                     ExecutorGroup group = new ExecutorGroup(NbBundle.getMessage(AnnotationsAction.class, "BK0001"));
113
114                     AnnotationsExecutor executor = new AnnotationsExecutor(cvss, annotate);
115                     executor.addLogOutputListener(ab);
116                     group.addExecutor(executor);
117
118                     // get commit message sfrom log
119

120                     LogCommand log = new LogCommand();
121                     log.setFiles(cmdFiles);
122                     log.setNoTags(true);
123
124                     LogExecutor lexecutor = new LogExecutor(cvss, log);
125                     lexecutor.addLogOutputListener(ab);
126                     group.addExecutor(lexecutor);
127
128                     group.addCancellable(new Cancellable(){
129                         public boolean cancel() {
130                             AnnotationBarManager.hideAnnotationBar(currentPane);
131                             return true;
132                         }
133                     });
134
135                     group.execute();
136                 } catch (IOException JavaDoc e) {
137                     ErrorManager err = ErrorManager.getDefault();
138                     err.annotate(e, NbBundle.getMessage(AnnotationsAction.class, "BK0002", file));
139                     err.notify(e);
140                 }
141             }
142         }
143     }
144
145     /**
146      * @param nodes or null (then taken from windowsystem, it may be wrong on editor tabs #66700).
147      */

148     public boolean visible(Node[] nodes) {
149         JEditorPane currentPane = activatedEditorPane(nodes);
150         return AnnotationBarManager.annotationBarVisible(currentPane);
151     }
152
153     /**
154      * @return active editor pane or null if selected node
155      * does not have any or more nodes selected.
156      */

157     private JEditorPane activatedEditorPane(Node[] nodes) {
158         EditorCookie ec = activatedEditorCookie(nodes);
159         if (ec != null) {
160             JEditorPane[] panes = ec.getOpenedPanes();
161             if (panes != null && panes.length > 0) {
162                 return panes[0];
163             }
164         }
165         return null;
166     }
167
168     private EditorCookie activatedEditorCookie(Node[] nodes) {
169         if (nodes == null) {
170             nodes = WindowManager.getDefault().getRegistry().getActivatedNodes();
171         }
172         if (nodes.length == 1) {
173             Node node = nodes[0];
174             return (EditorCookie) node.getCookie(EditorCookie.class);
175         }
176         return null;
177     }
178
179     private File JavaDoc activatedFile(Node[] nodes) {
180         if (nodes.length == 1) {
181             Node node = nodes[0];
182             DataObject dobj = (DataObject) node.getCookie(DataObject.class);
183             if (dobj != null) {
184                 FileObject fo = dobj.getPrimaryFile();
185                 return FileUtil.toFile(fo);
186             }
187         }
188         return null;
189
190     }
191 }
192
Popular Tags