1 11 package org.eclipse.ui.internal.presentations.util; 12 13 import org.eclipse.jface.util.Geometry; 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.events.ControlEvent; 16 import org.eclipse.swt.events.ControlListener; 17 import org.eclipse.swt.events.DisposeEvent; 18 import org.eclipse.swt.events.DisposeListener; 19 import org.eclipse.swt.graphics.Point; 20 import org.eclipse.swt.graphics.Rectangle; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.Event; 24 import org.eclipse.swt.widgets.Layout; 25 import org.eclipse.swt.widgets.Listener; 26 import org.eclipse.ui.internal.dnd.DragUtil; 27 import org.eclipse.ui.internal.dnd.SwtUtil; 28 import org.eclipse.ui.internal.layout.SizeCache; 29 30 68 public class ProxyControl { 69 70 73 private Composite control; 74 75 78 private Control target = null; 79 80 83 private SizeCache targetCache = null; 84 85 88 private Control commonAncestor; 89 90 96 private boolean visible = true; 97 98 102 private DisposeListener disposeListener = new DisposeListener() { 103 public void widgetDisposed(DisposeEvent e) { 104 if (e.widget == target || e.widget == control) { 105 setTargetControl(null); 106 } 107 } 108 }; 109 110 private Listener visibilityListener = new Listener() { 111 112 public void handleEvent(Event event) { 113 if (target != null) { 114 visible = control.getVisible(); 115 target.setVisible(visible); 116 } 117 } 118 119 }; 120 121 132 public void setVisible(boolean visible) { 133 } 135 136 140 private ControlListener controlListener = new ControlListener() { 141 142 public void controlMoved(ControlEvent e) { 143 ProxyControl.this.layout(); 144 } 145 146 public void controlResized(ControlEvent e) { 147 } 151 152 }; 153 154 161 public ProxyControl(Composite parent) { 162 control = new Composite(parent, SWT.NO_BACKGROUND); 164 control.setVisible(false); 165 166 control.setLayout(new Layout() { 169 protected void layout (Composite composite, boolean flushCache) { 170 ProxyControl.this.layout(); 171 } 173 174 protected Point computeSize (Composite composite, int wHint, int hHint, boolean flushCache) { 175 if (targetCache == null) { 176 if (target != null) { 177 return target.computeSize(wHint, hHint, flushCache); 178 } 179 return new Point(1,1); 181 } 182 183 return targetCache.computeSize(wHint, hHint); 184 } 185 }); 186 187 control.addDisposeListener(disposeListener); 189 control.addListener(SWT.Show, visibilityListener); 190 control.addListener(SWT.Hide, visibilityListener); 191 } 192 193 198 public void setTargetControl(Control target) { 199 targetCache = null; 200 internalSetTargetControl(target); 201 } 202 203 private void internalSetTargetControl(Control target) { 204 if (this.target != target) { 205 206 if (this.target != null) { 207 for (Control next = control; next != commonAncestor && next != null; next = next.getParent()) { 208 next.removeControlListener(controlListener); 209 } 210 commonAncestor = null; 211 212 if (!this.target.isDisposed()) { 215 this.target.removeDisposeListener(disposeListener); 216 } 217 } 218 219 if (this.target == null && target != null) { 220 control.setVisible(visible); 222 } 223 224 this.target = target; 225 226 if (target != null) { 227 commonAncestor = SwtUtil.findCommonAncestor(this.target, control); 228 for (Control next = control; next != null && next != commonAncestor; next = next.getParent()) { 229 next.addControlListener(controlListener); 230 } 231 232 target.setVisible(control.getVisible()); 234 target.addDisposeListener(disposeListener); 237 } else { 238 control.setVisible(false); 239 } 240 } 241 } 242 243 public void setTarget(SizeCache cache) { 244 targetCache = cache; 245 246 if (targetCache != null) { 247 setTargetControl(cache.getControl()); 248 } else { 249 setTargetControl(null); 250 } 251 } 252 253 258 public Control getControl() { 259 return control; 260 } 261 262 public Control getTarget() { 263 return target; 264 } 265 266 269 public void layout() { 270 if (getTarget() == null) { 271 return; 272 } 273 274 Rectangle displayBounds = Geometry.toDisplay(control.getParent(), control.getBounds()); 276 277 Rectangle clippingRegion = DragUtil.getDisplayBounds(control.getParent()); 279 displayBounds = displayBounds.intersection(clippingRegion); 280 281 Rectangle targetBounds = Geometry.toControl(getTarget().getParent(), displayBounds); 283 284 getTarget().setBounds(targetBounds); 286 } 287 } 288 | Popular Tags |