-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathHTMLPane.java
More file actions
91 lines (83 loc) · 3.24 KB
/
Copy pathHTMLPane.java
File metadata and controls
91 lines (83 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.codeblockutil;
import java.awt.Cursor;
// import java.awt.Desktop;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
/**
* Provides a Swing component (extending JScrollPane) that can display
* HTML-formatted text and clickable hyperlinks (using <a href>).
*
* @author sharon@google.com (Sharon Perl)
*
*/
public class HTMLPane extends JScrollPane {
final JEditorPane edPane;
public HTMLPane(String msgText) {
super();
edPane = new JEditorPane();
edPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
edPane.setEditable(false);
edPane.setContentType("text/html");
edPane.setText("<html><body> " + msgText + " </body></html>");
edPane.setBackground(this.getBackground());
// TIP: Make the JOptionPane resizable using the HierarchyListener
edPane.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(edPane);
if (window instanceof Dialog) {
Dialog dialog = (Dialog)window;
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
}
}
});
edPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(final HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// TIP: Show hand cursor
SwingUtilities.getWindowAncestor(edPane).setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
// TIP: Show URL as the tooltip
edPane.setToolTipText(e.getURL().toExternalForm());
}
});
} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Show default cursor
SwingUtilities.getWindowAncestor(edPane).setCursor(Cursor.getDefaultCursor());
// Reset tooltip
edPane.setToolTipText(null);
}
});
} else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
/* TODO(sharon): We still require Java 5 compatability for codeblocks, so need
* another way to do this - probably via reflection
// TIP: Starting with JDK6 you can show the URL in desktop browser
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch (Exception ex) {
}
}
//System.out.println("Go to URL: " + e.getURL());
*/
}
}});
setViewportView(edPane);
}
}