1 19 20 package org.netbeans.modules.ruby.rubyproject.ui; 21 22 import java.net.URL ; 23 import java.net.MalformedURLException ; 24 import java.text.MessageFormat ; 25 import org.openide.ErrorManager; 26 import org.openide.awt.HtmlBrowser; 27 import org.openide.awt.StatusDisplayer; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.URLMapper; 30 import org.openide.nodes.Node; 31 import org.openide.util.NbBundle; 32 import org.openide.util.HelpCtx; 33 import org.openide.util.actions.NodeAction; 34 35 36 46 final class ShowRDocAction extends NodeAction { 47 48 53 public static interface RDocProvider { 54 55 59 public abstract boolean hasRDoc (); 60 61 64 public abstract void showRDoc (); 65 } 66 67 protected void performAction(Node[] activatedNodes) { 68 if (activatedNodes.length!=1) { 69 return; 70 } 71 RDocProvider jd = (RDocProvider) activatedNodes[0].getLookup().lookup(RDocProvider.class); 72 if (jd == null) { 73 return; 74 } 75 jd.showRDoc(); 76 } 77 78 protected boolean enable(Node[] activatedNodes) { 79 if (activatedNodes.length!=1) { 80 return false; 81 } 82 RDocProvider jd = (RDocProvider) activatedNodes[0].getLookup().lookup(RDocProvider.class); 83 if (jd == null) { 84 return false; 85 } 86 return jd.hasRDoc(); 87 } 88 89 public final String getName() { 90 return NbBundle.getMessage(ShowRDocAction.class,"CTL_ShowRDoc"); 91 } 92 93 public final HelpCtx getHelpCtx() { 94 return new HelpCtx (ShowRDocAction.class); 95 } 96 97 public final boolean asynchronous () { 98 return false; 99 } 100 101 107 static void showRDoc (URL rdoc, String displayName) { 108 if (rdoc!=null) { 109 HtmlBrowser.URLDisplayer.getDefault().showURL(rdoc); 110 } 111 else { 112 StatusDisplayer.getDefault().setStatusText(MessageFormat.format(NbBundle.getMessage(ShowRDocAction.class, 113 "TXT_NoRDoc"), new Object [] {displayName})); } 115 } 116 117 123 static URL findRDoc (String resource, URL urls[]) { 124 for (int i=0; i<urls.length; i++) { 125 String base = urls[i].toExternalForm(); 126 if (!base.endsWith("/")) { base+="/"; } 129 try { 130 URL u = new URL (base+resource); 131 FileObject fo = URLMapper.findFileObject(u); 132 if (fo != null) { 133 return u; 134 } 135 } catch (MalformedURLException ex) { 136 ErrorManager.getDefault().log(ErrorManager.ERROR, "Cannot create URL for "+base+resource+". "+ex.toString()); continue; 138 } 139 } 140 return null; 141 } 142 } 143 | Popular Tags |