]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/AppletURLHandler.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / AppletURLHandler.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2005 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\r
10 /*  PJIRC is free software; you can redistribute     */\r
11 /*  it and/or modify it under the terms of the GNU   */\r
12 /*  General Public License as published by the       */\r
13 /*  Free Software Foundation; version 2 or later of  */\r
14 /*  the License.                                     */\r
15 /*                                                   */\r
16 /*  PJIRC is distributed in the hope that it will    */\r
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */\r
18 /*  even the implied warranty of MERCHANTABILITY or  */\r
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */\r
20 /*  General Public License for more details.         */\r
21 /*                                                   */\r
22 /*  You should have received a copy of the GNU       */\r
23 /*  General Public License along with PJIRC; if      */\r
24 /*  not, write to the Free Software Foundation,      */\r
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */\r
26 /*  MA  02111-1307  USA                              */\r
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc;\r
31 \r
32 import java.applet.AppletContext;\r
33 import java.net.MalformedURLException;\r
34 import java.net.URL;\r
35 \r
36 /**\r
37  * URL handling via applet.\r
38  */\r
39 public class AppletURLHandler implements URLHandler {\r
40 \r
41         private AppletContext _ctx;\r
42 \r
43         /**\r
44          * Create a new AppletURLHandler using the given AppletContext.\r
45          * \r
46          * @param ctx\r
47          *          the applet context to use.\r
48          */\r
49         public AppletURLHandler(AppletContext ctx) {\r
50                 _ctx = ctx;\r
51         }\r
52 \r
53         private String replace(String on, String what, String with) {\r
54                 int pos = on.indexOf(what);\r
55                 while (pos >= 0) {\r
56                         String before = on.substring(0, pos);\r
57                         String after = on.substring(pos + what.length());\r
58                         on = before + with + after;\r
59                         pos = on.indexOf(what);\r
60                 }\r
61                 return on;\r
62         }\r
63 \r
64         private URL decodeURL(String u) throws MalformedURLException {\r
65                 if (u.indexOf("://") == -1)\r
66                         u = "http://" + u;\r
67                 replace(u, " ", "%20");\r
68                 return new URL(u);\r
69         }\r
70 \r
71         @Override\r
72         public void stateURL(String url) {\r
73                 try {\r
74                         _ctx.showStatus(decodeURL(url).toString());\r
75                 } catch (Exception e) {\r
76                         throw new RuntimeException(e.toString());\r
77                 }\r
78         }\r
79 \r
80         @Override\r
81         public void openURL(String url) {\r
82                 openURL(url, "_blank");\r
83         }\r
84 \r
85         @Override\r
86         public void openURL(String url, String target) {\r
87                 try {\r
88                         _ctx.showDocument(decodeURL(url), target);\r
89                 } catch (Exception e) {\r
90                         throw new RuntimeException(e.toString());\r
91                 }\r
92         }\r
93 }\r