1. /*
  2. * @(#)SynthToolBarUI.java 1.10 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.synth;
  8. import javax.swing.*;
  9. import javax.swing.event.*;
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import java.beans.*;
  13. import javax.swing.border.*;
  14. import javax.swing.plaf.*;
  15. import javax.swing.plaf.basic.BasicToolBarUI;
  16. import sun.swing.plaf.synth.*;
  17. /**
  18. * A Synth L&F implementation of ToolBarUI. This implementation
  19. * is a "combined" view/controller.
  20. * <p>
  21. *
  22. * @version 1.10, 12/19/03
  23. * @author Georges Saab
  24. * @author Jeff Shapiro
  25. */
  26. class SynthToolBarUI extends BasicToolBarUI implements PropertyChangeListener,
  27. SynthUI {
  28. protected Icon handleIcon = null;
  29. protected Rectangle contentRect = new Rectangle();
  30. private SynthStyle style;
  31. private SynthStyle contentStyle;
  32. private SynthStyle dragWindowStyle;
  33. public static ComponentUI createUI(JComponent c) {
  34. return new SynthToolBarUI();
  35. }
  36. protected void installDefaults() {
  37. toolBar.setLayout(createLayout());
  38. updateStyle(toolBar);
  39. }
  40. protected void installListeners() {
  41. super.installListeners();
  42. toolBar.addPropertyChangeListener(this);
  43. }
  44. protected void uninstallListeners() {
  45. super.uninstallListeners();
  46. toolBar.removePropertyChangeListener(this);
  47. }
  48. private void updateStyle(JToolBar c) {
  49. SynthContext context = getContext(c, ENABLED);
  50. SynthStyle oldStyle = style;
  51. style = SynthLookAndFeel.updateStyle(context, this);
  52. if (oldStyle != style) {
  53. handleIcon =
  54. style.getIcon(context, "ToolBar.handleIcon");
  55. if (oldStyle != null) {
  56. uninstallKeyboardActions();
  57. installKeyboardActions();
  58. }
  59. }
  60. context.dispose();
  61. context = getContext(c, Region.TOOL_BAR_CONTENT, ENABLED);
  62. contentStyle = SynthLookAndFeel.updateStyle(context, this);
  63. context.dispose();
  64. context = getContext(c, Region.TOOL_BAR_DRAG_WINDOW, ENABLED);
  65. dragWindowStyle = SynthLookAndFeel.updateStyle(context, this);
  66. context.dispose();
  67. }
  68. protected void uninstallDefaults() {
  69. SynthContext context = getContext(toolBar, ENABLED);
  70. style.uninstallDefaults(context);
  71. context.dispose();
  72. style = null;
  73. handleIcon = null;
  74. context = getContext(toolBar, Region.TOOL_BAR_CONTENT, ENABLED);
  75. contentStyle.uninstallDefaults(context);
  76. context.dispose();
  77. contentStyle = null;
  78. context = getContext(toolBar, Region.TOOL_BAR_DRAG_WINDOW, ENABLED);
  79. dragWindowStyle.uninstallDefaults(context);
  80. context.dispose();
  81. dragWindowStyle = null;
  82. toolBar.setLayout(null);
  83. }
  84. protected void installComponents() {
  85. }
  86. protected void uninstallComponents() {
  87. }
  88. protected LayoutManager createLayout() {
  89. return new SynthToolBarLayoutManager();
  90. }
  91. public SynthContext getContext(JComponent c) {
  92. return getContext(c, getComponentState(c));
  93. }
  94. private SynthContext getContext(JComponent c, int state) {
  95. return SynthContext.getContext(SynthContext.class, c,
  96. SynthLookAndFeel.getRegion(c), style, state);
  97. }
  98. private SynthContext getContext(JComponent c, Region region) {
  99. return getContext(c, region, getComponentState(c, region));
  100. }
  101. private SynthContext getContext(JComponent c, Region region, int state) {
  102. return SynthContext.getContext(SynthContext.class, c, region,
  103. dragWindowStyle, state);
  104. }
  105. private Region getRegion(JComponent c) {
  106. return SynthLookAndFeel.getRegion(c);
  107. }
  108. private int getComponentState(JComponent c) {
  109. return SynthLookAndFeel.getComponentState(c);
  110. }
  111. private int getComponentState(JComponent c, Region region) {
  112. return SynthLookAndFeel.getComponentState(c);
  113. }
  114. public void update(Graphics g, JComponent c) {
  115. SynthContext context = getContext(c);
  116. SynthLookAndFeel.update(context, g);
  117. context.getPainter().paintToolBarBackground(context,
  118. g, 0, 0, c.getWidth(), c.getHeight());
  119. paint(context, g);
  120. context.dispose();
  121. }
  122. public void paint(Graphics g, JComponent c) {
  123. SynthContext context = getContext(c);
  124. paint(context, g);
  125. context.dispose();
  126. }
  127. public void paintBorder(SynthContext context, Graphics g, int x,
  128. int y, int w, int h) {
  129. context.getPainter().paintToolBarBorder(context, g, x, y, w, h);
  130. }
  131. // Overloaded to do nothing so we can share listeners.
  132. protected void setBorderToNonRollover(Component c) {}
  133. // Overloaded to do nothing so we can share listeners.
  134. protected void setBorderToRollover(Component c) {}
  135. // Overloaded to do nothing so we can share listeners.
  136. protected void setBorderToNormal(Component c) {}
  137. protected void paint(SynthContext context, Graphics g) {
  138. if (handleIcon != null && toolBar.isFloatable()) {
  139. int startX = toolBar.getComponentOrientation().isLeftToRight() ?
  140. 0 : toolBar.getWidth() -
  141. SynthIcon.getIconWidth(handleIcon, context);
  142. SynthIcon.paintIcon(handleIcon, context, g, startX, 0,
  143. SynthIcon.getIconWidth(handleIcon, context),
  144. SynthIcon.getIconHeight(handleIcon, context));
  145. }
  146. SynthContext subcontext = getContext(toolBar, Region.TOOL_BAR_CONTENT);
  147. paintContent(subcontext, g, contentRect);
  148. subcontext.dispose();
  149. }
  150. public void paintContent(SynthContext context, Graphics g,
  151. Rectangle bounds) {
  152. SynthLookAndFeel.updateSubregion(context, g, bounds);
  153. context.getPainter().paintToolBarContentBackground(context, g,
  154. bounds.x, bounds.y, bounds.width, bounds.height);
  155. context.getPainter().paintToolBarContentBorder(context, g,
  156. bounds.x, bounds.y, bounds.width, bounds.height);
  157. }
  158. protected void paintDragWindow(Graphics g) {
  159. int w = dragWindow.getWidth();
  160. int h = dragWindow.getHeight();
  161. SynthContext context = getContext(toolBar,Region.TOOL_BAR_DRAG_WINDOW);
  162. SynthLookAndFeel.updateSubregion(context, g, new Rectangle(
  163. 0, 0, w, h));
  164. context.getPainter().paintToolBarDragWindowBackground(context,
  165. g, 0, 0, w, h);
  166. context.getPainter().paintToolBarDragWindowBorder(context, g, 0,0,w,h);
  167. context.dispose();
  168. }
  169. //
  170. // PropertyChangeListener
  171. //
  172. public void propertyChange(PropertyChangeEvent e) {
  173. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  174. updateStyle((JToolBar)e.getSource());
  175. }
  176. }
  177. class SynthToolBarLayoutManager implements LayoutManager {
  178. public void addLayoutComponent(String name, Component comp) {}
  179. public void removeLayoutComponent(Component comp) {}
  180. public Dimension minimumLayoutSize(Container parent) {
  181. JToolBar tb = (JToolBar)parent;
  182. Dimension dim = new Dimension();
  183. SynthContext context = getContext(tb);
  184. if (tb.getOrientation() == JToolBar.HORIZONTAL) {
  185. dim.width = SynthIcon.getIconWidth(handleIcon, context);
  186. Dimension compDim;
  187. for (int i = 0; i < tb.getComponentCount(); i++) {
  188. compDim = tb.getComponent(i).getMinimumSize();
  189. dim.width += compDim.width;
  190. dim.height = Math.max(dim.height, compDim.height);
  191. }
  192. } else {
  193. dim.height =
  194. SynthIcon.getIconHeight(handleIcon, context);
  195. Dimension compDim;
  196. for (int i = 0; i < tb.getComponentCount(); i++) {
  197. compDim = tb.getComponent(i).getMinimumSize();
  198. dim.width = Math.max(dim.width, compDim.width);
  199. dim.height += compDim.height;
  200. }
  201. }
  202. context.dispose();
  203. return dim;
  204. }
  205. public Dimension preferredLayoutSize(Container parent) {
  206. JToolBar tb = (JToolBar)parent;
  207. Dimension dim = new Dimension();
  208. SynthContext context = getContext(tb);
  209. if (tb.getOrientation() == JToolBar.HORIZONTAL) {
  210. dim.width = SynthIcon.getIconWidth(handleIcon, context);
  211. Dimension compDim;
  212. for (int i = 0; i < tb.getComponentCount(); i++) {
  213. compDim = tb.getComponent(i).getPreferredSize();
  214. dim.width += compDim.width;
  215. dim.height = Math.max(dim.height, compDim.height);
  216. }
  217. } else {
  218. dim.height =
  219. SynthIcon.getIconHeight(handleIcon, context);
  220. Dimension compDim;
  221. for (int i = 0; i < tb.getComponentCount(); i++) {
  222. compDim = tb.getComponent(i).getPreferredSize();
  223. dim.width = Math.max(dim.width, compDim.width);
  224. dim.height += compDim.height;
  225. }
  226. }
  227. context.dispose();
  228. return dim;
  229. }
  230. public void layoutContainer(Container parent) {
  231. JToolBar tb = (JToolBar)parent;
  232. boolean ltr = tb.getComponentOrientation().isLeftToRight();
  233. SynthContext context = getContext(tb);
  234. int handleWidth = SynthIcon.getIconWidth(handleIcon, context);
  235. Component c;
  236. Dimension d;
  237. if (tb.getOrientation() == JToolBar.HORIZONTAL) {
  238. int x = ltr ? handleWidth : tb.getWidth() - handleWidth;
  239. for (int i = 0; i < tb.getComponentCount(); i++) {
  240. c = tb.getComponent(i);
  241. d = c.getPreferredSize();
  242. c.setBounds(ltr ? x : x - d.width, 0, d.width, d.height);
  243. x = ltr ? x + d.width : x - d.width;
  244. }
  245. contentRect.x = ltr ?
  246. SynthIcon.getIconWidth(handleIcon, context) : 0;
  247. contentRect.y = 0;
  248. contentRect.width = tb.getWidth() - contentRect.x;
  249. contentRect.height = tb.getHeight();
  250. } else {
  251. int y = SynthIcon.getIconHeight(handleIcon, context);
  252. for (int i = 0; i < tb.getComponentCount(); i++) {
  253. c = tb.getComponent(i);
  254. d = c.getPreferredSize();
  255. c.setBounds(0, y, d.width, d.height);
  256. y += d.height;
  257. }
  258. contentRect.x = 0;
  259. contentRect.y =
  260. SynthIcon.getIconHeight(handleIcon, context);
  261. contentRect.width = tb.getWidth();
  262. contentRect.height = tb.getHeight() - contentRect.y;
  263. }
  264. context.dispose();
  265. }
  266. }
  267. }