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