1 7 8 package javax.swing.plaf.basic; 9 10 import com.sun.java.swing.SwingUtilities2; 11 import sun.swing.DefaultLookup; 12 import sun.swing.UIAction; 13 import javax.swing.*; 14 import javax.swing.plaf.*; 15 import javax.swing.text.View ; 16 17 import java.awt.event.ActionEvent ; 18 import java.awt.event.ActionListener ; 19 import java.awt.Component ; 20 import java.awt.Container ; 21 import java.awt.Dimension ; 22 import java.awt.Rectangle ; 23 import java.awt.Insets ; 24 import java.awt.Color ; 25 import java.awt.Graphics ; 26 import java.awt.Font ; 27 import java.awt.FontMetrics ; 28 import java.beans.PropertyChangeEvent ; 29 import java.beans.PropertyChangeListener ; 30 31 39 public class BasicLabelUI extends LabelUI implements PropertyChangeListener 40 { 41 protected static BasicLabelUI labelUI = new BasicLabelUI (); 42 private final static BasicLabelUI SAFE_BASIC_LABEL_UI = new BasicLabelUI (); 43 44 static void loadActionMap(LazyActionMap map) { 45 map.put(new Actions(Actions.PRESS)); 46 map.put(new Actions(Actions.RELEASE)); 47 } 48 49 56 protected String layoutCL( 57 JLabel label, 58 FontMetrics fontMetrics, 59 String text, 60 Icon icon, 61 Rectangle viewR, 62 Rectangle iconR, 63 Rectangle textR) 64 { 65 return SwingUtilities.layoutCompoundLabel( 66 (JComponent) label, 67 fontMetrics, 68 text, 69 icon, 70 label.getVerticalAlignment(), 71 label.getHorizontalAlignment(), 72 label.getVerticalTextPosition(), 73 label.getHorizontalTextPosition(), 74 viewR, 75 iconR, 76 textR, 77 label.getIconTextGap()); 78 } 79 80 86 protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY) 87 { 88 int mnemIndex = l.getDisplayedMnemonicIndex(); 89 g.setColor(l.getForeground()); 90 SwingUtilities2.drawStringUnderlineCharAt(l, g, s, mnemIndex, 91 textX, textY); 92 } 93 94 95 102 protected void paintDisabledText(JLabel l, Graphics g, String s, int textX, int textY) 103 { 104 int accChar = l.getDisplayedMnemonicIndex(); 105 Color background = l.getBackground(); 106 g.setColor(background.brighter()); 107 SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar, 108 textX + 1, textY + 1); 109 g.setColor(background.darker()); 110 SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar, 111 textX, textY); 112 } 113 114 115 119 private static Rectangle paintIconR = new Rectangle (); 120 private static Rectangle paintTextR = new Rectangle (); 121 private static Rectangle paintViewR = new Rectangle (); 122 private static Insets paintViewInsets = new Insets (0, 0, 0, 0); 123 124 125 136 public void paint(Graphics g, JComponent c) 137 { 138 JLabel label = (JLabel)c; 139 String text = label.getText(); 140 Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon(); 141 142 if ((icon == null) && (text == null)) { 143 return; 144 } 145 146 FontMetrics fm = SwingUtilities2.getFontMetrics(label, g); 147 Insets insets = c.getInsets(paintViewInsets); 148 149 paintViewR.x = insets.left; 150 paintViewR.y = insets.top; 151 paintViewR.width = c.getWidth() - (insets.left + insets.right); 152 paintViewR.height = c.getHeight() - (insets.top + insets.bottom); 153 154 paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0; 155 paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0; 156 157 String clippedText = 158 layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR); 159 160 if (icon != null) { 161 icon.paintIcon(c, g, paintIconR.x, paintIconR.y); 162 } 163 164 if (text != null) { 165 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 166 if (v != null) { 167 v.paint(g, paintTextR); 168 } else { 169 int textX = paintTextR.x; 170 int textY = paintTextR.y + fm.getAscent(); 171 172 if (label.isEnabled()) { 173 paintEnabledText(label, g, clippedText, textX, textY); 174 } 175 else { 176 paintDisabledText(label, g, clippedText, textX, textY); 177 } 178 } 179 } 180 } 181 182 183 187 private static Rectangle iconR = new Rectangle (); 188 private static Rectangle textR = new Rectangle (); 189 private static Rectangle viewR = new Rectangle (); 190 private static Insets viewInsets = new Insets (0, 0, 0, 0); 191 192 193 public Dimension getPreferredSize(JComponent c) 194 { 195 JLabel label = (JLabel)c; 196 String text = label.getText(); 197 Icon icon = (label.isEnabled()) ? label.getIcon() : 198 label.getDisabledIcon(); 199 Insets insets = label.getInsets(viewInsets); 200 Font font = label.getFont(); 201 202 int dx = insets.left + insets.right; 203 int dy = insets.top + insets.bottom; 204 205 if ((icon == null) && 206 ((text == null) || 207 ((text != null) && (font == null)))) { 208 return new Dimension (dx, dy); 209 } 210 else if ((text == null) || ((icon != null) && (font == null))) { 211 return new Dimension (icon.getIconWidth() + dx, 212 icon.getIconHeight() + dy); 213 } 214 else { 215 FontMetrics fm = label.getFontMetrics(font); 216 217 iconR.x = iconR.y = iconR.width = iconR.height = 0; 218 textR.x = textR.y = textR.width = textR.height = 0; 219 viewR.x = dx; 220 viewR.y = dy; 221 viewR.width = viewR.height = Short.MAX_VALUE; 222 223 layoutCL(label, fm, text, icon, viewR, iconR, textR); 224 int x1 = Math.min(iconR.x, textR.x); 225 int x2 = Math.max(iconR.x + iconR.width, textR.x + textR.width); 226 int y1 = Math.min(iconR.y, textR.y); 227 int y2 = Math.max(iconR.y + iconR.height, textR.y + textR.height); 228 Dimension rv = new Dimension (x2 - x1, y2 - y1); 229 230 rv.width += dx; 231 rv.height += dy; 232 return rv; 233 } 234 } 235 236 237 240 public Dimension getMinimumSize(JComponent c) { 241 Dimension d = getPreferredSize(c); 242 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 243 if (v != null) { 244 d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS); 245 } 246 return d; 247 } 248 249 252 public Dimension getMaximumSize(JComponent c) { 253 Dimension d = getPreferredSize(c); 254 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 255 if (v != null) { 256 d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS); 257 } 258 return d; 259 } 260 261 262 public void installUI(JComponent c) { 263 installDefaults((JLabel)c); 264 installComponents((JLabel)c); 265 installListeners((JLabel)c); 266 installKeyboardActions((JLabel)c); 267 } 268 269 270 public void uninstallUI(JComponent c) { 271 uninstallDefaults((JLabel)c); 272 uninstallComponents((JLabel)c); 273 uninstallListeners((JLabel)c); 274 uninstallKeyboardActions((JLabel)c); 275 } 276 277 protected void installDefaults(JLabel c){ 278 LookAndFeel.installColorsAndFont(c, "Label.background", "Label.foreground", "Label.font"); 279 LookAndFeel.installProperty(c, "opaque", Boolean.FALSE); 280 } 281 282 protected void installListeners(JLabel c){ 283 c.addPropertyChangeListener(this); 284 } 285 286 protected void installComponents(JLabel c){ 287 BasicHTML.updateRenderer(c, c.getText()); 288 c.setInheritsPopupMenu(true); 289 } 290 291 protected void installKeyboardActions(JLabel l) { 292 int dka = l.getDisplayedMnemonic(); 293 Component lf = l.getLabelFor(); 294 if ((dka != 0) && (lf != null)) { 295 LazyActionMap.installLazyActionMap(l, BasicLabelUI .class, 296 "Label.actionMap"); 297 InputMap inputMap = SwingUtilities.getUIInputMap 298 (l, JComponent.WHEN_IN_FOCUSED_WINDOW); 299 if (inputMap == null) { 300 inputMap = new ComponentInputMapUIResource(l); 301 SwingUtilities.replaceUIInputMap(l, 302 JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap); 303 } 304 inputMap.clear(); 305 inputMap.put(KeyStroke.getKeyStroke(dka, ActionEvent.ALT_MASK, 306 false), "press"); 307 } 308 else { 309 InputMap inputMap = SwingUtilities.getUIInputMap 310 (l, JComponent.WHEN_IN_FOCUSED_WINDOW); 311 if (inputMap != null) { 312 inputMap.clear(); 313 } 314 } 315 } 316 317 protected void uninstallDefaults(JLabel c){ 318 } 319 320 protected void uninstallListeners(JLabel c){ 321 c.removePropertyChangeListener(this); 322 } 323 324 protected void uninstallComponents(JLabel c){ 325 BasicHTML.updateRenderer(c, ""); 326 } 327 328 protected void uninstallKeyboardActions(JLabel c) { 329 SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null); 330 SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW, 331 null); 332 SwingUtilities.replaceUIActionMap(c, null); 333 } 334 335 public static ComponentUI createUI(JComponent c) { 336 if (System.getSecurityManager() != null) { 337 return SAFE_BASIC_LABEL_UI; 338 } else { 339 return labelUI; 340 } 341 } 342 343 public void propertyChange(PropertyChangeEvent e) { 344 String name = e.getPropertyName(); 345 if (name == "text" || "font" == name || "foreground" == name) { 346 JLabel lbl = ((JLabel) e.getSource()); 350 String text = lbl.getText(); 351 BasicHTML.updateRenderer(lbl, text); 352 } 353 else if (name == "labelFor" || name == "displayedMnemonic") { 354 installKeyboardActions((JLabel) e.getSource()); 355 } 356 } 357 358 private static class Actions extends UIAction { 363 private static final String PRESS = "press"; 364 private static final String RELEASE = "release"; 365 366 Actions(String key) { 367 super(key); 368 } 369 370 public void actionPerformed(ActionEvent e) { 371 JLabel label = (JLabel)e.getSource(); 372 String key = getName(); 373 if (key == PRESS) { 374 doPress(label); 375 } 376 else if (key == RELEASE) { 377 doRelease(label); 378 } 379 } 380 381 private void doPress(JLabel label) { 382 Component labelFor = label.getLabelFor(); 383 if(labelFor != null && labelFor.isEnabled()) { 384 InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED); 385 if (inputMap == null) { 386 inputMap = new InputMapUIResource(); 387 SwingUtilities.replaceUIInputMap(label, JComponent.WHEN_FOCUSED, inputMap); 388 } 389 int dka = label.getDisplayedMnemonic(); 390 inputMap.put(KeyStroke.getKeyStroke(dka, ActionEvent.ALT_MASK, true), RELEASE); 391 inputMap.put(KeyStroke.getKeyStroke(0, ActionEvent.ALT_MASK, true), RELEASE); 393 Component owner = label.getLabelFor(); 394 label.requestFocus(); 395 } 396 } 397 398 private void doRelease(JLabel label) { 399 Component labelFor = label.getLabelFor(); 400 if(labelFor != null && labelFor.isEnabled()) { 401 InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED); 402 if (inputMap != null) { 403 inputMap.remove(KeyStroke.getKeyStroke (label.getDisplayedMnemonic(), ActionEvent.ALT_MASK, true)); 405 inputMap.remove(KeyStroke.getKeyStroke(0, ActionEvent.ALT_MASK, true)); 406 } 407 if (labelFor instanceof Container && 408 ((Container )labelFor).isFocusCycleRoot()) { 409 labelFor.requestFocus(); 410 } 411 else { 412 BasicLookAndFeel.compositeRequestFocus(labelFor); 413 } 414 } 415 } 416 } 417 } 418 | Popular Tags |