Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 23b35feb392515900ab43cc47bb93143ca39ca7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/********************************************************************************
 * Copyright (c) 2002, 2006 IBM Corporation. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is 
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Initial Contributors:
 * The following IBM employees contributed to the Remote System Explorer
 * component that contains this file: David McKnight, Kushal Munir, 
 * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, 
 * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley.
 * 
 * Contributors:
 * {Name} (company) - description of contribution.
 ********************************************************************************/

package org.eclipse.rse.ui.dialogs;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.rse.ui.view.SystemPropertySheetForm;
import org.eclipse.rse.ui.view.SystemViewForm;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;


/**
 * Helper class to save the enable/disable state of a control
 * including all its descendent controls.
 */
public class SystemControlEnableState 
{



	/**
	 * List of exception controls (element type: <code>Control</code>); 
	 * <code>null</code> if none.
	 */
	private List exceptions = null;

	/**
	 * List of saved states (element type: <code>ItemState</code>).
	 */
	private List states;

	/**
	 * Internal class for recording the enable/disable state of a
	 * single control.
	 */
	private class ItemState 
	{
		protected Control item;
		protected boolean state;
		public ItemState(Control item, boolean state) 
		{
			this.item = item;
			this.state = state;
		}
		public void restore() 
		{			
			if (item != null)
				item.setEnabled(state);
		}
	}

	/**
	 * Creates a new object and saves in it the current enable/disable
	 * state of the given control and its descendents; the controls 
	 * that are saved are also disabled.
	 *
	 * @param w the control
	 */
	protected SystemControlEnableState(Control w) 
	{
		this(w, null);
	}
	/**
	 * Creates a new object and saves in it the current enable/disable
	 * state of the given control and its descendents except for the 
	 * given list of exception cases; the controls that are saved
	 * are also disabled.
	 *
	 * @param w the control
	 * @param exceptions the list of controls to not disable
	 *  (element type: <code>Control</code>), or <code>null</code> if none
	 */
	protected SystemControlEnableState(Control w, List exceptions) 
	{
		super();
		states = new ArrayList();
		this.exceptions = exceptions;
		readStateForAndDisable(w);
	}
	/**
	 * Saves the current enable/disable state of the given control
	 * and its descendents in the returned object; the controls
	 * are all disabled.
	 *
	 * @param w the control
	 * @return an object capturing the enable/disable state
	 */
	public static SystemControlEnableState disable(Control w) 
	{
		return new SystemControlEnableState(w);
	}
	/**
	 * Saves the current enable/disable state of the given control
	 * and its descendents in the returned object except for the 
	 * given list of exception cases; the controls that are saved
	 * are also disabled.
	 *
	 * @param w the control
	 * @param exceptions the list of controls to not disable
	 *  (element type: <code>Control</code>)
	 * @return an object capturing the enable/disable state
	 */
	public static SystemControlEnableState disable(Control w, List exceptions) 
	{
		return new SystemControlEnableState(w, exceptions);
	}
	/**
	 * Recursively reads the enable/disable state for the given window
	 * and disables all controls.
	 */
	private void readStateForAndDisable(Control w) 
	{
		if ((exceptions != null && exceptions.contains(w)))
			return;

		if ((w instanceof Composite) && !(w instanceof SystemViewForm) && !(w instanceof SystemPropertySheetForm))
		{
			Composite c = (Composite) w;
			Control[] children = c.getChildren();
			for (int i = 0; i < children.length; i++) 
			{
				readStateForAndDisable(children[i]);
			}
		}
		// XXX: Workaround for 1G2Q8SS: ITPUI:Linux - Combo box is not enabled in "File->New->Solution"
		states.add(new ItemState(w, w.getEnabled()));
		w.setEnabled(false);
	}
	/**
	 * Restores the window enable state saved in this object.
	 */
	public void restore() 
	{
		int size = states.size();
		for (int i = 0; i < size; i++) 
		{
			((ItemState) states.get(i)).restore();
		}
	}
}

Back to the top