001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 005 package graphlab.graph.ui; 006 007 import graphlab.platform.core.BlackBoard; 008 009 import javax.swing.*; 010 import javax.swing.event.HyperlinkEvent; 011 import javax.swing.event.HyperlinkListener; 012 import javax.swing.text.html.HTMLDocument; 013 import javax.swing.text.html.HTMLFrameHyperlinkEvent; 014 import java.awt.*; 015 import java.io.IOException; 016 import java.net.URL; 017 import java.util.HashMap; 018 019 /** 020 * this class is able to show a HTML text, 021 * It's main capability is to handle hyperlinks, 022 * the default operation when user clicks on a link is to open the link, 023 * It is also possible to add other modes, for example when the 024 * link address is: "command?handler=BSH" it is possible to run the command, 025 * for this a HyperlinkHandler should be registered. 026 * One simple handler is also added, if the link address is "http://www.google.com/search?q=Graph&handler=external", google(your url) 027 * will be opened in an external viewer like FireFox 028 * another added handler is the "yoururl.com,yourtitle?handler=dialog" which opens your url in a new dialog! which 029 * the dialog title is yourtitle 030 * <p/> 031 * (Actually it is not important which character you put before handler(? or & or ...) The only important thing is 032 * to make it compatible with http urls) 033 * 034 * @author Azin Azadi 035 * @see graphlab.plugins.commandline.ShellHyperlinkHandler 036 */ 037 public class GHTMLPageComponent extends JScrollPane implements HyperlinkListener { 038 private BlackBoard blackboard; 039 private static HashMap<String, HyperlinkHandler> handlers = new HashMap<String, HyperlinkHandler>(); 040 JEditorPane jta; 041 042 static { 043 registerHyperLinkHandler("external", new ExternalLinkHandler()); 044 registerHyperLinkHandler("dialog", new DialogLinkHandler()); 045 } 046 047 public GHTMLPageComponent(BlackBoard b) { 048 blackboard = b; 049 050 jta = new JEditorPane(); 051 jta.setEditable(false); 052 jta.addHyperlinkListener(this); 053 JViewport jvp = new JViewport(); 054 055 jta.setBackground(Color.white); 056 jvp.add(jta); 057 jta.setContentType("text/html"); 058 059 this.setViewport(jvp); 060 } 061 062 public void setPage(URL page) { 063 try { 064 jta.setPage(page); 065 } catch (IOException e) { 066 e.printStackTrace(); 067 } 068 } 069 070 public void setHTML(String html) { 071 072 jta.setText(html); 073 } 074 075 /** 076 * @param protocol for example in "BSH:" protocol will be "BSH" 077 * @param h 078 * @see graphlab.plugins.commandline.ShellHyperlinkHandler 079 */ 080 public static void registerHyperLinkHandler(String protocol, HyperlinkHandler h) { 081 handlers.put(protocol.toUpperCase(), h); 082 } 083 084 public void hyperlinkUpdate(HyperlinkEvent e) { 085 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 086 String url = e.getDescription(); 087 if (!url.contains("=")) 088 normalHyperlinkUpdate(e); 089 else { 090 // String prefix = url.toUpperCase().substring(0, url.indexOf(":")); 091 HyperlinkHandler han = null; 092 String postfix; 093 int pi = url.indexOf("handler"); 094 if (pi != -1) { 095 postfix = url.toUpperCase().substring(pi + 8); 096 han = handlers.get(postfix); 097 } 098 if (han != null) { 099 String command = url.substring(0, pi - 1); 100 han.handle(command, blackboard, jta.getPage()); 101 } else 102 normalHyperlinkUpdate(e); 103 } 104 } 105 } 106 107 public void normalHyperlinkUpdate(HyperlinkEvent e) { 108 JEditorPane pane = (JEditorPane) e.getSource(); 109 if (e instanceof HTMLFrameHyperlinkEvent) { 110 HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e; 111 HTMLDocument doc = (HTMLDocument) pane.getDocument(); 112 doc.processHTMLFrameHyperlinkEvent(evt); 113 } else { 114 try { 115 pane.setPage(e.getURL()); 116 } catch (Throwable t) { 117 t.printStackTrace(); 118 } 119 } 120 } 121 122 123 //------------------------------------------------ 124 String message; 125 126 public void showNotificationMessage(String message) { 127 setHTML(message); 128 } 129 130 public void setMessage(String message) { 131 setHTML(message); 132 this.message = message; 133 134 } 135 136 public void hideNotificationMessage() { 137 setHTML(message); 138 } 139 140 } 141