VerticalLabelUI.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package CertainTrust;
  2. /**
  3. * @(#)VerticalLabelUI.java 1.0 02/18/09
  4. *
  5. * @see http://tips4java.wordpress.com/2009/02/21/vertical-label-ui/
  6. * @author Darryl Burke
  7. *
  8. * Some modifications to fix warnings shown by picky settings
  9. * like "possible static methods" and co
  10. * @author David Kalnischkies <kalnischkies@gmail.com>
  11. */
  12. import java.awt.Component;
  13. import java.awt.Dimension;
  14. import java.awt.FontMetrics;
  15. import java.awt.Graphics;
  16. import java.awt.Graphics2D;
  17. import java.awt.Rectangle;
  18. import javax.swing.Icon;
  19. import javax.swing.JComponent;
  20. import javax.swing.JLabel;
  21. import javax.swing.SwingUtilities;
  22. import javax.swing.plaf.ComponentUI;
  23. import javax.swing.plaf.basic.BasicLabelUI;
  24. /**
  25. * A UI delegate for JLabel that rotates the label 90�
  26. * <P>
  27. * Extends {@link BasicLabelUI}.
  28. * <P>
  29. * The only difference between the appearance of labels in the Basic and Metal
  30. * L&Fs is the manner in which diabled text is painted. As VerticalLabelUI
  31. * does not override the method paintDisabledText, this class can be adapted
  32. * for Metal L&F by extending MetalLabelUI instead of BasicLabelUI.
  33. * <P>
  34. * No other changes are required.
  35. *
  36. * @author Darryl
  37. */
  38. public class VerticalLabelUI extends BasicLabelUI {
  39. private boolean clockwise = false;
  40. // see comment in BasicLabelUI
  41. Rectangle verticalViewR = new Rectangle();
  42. Rectangle verticalIconR = new Rectangle();
  43. Rectangle verticalTextR = new Rectangle();
  44. protected static VerticalLabelUI verticalLabelUI =
  45. new VerticalLabelUI();
  46. private final static VerticalLabelUI SAFE_VERTICAL_LABEL_UI =
  47. new VerticalLabelUI();
  48. /**
  49. * Constructs a <code>VerticalLabelUI</code> with the default anticlockwise
  50. * rotation
  51. */
  52. public VerticalLabelUI() {
  53. }
  54. /**
  55. * Constructs a <code>VerticalLabelUI</code> with the desired rotation.
  56. * <P>
  57. * @param clockwise true to rotate clockwise, false for anticlockwise
  58. */
  59. public VerticalLabelUI(boolean clockwise) {
  60. this.clockwise = clockwise;
  61. }
  62. /**
  63. * @see ComponentUI#createUI(javax.swing.JComponent)
  64. */
  65. public static ComponentUI createUI(JComponent c) {
  66. if (System.getSecurityManager() != null)
  67. return SAFE_VERTICAL_LABEL_UI;
  68. return verticalLabelUI;
  69. }
  70. /**
  71. * Overridden to always return -1, since a vertical label does not have a
  72. * meaningful baseline.
  73. *
  74. * @see ComponentUI#getBaseline(JComponent, int, int)
  75. */
  76. @Override
  77. public int getBaseline(JComponent c, int width, int height) {
  78. super.getBaseline(c, width, height);
  79. return -1;
  80. }
  81. /**
  82. * Overridden to always return Component.BaselineResizeBehavior.OTHER,
  83. * since a vertical label does not have a meaningful baseline
  84. *
  85. * @see ComponentUI#getBaselineResizeBehavior(javax.swing.JComponent)
  86. */
  87. @Override
  88. public Component.BaselineResizeBehavior getBaselineResizeBehavior(
  89. JComponent c) {
  90. super.getBaselineResizeBehavior(c);
  91. return Component.BaselineResizeBehavior.OTHER;
  92. }
  93. /**
  94. * Transposes the view rectangles as appropriate for a vertical view
  95. * before invoking the super method and copies them after they have been
  96. * altered by {@link SwingUtilities#layoutCompoundLabel(FontMetrics, String,
  97. * Icon, int, int, int, int, Rectangle, Rectangle, Rectangle, int)}
  98. */
  99. @Override
  100. protected String layoutCL(JLabel label, FontMetrics fontMetrics,
  101. String text, Icon icon, Rectangle viewR, Rectangle iconR,
  102. Rectangle textR) {
  103. verticalViewR = transposeRectangle(viewR, verticalViewR);
  104. verticalIconR = transposeRectangle(iconR, verticalIconR);
  105. verticalTextR = transposeRectangle(textR, verticalTextR);
  106. text = super.layoutCL(label, fontMetrics, text, icon,
  107. verticalViewR, verticalIconR, verticalTextR);
  108. viewR = copyRectangle(verticalViewR, viewR);
  109. iconR = copyRectangle(verticalIconR, iconR);
  110. textR = copyRectangle(verticalTextR, textR);
  111. return text;
  112. }
  113. /**
  114. * Transforms the Graphics for vertical rendering and invokes the
  115. * super method.
  116. */
  117. @Override
  118. public void paint(Graphics g, JComponent c) {
  119. Graphics2D g2 = (Graphics2D) g.create();
  120. if (clockwise) {
  121. g2.rotate(Math.PI / 2, c.getSize().width / 2, c.getSize().width / 2);
  122. } else {
  123. g2.rotate(-Math.PI / 2, c.getSize().height / 2, c.getSize().height / 2);
  124. }
  125. super.paint(g2, c);
  126. }
  127. /**
  128. * Returns a Dimension appropriate for vertical rendering
  129. *
  130. * @see ComponentUI#getPreferredSize(javax.swing.JComponent)
  131. */
  132. @Override
  133. public Dimension getPreferredSize(JComponent c) {
  134. return transposeDimension(super.getPreferredSize(c));
  135. }
  136. /**
  137. * Returns a Dimension appropriate for vertical rendering
  138. *
  139. * @see ComponentUI#getMaximumSize(javax.swing.JComponent)
  140. */
  141. @Override
  142. public Dimension getMaximumSize(JComponent c) {
  143. return transposeDimension(super.getMaximumSize(c));
  144. }
  145. /**
  146. * Returns a Dimension appropriate for vertical rendering
  147. *
  148. * @see ComponentUI#getMinimumSize(javax.swing.JComponent)
  149. */
  150. @Override
  151. public Dimension getMinimumSize(JComponent c) {
  152. return transposeDimension(super.getMinimumSize(c));
  153. }
  154. private static Dimension transposeDimension(Dimension from) {
  155. return new Dimension(from.height, from.width + 2);
  156. }
  157. private static Rectangle transposeRectangle(Rectangle from, Rectangle to) {
  158. if (to == null) {
  159. to = new Rectangle();
  160. }
  161. to.x = from.y;
  162. to.y = from.x;
  163. to.width = from.height;
  164. to.height = from.width;
  165. return to;
  166. }
  167. private static Rectangle copyRectangle(Rectangle from, Rectangle to) {
  168. if (to == null) {
  169. to = new Rectangle();
  170. }
  171. to.x = from.x;
  172. to.y = from.y;
  173. to.width = from.width;
  174. to.height = from.height;
  175. return to;
  176. }
  177. }