1 43 44 package org.jfree.layout; 45 46 import java.awt.Component ; 47 import java.awt.Container ; 48 import java.awt.Dimension ; 49 import java.awt.Insets ; 50 import java.awt.LayoutManager ; 51 import java.io.Serializable ; 52 53 59 public class CenterLayout implements LayoutManager , Serializable { 60 61 62 private static final long serialVersionUID = 469319532333015042L; 63 64 67 public CenterLayout() { 68 } 69 70 77 public Dimension preferredLayoutSize(final Container parent) { 78 79 synchronized (parent.getTreeLock()) { 80 final Insets insets = parent.getInsets(); 81 if (parent.getComponentCount() > 0) { 82 final Component component = parent.getComponent(0); 83 final Dimension d = component.getPreferredSize(); 84 return new Dimension ( 85 (int) d.getWidth() + insets.left + insets.right, 86 (int) d.getHeight() + insets.top + insets.bottom 87 ); 88 } 89 else { 90 return new Dimension ( 91 insets.left + insets.right, insets.top + insets.bottom 92 ); 93 } 94 } 95 96 } 97 98 105 public Dimension minimumLayoutSize(final Container parent) { 106 107 synchronized (parent.getTreeLock()) { 108 final Insets insets = parent.getInsets(); 109 if (parent.getComponentCount() > 0) { 110 final Component component = parent.getComponent(0); 111 final Dimension d = component.getMinimumSize(); 112 return new Dimension (d.width + insets.left + insets.right, 113 d.height + insets.top + insets.bottom); 114 } 115 else { 116 return new Dimension (insets.left + insets.right, 117 insets.top + insets.bottom); 118 } 119 } 120 121 } 122 123 128 public void layoutContainer(final Container parent) { 129 130 synchronized (parent.getTreeLock()) { 131 if (parent.getComponentCount() > 0) { 132 final Insets insets = parent.getInsets(); 133 final Dimension parentSize = parent.getSize(); 134 final Component component = parent.getComponent(0); 135 final Dimension componentSize = component.getPreferredSize(); 136 final int xx = insets.left + ( 137 Math.max((parentSize.width - insets.left - insets.right 138 - componentSize.width) / 2, 0) 139 ); 140 final int yy = insets.top + ( 141 Math.max((parentSize.height - insets.top - insets.bottom 142 - componentSize.height) / 2, 0)); 143 component.setBounds(xx, yy, componentSize.width, 144 componentSize.height); 145 } 146 } 147 148 } 149 150 155 public void addLayoutComponent(final Component comp) { 156 } 158 159 164 public void removeLayoutComponent(final Component comp) { 165 } 167 168 174 public void addLayoutComponent(final String name, final Component comp) { 175 } 177 178 184 public void removeLayoutComponent(final String name, final Component comp) { 185 } 187 188 } 189 | Popular Tags |