KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > dialogs > ControlEnableState


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jface.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Control;
18
19 /**
20  * Helper class to save the enable/disable state of a control including all its
21  * descendent controls.
22  */

23 public class ControlEnableState {
24     /**
25      * List of exception controls (element type: <code>Control</code>);
26      * <code>null</code> if none.
27      */

28     private List JavaDoc exceptions = null;
29
30     /**
31      * List of saved states (element type: <code>ItemState</code>).
32      */

33     private List JavaDoc states;
34
35     /**
36      * Internal class for recording the enable/disable state of a single
37      * control.
38      */

39     private class ItemState {
40         /** the control */
41         protected Control item;
42
43         /** the state */
44         protected boolean state;
45
46         /**
47          * Create a new instance of the receiver.
48          *
49          * @param item
50          * @param state
51          */

52         public ItemState(Control item, boolean state) {
53             this.item = item;
54             this.state = state;
55         }
56
57         /**
58          * Restore the enabled state to the original value.
59          *
60          */

61         public void restore() {
62             if (item == null || item.isDisposed()) {
63                 return;
64             }
65             item.setEnabled(state);
66         }
67     }
68
69     /**
70      * Creates a new object and saves in it the current enable/disable state of
71      * the given control and its descendents; the controls that are saved are
72      * also disabled.
73      *
74      * @param w
75      * the control
76      */

77     protected ControlEnableState(Control w) {
78         this(w, null);
79     }
80
81     /**
82      * Creates a new object and saves in it the current enable/disable state of
83      * the given control and its descendents except for the given list of
84      * exception cases; the controls that are saved are also disabled.
85      *
86      * @param w
87      * the control
88      * @param exceptions
89      * the list of controls to not disable (element type:
90      * <code>Control</code>), or <code>null</code> if none
91      */

92     protected ControlEnableState(Control w, List JavaDoc exceptions) {
93         super();
94         states = new ArrayList JavaDoc();
95         this.exceptions = exceptions;
96         readStateForAndDisable(w);
97     }
98
99     /**
100      * Saves the current enable/disable state of the given control and its
101      * descendents in the returned object; the controls are all disabled.
102      *
103      * @param w
104      * the control
105      * @return an object capturing the enable/disable state
106      */

107     public static ControlEnableState disable(Control w) {
108         return new ControlEnableState(w);
109     }
110
111     /**
112      * Saves the current enable/disable state of the given control and its
113      * descendents in the returned object except for the given list of exception
114      * cases; the controls that are saved are also disabled.
115      *
116      * @param w
117      * the control
118      * @param exceptions
119      * the list of controls to not disable (element type:
120      * <code>Control</code>)
121      * @return an object capturing the enable/disable state
122      */

123     public static ControlEnableState disable(Control w, List JavaDoc exceptions) {
124         return new ControlEnableState(w, exceptions);
125     }
126
127     /**
128      * Recursively reads the enable/disable state for the given window and
129      * disables all controls.
130      * @param control Control
131      */

132     private void readStateForAndDisable(Control control) {
133         if ((exceptions != null && exceptions.contains(control))) {
134             return;
135         }
136         if (control instanceof Composite) {
137             Composite c = (Composite) control;
138             Control[] children = c.getChildren();
139             for (int i = 0; i < children.length; i++) {
140                 readStateForAndDisable(children[i]);
141             }
142         }
143         // XXX: Workaround for 1G2Q8SS: ITPUI:Linux - Combo box is not enabled
144
// in "File->New->Solution"
145
states.add(new ItemState(control, control.getEnabled()));
146         control.setEnabled(false);
147     }
148
149     /**
150      * Restores the window enable state saved in this object.
151      */

152     public void restore() {
153         int size = states.size();
154         for (int i = 0; i < size; i++) {
155             ((ItemState) states.get(i)).restore();
156         }
157     }
158 }
159
Popular Tags