/plugins/LookAndFeel/tags/1.3.0/lipstik-1.1/src/com/lipstikLF/delegate/LipstikComboBoxButton.java

# · Java · 240 lines · 198 code · 33 blank · 9 comment · 21 complexity · 6e4a28536654363e0223d9cc81d1084f MD5 · raw file

  1. package com.lipstikLF.delegate;
  2. import java.awt.Component;
  3. import java.awt.Graphics;
  4. import java.awt.Insets;
  5. import java.io.Serializable;
  6. import javax.swing.CellRendererPane;
  7. import javax.swing.DefaultButtonModel;
  8. import javax.swing.Icon;
  9. import javax.swing.JButton;
  10. import javax.swing.JComboBox;
  11. import javax.swing.JComponent;
  12. import javax.swing.JList;
  13. import javax.swing.JPanel;
  14. import javax.swing.ListCellRenderer;
  15. import javax.swing.UIManager;
  16. import javax.swing.plaf.metal.MetalLookAndFeel;
  17. import com.lipstikLF.LipstikLookAndFeel;
  18. import com.lipstikLF.theme.LipstikColorTheme;
  19. import com.lipstikLF.util.LipstikBorderFactory;
  20. public class LipstikComboBoxButton extends JButton
  21. {
  22. private final JList listBox;
  23. private final CellRendererPane rendererPane;
  24. private static final ComboBoxButtonIcon comboIcon = new ComboBoxButtonIcon();
  25. private JComboBox comboBox;
  26. protected boolean iconOnly = false;
  27. LipstikComboBoxButton(JComboBox comboBox,boolean iconOnly,
  28. CellRendererPane rendererPane, JList listBox)
  29. {
  30. super("");
  31. setModel(new DefaultButtonModel()
  32. {
  33. public void setArmed(boolean armed)
  34. {
  35. super.setArmed(isPressed() || armed);
  36. }
  37. });
  38. this.comboBox = comboBox;
  39. this.iconOnly = iconOnly;
  40. this.rendererPane = rendererPane;
  41. this.listBox = listBox;
  42. setEnabled(comboBox.isEnabled());
  43. setFocusable(false);
  44. setRequestFocusEnabled(comboBox.isEnabled());
  45. if (iconOnly)
  46. setBorder(UIManager.getBorder("ComboBox.arrowButtonBorder"));
  47. setMargin(new Insets(2, 4, 0, 3));
  48. }
  49. public JComboBox getComboBox()
  50. {
  51. return comboBox;
  52. }
  53. public void setComboBox(JComboBox cb)
  54. {
  55. comboBox = cb;
  56. }
  57. public Icon getComboIcon()
  58. {
  59. return comboIcon;
  60. }
  61. public boolean isIconOnly()
  62. {
  63. return iconOnly;
  64. }
  65. public boolean isFocusTraversable()
  66. {
  67. return false;
  68. }
  69. public void setIconOnly(boolean b)
  70. {
  71. iconOnly = b;
  72. }
  73. /**
  74. * Paints the component; honors the 3D settings and
  75. * tries to switch the renderer component to transparent.
  76. */
  77. public void paintComponent(Graphics g)
  78. {
  79. super.paintComponent(g);
  80. boolean leftToRight = comboBox.getComponentOrientation().isLeftToRight();
  81. Insets insets = getInsets();
  82. int width = getWidth() - (insets.left + insets.right);
  83. int height = getHeight() - (insets.top + insets.bottom);
  84. if (height <= 0 || width <= 0)
  85. return;
  86. int left = insets.left;
  87. int top = insets.top;
  88. int iconLeft;
  89. int iconWidth = 0;
  90. int hoffs = 0;
  91. int xoffs = 0;
  92. // Paint the icon
  93. if (comboIcon != null)
  94. {
  95. iconWidth = comboIcon.getIconWidth();
  96. int iconHeight = comboIcon.getIconHeight();
  97. int iconTop = (getHeight() - iconHeight) >> 1;
  98. if (iconOnly)
  99. {
  100. iconLeft = (width - iconWidth) >> 1;
  101. if (!leftToRight) iconLeft++;
  102. }
  103. else
  104. {
  105. if (leftToRight)
  106. {
  107. iconLeft = (left + (width)) - iconWidth;
  108. hoffs = iconLeft-4;
  109. xoffs = left;
  110. }
  111. else
  112. {
  113. iconLeft = left;
  114. hoffs = iconLeft+iconWidth+2;
  115. xoffs = hoffs+5;
  116. }
  117. }
  118. comboIcon.paintIcon(this, g, iconLeft, iconTop);
  119. }
  120. // Let the renderer paint
  121. if (!iconOnly && comboBox != null)
  122. {
  123. LipstikColorTheme theme = LipstikLookAndFeel.getMyCurrentTheme();
  124. g.setColor(theme.getControlDarkShadow());
  125. g.drawLine(hoffs,0,hoffs,height+1);
  126. ListCellRenderer renderer = comboBox.getRenderer();
  127. Component c = renderer.getListCellRendererComponent(
  128. listBox,
  129. comboBox.getSelectedItem(),
  130. -1,
  131. getModel().isPressed(),
  132. false);
  133. c.setFont(rendererPane.getFont());
  134. if (model.isArmed() && model.isPressed())
  135. {
  136. if (isOpaque())
  137. c.setBackground(UIManager.getColor("Button.select"));
  138. c.setForeground(comboBox.getForeground());
  139. }
  140. else
  141. if (!comboBox.isEnabled())
  142. {
  143. if (isOpaque())
  144. c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
  145. c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
  146. }
  147. else
  148. {
  149. c.setForeground(comboBox.getForeground());
  150. c.setBackground(comboBox.getBackground());
  151. }
  152. int cWidth = width - (insets.right + iconWidth);
  153. // Fix for 4238829: should lay out the JPanel.
  154. boolean shouldValidate = c instanceof JPanel;
  155. if (c instanceof JComponent)
  156. {
  157. // In case, we are in 3D mode _and_ have a JComponent renderer,
  158. // store the opaque state, set it to transparent, paint, then restore.
  159. JComponent component = (JComponent) c;
  160. component.setOpaque(false);
  161. rendererPane.paintComponent(
  162. g,
  163. c,
  164. this,
  165. xoffs,
  166. top+1,
  167. cWidth,
  168. height-3,
  169. shouldValidate);
  170. component.setOpaque(true);
  171. }
  172. else
  173. {
  174. rendererPane.paintComponent(
  175. g,
  176. c,
  177. this,
  178. xoffs,
  179. top+1,
  180. cWidth,
  181. height-3,
  182. shouldValidate);
  183. }
  184. }
  185. if (comboBox != null && comboBox.hasFocus())
  186. LipstikBorderFactory.paintFocusBorder(g, 2, 2, getWidth()-4, getHeight()-4);
  187. }
  188. }
  189. class ComboBoxButtonIcon implements Icon, Serializable
  190. {
  191. public void paintIcon(Component c, Graphics g, int x, int y)
  192. {
  193. JComponent component = (JComponent)c;
  194. int iconWidth = getIconWidth();
  195. g.translate(x, y);
  196. g.setColor( component.isEnabled()
  197. ? MetalLookAndFeel.getControlInfo()
  198. : MetalLookAndFeel.getControlDarkShadow() );
  199. g.drawLine( 0, 0, iconWidth - 1, 0 );
  200. g.drawLine( 1, 1, 1 + (iconWidth - 3), 1 );
  201. g.drawLine( 2, 2, 2 + (iconWidth - 5), 2 );
  202. g.drawLine( 3, 3, 3 + (iconWidth - 7), 3 );
  203. g.translate( -x, -y );
  204. }
  205. public int getIconWidth() { return 8; }
  206. public int getIconHeight() { return 4; }
  207. }