KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > 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.retouche.navigation.base;
26
27 import java.awt.*;
28
29 /**
30  * This file is originally from Retouche, the Java Support
31  * infrastructure in NetBeans. I have modified the file as little
32  * as possible to make merging Retouche fixes back as simple as
33  * possible.
34  * <p>
35  * Trivial layout manager class used by the panels for selecting look and filter. Simply uses the preferred size of the
36  * first compnent and fills the rest of the space with the second, to the height of the tallest.
37  *
38  * @author Tim Boudreau
39  */

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

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

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