1 package net.suberic.pooka.gui; 2 import net.suberic.pooka.*; 3 import javax.swing.event.*; 4 import java.util.StringTokenizer ; 5 import net.suberic.util.swing.HyperlinkMouseHandler; 6 import javax.swing.JTextPane ; 7 import javax.swing.text.*; 8 9 12 13 public class HyperlinkDispatcher implements HyperlinkListener { 14 15 ErrorHandler errorHandler = null; 16 17 20 public HyperlinkDispatcher() { 21 } 22 23 26 public HyperlinkDispatcher(ErrorHandler handler) { 27 errorHandler = handler; 28 } 29 30 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 ) { 42 JTextPane pane = (JTextPane ) 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 ) { 55 JTextPane pane = (JTextPane ) 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 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 parsedVerb = Pooka.getProperty("Pooka.urlHandler", "firefox %s"); 78 if (parsedVerb.indexOf("%s") == -1) 79 parsedVerb = parsedVerb + " %s"; 80 81 String [] cmdArray; 82 83 parsedVerb = ExternalLauncher.substituteString(parsedVerb, "%s", e.getURL().toString()); 84 85 StringTokenizer tok = new StringTokenizer (parsedVerb); 86 cmdArray = new String [tok.countTokens()]; 87 for (int i = 0; tok.hasMoreTokens(); i++) { 88 String currentString = tok.nextToken(); 89 cmdArray[i]=currentString; 90 } 91 try { 92 Runtime.getRuntime().exec(cmdArray); 93 } catch (java.io.IOException 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 |