1. /*
  2. * @(#)SynthLabelUI.java 1.15 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.gtk;
  8. import javax.swing.*;
  9. import javax.swing.plaf.*;
  10. import javax.swing.plaf.basic.BasicHTML;
  11. import javax.swing.text.View;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.awt.Component;
  15. import java.awt.Container;
  16. import java.awt.Dimension;
  17. import java.awt.Rectangle;
  18. import java.awt.Insets;
  19. import java.awt.Color;
  20. import java.awt.Graphics;
  21. import java.awt.Font;
  22. import java.awt.FontMetrics;
  23. import java.beans.PropertyChangeEvent;
  24. import java.beans.PropertyChangeListener;
  25. /**
  26. * A skinnable implementation of LabelUI.
  27. *
  28. * @version 1.15, 01/23/03 (based on BasicLabelUI v 1.76)
  29. * @author Hans Muller
  30. */
  31. class SynthLabelUI extends LabelUI implements PropertyChangeListener,
  32. SynthUI {
  33. private SynthStyle style;
  34. public static void loadActionMap(ActionMap map) {
  35. // NOTE: this needs to remain static. If you have a need to
  36. // have Actions that reference the UI in the ActionMap,
  37. // then you'll also need to change the registeration of the
  38. // ActionMap.
  39. map.put("press", new PressAction());
  40. map.put("release", new ReleaseAction());
  41. }
  42. /**
  43. * Returns the LabelUI implementation used for the skins look and feel.
  44. */
  45. public static ComponentUI createUI(JComponent c){
  46. return new SynthLabelUI();
  47. }
  48. public void installUI(JComponent c) {
  49. installDefaults((JLabel)c);
  50. installComponents((JLabel)c);
  51. installListeners((JLabel)c);
  52. installKeyboardActions((JLabel)c);
  53. }
  54. protected void installDefaults(JLabel c) {
  55. fetchStyle(c);
  56. }
  57. void forceFetchStyle(JLabel c) {
  58. style = null;
  59. fetchStyle(c);
  60. }
  61. private void fetchStyle(JLabel c) {
  62. SynthContext context = getContext(c, ENABLED);
  63. style = SynthLookAndFeel.updateStyle(context, this);
  64. context.dispose();
  65. }
  66. protected void installListeners(JLabel c) {
  67. c.addPropertyChangeListener(this);
  68. }
  69. protected void installComponents(JLabel c){
  70. BasicHTML.updateRenderer(c, c.getText());
  71. }
  72. protected void installKeyboardActions(JLabel l) {
  73. int dka = l.getDisplayedMnemonic();
  74. Component lf = l.getLabelFor();
  75. if ((dka != 0) && (lf != null)) {
  76. ActionMap map = SwingUtilities.getUIActionMap(l);
  77. if (map == null) {
  78. LazyActionMap.installLazyActionMap(l, SynthLabelUI.class,
  79. "Label.actionMap");
  80. }
  81. InputMap inputMap = SwingUtilities.getUIInputMap
  82. (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
  83. if (inputMap == null) {
  84. inputMap = new ComponentInputMapUIResource(l);
  85. SwingUtilities.replaceUIInputMap(l,
  86. JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
  87. }
  88. inputMap.clear();
  89. inputMap.put(KeyStroke.getKeyStroke(dka, ActionEvent.ALT_MASK,
  90. false), "press");
  91. }
  92. else {
  93. InputMap inputMap = SwingUtilities.getUIInputMap
  94. (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
  95. if (inputMap != null) {
  96. inputMap.clear();
  97. }
  98. }
  99. }
  100. public void uninstallUI(JComponent c) {
  101. uninstallDefaults((JLabel)c);
  102. uninstallComponents((JLabel)c);
  103. uninstallListeners((JLabel)c);
  104. uninstallKeyboardActions((JLabel)c);
  105. }
  106. protected void uninstallDefaults(JLabel c){
  107. SynthContext context = getContext(c, ENABLED);
  108. style.uninstallDefaults(context);
  109. context.dispose();
  110. style = null;
  111. }
  112. protected void uninstallListeners(JLabel c){
  113. c.removePropertyChangeListener(this);
  114. }
  115. protected void uninstallComponents(JLabel c){
  116. BasicHTML.updateRenderer(c, "");
  117. }
  118. protected void uninstallKeyboardActions(JLabel c) {
  119. SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null);
  120. SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW,
  121. null);
  122. SwingUtilities.replaceUIActionMap(c, null);
  123. }
  124. public SynthContext getContext(JComponent c) {
  125. return getContext(c, getComponentState(c));
  126. }
  127. private SynthContext getContext(JComponent c, int state) {
  128. return SynthContext.getContext(SynthContext.class, c,
  129. SynthLookAndFeel.getRegion(c), style, state);
  130. }
  131. private Region getRegion(JComponent c) {
  132. return SynthLookAndFeel.getRegion(c);
  133. }
  134. private int getComponentState(JComponent c) {
  135. return SynthLookAndFeel.getComponentState(c);
  136. }
  137. /**
  138. * Notifies this UI delegate that it's time to paint the specified
  139. * component. This method is invoked by <code>JComponent</code>
  140. * when the specified component is being painted.
  141. */
  142. public void update(Graphics g, JComponent c) {
  143. SynthContext context = getContext(c);
  144. SynthLookAndFeel.update(context, g);
  145. paint(context, g);
  146. context.dispose();
  147. }
  148. public void paint(Graphics g, JComponent c) {
  149. SynthContext context = getContext(c);
  150. paint(context, g);
  151. context.dispose();
  152. }
  153. protected void paint(SynthContext context, Graphics g) {
  154. JLabel label = (JLabel)context.getComponent();
  155. Icon icon = (label.isEnabled()) ? label.getIcon() :
  156. label.getDisabledIcon();
  157. g.setColor(context.getStyle().getColor(context,
  158. ColorType.TEXT_FOREGROUND));
  159. g.setFont(style.getFont(context));
  160. context.getStyle().getSynthGraphics(context).paintText(
  161. context, g, label.getText(), icon,
  162. label.getHorizontalAlignment(), label.getVerticalAlignment(),
  163. label.getHorizontalTextPosition(), label.getVerticalTextPosition(),
  164. label.getIconTextGap(), label.getDisplayedMnemonicIndex(), 0);
  165. }
  166. public Dimension getPreferredSize(JComponent c) {
  167. JLabel label = (JLabel)c;
  168. Icon icon = (label.isEnabled()) ? label.getIcon() :
  169. label.getDisabledIcon();
  170. SynthContext context = getContext(c);
  171. Dimension size = context.getStyle().getSynthGraphics(context).
  172. getPreferredSize(
  173. context, context.getStyle().getFont(context), label.getText(),
  174. icon, label.getHorizontalAlignment(),
  175. label.getVerticalAlignment(), label.getHorizontalTextPosition(),
  176. label.getVerticalTextPosition(), label.getIconTextGap(),
  177. label.getDisplayedMnemonicIndex());
  178. context.dispose();
  179. return size;
  180. }
  181. public Dimension getMinimumSize(JComponent c) {
  182. JLabel label = (JLabel)c;
  183. Icon icon = (label.isEnabled()) ? label.getIcon() :
  184. label.getDisabledIcon();
  185. SynthContext context = getContext(c);
  186. Dimension size = context.getStyle().getSynthGraphics(context).
  187. getMinimumSize(
  188. context, context.getStyle().getFont(context), label.getText(),
  189. icon, label.getHorizontalAlignment(),
  190. label.getVerticalAlignment(), label.getHorizontalTextPosition(),
  191. label.getVerticalTextPosition(), label.getIconTextGap(),
  192. label.getDisplayedMnemonicIndex());
  193. context.dispose();
  194. return size;
  195. }
  196. public Dimension getMaximumSize(JComponent c) {
  197. JLabel label = (JLabel)c;
  198. Icon icon = (label.isEnabled()) ? label.getIcon() :
  199. label.getDisabledIcon();
  200. SynthContext context = getContext(c);
  201. Dimension size = context.getStyle().getSynthGraphics(context).
  202. getMaximumSize(
  203. context, context.getStyle().getFont(context), label.getText(),
  204. icon, label.getHorizontalAlignment(),
  205. label.getVerticalAlignment(), label.getHorizontalTextPosition(),
  206. label.getVerticalTextPosition(), label.getIconTextGap(),
  207. label.getDisplayedMnemonicIndex());
  208. context.dispose();
  209. return size;
  210. }
  211. public void propertyChange(PropertyChangeEvent e) {
  212. String name = e.getPropertyName();
  213. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  214. fetchStyle((JLabel)e.getSource());
  215. }
  216. if (name.equals("text") || "font".equals(name) ||
  217. "foreground".equals(name)) {
  218. // remove the old html view client property if one
  219. // existed, and install a new one if the text installed
  220. // into the JLabel is html source.
  221. JLabel lbl = ((JLabel) e.getSource());
  222. String text = lbl.getText();
  223. BasicHTML.updateRenderer(lbl, text);
  224. }
  225. else if (name.equals("labelFor") ||
  226. name.equals("displayedMnemonic")) {
  227. installKeyboardActions((JLabel) e.getSource());
  228. }
  229. }
  230. // When the accelerator is pressed, temporarily make the JLabel
  231. // focusTraversable by registering a WHEN_FOCUSED action for the
  232. // release of the accelerator. Then give it focus so it can
  233. // prevent unwanted keyTyped events from getting to other components.
  234. static class PressAction extends AbstractAction {
  235. PressAction() {
  236. }
  237. public void actionPerformed(ActionEvent e) {
  238. JLabel label = (JLabel)e.getSource();
  239. Component labelFor = label.getLabelFor();
  240. if(labelFor != null && labelFor.isEnabled()) {
  241. InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
  242. if (inputMap == null) {
  243. inputMap = new InputMapUIResource();
  244. SwingUtilities.replaceUIInputMap(label, JComponent.WHEN_FOCUSED, inputMap);
  245. }
  246. int dka = label.getDisplayedMnemonic();
  247. inputMap.put(KeyStroke.getKeyStroke(dka, ActionEvent.ALT_MASK, true), "release");
  248. // Need this if the accelerator is released before the ALT key
  249. inputMap.put(KeyStroke.getKeyStroke(0, ActionEvent.ALT_MASK, true), "release");
  250. Component owner = label.getLabelFor();
  251. label.requestFocus();
  252. }
  253. }
  254. }
  255. // On the release of the accelerator, remove the keyboard action
  256. // that allows the label to take focus and then give focus to the
  257. // labelFor component.
  258. static class ReleaseAction extends AbstractAction {
  259. ReleaseAction() {
  260. }
  261. public void actionPerformed(ActionEvent e) {
  262. JLabel label = (JLabel)e.getSource();
  263. Component labelFor = label.getLabelFor();
  264. if(labelFor != null && labelFor.isEnabled()) {
  265. InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
  266. if (inputMap != null) {
  267. // inputMap should never be null.
  268. inputMap.remove(KeyStroke.getKeyStroke (label.getDisplayedMnemonic(), ActionEvent.ALT_MASK, true));
  269. inputMap.remove(KeyStroke.getKeyStroke(0, ActionEvent.ALT_MASK, true));
  270. }
  271. label.getLabelFor().requestFocus();
  272. }
  273. }
  274. }
  275. }