KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > colorchooser > CenterLayout


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
8 package javax.swing.colorchooser;
9
10 import java.awt.*;
11 import java.io.*;
12
13
14 /**
15   * Center-positioning layout manager.
16   * @version 1.10 12/19/03
17   * @author Tom Santos
18   * @author Steve Wilson
19   */

20 class CenterLayout implements LayoutManager, Serializable {
21     public void addLayoutComponent(String JavaDoc name, Component comp) { }
22     public void removeLayoutComponent(Component comp) { }
23
24     public Dimension preferredLayoutSize( Container container ) {
25     Component c = container.getComponent( 0 );
26     if ( c != null ) {
27         Dimension size = c.getPreferredSize();
28         Insets insets = container.getInsets();
29         size.width += insets.left + insets.right;
30         size.height += insets.top + insets.bottom;
31         return size;
32     }
33     else {
34         return new Dimension( 0, 0 );
35     }
36     }
37
38     public Dimension minimumLayoutSize(Container cont) {
39         return preferredLayoutSize(cont);
40     }
41
42     public void layoutContainer(Container container) {
43     try {
44        Component c = container.getComponent( 0 );
45
46            c.setSize( c.getPreferredSize() );
47        Dimension size = c.getSize();
48            Dimension containerSize = container.getSize();
49        Insets containerInsets = container.getInsets();
50        containerSize.width -= containerInsets.left + containerInsets.right;
51        containerSize.height -= containerInsets.top + containerInsets.bottom;
52        int componentLeft = (containerSize.width / 2) - (size.width / 2);
53        int componentTop = (containerSize.height / 2) - (size.height / 2);
54        componentLeft += containerInsets.left;
55        componentTop += containerInsets.top;
56
57         c.setBounds( componentLeft, componentTop, size.width, size.height );
58      }
59          catch( Exception JavaDoc e ) {
60          }
61     }
62 }
63
Popular Tags