1. /*
  2. * @(#)CenterLayout.java 1.8 00/02/02
  3. *
  4. * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.plaf.basic;
  11. import java.awt.*;
  12. import java.io.*;
  13. /**
  14. * Center-positioning layout manager.
  15. * @version 1.8 02/02/00
  16. * @author Tom Santos
  17. * @author Steve Wilson
  18. */
  19. class CenterLayout implements LayoutManager, Serializable {
  20. public void addLayoutComponent(String name, Component comp) { }
  21. public void removeLayoutComponent(Component comp) { }
  22. public Dimension preferredLayoutSize( Container container ) {
  23. Component c = container.getComponent( 0 );
  24. if ( c != null ) {
  25. Dimension size = c.getPreferredSize();
  26. Insets insets = container.getInsets();
  27. size.width += insets.left + insets.right;
  28. size.height += insets.top + insets.bottom;
  29. return size;
  30. }
  31. else {
  32. return new Dimension( 0, 0 );
  33. }
  34. }
  35. public Dimension minimumLayoutSize(Container cont) {
  36. return preferredLayoutSize(cont);
  37. }
  38. public void layoutContainer(Container container) {
  39. try {
  40. Component c = container.getComponent( 0 );
  41. c.setSize( c.getPreferredSize() );
  42. Dimension size = c.getSize();
  43. Dimension containerSize = container.getSize();
  44. Insets containerInsets = container.getInsets();
  45. containerSize.width -= containerInsets.left + containerInsets.right;
  46. containerSize.height -= containerInsets.top + containerInsets.bottom;
  47. int componentLeft = (containerSize.width / 2) - (size.width / 2);
  48. int componentTop = (containerSize.height / 2) - (size.height / 2);
  49. componentLeft += containerInsets.left;
  50. componentTop += containerInsets.top;
  51. c.setBounds( componentLeft, componentTop, size.width, size.height );
  52. }
  53. catch( Exception e ) {
  54. }
  55. }
  56. }