1 19 20 21 package org.netbeans.modules.url; 22 23 24 import java.io.*; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import org.openide.DialogDisplayer; 28 29 import org.openide.cookies.EditCookie; 30 import org.openide.cookies.InstanceCookie; 31 import org.openide.cookies.OpenCookie; 32 import org.openide.filesystems.FileLock; 33 import org.openide.filesystems.FileObject; 34 import org.openide.loaders.*; 35 import org.openide.nodes.*; 36 import org.openide.NotifyDescriptor; 37 import org.openide.ErrorManager; 38 import org.openide.util.HelpCtx; 39 import org.openide.util.NbBundle; 40 41 42 47 public class URLDataObject extends MultiDataObject 48 implements OpenCookie, InstanceCookie { 49 50 51 static final String PROP_URL = "url"; 53 54 static final long serialVersionUID = 6829522922370124627L; 55 56 57 64 public URLDataObject(final FileObject file, MultiFileLoader loader) 65 throws DataObjectExistsException { 66 super(file, loader); 67 getCookieSet().add(this); 68 } 69 70 75 76 86 String getURLString() { 87 FileObject urlFile = getPrimaryFile(); 88 if (!urlFile.isValid()) { 89 return null; 90 } 91 String urlString = null; 92 93 InputStream is = null; 94 try { 95 is = urlFile.getInputStream(); 96 urlString = new BufferedReader(new InputStreamReader(is)) 97 .readLine(); 98 } catch (FileNotFoundException fne) { 99 ErrorManager.getDefault().notify(ErrorManager.WARNING, fne); 100 return null; 101 } catch (IOException ioe) { 102 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); 103 return null; 104 } finally { 105 if (is != null) { 106 try { 107 is.close (); 108 } catch (IOException e) { 109 ErrorManager.getDefault().notify( 110 ErrorManager.INFORMATIONAL, e); 111 } 112 } 113 } 114 115 if (urlString == null) { 116 120 urlString = ""; } 122 return urlString; 123 } 124 125 130 void setURLString(String newUrlString) { 131 FileObject urlFile = getPrimaryFile(); 132 if (!urlFile.isValid()) { 133 return; 134 } 135 FileLock lock = null; 136 try { 137 lock = urlFile.lock(); 138 OutputStream os = urlFile.getOutputStream(lock); 139 os.write(newUrlString.getBytes()); 140 os.close(); 141 } catch (IOException ioe) { 142 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); 143 } finally { 144 if (lock != null) { 145 lock.releaseLock(); 146 } 147 } 148 } 149 150 151 public HelpCtx getHelpCtx () { 152 return new HelpCtx(URLDataObject.class); 153 } 154 155 156 public void open() { 157 String urlString = getURLString(); 158 if (urlString == null) { 159 return; 160 } 161 URL url = getURLFromString(urlString); 162 if (url == null) { 163 return; 164 } 165 org.openide.awt.HtmlBrowser.URLDisplayer.getDefault().showURL(url); 166 } 167 168 176 private static URL getURLFromString(String urlString) { 177 try { 178 return new URL (urlString); 179 } catch (MalformedURLException mue1) { 180 } 181 182 183 184 try { 185 return new URL ("http://" + urlString); } catch (MalformedURLException mue1) { 187 } 188 189 190 String msg; 191 if (urlString.length() > 50) { msg = NbBundle.getMessage(URLDataObject.class, 193 "MSG_MalformedURLError"); } else { 195 msg = NbBundle.getMessage(URLDataObject.class, 196 "MSG_FMT_MalformedURLError", urlString); 198 } 199 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 200 msg, 201 NotifyDescriptor.ERROR_MESSAGE)); 202 return null; 203 } 204 205 206 public String instanceName () { 207 return getName(); 208 } 209 210 211 215 public Class instanceClass () throws IOException, ClassNotFoundException { 216 return URLPresenter.class; 217 } 218 219 220 226 public Object instanceCreate() throws IOException, ClassNotFoundException { 227 return new URLPresenter(this); 228 } 229 230 } 231 | Popular Tags |