KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > nborgsourcebrowse > BrowseSubmenu


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.nborgsourcebrowse;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.regex.Matcher JavaDoc;
33 import java.util.regex.Pattern JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JMenu JavaDoc;
36 import javax.swing.JMenuItem JavaDoc;
37 import org.netbeans.api.project.FileOwnerQuery;
38 import org.netbeans.api.project.Project;
39 import org.netbeans.api.project.ProjectUtils;
40 import org.openide.awt.DynamicMenuContent;
41 import org.openide.awt.HtmlBrowser;
42 import org.openide.filesystems.FileObject;
43 import org.openide.loaders.DataObject;
44 import org.openide.util.Exceptions;
45 import org.openide.util.Utilities;
46 import org.openide.util.actions.Presenter;
47
48 /**
49  * Submenu active for nb.org files permitting you to browse to their sources, Javadoc, etc.
50  * XXX handle branches, tags, specific revision numbers...
51  * @author Jesse Glick
52  */

53 public class BrowseSubmenu implements Presenter.Menu {
54
55     /** Tuples of display label, URL format; use @FILEPATH@, @DIRPATH@, @JAVABASENAME@, @CNBDASHES@ */
56     private static final String JavaDoc[][] LINKS_FILE = {
57         {"Source (ViewCVS)", "http://www.netbeans.org/nonav/source/browse/~checkout~/@FILEPATH@?content-type=text/plain"},
58         {"Source (ViewCVS)", "http://www.netbeans.org/nonav/source/browse/@DIRPATH@/"},
59         {"Source (OpenGrok)", "http://deadlock.nbextras.org/opengrok/xref/@FILEPATH@"},
60         {"Source (OpenGrok)", "http://deadlock.nbextras.org/opengrok/xref/@DIRPATH@/"},
61         {"Source (Fisheye)", "http://deadlock.nbextras.org/fisheye/browse/~raw,r=HEAD/netbeans/@FILEPATH@"},
62         {"Source (Fisheye)", "http://deadlock.nbextras.org/fisheye/browse/netbeans/@DIRPATH@/"},
63         {"Source (Hudson trunk)", "http://deadlock.nbextras.org/hudson/job/trunk/ws/@FILEPATH@"},
64         {"Source (Hudson trunk)", "http://deadlock.nbextras.org/hudson/job/trunk/ws/@DIRPATH@/"},
65         {"Javadoc (official)", "http://www.netbeans.org/download/dev/javadoc/@CNBDASHES@/@JAVABASENAME@.html"},
66         {"Javadoc (Hudson javadoc-nbms)", "http://deadlock.nbextras.org/hudson/job/javadoc-nbms/javadoc/@CNBDASHES@/@JAVABASENAME@.html"},
67     };
68     private static final String JavaDoc[][] LINKS_PRJ = {
69         {"Javadoc (official)", "http://www.netbeans.org/download/dev/javadoc/@CNBDASHES@/"},
70         {"Javadoc (Hudson javadoc-nbms)", "http://deadlock.nbextras.org/hudson/job/javadoc-nbms/javadoc/@CNBDASHES@/"},
71     };
72
73     /** Default constructor for layer */
74     public BrowseSubmenu() {}
75
76     public JMenuItem JavaDoc getMenuPresenter() {
77         return new Menu JavaDoc();
78     }
79
80     private static final class Menu extends JMenu JavaDoc implements DynamicMenuContent {
81
82         public JComponent JavaDoc[] getMenuPresenters() {
83             List JavaDoc<String JavaDoc[]> labelsAndUrls = new ArrayList JavaDoc<String JavaDoc[]>();
84             DataObject d = Utilities.actionsGlobalContext().lookup(DataObject.class);
85             if (d != null) {
86                 FileObject f = d.getPrimaryFile();
87                 FileObject dir = f.isFolder() ? f : f.getParent();
88                 String JavaDoc root = slurp(dir.getFileObject("CVS/Root"));
89                 if (root != null && root.contains("netbeans.org")) {
90                     String JavaDoc repos = slurp(dir.getFileObject("CVS/Repository"));
91                     if (repos != null) {
92                         /* XXX branch handling not yet implemented... most of these viewers will not handle it well anyway
93                         String tag = slurp(dir.getFileObject("CVS/Tag"));
94                         String branch = (tag != null && tag.startsWith("T")) ? tag.substring(1) : null;
95                          */

96                         for (String JavaDoc[] data : LINKS_FILE) {
97                             String JavaDoc label = data[0];
98                             String JavaDoc url = data[1];
99                             if (url.contains("@FILEPATH@")) {
100                                 if (f.isData()) {
101                                     url = url.replace("@FILEPATH@", repos + "/" + f.getNameExt());
102                                 } else {
103                                     continue;
104                                 }
105                             }
106                             if (url.contains("@DIRPATH@")) {
107                                 if (f.isFolder()) {
108                                     url = url.replace("@DIRPATH@", repos);
109                                 } else {
110                                     continue;
111                                 }
112                             }
113                             if (url.contains("@JAVABASENAME@")) {
114                                 Matcher JavaDoc m = Pattern.compile(".*/src/(.+)\\.java").matcher(repos + "/" + f.getNameExt());
115                                 if (m.matches()) {
116                                     url = url.replace("@JAVABASENAME@", m.group(1));
117                                 } else {
118                                     continue;
119                                 }
120                             }
121                             if (url.contains("@CNBDASHES@")) {
122                                 Project p = FileOwnerQuery.getOwner(f);
123                                 if (p == null) {
124                                     continue;
125                                 } else {
126                                     url = url.replace("@CNBDASHES@", ProjectUtils.getInformation(p).getName().replace('.', '-'));
127                                 }
128                             }
129                             labelsAndUrls.add(new String JavaDoc[] {label, url});
130                         }
131                     }
132                 }
133             } else {
134                 Project p = Utilities.actionsGlobalContext().lookup(Project.class);
135                 if (p != null) {
136                     for (String JavaDoc[] data : LINKS_PRJ) {
137                         String JavaDoc label = data[0];
138                         String JavaDoc url = data[1];
139                         labelsAndUrls.add(new String JavaDoc[] {data[0], data[1].replace("@CNBDASHES@", ProjectUtils.getInformation(p).getName().replace('.', '-'))});
140                     }
141                 }
142             }
143             if (labelsAndUrls.isEmpty()) {
144                 return new JComponent JavaDoc[0];
145             } else {
146                 JMenu JavaDoc menu = new JMenu JavaDoc("Browse cvs.netbeans.org");
147                 for (String JavaDoc[] labelAndUrl : labelsAndUrls) {
148                     JMenuItem JavaDoc mi = new JMenuItem JavaDoc(labelAndUrl[0]);
149                     final URL JavaDoc u;
150                     try {
151                         u = new URL JavaDoc(labelAndUrl[1]);
152                     } catch (MalformedURLException JavaDoc x) {
153                         throw new AssertionError JavaDoc(x);
154                     }
155                     mi.addActionListener(new ActionListener JavaDoc() {
156                         public void actionPerformed(ActionEvent JavaDoc ev) {
157                             HtmlBrowser.URLDisplayer.getDefault().showURL(u);
158                         }
159                     });
160                     menu.add(mi);
161                 }
162                 return new JComponent JavaDoc[] {menu};
163             }
164         }
165
166         private static String JavaDoc slurp(FileObject f) {
167             if (f != null) {
168                 try {
169                     InputStream JavaDoc is = f.getInputStream();
170                     try {
171                         BufferedReader JavaDoc r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
172                         return r.readLine();
173                     } finally {
174                         is.close();
175                     }
176                 } catch (IOException JavaDoc x) {
177                     Exceptions.printStackTrace(x);
178                 }
179             }
180             return null;
181         }
182
183         public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
184             return getMenuPresenters();
185         }
186
187     }
188
189 }
190
Popular Tags