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.panels.coreadm.client.swing.path; |
|
27 |
|
28 import java.awt.*; |
|
29 import java.util.*; |
|
30 import javax.swing.*; |
|
31 import javax.swing.border.Border; |
|
32 import com.oracle.solaris.vp.util.misc.finder.Finder; |
|
33 import com.oracle.solaris.vp.util.swing.*; |
|
34 |
|
35 @SuppressWarnings({"serial"}) |
|
36 public class TokenLabel extends JLabel { |
|
37 // |
|
38 // Inner classes |
|
39 // |
|
40 |
|
41 protected static class TokenRenderInfo { |
|
42 // |
|
43 // Instance data |
|
44 // |
|
45 |
|
46 private String token; |
|
47 private String tooltip; |
|
48 private Color background; |
|
49 |
|
50 // |
|
51 // Constructors |
|
52 // |
|
53 |
|
54 public TokenRenderInfo(String token, String tooltip, Color background) { |
|
55 this.token = token; |
|
56 this.tooltip = tooltip; |
|
57 this.background = background; |
|
58 } |
|
59 |
|
60 // |
|
61 // TokenRenderInfo methods |
|
62 // |
|
63 |
|
64 public String getToken() { |
|
65 return token; |
|
66 } |
|
67 |
|
68 public String getToolTip() { |
|
69 return tooltip; |
|
70 } |
|
71 |
|
72 public Color getBackground() { |
|
73 return background; |
|
74 } |
|
75 } |
|
76 |
|
77 // |
|
78 // Static data |
|
79 // |
|
80 |
|
81 protected static final Border BORDER; |
|
82 static { |
|
83 Border line = BorderFactory.createLineBorder(Color.black, 1); |
|
84 Border space = BorderFactory.createEmptyBorder(0, 1, 0, 1); |
|
85 BORDER = BorderFactory.createCompoundBorder(space, line); |
|
86 } |
|
87 |
|
88 protected static final Map<String, TokenRenderInfo> RENDER_INFO = |
|
89 new HashMap<String, TokenRenderInfo>(); |
|
90 |
|
91 static { |
|
92 for (int i = 0; i < PathDocument.TOKENS.length; i++) { |
|
93 String token = PathDocument.TOKENS[i]; |
|
94 String toolTip = Finder.getString("path.token.label." + token); |
|
95 Color bg; |
|
96 |
|
97 switch (i) { |
|
98 case 0: bg = ColorUtil.lighter(Color.blue, .25f); break; |
|
99 case 1: bg = ColorUtil.lighter(Color.green, .25f); break; |
|
100 case 2: bg = ColorUtil.lighter(Color.red, .25f); break; |
|
101 case 3: bg = Color.yellow; break; |
|
102 case 4: bg = Color.gray; break; |
|
103 |
|
104 case 5: bg = ColorUtil.lighter(Color.blue, .5f); break; |
|
105 case 6: bg = ColorUtil.lighter(Color.green, .75f); break; |
|
106 case 7: bg = ColorUtil.lighter(Color.red, .5f); break; |
|
107 case 8: bg = ColorUtil.lighter(Color.yellow, .75f); break; |
|
108 default: bg = ColorUtil.lighter(Color.gray, .5f); break; |
|
109 } |
|
110 |
|
111 RENDER_INFO.put(token, new TokenRenderInfo(token, toolTip, bg)); |
|
112 } |
|
113 } |
|
114 |
|
115 // |
|
116 // Instance data |
|
117 // |
|
118 |
|
119 private boolean initialized = false; |
|
120 |
|
121 // |
|
122 // Constructors |
|
123 // |
|
124 |
|
125 public TokenLabel() { |
|
126 addMouseListener(ToolTipDelayOverrider.IMMEDIATE); |
|
127 setBorder(BORDER); |
|
128 |
|
129 initialized = true; |
|
130 calculateAlignmentY(); |
|
131 } |
|
132 |
|
133 public TokenLabel(String token) { |
|
134 this(); |
|
135 setText(token); |
|
136 } |
|
137 |
|
138 // |
|
139 // Component methods |
|
140 // |
|
141 |
|
142 @Override |
|
143 public void setFont(Font font) { |
|
144 super.setFont(font); |
|
145 |
|
146 // getPreferredSize throws a NullPointerException under synth if called |
|
147 // before the superclass is initialized |
|
148 if (initialized) { |
|
149 calculateAlignmentY(); |
|
150 } |
|
151 } |
|
152 |
|
153 // |
|
154 // JComponent methods |
|
155 // |
|
156 |
|
157 @Override |
|
158 public void paintComponent(Graphics g) { |
|
159 Insets insets = getInsets(); |
|
160 Dimension size = getSize(); |
|
161 |
|
162 g.setColor(getBackground()); |
|
163 g.fillRect(insets.left, insets.top, |
|
164 size.width - insets.left - insets.right, |
|
165 size.height - insets.top - insets.bottom); |
|
166 |
|
167 super.paintComponent(g); |
|
168 } |
|
169 |
|
170 // |
|
171 // JLabel methods |
|
172 // |
|
173 |
|
174 @Override |
|
175 public void setText(String token) { |
|
176 super.setText(token); |
|
177 |
|
178 TokenRenderInfo info = RENDER_INFO.get(token); |
|
179 if (info != null) { |
|
180 setToolTipText(info.getToolTip()); |
|
181 setBackground(info.getBackground()); |
|
182 } |
|
183 } |
|
184 |
|
185 // |
|
186 // TokenLabel methods |
|
187 // |
|
188 |
|
189 protected void calculateAlignmentY() { |
|
190 FontMetrics metrics = getFontMetrics(getFont()); |
|
191 int ascent = metrics.getAscent(); |
|
192 int top = getInsets().top; |
|
193 int height = getPreferredSize().height; |
|
194 float hAlign = (float)(ascent + top) / (float)height; |
|
195 setAlignmentY(hAlign); |
|
196 } |
|
197 } |
|