1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing; |
|
27 |
|
28 import javax.swing.*; |
|
29 import com.oracle.solaris.vp.util.misc.*; |
|
30 import com.oracle.solaris.vp.util.misc.finder.*; |
|
31 |
|
32 @SuppressWarnings({"serial"}) |
|
33 public class InfoLabel extends JLabel { |
|
34 // |
|
35 // Static data |
|
36 // |
|
37 |
|
38 public static final Icon ICON; |
|
39 static { |
|
40 Icon icon = GnomeUtil.getIcon("info", 12, true); |
|
41 if (icon == null) { |
|
42 icon = Finder.getIcon("images/general/info.png"); |
|
43 } |
|
44 ICON = icon; |
|
45 } |
|
46 public static final int DEFAULT_WIDTH = 65; |
|
47 |
|
48 // |
|
49 // Instance data |
|
50 // |
|
51 |
|
52 private int width; |
|
53 |
|
54 // |
|
55 // Constructors |
|
56 // |
|
57 |
|
58 public InfoLabel(String text, int width) { |
|
59 super(ICON); |
|
60 |
|
61 setToolTipText(text); |
|
62 setToolTipWidth(width); |
|
63 addMouseListener(ToolTipDelayOverrider.IMMEDIATE); |
|
64 } |
|
65 |
|
66 public InfoLabel(String text) { |
|
67 this(text, DEFAULT_WIDTH); |
|
68 } |
|
69 |
|
70 public InfoLabel() { |
|
71 this(null); |
|
72 } |
|
73 |
|
74 // |
|
75 // JComponent methods |
|
76 // |
|
77 |
|
78 @Override |
|
79 public void setToolTipText(String text) { |
|
80 super.setToolTipText(text); |
|
81 reformat(); |
|
82 } |
|
83 |
|
84 // |
|
85 // InfoLabel methods |
|
86 // |
|
87 |
|
88 public int getToolTipWidth() { |
|
89 return width; |
|
90 } |
|
91 |
|
92 public void setToolTipWidth(int width) { |
|
93 this.width = width; |
|
94 reformat(); |
|
95 } |
|
96 |
|
97 // |
|
98 // Private methods |
|
99 // |
|
100 |
|
101 private void reformat() { |
|
102 String text = getToolTipText(); |
|
103 |
|
104 if (text != null && !text.matches("(?i)^<html\\b.*")) { |
|
105 int width = getToolTipWidth(); |
|
106 if (width > 0) { |
|
107 text = "<html>" + TextUtil.escapeHTMLChars( |
|
108 TextUtil.format(text, width, false)) + "</html>"; |
|
109 |
|
110 super.setToolTipText(text); |
|
111 } |
|
112 } |
|
113 } |
|
114 } |
|