KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > plaf > StackLayout


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 package org.netbeans.swing.tabcontrol.plaf;
21
22 import java.awt.*;
23 import javax.swing.JComponent JavaDoc;
24
25 /**
26  * Special simple layout used in TabbedContainer. Shows component in the
27  * "stack", it means that only one component is visible at any time, others are
28  * always hidden "below" the visible one. Use method showComponent to select
29  * visible component.
30  *
31  * @author Dafe Simonek
32  */

33 class StackLayout implements LayoutManager {
34     /**
35      * Default size when no components are contained
36      */

37     private static Dimension emptySize = null;
38     /**
39      * Holds currently visible component or null if no comp is visible
40      */

41     private Component JavaDoc visibleComp = null;
42
43     /**
44      * Set the currently displayed component. If passed null for the component,
45      * all contained components will be made invisible (sliding windows do this)
46      */

47     public void showComponent(Component JavaDoc c, Container parent) {
48         if (visibleComp != c) {
49             if (!parent.isAncestorOf(c) && c != null) {
50                 parent.add(c);
51             }
52             synchronized (parent.getTreeLock()) {
53                 if (visibleComp != null) {
54                     visibleComp.setVisible(false);
55                 }
56                 visibleComp = c;
57                 if (c != null) {
58                     visibleComp.setVisible(true);
59                 }
60         // trigger re-layout
61
if (c instanceof JComponent JavaDoc) {
62             ((JComponent JavaDoc)c).revalidate();
63         }
64         else {
65             parent.validate(); //XXX revalidate should work!
66
}
67             }
68         }
69     }
70     
71     /** Allows support for content policies */
72     public Component JavaDoc getVisibleComponent() {
73         return visibleComp;
74     }
75
76     /**
77      * ********** Implementation of LayoutManager interface *********
78      */

79
80     public void addLayoutComponent(String JavaDoc name, Component JavaDoc comp) {
81         synchronized (comp.getTreeLock()) {
82             comp.setVisible(false);
83             // keep consistency if showComponent was already called on this
84
// component before
85
if (comp == visibleComp) {
86                 visibleComp = null;
87             }
88 /*System.out.println("Border dump for " + comp.getName());
89 borderDump((javax.swing.JComponent)comp, "");*/

90         }
91     }
92     
93 /*private void borderDump (javax.swing.JComponent comp, String space) {
94     javax.swing.border.Border compBorder = comp.getBorder();
95     if (compBorder == null) {
96         System.out.println(space + comp.getClass().getName() + " has no border.");
97     } else {
98         System.out.println(space + comp.getClass().getName() + ": " + compBorder.getClass().getName());
99     }
100     Component curComp;
101     for (int i = 0; i < comp.getComponentCount(); i++) {
102         curComp = comp.getComponent(i);
103         if (curComp instanceof javax.swing.JComponent) {
104             borderDump((javax.swing.JComponent)curComp, space + " ");
105         }
106     }
107 }*/

108     
109     public void removeLayoutComponent(Component JavaDoc comp) {
110         synchronized (comp.getTreeLock()) {
111             if (comp == visibleComp) {
112                 visibleComp = null;
113             }
114             // kick out removed component as visible, so that others
115
// don't have problems with hidden components
116
comp.setVisible(true);
117         }
118     }
119
120     public void layoutContainer(Container parent) {
121         if (visibleComp != null) {
122             synchronized (parent.getTreeLock()) {
123                 Insets insets = parent.getInsets();
124                 visibleComp.setBounds(insets.left, insets.top, parent.getWidth()
125                    - (insets.left + insets.right), parent.getHeight()
126                    - (insets.top + insets.bottom));
127             }
128         }
129     }
130
131     public Dimension minimumLayoutSize(Container parent) {
132         return getEmptySize();
133     }
134
135     public Dimension preferredLayoutSize(Container parent) {
136         return getEmptySize();
137     }
138
139     /**
140      * Specifies default size of empty container
141      */

142     private static Dimension getEmptySize() {
143         return new Dimension(50, 50);
144     }
145
146 }
Popular Tags