CenterLayout.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * -----------------
  28. * CenterLayout.java
  29. * -----------------
  30. * (C) Copyright 2000-2005, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * $Id: CenterLayout.java,v 1.6 2005/11/16 15:58:40 taqua Exp $
  36. *
  37. * Changes (from 5-Nov-2001)
  38. * -------------------------
  39. * 05-Nov-2001 : Changed package to com.jrefinery.layout.* (DG);
  40. * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41. *
  42. */
  43. package org.jfree.layout;
  44. import java.awt.Component;
  45. import java.awt.Container;
  46. import java.awt.Dimension;
  47. import java.awt.Insets;
  48. import java.awt.LayoutManager;
  49. import java.io.Serializable;
  50. /**
  51. * A layout manager that displays a single component in the center of its
  52. * container.
  53. *
  54. * @author David Gilbert
  55. */
  56. public class CenterLayout implements LayoutManager, Serializable {
  57. /** For serialization. */
  58. private static final long serialVersionUID = 469319532333015042L;
  59. /**
  60. * Creates a new layout manager.
  61. */
  62. public CenterLayout() {
  63. }
  64. /**
  65. * Returns the preferred size.
  66. *
  67. * @param parent the parent.
  68. *
  69. * @return the preferred size.
  70. */
  71. public Dimension preferredLayoutSize(final Container parent) {
  72. synchronized (parent.getTreeLock()) {
  73. final Insets insets = parent.getInsets();
  74. if (parent.getComponentCount() > 0) {
  75. final Component component = parent.getComponent(0);
  76. final Dimension d = component.getPreferredSize();
  77. return new Dimension(
  78. (int) d.getWidth() + insets.left + insets.right,
  79. (int) d.getHeight() + insets.top + insets.bottom
  80. );
  81. }
  82. else {
  83. return new Dimension(
  84. insets.left + insets.right, insets.top + insets.bottom
  85. );
  86. }
  87. }
  88. }
  89. /**
  90. * Returns the minimum size.
  91. *
  92. * @param parent the parent.
  93. *
  94. * @return the minimum size.
  95. */
  96. public Dimension minimumLayoutSize(final Container parent) {
  97. synchronized (parent.getTreeLock()) {
  98. final Insets insets = parent.getInsets();
  99. if (parent.getComponentCount() > 0) {
  100. final Component component = parent.getComponent(0);
  101. final Dimension d = component.getMinimumSize();
  102. return new Dimension(d.width + insets.left + insets.right,
  103. d.height + insets.top + insets.bottom);
  104. }
  105. else {
  106. return new Dimension(insets.left + insets.right,
  107. insets.top + insets.bottom);
  108. }
  109. }
  110. }
  111. /**
  112. * Lays out the components.
  113. *
  114. * @param parent the parent.
  115. */
  116. public void layoutContainer(final Container parent) {
  117. synchronized (parent.getTreeLock()) {
  118. if (parent.getComponentCount() > 0) {
  119. final Insets insets = parent.getInsets();
  120. final Dimension parentSize = parent.getSize();
  121. final Component component = parent.getComponent(0);
  122. final Dimension componentSize = component.getPreferredSize();
  123. final int xx = insets.left + (
  124. Math.max((parentSize.width - insets.left - insets.right
  125. - componentSize.width) / 2, 0)
  126. );
  127. final int yy = insets.top + (
  128. Math.max((parentSize.height - insets.top - insets.bottom
  129. - componentSize.height) / 2, 0));
  130. component.setBounds(xx, yy, componentSize.width,
  131. componentSize.height);
  132. }
  133. }
  134. }
  135. /**
  136. * Not used.
  137. *
  138. * @param comp the component.
  139. */
  140. public void addLayoutComponent(final Component comp) {
  141. // not used.
  142. }
  143. /**
  144. * Not used.
  145. *
  146. * @param comp the component.
  147. */
  148. public void removeLayoutComponent(final Component comp) {
  149. // not used
  150. }
  151. /**
  152. * Not used.
  153. *
  154. * @param name the component name.
  155. * @param comp the component.
  156. */
  157. public void addLayoutComponent(final String name, final Component comp) {
  158. // not used
  159. }
  160. /**
  161. * Not used.
  162. *
  163. * @param name the component name.
  164. * @param comp the component.
  165. */
  166. public void removeLayoutComponent(final String name, final Component comp) {
  167. // not used
  168. }
  169. }