Mercurial > projects > dwt-addons
annotate dwtx/jface/dialogs/ControlEnableState.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | 7615869f89e6 |
children |
rev | line source |
---|---|
18 | 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.dialogs.ControlEnableState; | |
14 | |
15 | |
16 import dwt.widgets.Composite; | |
17 import dwt.widgets.Control; | |
18 | |
19 import dwt.dwthelper.utils; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
18
diff
changeset
|
20 import dwtx.dwtxhelper.Collection; |
18 | 21 |
22 /** | |
23 * Helper class to save the enable/disable state of a control including all its | |
24 * descendent controls. | |
25 */ | |
26 public class ControlEnableState { | |
27 /** | |
28 * List of exception controls (element type: <code>Control</code>); | |
29 * <code>null</code> if none. | |
30 */ | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
18
diff
changeset
|
31 private List exceptions = null; |
18 | 32 |
33 /** | |
34 * List of saved states (element type: <code>ItemState</code>). | |
35 */ | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
18
diff
changeset
|
36 private List states; |
18 | 37 |
38 /** | |
39 * Internal class for recording the enable/disable state of a single | |
40 * control. | |
41 */ | |
42 private class ItemState { | |
43 /** the control */ | |
44 protected Control item; | |
45 | |
46 /** the state */ | |
47 protected bool state; | |
48 | |
49 /** | |
50 * Create a new instance of the receiver. | |
51 * | |
52 * @param item | |
53 * @param state | |
54 */ | |
55 public this(Control item, bool state) { | |
56 this.item = item; | |
57 this.state = state; | |
58 } | |
59 | |
60 /** | |
61 * Restore the enabled state to the original value. | |
62 * | |
63 */ | |
64 public void restore() { | |
65 if (item is null || item.isDisposed()) { | |
66 return; | |
67 } | |
68 item.setEnabled(state); | |
69 } | |
70 } | |
71 | |
72 /** | |
73 * Creates a new object and saves in it the current enable/disable state of | |
74 * the given control and its descendents; the controls that are saved are | |
75 * also disabled. | |
76 * | |
77 * @param w | |
78 * the control | |
79 */ | |
80 protected this(Control w) { | |
81 this(w, null); | |
82 } | |
83 | |
84 /** | |
85 * Creates a new object and saves in it the current enable/disable state of | |
86 * the given control and its descendents except for the given list of | |
87 * exception cases; the controls that are saved are also disabled. | |
88 * | |
89 * @param w | |
90 * the control | |
91 * @param exceptions | |
92 * the list of controls to not disable (element type: | |
93 * <code>Control</code>), or <code>null</code> if none | |
94 */ | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
18
diff
changeset
|
95 protected this(Control w, List exceptions) { |
18 | 96 // super(); |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
18
diff
changeset
|
97 states = new ArrayList(); |
18 | 98 this.exceptions = exceptions; |
99 readStateForAndDisable(w); | |
100 } | |
101 | |
102 /** | |
103 * Saves the current enable/disable state of the given control and its | |
104 * descendents in the returned object; the controls are all disabled. | |
105 * | |
106 * @param w | |
107 * the control | |
108 * @return an object capturing the enable/disable state | |
109 */ | |
110 public static ControlEnableState disable(Control w) { | |
111 return new ControlEnableState(w); | |
112 } | |
113 | |
114 /** | |
115 * Saves the current enable/disable state of the given control and its | |
116 * descendents in the returned object except for the given list of exception | |
117 * cases; the controls that are saved are also disabled. | |
118 * | |
119 * @param w | |
120 * the control | |
121 * @param exceptions | |
122 * the list of controls to not disable (element type: | |
123 * <code>Control</code>) | |
124 * @return an object capturing the enable/disable state | |
125 */ | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
18
diff
changeset
|
126 public static ControlEnableState disable(Control w, List exceptions) { |
18 | 127 return new ControlEnableState(w, exceptions); |
128 } | |
129 | |
130 /** | |
131 * Recursively reads the enable/disable state for the given window and | |
132 * disables all controls. | |
133 * @param control Control | |
134 */ | |
135 private void readStateForAndDisable(Control control) { | |
136 if ((exceptions !is null && exceptions.contains(control))) { | |
137 return; | |
138 } | |
139 if (auto c = cast(Composite)control ) { | |
140 Control[] children = c.getChildren(); | |
141 for (int i = 0; i < children.length; i++) { | |
142 readStateForAndDisable(children[i]); | |
143 } | |
144 } | |
145 // XXX: Workaround for 1G2Q8SS: ITPUI:Linux - Combo box is not enabled | |
146 // in "File->New->Solution" | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
18
diff
changeset
|
147 states.add(new ItemState(control, control.getEnabled())); |
18 | 148 control.setEnabled(false); |
149 } | |
150 | |
151 /** | |
152 * Restores the window enable state saved in this object. | |
153 */ | |
154 public void restore() { | |
155 int size = states.size(); | |
156 for (int i = 0; i < size; i++) { | |
157 (cast(ItemState) states.get(i)).restore(); | |
158 } | |
159 } | |
160 } |