KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > Splitter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.compare;
12
13 import org.eclipse.swt.widgets.*;
14 import org.eclipse.swt.custom.SashForm;
15
16 /**
17  * The Splitter adds support for nesting to a SashForm.
18  * <P>
19  * If Splitters are nested directly:
20  * <UL>
21  * <LI>changing the visibility of a child may propagate upward to the parent Splitter if the child
22  * is the last child to become invisible or the first to become visible.</LI>
23  * <LI>maximizing a child makes it as large as the topmost enclosing Splitter</LI>
24  * </UL>
25  *
26  * @since 2.1
27  */

28 public class Splitter extends SashForm {
29     
30     private static final String JavaDoc VISIBILITY= "org.eclipse.compare.internal.visibility"; //$NON-NLS-1$
31

32     /**
33      * Constructs a new instance of this class given its parent
34      * and a style value describing its behavior and appearance.
35      * <p>
36      * The style value is either one of the style constants defined in
37      * class <code>SWT</code> which is applicable to instances of this
38      * class, or must be built by <em>bitwise OR</em>'ing together
39      * (that is, using the <code>int</code> "|" operator) two or more
40      * of those <code>SWT</code> style constants. The class description
41      * lists the style constants that are applicable to the class.
42      * Style bits are also inherited from superclasses.
43      * </p>
44      *
45      * @param parent a widget which will be the parent of the new instance (cannot be null)
46      * @param style the style of widget to construct
47      *
48      * @exception IllegalArgumentException <ul>
49      * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
50      * </ul>
51      * @exception org.eclipse.swt.SWTException <ul>
52      * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
53      * </ul>
54      */

55     public Splitter(Composite parent, int style) {
56         super(parent, style);
57     }
58         
59     /**
60      * Sets the visibility of the given child in this Splitter. If this change
61      * affects the visibility state of the whole Splitter, and if the Splitter
62      * is directly nested in one or more Splitters, this method recursively
63      * propagates the new state upward.
64      *
65      * @param child the child control for which the visibility is changed
66      * @param visible the new visibility state
67      */

68     public void setVisible(Control child, boolean visible) {
69         
70         boolean wasEmpty= isEmpty();
71                 
72         child.setVisible(visible);
73         child.setData(VISIBILITY, new Boolean JavaDoc(visible));
74         
75         if (wasEmpty != isEmpty()) {
76             // recursively walk up
77
Composite parent= getParent();
78             if (parent instanceof Splitter) {
79                 Splitter sp= (Splitter) parent;
80                 sp.setVisible(this, visible);
81                 sp.layout();
82             }
83         } else {
84             layout();
85         }
86     }
87
88     /* (non-Javadoc)
89      * Recursively calls setMaximizedControl for all direct parents that are
90      * itself Splitters.
91      */

92     public void setMaximizedControl(Control control) {
93         if (control == null || control == getMaximizedControl())
94             super.setMaximizedControl(null);
95         else
96             super.setMaximizedControl(control);
97
98         // recursively walk upward
99
Composite parent= getParent();
100         if (parent instanceof Splitter)
101             ((Splitter) parent).setMaximizedControl(this);
102         else
103             layout(true);
104     }
105
106     /* (non-Javadoc)
107      * Returns true if Splitter has no children or if all children are invisible.
108      */

109     private boolean isEmpty() {
110         Control[] controls= getChildren();
111         for (int i= 0; i < controls.length; i++)
112             if (isVisible(controls[i]))
113                 return false;
114         return true;
115     }
116     
117     /* (non-Javadoc)
118      * Returns the visibility state of the given child control. If the
119      * control is a Sash, this method always returns false.
120      */

121     private boolean isVisible(Control child) {
122         if (child instanceof Sash)
123             return false;
124         Object JavaDoc data= child.getData(VISIBILITY);
125         if (data instanceof Boolean JavaDoc)
126             return ((Boolean JavaDoc)data).booleanValue();
127         return true;
128     }
129 }
130
Popular Tags