KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > navigation > base > TrivialLayout


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * TrivialLayout.java
21  *
22  * Created on 20. srpen 2003, 18:33
23  */

24
25 package org.netbeans.modules.java.navigation.base;
26
27 import java.awt.*;
28
29 /**
30  * Trivial layout manager class used by the panels for selecting look and filter. Simply uses the preferred size of the
31  * first compnent and fills the rest of the space with the second, to the height of the tallest.
32  *
33  * @author Tim Boudreau
34  */

35 final class TrivialLayout implements LayoutManager {
36     public void addLayoutComponent (String JavaDoc name, Component comp) {
37         //do nothing
38
}
39
40     public void removeLayoutComponent (Component comp) {
41         //do nothing
42
}
43
44     public void layoutContainer (Container parent) {
45         if ( parent instanceof TapPanel ) {
46             layoutTapPanel ( (TapPanel) parent );
47         } else {
48             layoutComp ( parent );
49         }
50     }
51
52     /**
53      * Standard layout for any container
54      */

55     private void layoutComp (Container parent) {
56         Component[] c = parent.getComponents ();
57         if ( c.length > 1 ) {
58             Dimension d1 = c[ 0 ].getPreferredSize ();
59             Dimension d2 = c[ 1 ].getPreferredSize ();
60             int labely = 0;
61             d1.width += 10; //Aqua displays elipsis
62
if ( d2.height > d1.height ) {
63                 labely = ( d2.height / 2 ) - ( d1.height / 2 );
64             }
65             if ( parent.getWidth () - d1.width < d2.width ) {
66                 c[ 0 ].setBounds ( 0, 0, 0, 0 );
67                 c[ 1 ].setBounds ( 0, 0, parent.getWidth (), parent.getHeight () );
68             } else {
69                 c[ 0 ].setBounds ( 0, labely, d1.width, d1.height );
70                 c[ 1 ].setBounds ( d1.width + 1, 0, parent.getWidth () - d1.width,
71                         Math.min ( parent.getHeight (), d2.height ) );
72             }
73         }
74     }
75
76     /**
77      * Layout for TapPanel, taking into account its minimumHeight
78      */

79     private void layoutTapPanel (TapPanel tp) {
80         Component[] c = tp.getComponents ();
81         if ( c.length > 1 ) {
82             Dimension d1 = c[ 0 ].getPreferredSize ();
83             Dimension d2 = c[ 1 ].getPreferredSize ();
84             int labely = 0;
85             if ( d2.height > d1.height ) {
86                 labely = ( ( d2.height / 2 ) - ( d1.height / 2 ) ) + 2; //+2 fudge factor for font baseline
87
}
88
89             if ( tp.isExpanded () ) {
90                 int top = tp.getOrientation () == tp.UP ? 0 : tp.getMinimumHeight ();
91                 int height = Math.min ( tp.getHeight () - tp.getMinimumHeight (), d2.height );
92                 if ( tp.getWidth () - d1.width < d2.width ) {
93                     c[ 0 ].setBounds ( 0, 0, 0, 0 );
94                     c[ 1 ].setBounds ( 0, top, tp.getWidth (), height );
95                 } else {
96                     c[ 0 ].setBounds ( 0, top + labely, d1.width, d1.height );
97                     c[ 1 ].setBounds ( d1.width + 1, top, tp.getWidth () - d1.width,
98                             height );
99                 }
100             } else {
101                 c[ 0 ].setBounds ( 0, 0, 0, 0 );
102                 c[ 1 ].setBounds ( 0, 0, 0, 0 );
103             }
104         }
105     }
106
107
108     public Dimension minimumLayoutSize (Container parent) {
109         Dimension result = new Dimension ( 20, 10 );
110         Component[] c = parent.getComponents ();
111         TapPanel tp = (TapPanel) parent;
112         if ( c.length > 1 ) {
113             Dimension d1 = c[ 0 ].getPreferredSize ();
114             Dimension d2 = c[ 1 ].getPreferredSize ();
115             result.width = d1.width + d2.width;
116             result.height = tp.isExpanded () ? Math.max ( d1.height, d2.height ) + tp.getMinimumHeight () : tp.getMinimumHeight ();
117         }
118         return result;
119     }
120
121     public Dimension preferredLayoutSize (Container parent) {
122         return minimumLayoutSize ( parent );
123     }
124 }
125
126
Popular Tags