KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > LayoutPartSash


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  * Randy Hudson <hudsonr@us.ibm.com>
11  * - Fix for bug 19524 - Resizing WorkbenchWindow resizes views
12  * Cagatay Kavukcuoglu <cagatayk@acm.org>
13  * - Fix for bug 10025 - Resizing views should not use height ratios
14  *******************************************************************************/

15 package org.eclipse.ui.internal;
16
17 import org.eclipse.jface.util.Geometry;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.SelectionListener;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Sash;
26
27 class LayoutPartSash extends LayoutPart {
28
29     private Sash sash;
30     private boolean enabled = false;
31
32     private PartSashContainer rootContainer;
33
34     private int style;
35
36     private LayoutPartSash preLimit;
37
38     private LayoutPartSash postLimit;
39
40     SelectionListener selectionListener;
41
42     private int left = 300, right = 300;
43
44     private Rectangle bounds = new Rectangle(0,0,0,0);
45     
46     /**
47      * Stores whether or not the sash is visible. (This is expected to have a meaningful
48      * value even if the underlying control doesn't exist).
49      */

50     private boolean isVisible;
51     
52     LayoutPartSash(PartSashContainer rootContainer, int style) {
53         super(null);
54         this.style = style;
55         this.rootContainer = rootContainer;
56
57         selectionListener = new SelectionAdapter() {
58             public void widgetSelected(SelectionEvent e) {
59                 checkDragLimit(e);
60                 
61                 if (e.detail != SWT.DRAG) {
62                     LayoutPartSash.this.widgetSelected(e.x, e.y, e.width,
63                             e.height);
64                 }
65             }
66         };
67     }
68
69     // checkDragLimit contains changes by cagatayk@acm.org
70
private void checkDragLimit(SelectionEvent event) {
71         LayoutTree root = rootContainer.getLayoutTree();
72         LayoutTreeNode node = root.findSash(this);
73         Rectangle nodeBounds = node.getBounds();
74         Rectangle eventRect = new Rectangle(event.x, event.y, event.width, event.height);
75         
76         boolean vertical = (style == SWT.VERTICAL);
77         
78         // If a horizontal sash, flip the coordinate system so that we
79
// can handle horizontal and vertical sashes without special cases
80
if (!vertical) {
81             Geometry.flipXY(nodeBounds);
82             Geometry.flipXY(eventRect);
83         }
84
85         int eventX = eventRect.x;
86         int left = Math.max(0, eventX - nodeBounds.x);
87         left = Math.min(left, nodeBounds.width - LayoutTreeNode.SASH_WIDTH);
88         int right = nodeBounds.width - left - LayoutTreeNode.SASH_WIDTH;
89         
90         LayoutTreeNode.ChildSizes sizes = node.computeChildSizes(nodeBounds.width, nodeBounds.height, left, right, nodeBounds.width);
91
92         eventRect.x = nodeBounds.x + sizes.left;
93         
94         // If it's a horizontal sash, restore eventRect to its original coordinate system
95
if (!vertical) {
96             Geometry.flipXY(eventRect);
97         }
98         
99         event.x = eventRect.x;
100         event.y = eventRect.y;
101     }
102
103     /**
104      * Creates the control. As an optimization, creation of the control is deferred if
105      * the control is invisible.
106      */

107     public void createControl(Composite parent) {
108         // Defer creation of the control until it becomes visible
109
if (isVisible) {
110             doCreateControl();
111         }
112     }
113     
114     /**
115      * Creates the underlying SWT control.
116      *
117      * @since 3.1
118      */

119     private void doCreateControl() {
120         if (sash == null) {
121             sash = new Sash(this.rootContainer.getParent(), style | SWT.SMOOTH);
122             sash.addSelectionListener(selectionListener);
123             sash.setEnabled(enabled);
124             sash.setBounds(bounds);
125         }
126     }
127     
128     public void setBounds(Rectangle r) {
129         super.setBounds(r);
130         
131         bounds = r;
132     }
133
134     /**
135      * Makes the sash visible or invisible. Note: as an optimization, the actual widget is destroyed when the
136      * sash is invisible.
137      */

138     public void setVisible(boolean visible) {
139         if (visible == isVisible) {
140             return;
141         }
142         
143         if (visible) {
144             doCreateControl();
145         } else {
146             dispose();
147         }
148         
149         super.setVisible(visible);
150         
151         isVisible = visible;
152     }
153     
154     public boolean isVisible() {
155         return isVisible;
156     }
157     
158     /**
159      * See LayoutPart#dispose
160      */

161     public void dispose() {
162
163         if (sash != null) {
164             bounds = sash.getBounds();
165             sash.dispose();
166         }
167         sash = null;
168     }
169
170     /**
171      * Gets the presentation bounds.
172      */

173     public Rectangle getBounds() {
174         if (sash == null) {
175             return bounds;
176         }
177
178         return sash.getBounds();
179     }
180
181     /**
182      * Returns the part control.
183      */

184     public Control getControl() {
185         return sash;
186     }
187
188     /**
189      *
190      */

191     public String JavaDoc getID() {
192         return null;
193     }
194
195     LayoutPartSash getPostLimit() {
196         return postLimit;
197     }
198
199     LayoutPartSash getPreLimit() {
200         return preLimit;
201     }
202
203     int getLeft() {
204         return left;
205     }
206
207     int getRight() {
208         return right;
209     }
210
211     boolean isHorizontal() {
212         return ((style & SWT.HORIZONTAL) == SWT.HORIZONTAL);
213     }
214
215     boolean isVertical() {
216         return ((style & SWT.VERTICAL) == SWT.VERTICAL);
217     }
218
219     void setPostLimit(LayoutPartSash newPostLimit) {
220         postLimit = newPostLimit;
221     }
222
223     void setPreLimit(LayoutPartSash newPreLimit) {
224         preLimit = newPreLimit;
225     }
226
227     void setRatio(float newRatio) {
228         int total = left + right;
229         int newLeft = (int) (total * newRatio);
230         setSizes(newLeft, total - newLeft);
231     }
232
233     void setSizes(int left, int right) {
234         if (left < 0 || right < 0) {
235             return;
236         }
237         
238         if (left == this.left && right == this.right) {
239             return;
240         }
241         
242         this.left = left;
243         this.right = right;
244         
245         flushCache();
246     }
247     
248     private void flushCache() {
249         LayoutTree root = rootContainer.getLayoutTree();
250
251         if (root != null) {
252             LayoutTreeNode node = root.findSash(this);
253             if (node != null) {
254                 node.flushCache();
255             }
256         }
257     }
258
259     private void widgetSelected(int x, int y, int width, int height) {
260         if (!enabled) {
261             return;
262         }
263         
264         LayoutTree root = rootContainer.getLayoutTree();
265         LayoutTreeNode node = root.findSash(this);
266         Rectangle nodeBounds = node.getBounds();
267         //Recompute ratio
268
x -= nodeBounds.x;
269         y -= nodeBounds.y;
270         if (style == SWT.VERTICAL) {
271             setSizes(x, nodeBounds.width - x - LayoutTreeNode.SASH_WIDTH);
272         } else {
273             setSizes(y, nodeBounds.height - y - LayoutTreeNode.SASH_WIDTH);
274         }
275
276         node.setBounds(nodeBounds);
277     }
278
279     /**
280      * @param resizable
281      * @since 3.1
282      */

283     public void setEnabled(boolean resizable) {
284         this.enabled = resizable;
285         if (sash != null) {
286             sash.setEnabled(enabled);
287         }
288     }
289
290 }
291
Popular Tags