KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > ProxyControl


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.presentations;
12
13 import org.eclipse.jface.util.Geometry;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.Point;
16 import org.eclipse.swt.graphics.Rectangle;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Layout;
20 import org.eclipse.ui.internal.dnd.DragUtil;
21 import org.eclipse.ui.internal.layout.SizeCache;
22
23 /**
24  * This invisible control forces some target control to have the
25  * same size and position.
26  *
27  * @since 3.0
28  */

29 public class ProxyControl {
30     private Composite control;
31     private SizeCache target;
32     
33     public ProxyControl(Composite parent) {
34         control = new Composite(parent, SWT.NONE);
35         control.setVisible(false);
36         
37         control.setLayout(new Layout() {
38             protected void layout (Composite composite, boolean flushCache) {
39                 //ProxyControl.this.layout();
40
}
41             
42             protected Point computeSize (Composite composite, int wHint, int hHint, boolean flushCache) {
43                 if (target == null) {
44                     return new Point(0,0);
45                 }
46                 
47                 return target.computeSize(wHint, hHint);
48             }
49         });
50     }
51     
52     /**
53      * Sets the control whose position will be managed by this proxy
54      *
55      * @param target the control, or null if none
56      */

57     public void setTarget(SizeCache target) {
58         if (this.target != target) {
59             this.target = target;
60         }
61     }
62     
63     /**
64      * Returns the target control (the control whose size is being managed)
65      *
66      * @return the target control (or null)
67      */

68     public Control getTargetControl() {
69         if (target == null) {
70             return null;
71         }
72         
73         return target.getControl();
74     }
75     
76     /**
77      * Returns the proxy control
78      *
79      * @return the proxy control (not null)
80      */

81     public Control getControl() {
82         return control;
83     }
84     
85     /**
86      * Layout the target control
87      */

88     public void layout() {
89         if (getTargetControl() == null) {
90             return;
91         }
92         
93         Rectangle parentBounds = DragUtil.getDisplayBounds(control.getParent());
94         
95         // Compute the clipped bounds for the control (display coordinates)
96
Rectangle bounds = control.getBounds();
97         bounds.x += parentBounds.x;
98         bounds.y += parentBounds.y;
99         bounds = bounds.intersection(parentBounds);
100         
101         Rectangle targetBounds = Geometry.toControl(getTargetControl().getParent(), bounds);
102         
103         getTargetControl().setBounds(targetBounds);
104     }
105     
106     /**
107      * Destroys this object. Should be the last method called on this object. No further methods should
108      * be invoked after calling this.
109      */

110     public void dispose() {
111         if (control == null) {
112             return;
113         }
114         this.target = null;
115         control.dispose();
116         control = null;
117     }
118 }
119
Popular Tags