source: src/main/java/weka/gui/BrowserHelper.java @ 16

Last change on this file since 16 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 4.4 KB
RevLine 
[4]1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (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., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * BrowserHelper.java
19 * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.gui;
23
24import java.awt.Color;
25import java.awt.Component;
26import java.awt.Font;
27import java.awt.event.MouseAdapter;
28import java.awt.event.MouseEvent;
29import java.lang.reflect.Method;
30
31import javax.swing.JLabel;
32import javax.swing.JOptionPane;
33
34
35/**
36 * A little helper class for browser related stuff. <p/>
37 *
38 * The <code>openURL</code> method is based on
39 * <a href="http://www.centerkey.com/java/browser/" target="_blank">Bare Bones Browser Launch</a>,
40 * which is placed in the public domain.
41 *
42 * @author  fracpete (fracpete at waikato dot ac dot nz)
43 * @version $Revision: 5054 $
44 */
45public class BrowserHelper {
46
47  /** Linux/Unix binaries to look for */
48  public final static String[] LINUX_BROWSERS = 
49    {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"};
50 
51  /**
52   * opens the URL in a browser.
53   *
54   * @param url         the URL to open
55   */
56  public static void openURL(String url) {
57    openURL(null, url);
58  }
59
60  /**
61   * opens the URL in a browser.
62   *
63   * @param parent      the parent component
64   * @param url         the URL to open
65   */
66  public static void openURL(Component parent, String url) {
67    openURL(parent, url, true);
68  }
69
70  /**
71   * opens the URL in a browser.
72   *
73   * @param parent      the parent component
74   * @param url         the URL to open
75   * @param showDialog  whether to display a dialog in case of an error or
76   *                    just print the error to the console
77   */
78  public static void openURL(Component parent, String url, boolean showDialog) {
79    String osName = System.getProperty("os.name"); 
80    try { 
81      // Mac OS
82      if (osName.startsWith("Mac OS")) { 
83        Class fileMgr = Class.forName("com.apple.eio.FileManager"); 
84        Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] {String.class}); 
85        openURL.invoke(null, new Object[] {url}); 
86      } 
87      // Windows
88      else if (osName.startsWith("Windows")) {
89        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); 
90      }
91      // assume Unix or Linux
92      else { 
93        String browser = null; 
94        for (int count = 0; count < LINUX_BROWSERS.length && browser == null; count++) {
95          // look for binaries and take first that's available
96          if (Runtime.getRuntime().exec(new String[] {"which", LINUX_BROWSERS[count]}).waitFor() == 0) {
97            browser = LINUX_BROWSERS[count];
98            break;
99          }
100        }
101        if (browser == null) 
102          throw new Exception("Could not find web browser");
103        else
104          Runtime.getRuntime().exec(new String[] {browser, url});
105      }
106    }
107    catch (Exception e) {
108      String errMsg = "Error attempting to launch web browser:\n" + e.getMessage();
109     
110      if (showDialog)
111        JOptionPane.showMessageDialog(
112            parent, errMsg);
113      else
114        System.err.println(errMsg);
115    }
116  } 
117 
118  /**
119   * Generates a label with a clickable link.
120   *
121   * @param url         the url of the link
122   * @param text        the text to display instead of URL. if null or of
123   *                    length 0 then the URL is used
124   * @return            the generated label
125   */
126  public static JLabel createLink(String url, String text) {
127    final String urlF = url;
128    final JLabel result = new JLabel();
129    result.setText((text == null) || (text.length() == 0) ? url : text);
130    result.setToolTipText("Click to open link in browser");
131    result.setForeground(Color.BLUE);
132    result.addMouseListener(new MouseAdapter() {
133      public void mouseClicked(MouseEvent e) {
134        if (e.getButton() == MouseEvent.BUTTON1) {
135          BrowserHelper.openURL(urlF);
136        }
137        else {
138          super.mouseClicked(e);
139        }
140      }
141      public void mouseEntered(MouseEvent e) {
142        result.setForeground(Color.RED);
143      }
144      public void mouseExited(MouseEvent e) {
145        result.setForeground(Color.BLUE);
146      }
147    });
148   
149    return result;
150  }
151}
Note: See TracBrowser for help on using the repository browser.