1. /*
  2. * @(#)CenterLayout.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.colorchooser;
  8. import java.awt.*;
  9. import java.io.*;
  10. /**
  11. * Center-positioning layout manager.
  12. * @version 1.10 12/19/03
  13. * @author Tom Santos
  14. * @author Steve Wilson
  15. */
  16. class CenterLayout implements LayoutManager, Serializable {
  17. public void addLayoutComponent(String name, Component comp) { }
  18. public void removeLayoutComponent(Component comp) { }
  19. public Dimension preferredLayoutSize( Container container ) {
  20. Component c = container.getComponent( 0 );
  21. if ( c != null ) {
  22. Dimension size = c.getPreferredSize();
  23. Insets insets = container.getInsets();
  24. size.width += insets.left + insets.right;
  25. size.height += insets.top + insets.bottom;
  26. return size;
  27. }
  28. else {
  29. return new Dimension( 0, 0 );
  30. }
  31. }
  32. public Dimension minimumLayoutSize(Container cont) {
  33. return preferredLayoutSize(cont);
  34. }
  35. public void layoutContainer(Container container) {
  36. try {
  37. Component c = container.getComponent( 0 );
  38. c.setSize( c.getPreferredSize() );
  39. Dimension size = c.getSize();
  40. Dimension containerSize = container.getSize();
  41. Insets containerInsets = container.getInsets();
  42. containerSize.width -= containerInsets.left + containerInsets.right;
  43. containerSize.height -= containerInsets.top + containerInsets.bottom;
  44. int componentLeft = (containerSize.width / 2) - (size.width / 2);
  45. int componentTop = (containerSize.height / 2) - (size.height / 2);
  46. componentLeft += containerInsets.left;
  47. componentTop += containerInsets.top;
  48. c.setBounds( componentLeft, componentTop, size.width, size.height );
  49. }
  50. catch( Exception e ) {
  51. }
  52. }
  53. }