1 11 package org.eclipse.jface.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.swt.widgets.Composite; 17 import org.eclipse.swt.widgets.Control; 18 19 23 public class ControlEnableState { 24 28 private List exceptions = null; 29 30 33 private List states; 34 35 39 private class ItemState { 40 41 protected Control item; 42 43 44 protected boolean state; 45 46 52 public ItemState(Control item, boolean state) { 53 this.item = item; 54 this.state = state; 55 } 56 57 61 public void restore() { 62 if (item == null || item.isDisposed()) { 63 return; 64 } 65 item.setEnabled(state); 66 } 67 } 68 69 77 protected ControlEnableState(Control w) { 78 this(w, null); 79 } 80 81 92 protected ControlEnableState(Control w, List exceptions) { 93 super(); 94 states = new ArrayList (); 95 this.exceptions = exceptions; 96 readStateForAndDisable(w); 97 } 98 99 107 public static ControlEnableState disable(Control w) { 108 return new ControlEnableState(w); 109 } 110 111 123 public static ControlEnableState disable(Control w, List exceptions) { 124 return new ControlEnableState(w, exceptions); 125 } 126 127 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 states.add(new ItemState(control, control.getEnabled())); 146 control.setEnabled(false); 147 } 148 149 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 |