1 19 20 package org.netbeans.modules.nborgsourcebrowse; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.io.BufferedReader ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 import java.util.regex.Matcher ; 33 import java.util.regex.Pattern ; 34 import javax.swing.JComponent ; 35 import javax.swing.JMenu ; 36 import javax.swing.JMenuItem ; 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 53 public class BrowseSubmenu implements Presenter.Menu { 54 55 56 private static final String [][] 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 [][] 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 74 public BrowseSubmenu() {} 75 76 public JMenuItem getMenuPresenter() { 77 return new Menu (); 78 } 79 80 private static final class Menu extends JMenu implements DynamicMenuContent { 81 82 public JComponent [] getMenuPresenters() { 83 List <String []> labelsAndUrls = new ArrayList <String []>(); 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 root = slurp(dir.getFileObject("CVS/Root")); 89 if (root != null && root.contains("netbeans.org")) { 90 String repos = slurp(dir.getFileObject("CVS/Repository")); 91 if (repos != null) { 92 96 for (String [] data : LINKS_FILE) { 97 String label = data[0]; 98 String 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 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 [] {label, url}); 130 } 131 } 132 } 133 } else { 134 Project p = Utilities.actionsGlobalContext().lookup(Project.class); 135 if (p != null) { 136 for (String [] data : LINKS_PRJ) { 137 String label = data[0]; 138 String url = data[1]; 139 labelsAndUrls.add(new String [] {data[0], data[1].replace("@CNBDASHES@", ProjectUtils.getInformation(p).getName().replace('.', '-'))}); 140 } 141 } 142 } 143 if (labelsAndUrls.isEmpty()) { 144 return new JComponent [0]; 145 } else { 146 JMenu menu = new JMenu ("Browse cvs.netbeans.org"); 147 for (String [] labelAndUrl : labelsAndUrls) { 148 JMenuItem mi = new JMenuItem (labelAndUrl[0]); 149 final URL u; 150 try { 151 u = new URL (labelAndUrl[1]); 152 } catch (MalformedURLException x) { 153 throw new AssertionError (x); 154 } 155 mi.addActionListener(new ActionListener () { 156 public void actionPerformed(ActionEvent ev) { 157 HtmlBrowser.URLDisplayer.getDefault().showURL(u); 158 } 159 }); 160 menu.add(mi); 161 } 162 return new JComponent [] {menu}; 163 } 164 } 165 166 private static String slurp(FileObject f) { 167 if (f != null) { 168 try { 169 InputStream is = f.getInputStream(); 170 try { 171 BufferedReader r = new BufferedReader (new InputStreamReader (is)); 172 return r.readLine(); 173 } finally { 174 is.close(); 175 } 176 } catch (IOException x) { 177 Exceptions.printStackTrace(x); 178 } 179 } 180 return null; 181 } 182 183 public JComponent [] synchMenuPresenters(JComponent [] items) { 184 return getMenuPresenters(); 185 } 186 187 } 188 189 } 190 | Popular Tags |