ExternalLinkContentViewerUI.java
01 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
02  *
03  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
04  *
05  * "Hilbert II" is free software; you can redistribute
06  * it and/or modify it under the terms of the GNU General Public
07  * License as published by the Free Software Foundation; either
08  * version 2 of the License, or (at your option) any later version.
09  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  */
15 
16 package org.qedeq.gui.se.util;
17 
18 import java.net.URL;
19 
20 import javax.help.JHelpContentViewer;
21 import javax.help.plaf.basic.BasicContentViewerUI;
22 import javax.swing.JComponent;
23 import javax.swing.event.HyperlinkEvent;
24 import javax.swing.plaf.ComponentUI;
25 
26 /**
27  * A UI subclass that will open external links (website or mail links) in an external browser.
28  * Thanks to mw219725 see <a href="http://bioputer.mimuw.edu.pl/modevo/">cytoscape plugins</a>.
29  *
30  @author  Michael Meyling
31  */
32 public class ExternalLinkContentViewerUI extends BasicContentViewerUI {
33 
34         public ExternalLinkContentViewerUI(final JHelpContentViewer b) {
35             super(b);
36     }
37 
38     public static ComponentUI createUI(final JComponent x) {
39         return new ExternalLinkContentViewerUI((JHelpContentViewerx);
40     }
41 
42     public void hyperlinkUpdate(final HyperlinkEvent he) {
43         if (he.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
44             try {
45                 URL u = he.getURL();
46 
47                 if (u.getProtocol().equalsIgnoreCase("mailto")
48                         || u.getProtocol().equalsIgnoreCase("http")
49                         || u.getProtocol().equalsIgnoreCase("https")
50                         || u.getProtocol().equalsIgnoreCase("ftp")) {
51                     BareBonesBrowserLaunch.openURL(u.toString());
52                     return;
53                 }
54             catch (Throwable t) {
55                 // opening failed, ignore
56             }
57         }
58         super.hyperlinkUpdate(he);
59     }
60 
61 }