KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > gui > tasks > HttpBrowser


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package net.sf.jftp.gui.tasks;
17
18 import net.sf.jftp.*;
19 import net.sf.jftp.config.*;
20 import net.sf.jftp.gui.framework.*;
21 import net.sf.jftp.net.*;
22 import net.sf.jftp.system.logging.Log;
23 import net.sf.jftp.util.*;
24
25 import java.awt.*;
26
27 import java.io.*;
28
29 import java.util.*;
30
31 import javax.swing.*;
32 import javax.swing.event.*;
33 import javax.swing.text.html.*;
34
35
36 public class HttpBrowser extends JInternalFrame implements HyperlinkListener
37 {
38     public HttpBrowser(String JavaDoc url)
39     {
40         super("Http Browser", true, true, true, true);
41
42         try
43         {
44             setTitle(url);
45
46             JEditorPane pane = new JEditorPane(url);
47             pane.setEditable(false);
48             pane.addHyperlinkListener(this);
49
50             if(!pane.getEditorKit().getContentType().equals("text/html") &&
51                    !pane.getEditorKit().getContentType().equals("text/rtf"))
52             {
53                 if(!pane.getEditorKit().getContentType().equals("text/plain"))
54                 {
55                     Log.debug("Could not display URL.");
56
57                     return;
58                 }
59
60                 pane.setEditable(false);
61                 pane.addHyperlinkListener(this);
62             }
63
64             JScrollPane jsp = new JScrollPane(pane);
65
66             getContentPane().setLayout(new BorderLayout());
67             getContentPane().add("Center", jsp);
68
69             setLocation(50, 50);
70             setSize(600, 400);
71             show();
72             requestFocus();
73         }
74         catch(Exception JavaDoc ex)
75         {
76             Log.debug("Error fetching URL: " + ex);
77             ex.printStackTrace();
78         }
79     }
80
81     public void hyperlinkUpdate(HyperlinkEvent e)
82     {
83         if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
84         {
85             JEditorPane pane = (JEditorPane) e.getSource();
86
87             if(e instanceof HTMLFrameHyperlinkEvent)
88             {
89                 HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
90                 HTMLDocument doc = (HTMLDocument) pane.getDocument();
91                 doc.processHTMLFrameHyperlinkEvent(evt);
92             }
93             else
94             {
95                 try
96                 {
97                     String JavaDoc url = e.getURL().toString();
98                     String JavaDoc tmp = url.substring(url.lastIndexOf("/"));
99
100                     Vector listeners = new Vector();
101                     listeners.add(JFtp.localDir);
102
103                     if(!url.endsWith(".htm") && !url.endsWith(".html") &&
104                            (tmp.indexOf(".") >= 0))
105                     {
106                         JFtp.statusP.startTransfer(url,
107                                                    JFtp.localDir.getPath(),
108                                                    listeners,
109                                                    JFtp.getConnectionHandler());
110                     }
111                     else
112                     {
113                         pane.setPage(e.getURL());
114                     }
115                 }
116                 catch(Throwable JavaDoc t)
117                 {
118                     t.printStackTrace();
119                 }
120             }
121         }
122     }
123 }
124
Popular Tags