1 11 package org.eclipse.ui.internal.dnd; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.swt.graphics.Point; 15 import org.eclipse.swt.graphics.Rectangle; 16 import org.eclipse.swt.widgets.Composite; 17 import org.eclipse.swt.widgets.Control; 18 import org.eclipse.swt.widgets.Display; 19 import org.eclipse.swt.widgets.Monitor; 20 import org.eclipse.swt.widgets.Shell; 21 22 27 public class SwtUtil { 28 29 private SwtUtil() { 30 31 } 32 33 39 public static boolean isDisposed(Control toTest) { 40 return toTest == null || toTest.isDisposed(); 41 } 42 43 49 public static Control controlThatCovers(Control toTest) { 50 return controlThatCovers(toTest, DragUtil.getDisplayBounds(toTest)); 51 } 52 53 private static Control controlThatCovers(Control toTest, Rectangle testRegion) { 54 Composite parent = toTest.getParent(); 55 56 if (parent == null || toTest instanceof Shell) { 57 return null; 58 } 59 60 Control[] children = parent.getChildren(); 61 for (int i = 0; i < children.length; i++) { 62 Control control = children[i]; 63 64 if (control == toTest) { 65 break; 66 } 67 68 if (!control.isVisible()) { 69 continue; 70 } 71 72 Rectangle nextBounds = DragUtil.getDisplayBounds(control); 73 74 if (nextBounds.intersects(testRegion)) { 75 return control; 76 } 77 } 78 79 return controlThatCovers(parent, testRegion); 80 } 81 82 90 public static boolean isChild(Control potentialParent, Control childToTest) { 91 if (childToTest == null) { 92 return false; 93 } 94 95 if (childToTest == potentialParent) { 96 return true; 97 } 98 99 return isChild(potentialParent, childToTest.getParent()); 100 } 101 102 public static boolean isFocusAncestor(Control potentialParent) { 103 Assert.isNotNull(potentialParent); 104 Control focusControl = Display.getCurrent().getFocusControl(); 105 if (focusControl == null) { 106 return false; 107 } 108 return isChild(potentialParent, focusControl); 109 } 110 111 119 public static Control findControl(Display displayToSearch, 120 Point locationToFind) { 121 Shell[] shells = displayToSearch.getShells(); 122 123 return findControl(shells, locationToFind); 124 } 125 126 135 public static Control findControl(Control[] toSearch, Point locationToFind) { 136 for (int idx = toSearch.length - 1; idx >= 0; idx--) { 137 Control next = toSearch[idx]; 138 139 if (!next.isDisposed() && next.isVisible()) { 140 141 Rectangle bounds = DragUtil.getDisplayBounds(next); 142 143 if (bounds.contains(locationToFind)) { 144 if (next instanceof Composite) { 145 Control result = findControl((Composite) next, 146 locationToFind); 147 148 if (result != null) { 149 return result; 150 } 151 } 152 153 return next; 154 } 155 } 156 } 157 158 return null; 159 } 160 161 public static Control[] getAncestors(Control theControl) { 162 return getAncestors(theControl, 1); 163 } 164 165 private static Control[] getAncestors(Control theControl, int children) { 166 Control[] result; 167 168 if (theControl.getParent() == null) { 169 result = new Control[children]; 170 } else { 171 result = getAncestors(theControl.getParent(), children + 1); 172 } 173 174 result[result.length - children] = theControl; 175 176 return result; 177 } 178 179 public static Control findCommonAncestor(Control control1, Control control2) { 180 Control[] control1Ancestors = getAncestors(control1); 181 Control[] control2Ancestors = getAncestors(control2); 182 183 Control mostSpecific = null; 184 185 for (int idx = 0; idx < Math.min(control1Ancestors.length, control2Ancestors.length); idx++) { 186 Control control1Ancestor = control1Ancestors[idx]; 187 if (control1Ancestor == control2Ancestors[idx]) { 188 mostSpecific = control1Ancestor; 189 } else { 190 break; 191 } 192 } 193 194 return mostSpecific; 195 } 196 197 204 public static Control findControl(Composite toSearch, Point locationToFind) { 205 Control[] children = toSearch.getChildren(); 206 207 return findControl(children, locationToFind); 208 } 209 210 218 public static boolean intersectsAnyMonitor(Display display, 219 Rectangle someRectangle) { 220 Monitor[] monitors = display.getMonitors(); 221 222 for (int idx = 0; idx < monitors.length; idx++) { 223 Monitor mon = monitors[idx]; 224 225 if (mon.getClientArea().intersects(someRectangle)) { 226 return true; 227 } 228 } 229 230 return false; 231 } 232 233 } 234
| Popular Tags
|