4
* TipUtil.java - Utility methods for homemade tool tips.
6
* This library is distributed under a modified BSD license. See the included
7
* RSyntaxTextArea.License.txt file for details.
9
package org.fife.ui.rsyntaxtextarea.focusabletip;
11
import java.awt.Color;
13
import java.awt.GraphicsConfiguration;
14
import java.awt.GraphicsDevice;
15
import java.awt.GraphicsEnvironment;
16
import java.awt.Rectangle;
17
import java.awt.SystemColor;
18
import javax.swing.BorderFactory;
19
import javax.swing.JEditorPane;
20
import javax.swing.UIManager;
21
import javax.swing.border.Border;
22
import javax.swing.plaf.ColorUIResource;
23
import javax.swing.text.html.HTMLDocument;
27
* Static utility methods for focusable tips.
29
* @author Robert Futrell
32
public class TipUtil {
40
* Returns a hex string for the specified color, suitable for HTML.
43
* @return The string representation, in the form "<code>#rrggbb</code>",
44
* or <code>null</code> if <code>c</code> is <code>null</code>.
46
private static final String getHexString(Color c) {
52
StringBuilder sb = new StringBuilder("#");
57
sb.append(Integer.toHexString(r));
62
sb.append(Integer.toHexString(g));
67
sb.append(Integer.toHexString(b));
75
* Returns the screen coordinates for the monitor that contains the
76
* specified point. This is useful for setups with multiple monitors,
77
* to ensure that popup windows are positioned properly.
79
* @param x The x-coordinate, in screen coordinates.
80
* @param y The y-coordinate, in screen coordinates.
81
* @return The bounds of the monitor that contains the specified point.
83
public static Rectangle getScreenBoundsForPoint(int x, int y) {
84
GraphicsEnvironment env = GraphicsEnvironment.
85
getLocalGraphicsEnvironment();
86
GraphicsDevice[] devices = env.getScreenDevices();
87
for (int i=0; i<devices.length; i++) {
88
GraphicsConfiguration[] configs = devices[i].getConfigurations();
89
for (int j=0; j<configs.length; j++) {
90
Rectangle gcBounds = configs[j].getBounds();
91
if (gcBounds.contains(x, y)) {
96
// If point is outside all monitors, default to default monitor (?)
97
return env.getMaximumWindowBounds();
102
* Returns the default background color to use for tool tip windows.
104
* @return The default background color.
106
public static Color getToolTipBackground() {
108
Color c = UIManager.getColor("ToolTip.background");
110
// Tooltip.background is wrong color on Nimbus (!)
111
boolean isNimbus = isNimbusLookAndFeel();
112
if (c==null || isNimbus) {
113
c = UIManager.getColor("info"); // Used by Nimbus (and others)
114
if (c==null || (isNimbus && isDerivedColor(c))) {
115
c = SystemColor.info; // System default
119
// Workaround for a bug (?) with Nimbus - calling JLabel.setBackground()
120
// with a ColorUIResource does nothing, must be a normal Color
121
if (c instanceof ColorUIResource) {
122
c = new Color(c.getRGB());
131
* Returns the border used by tool tips in this look and feel.
133
* @return The border.
135
public static Border getToolTipBorder() {
137
Border border = UIManager.getBorder("ToolTip.border");
139
if (border==null || isNimbusLookAndFeel()) {
140
border = UIManager.getBorder("nimbusBorder");
142
border = BorderFactory.createLineBorder(SystemColor.controlDkShadow);
152
* Returns whether a color is a Nimbus DerivedColor, which is troublesome
153
* in that it doesn't use its RGB values (uses HSB instead?) and so
154
* querying them is useless.
156
* @param c The color to check.
157
* @return Whether it is a DerivedColor
159
private static final boolean isDerivedColor(Color c) {
160
return c!=null && c.getClass().getName().endsWith(".DerivedColor");
165
* Returns whether the Nimbus Look and Feel is installed.
167
* @return Whether the current LAF is Nimbus.
169
private static final boolean isNimbusLookAndFeel() {
170
return UIManager.getLookAndFeel().getName().equals("Nimbus");
175
* Tweaks a <code>JEditorPane</code> so it can be used to render the
176
* content in a focusable pseudo-tool tip. It is assumed that the editor
177
* pane is using an <code>HTMLDocument</code>.
179
* @param textArea The editor pane to tweak.
181
public static void tweakTipEditorPane(JEditorPane textArea) {
183
// Jump through a few hoops to get things looking nice in Nimbus
184
boolean isNimbus = isNimbusLookAndFeel();
186
Color selBG = textArea.getSelectionColor();
187
Color selFG = textArea.getSelectedTextColor();
188
textArea.setUI(new javax.swing.plaf.basic.BasicEditorPaneUI());
189
textArea.setSelectedTextColor(selFG);
190
textArea.setSelectionColor(selBG);
193
textArea.setEditable(false); // Required for links to work!
194
textArea.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
196
// Make selection visible even though we are not (initially) focusable.
197
textArea.getCaret().setSelectionVisible(true);
199
// Set the foreground color. Important because when rendering HTML,
200
// default foreground becomes black, which may not match all LAF's
202
Color fg = UIManager.getColor("Label.foreground");
203
if (fg==null || (isNimbus && isDerivedColor(fg))) {
204
fg = SystemColor.textText;
206
textArea.setForeground(fg);
208
// Make it use the "tool tip" background color.
209
textArea.setBackground(TipUtil.getToolTipBackground());
211
// Force JEditorPane to use a certain font even in HTML.
212
// All standard LookAndFeels, even Nimbus (!), define Label.font.
213
Font font = UIManager.getFont("Label.font");
214
if (font == null) { // Try to make a sensible default
215
font = new Font("SansSerif", Font.PLAIN, 12);
217
HTMLDocument doc = (HTMLDocument) textArea.getDocument();
218
doc.getStyleSheet().addRule(
219
"body { font-family: " + font.getFamily() +
220
"; font-size: " + font.getSize() + "pt" +
221
"; color: " + getHexString(fg) + "; }");
b'\\ No newline at end of file'