KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > url > URLDataObject


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
21 package org.netbeans.modules.url;
22
23
24 import java.io.*;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
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 /** Data object that represents one bookmark, one .url file containing url.
43  *
44  * @author Ian Formanek
45  * @see org.openide.Places.Folders#bookmarks
46  */

47 public class URLDataObject extends MultiDataObject
48                            implements OpenCookie, InstanceCookie {
49
50     /** Name for url property. */
51     static final String JavaDoc PROP_URL = "url"; //NOI18N
52

53     /** Generated serial version UID. */
54     static final long serialVersionUID = 6829522922370124627L;
55
56     
57     /**
58      * Constructs a new URL data object.
59      *
60      * @param file file to create an object from
61      * @param loader <code>DataLoader</code> which recognized the file
62      * and initiated calling this constructor
63      */

64     public URLDataObject(final FileObject file, MultiFileLoader loader)
65             throws DataObjectExistsException {
66         super(file, loader);
67         getCookieSet().add(this);
68     }
69     
70     /*
71      * PENDING: it would be neat to have get/setURL methods
72      * but, there is a problem(at least at jdk1.3 for linux) with URL.equals
73      * (too much time consuming in underlying native method).
74      */

75     
76     /**
77      * Gets a <code>URL</code> string from the underlying .url file.
78      * The user is notified if an error occures during reading the file.
79      * If there are multiple lines of text in the file, only the first one is
80      * returned and no error is reported.
81      *
82      * @return <code>URL</code> string stored in the file,
83      * an empty string if the file is empty,
84      * or <code>null</code> if an error occured while reading the file
85      */

86     String JavaDoc getURLString() {
87         FileObject urlFile = getPrimaryFile();
88         if (!urlFile.isValid()) {
89             return null;
90         }
91         String JavaDoc 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             /*
117              * If the file is empty, return an empty string.
118              * <null> is reserved for notifications of failures.
119              */

120             urlString = ""; //NOI18N
121
}
122         return urlString;
123     }
124
125     /**
126      * Stores a specified URL into the file backing up this URL object.
127      *
128      * @param newUrlString URL to be stored in the file
129      */

130     void setURLString(String JavaDoc 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     /* implements interface OpenCookie */
156     public void open() {
157         String JavaDoc urlString = getURLString();
158         if (urlString == null) {
159             return;
160         }
161         URL JavaDoc url = getURLFromString(urlString);
162         if (url == null) {
163             return;
164         }
165         org.openide.awt.HtmlBrowser.URLDisplayer.getDefault().showURL(url);
166     }
167
168     /**
169      * Converts an URL string to an <code>URL</code> object.
170      * Notifies the user in case of failure.
171      *
172      * @param urlString string to convert to <code>URL</code>
173      * @return <code>URL</code> object representing the specified URL;
174      * or <code>null</code> in case of failure
175      */

176     private static URL JavaDoc getURLFromString(String JavaDoc urlString) {
177         try {
178             return new URL JavaDoc(urlString);
179         } catch (MalformedURLException JavaDoc mue1) {
180         }
181         
182
183         /* failed - try to prepend 'http://' */
184         try {
185             return new URL JavaDoc("http://" + urlString); //NOI18N
186
} catch (MalformedURLException JavaDoc mue1) {
187         }
188         
189         /* failed again - notify about the failure and return null: */
190         String JavaDoc msg;
191         if (urlString.length() > 50) { //too long URL
192
msg = NbBundle.getMessage(URLDataObject.class,
193                                       "MSG_MalformedURLError"); //NOI18N
194
} else {
195             msg = NbBundle.getMessage(URLDataObject.class,
196                                       "MSG_FMT_MalformedURLError", //NOI18N
197
urlString);
198         }
199         DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
200                 msg,
201                 NotifyDescriptor.ERROR_MESSAGE));
202         return null;
203     }
204
205     /* implements interface InstanceCookie */
206     public String JavaDoc instanceName () {
207         return getName();
208     }
209
210     /* implements interface InstanceCookie */
211     /**
212      * @return class <code>URLPresenter</code>
213      * @see URLPresenter
214      */

215     public Class JavaDoc instanceClass () throws IOException, ClassNotFoundException JavaDoc {
216         return URLPresenter.class;
217     }
218
219     /* implements interface InstanceCookie */
220     /**
221      * Creates an instance of <code>URLPresenter</code>.
222      *
223      * @return instance of class <code>URLPresenter</code>
224      * @see URLPresenter
225      */

226     public Object JavaDoc instanceCreate() throws IOException, ClassNotFoundException JavaDoc {
227         return new URLPresenter(this);
228     }
229     
230 }
231
Popular Tags