|
1 |
| -/* |
2 |
| - * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
3 |
| - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
| - * |
5 |
| - * This code is free software; you can redistribute it and/or modify it |
6 |
| - * under the terms of the GNU General Public License version 2 only, as |
7 |
| - * published by the Free Software Foundation. Oracle designates this |
8 |
| - * particular file as subject to the "Classpath" exception as provided |
9 |
| - * by Oracle in the LICENSE file that accompanied this code. |
10 |
| - * |
11 |
| - * This code is distributed in the hope that it will be useful, but WITHOUT |
12 |
| - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 |
| - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 |
| - * version 2 for more details (a copy is included in the LICENSE file that |
15 |
| - * accompanied this code). |
16 |
| - * |
17 |
| - * You should have received a copy of the GNU General Public License version |
18 |
| - * 2 along with this work; if not, write to the Free Software Foundation, |
19 |
| - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
| - * |
21 |
| - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
| - * or visit www.oracle.com if you need additional information or have any |
23 |
| - * questions. |
24 |
| - */ |
25 |
| - |
26 |
| -package com.sun.tools.visualvm.uisupport; |
27 |
| - |
28 |
| -import java.awt.*; |
29 |
| -import java.net.URL; |
30 |
| -import javax.swing.*; |
31 |
| -import javax.swing.event.HyperlinkEvent; |
32 |
| -import javax.swing.event.HyperlinkListener; |
33 |
| -import javax.swing.text.BadLocationException; |
34 |
| -import javax.swing.text.JTextComponent; |
35 |
| -import javax.swing.text.NavigationFilter; |
36 |
| -import javax.swing.text.Position; |
37 |
| - |
38 |
| - |
39 |
| -/** |
40 |
| - * Copy of org.netbeans.lib.profiler.ui.components.HTMLLabel to be used in |
41 |
| - * VisualVM tool an plugins. |
42 |
| - * |
43 |
| - * @author Jiri Sedlacek |
44 |
| - */ |
45 |
| -public class HTMLLabel extends JEditorPane implements HyperlinkListener { |
46 |
| - //~ Constructors ------------------------------------------------------------------------------------------------------------- |
47 |
| - |
48 |
| - public HTMLLabel() { |
49 |
| - setEditorKit(new javax.swing.text.html.HTMLEditorKit()); |
50 |
| - setEditable(false); |
51 |
| - setOpaque(false); |
52 |
| - setNavigationFilter(new NavigationFilter() { |
53 |
| - public void moveDot(FilterBypass fb, int dot, Position.Bias bias) { |
54 |
| - super.moveDot(fb, 0, bias); |
55 |
| - } |
56 |
| - |
57 |
| - public void setDot(FilterBypass fb, int dot, Position.Bias bias) { |
58 |
| - super.setDot(fb, 0, bias); |
59 |
| - } |
60 |
| - |
61 |
| - public int getNextVisualPositionFrom(JTextComponent text, int pos, Position.Bias bias, int direction, |
62 |
| - Position.Bias[] biasRet) |
63 |
| - throws BadLocationException { |
64 |
| - return 0; |
65 |
| - } |
66 |
| - }); |
67 |
| - setFont(UIManager.getFont("Label.font")); //NOI18N |
68 |
| - addHyperlinkListener(this); |
69 |
| - } |
70 |
| - |
71 |
| - public HTMLLabel(String text) { |
72 |
| - this(); |
73 |
| - setText(text); |
74 |
| - } |
75 |
| - |
76 |
| - //~ Methods ------------------------------------------------------------------------------------------------------------------ |
77 |
| - |
78 |
| - public void setText(String value) { |
79 |
| - Font font = getFont(); |
80 |
| - Color textColor = getForeground(); |
81 |
| - |
82 |
| - value = value.replaceAll("\\n\\r|\\r\\n|\\n|\\r", "<br>"); //NOI18N |
83 |
| - value = value.replace("<code>", "<code style=\"font-size: " + font.getSize() + "pt;\">"); //NOI18N |
84 |
| - |
85 |
| - String colorText = "rgb(" + textColor.getRed() + "," + textColor.getGreen() + "," + textColor.getBlue() + ")"; //NOI18N |
86 |
| - super.setText("<html><body text=\"" + colorText + "\" style=\"font-size: " + font.getSize() + "pt; font-family: " + font.getName() + ";\">" + value |
87 |
| - + "</body></html>"); //NOI18N |
88 |
| - } |
89 |
| - |
90 |
| - public void hyperlinkUpdate(HyperlinkEvent e) { |
91 |
| - if (!isEnabled()) { |
92 |
| - return; |
93 |
| - } |
94 |
| - |
95 |
| - if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { |
96 |
| - showURL(e.getURL()); |
97 |
| - } else if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) { |
98 |
| - setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
99 |
| - } else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) { |
100 |
| - setCursor(Cursor.getDefaultCursor()); |
101 |
| - } |
102 |
| - } |
103 |
| - |
104 |
| - protected void showURL(URL url) { |
105 |
| - // override to react to URL clicks |
106 |
| - } |
107 |
| -} |
| 1 | +/* |
| 2 | + * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package com.sun.tools.visualvm.uisupport; |
| 27 | + |
| 28 | +import java.awt.*; |
| 29 | +import java.net.URL; |
| 30 | +import javax.swing.*; |
| 31 | +import javax.swing.event.HyperlinkEvent; |
| 32 | +import javax.swing.event.HyperlinkListener; |
| 33 | +import javax.swing.text.BadLocationException; |
| 34 | +import javax.swing.text.JTextComponent; |
| 35 | +import javax.swing.text.NavigationFilter; |
| 36 | +import javax.swing.text.Position; |
| 37 | +import javax.swing.text.html.HTMLEditorKit; |
| 38 | +import org.netbeans.lib.profiler.ui.UIUtils; |
| 39 | + |
| 40 | + |
| 41 | +/** |
| 42 | + * Copy of org.netbeans.lib.profiler.ui.components.HTMLLabel to be used in |
| 43 | + * VisualVM tool an plugins. |
| 44 | + * |
| 45 | + * @author Jiri Sedlacek |
| 46 | + */ |
| 47 | +public class HTMLLabel extends JEditorPane implements HyperlinkListener { |
| 48 | + |
| 49 | + private int halign = SwingConstants.LEADING; |
| 50 | + |
| 51 | + |
| 52 | + public HTMLLabel() { |
| 53 | + this(null); |
| 54 | + } |
| 55 | + |
| 56 | + public HTMLLabel(String text) { |
| 57 | + setEditorKit(new HTMLEditorKit()); |
| 58 | + setEditable(false); |
| 59 | + setOpaque(false); |
| 60 | + setNavigationFilter(new NavigationFilter() { |
| 61 | + public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) { |
| 62 | + super.moveDot(fb, 0, bias); |
| 63 | + } |
| 64 | + |
| 65 | + public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) { |
| 66 | + super.setDot(fb, 0, bias); |
| 67 | + } |
| 68 | + |
| 69 | + public int getNextVisualPositionFrom(JTextComponent text, int pos, Position.Bias bias, int direction, |
| 70 | + Position.Bias[] biasRet) |
| 71 | + throws BadLocationException { |
| 72 | + return 0; |
| 73 | + } |
| 74 | + }); |
| 75 | + setFont(UIManager.getFont("Label.font")); //NOI18N |
| 76 | + addHyperlinkListener(this); |
| 77 | + |
| 78 | + if (text != null) setText(text); |
| 79 | + } |
| 80 | + |
| 81 | + //~ Methods ------------------------------------------------------------------------------------------------------------------ |
| 82 | + |
| 83 | + public void setOpaque(boolean o) { |
| 84 | + super.setOpaque(o); |
| 85 | + if (UIUtils.isNimbusLookAndFeel() && !o) |
| 86 | + setBackground(new Color(0, 0, 0, 0)); |
| 87 | + if (txt != null) setText(txt); |
| 88 | + } |
| 89 | + |
| 90 | + private String txt; |
| 91 | + |
| 92 | + public void setText(String value) { |
| 93 | + txt = value; |
| 94 | + |
| 95 | + Font font = getFont(); |
| 96 | + Color fgColor = getForeground(); |
| 97 | + Color bgColor = getBackground(); |
| 98 | + |
| 99 | + value = value.replaceAll("\\n\\r|\\r\\n|\\n|\\r", "<br>"); //NOI18N |
| 100 | + value = value.replace("<code>", "<code style=\"font-size: " + font.getSize() + "pt;\">"); //NOI18N |
| 101 | + |
| 102 | + String fgText = "rgb(" + fgColor.getRed() + "," + fgColor.getGreen() + "," + fgColor.getBlue() + ")"; //NOI18N |
| 103 | + String bgText = isOpaque() ? "rgb(" + bgColor.getRed() + "," + bgColor.getGreen() + "," + bgColor.getBlue() + ")" : null; //NOI18N |
| 104 | + |
| 105 | + String alignText = null; |
| 106 | + switch (halign) { |
| 107 | + case SwingConstants.CENTER: |
| 108 | + alignText = "center"; //NOI18N |
| 109 | + break; |
| 110 | + case SwingConstants.RIGHT: |
| 111 | + case SwingConstants.TRAILING: |
| 112 | + alignText = "right"; //NOI18N |
| 113 | + break; |
| 114 | + } |
| 115 | + |
| 116 | + String bodyFlags = "text=\"" + fgText + "\""; //NOI18N |
| 117 | + if (bgText != null) bodyFlags += " bgcolor=\"" + bgText + "\""; //NOI18N |
| 118 | + if (alignText != null) bodyFlags += " align=\"" + alignText + "\""; //NOI18N |
| 119 | + |
| 120 | + super.setText("<html><body " + bodyFlags + " style=\"font-size: " + font.getSize() //NOI18N |
| 121 | + + "pt; font-family: " + font.getName() + ";\">" + value + "</body></html>"); //NOI18N |
| 122 | + } |
| 123 | + |
| 124 | + public void setForeground(Color fg) { |
| 125 | + super.setForeground(fg); |
| 126 | + if (txt != null) setText(txt); |
| 127 | + } |
| 128 | + |
| 129 | + public void setBackground(Color bg) { |
| 130 | + super.setBackground(bg); |
| 131 | +// setBorder(getBorder()); |
| 132 | + if (txt != null) setText(txt); |
| 133 | + } |
| 134 | + |
| 135 | +// public void setBorder(Border b) { |
| 136 | +// Insets i = b == null ? new Insets(0, 0, 0, 0) : b.getBorderInsets(this); |
| 137 | +// if (!isOpaque()) super.setBorder(BorderFactory.createEmptyBorder(i.top, i.left, i.bottom, i.right)); |
| 138 | +// else super.setBorder(BorderFactory.createMatteBorder(i.top, i.left, i.bottom, i.right, getBackground())); |
| 139 | +// } |
| 140 | + |
| 141 | + public void setHorizontalAlignment(int alignment) { |
| 142 | + if (alignment == halign) return; |
| 143 | + halign = alignment; |
| 144 | + if (txt != null) setText(txt); |
| 145 | + } |
| 146 | + |
| 147 | + public void hyperlinkUpdate(HyperlinkEvent e) { |
| 148 | + if (!isEnabled()) { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { |
| 153 | + showURL(e.getURL()); |
| 154 | + } else if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) { |
| 155 | + setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
| 156 | + } else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) { |
| 157 | + setCursor(Cursor.getDefaultCursor()); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + protected void showURL(URL url) { |
| 162 | + // override to react to URL clicks |
| 163 | + } |
| 164 | +} |
0 commit comments