KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > HyperlinkDispatcher


1 package net.suberic.pooka.gui;
2 import net.suberic.pooka.*;
3 import javax.swing.event.*;
4 import java.util.StringTokenizer JavaDoc;
5 import net.suberic.util.swing.HyperlinkMouseHandler;
6 import javax.swing.JTextPane JavaDoc;
7 import javax.swing.text.*;
8
9 /**
10  * This is a simple class which implements HyperlinkListener.
11  */

12
13 public class HyperlinkDispatcher implements HyperlinkListener {
14
15   ErrorHandler errorHandler = null;
16
17   /**
18    * Default Constructor.
19    */

20   public HyperlinkDispatcher() {
21   }
22
23   /**
24    * Creates a HyperlinkDispatcher that uses the given ErrorHandler.
25    */

26   public HyperlinkDispatcher(ErrorHandler handler) {
27     errorHandler = handler;
28   }
29
30   /**
31    * This handles HyperlinkEvents. For now, we're just taking
32    * ACTIVATED events, and dispatching the url's to the external
33    * program indicated by Pooka.urlHandler.
34    *
35    * Specified in javax.swing.event.HyperlinkListener.
36    */

37   public void hyperlinkUpdate(HyperlinkEvent e) {
38     if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
39       if (e.getSource() instanceof HyperlinkMouseHandler.URLSelection) {
40         HyperlinkMouseHandler.URLSelection selection = (HyperlinkMouseHandler.URLSelection) e.getSource();
41         if (selection.editor instanceof JTextPane JavaDoc) {
42           JTextPane JavaDoc pane = (JTextPane JavaDoc) selection.editor;
43           StyledDocument doc = pane.getStyledDocument();
44           StyledEditorKit kit = (StyledEditorKit) pane.getEditorKit();
45           MutableAttributeSet attr = kit.getInputAttributes();
46           SimpleAttributeSet sas = new SimpleAttributeSet();
47           StyleConstants.setUnderline(sas, true);
48           doc.setCharacterAttributes(selection.start, (selection.end - selection.start + 1), sas, false);
49         }
50       }
51     } else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
52       if (e.getSource() instanceof HyperlinkMouseHandler.URLSelection) {
53         HyperlinkMouseHandler.URLSelection selection = (HyperlinkMouseHandler.URLSelection) e.getSource();
54         if (selection.editor instanceof JTextPane JavaDoc) {
55           JTextPane JavaDoc pane = (JTextPane JavaDoc) selection.editor;
56           StyledDocument doc = pane.getStyledDocument();
57           StyledEditorKit kit = (StyledEditorKit) pane.getEditorKit();
58           MutableAttributeSet attr = kit.getInputAttributes();
59           SimpleAttributeSet sas = new SimpleAttributeSet();
60           StyleConstants.setUnderline(sas, false);
61           doc.setCharacterAttributes(selection.start, (selection.end - selection.start + 1), sas, false);
62         }
63       }
64     } else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
65       if (Pooka.getProperty("Pooka.url.useDefaultHandler", "true").equalsIgnoreCase("true")) {
66         try {
67           java.awt.Desktop.getDesktop().browse(e.getURL().toURI());
68         } catch(Exception JavaDoc ex) {
69           if (errorHandler != null) {
70             errorHandler.showError(java.text.MessageFormat.format(Pooka.getProperty("error.urlHandler.openingUrl", "Error opening url {0}: "), e.getURL()), ex);
71           } else {
72             System.err.println(Pooka.getProperty("error.urlHandler.openingUrl", "Error opening url {0}: " + e.getURL()));
73             ex.printStackTrace();
74           }
75         }
76       } else {
77         String JavaDoc parsedVerb = Pooka.getProperty("Pooka.urlHandler", "firefox %s");
78         if (parsedVerb.indexOf("%s") == -1)
79           parsedVerb = parsedVerb + " %s";
80
81         String JavaDoc[] cmdArray;
82
83         parsedVerb = ExternalLauncher.substituteString(parsedVerb, "%s", e.getURL().toString());
84
85         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(parsedVerb);
86         cmdArray = new String JavaDoc[tok.countTokens()];
87         for (int i = 0; tok.hasMoreTokens(); i++) {
88           String JavaDoc currentString = tok.nextToken();
89           cmdArray[i]=currentString;
90         }
91         try {
92           Runtime.getRuntime().exec(cmdArray);
93         } catch (java.io.IOException JavaDoc ioe) {
94           if (errorHandler != null) {
95             errorHandler.showError(java.text.MessageFormat.format(Pooka.getProperty("error.urlHandler.openingUrl", "Error opening url {0}: "), e.getURL()), ioe);
96           } else {
97             System.err.println(Pooka.getProperty("error.urlHandler.openingUrl", "Error opening url {0}: " + e.getURL()));
98             ioe.printStackTrace();
99           }
100         }
101       }
102     }
103   }
104 }
105
106
Popular Tags