KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Cagatay Kavukcuoglu <cagatayk@acm.org>
11  * - Fix for bug 10025 - Resizing views should not use height ratios
12  *******************************************************************************/

13 package org.eclipse.ui.internal;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.graphics.Rectangle;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.ui.ISizeProvider;
22 import org.eclipse.ui.IWorkbenchWindow;
23 import org.eclipse.ui.internal.dnd.IDropTarget;
24 import org.eclipse.ui.internal.dnd.SwtUtil;
25
26 /**
27  * A presentation part is used to build the presentation for the
28  * workbench. Common subclasses are pane and folder.
29  */

30 abstract public class LayoutPart implements ISizeProvider {
31     protected ILayoutContainer container;
32
33     protected String JavaDoc id;
34
35     public static final String JavaDoc PROP_VISIBILITY = "PROP_VISIBILITY"; //$NON-NLS-1$
36

37     /**
38      * Number of times deferUpdates(true) has been called without a corresponding
39      * deferUpdates(false)
40      */

41     private int deferCount = 0;
42
43     /**
44      * PresentationPart constructor comment.
45      */

46     public LayoutPart(String JavaDoc id) {
47         super();
48         this.id = id;
49     }
50     
51     /**
52      * When a layout part closes, focus will return to a previously active part.
53      * This method determines whether this part should be considered for activation
54      * when another part closes. If a group of parts are all closing at the same time,
55      * they will all return false from this method while closing to ensure that the
56      * parent does not activate a part that is in the process of closing. Parts will
57      * also return false from this method if they are minimized, closed fast views,
58      * obscured by zoom, etc.
59      *
60      * @return true iff the parts in this container may be given focus when the active
61      * part is closed
62      */

63     public boolean allowsAutoFocus() {
64         if (container != null) {
65             return container.allowsAutoFocus();
66         }
67         return true;
68     }
69
70
71     /**
72      * Creates the SWT control
73      */

74     abstract public void createControl(Composite parent);
75
76     /**
77      * Disposes the SWT control
78      */

79     public void dispose() {
80     }
81
82     /**
83      * Gets the presentation bounds.
84      */

85     public Rectangle getBounds() {
86         return new Rectangle(0, 0, 0, 0);
87     }
88
89     /**
90      * Gets the parent for this part.
91      * <p>
92      * In general, this is non-null if the object has been added to a container and the
93      * container's widgetry exists. The exception to this rule is PartPlaceholders
94      * created when restoring a ViewStack using restoreState, which point to the
95      * ViewStack even if its widgetry doesn't exist yet. Returns null in the remaining
96      * cases.
97      * </p>
98      * <p>
99      * TODO: change the semantics of this method to always point to the parent container,
100      * regardless of whether its widgetry exists. Locate and refactor code that is currently
101      * depending on the special cases.
102      * </p>
103      */

104     public ILayoutContainer getContainer() {
105         return container;
106     }
107
108     /**
109      * Get the part control. This method may return null.
110      */

111     abstract public Control getControl();
112
113     /**
114      * Gets the ID for this part.
115      */

116     public String JavaDoc getID() {
117         return id;
118     }
119
120     /**
121      * Returns the compound ID for this part.
122      * The compound ID is of the form: primaryId [':' + secondaryId]
123      *
124      * @return the compound ID for this part.
125      */

126     public String JavaDoc getCompoundId() {
127         return getID();
128     }
129
130     public boolean isCompressible() {
131         return false;
132     }
133
134     /**
135      * Gets the presentation size.
136      */

137     public Point getSize() {
138         Rectangle r = getBounds();
139         Point ptSize = new Point(r.width, r.height);
140         return ptSize;
141     }
142
143     /**
144      * @see org.eclipse.ui.presentations.StackPresentation#getSizeFlags(boolean)
145      *
146      * @since 3.1
147      */

148     public int getSizeFlags(boolean horizontal) {
149         return SWT.MIN;
150     }
151     
152     /**
153      * @see org.eclipse.ui.presentations.StackPresentation#computePreferredSize(boolean, int, int, int)
154      *
155      * @since 3.1
156      */

157     public int computePreferredSize(boolean width, int availableParallel, int availablePerpendicular, int preferredParallel) {
158         
159         return preferredParallel;
160     }
161     
162     public IDropTarget getDropTarget(Object JavaDoc draggedObject, Point displayCoordinates) {
163         return null;
164     }
165     
166     public boolean isDocked() {
167         Shell s = getShell();
168         if (s == null) {
169             return false;
170         }
171         
172         return s.getData() instanceof IWorkbenchWindow;
173     }
174     
175     public Shell getShell() {
176         Control ctrl = getControl();
177         if (!SwtUtil.isDisposed(ctrl)) {
178             return ctrl.getShell();
179         }
180         return null;
181     }
182
183     /**
184      * Returns the workbench window window for a part.
185      *
186      * @return the workbench window, or <code>null</code> if there's no window
187      * associated with this part.
188      */

189     public IWorkbenchWindow getWorkbenchWindow() {
190         Shell s = getShell();
191         if (s==null) {
192             return null;
193         }
194         Object JavaDoc data = s.getData();
195         if (data instanceof IWorkbenchWindow) {
196             return (IWorkbenchWindow)data;
197         } else if (data instanceof DetachedWindow) {
198             return ((DetachedWindow) data).getWorkbenchPage()
199                 .getWorkbenchWindow();
200         }
201         
202         return null;
203         
204     }
205
206     /**
207      * Move the control over another one.
208      */

209     public void moveAbove(Control refControl) {
210     }
211
212     /**
213      * Reparent a part.
214      */

215     public void reparent(Composite newParent) {
216         Control control = getControl();
217         if ((control == null) || (control.getParent() == newParent)) {
218             return;
219         }
220
221         if (control.isReparentable()) {
222             // make control small in case it is not resized with other controls
223
//control.setBounds(0, 0, 0, 0);
224
// By setting the control to disabled before moving it,
225
// we ensure that the focus goes away from the control and its children
226
// and moves somewhere else
227
boolean enabled = control.getEnabled();
228             control.setEnabled(false);
229             control.setParent(newParent);
230             control.setEnabled(enabled);
231             control.moveAbove(null);
232         }
233     }
234
235     /**
236      * Returns true if this part was set visible. This returns whatever was last passed into
237      * setVisible, but does not necessarily indicate that the part can be seen (ie: one of its
238      * ancestors may be invisible)
239      */

240     public boolean getVisible() {
241         Control ctrl = getControl();
242         if (!SwtUtil.isDisposed(ctrl)) {
243             return ctrl.getVisible();
244         }
245         return false;
246     }
247     
248     /**
249      * Returns true if this part can be seen. Returns false if the part or any of its ancestors
250      * are invisible.
251      */

252     public boolean isVisible() {
253         Control ctrl = getControl();
254         if (ctrl != null && !ctrl.isDisposed()) {
255             return ctrl.isVisible();
256         }
257         return false;
258     }
259
260     /**
261      * Shows the receiver if <code>visible</code> is true otherwise hide it.
262      */

263     public void setVisible(boolean makeVisible) {
264         Control ctrl = getControl();
265         if (!SwtUtil.isDisposed(ctrl)) {
266             if (makeVisible == ctrl.getVisible()) {
267                 return;
268             }
269
270             if (!makeVisible && isFocusAncestor(ctrl)) {
271                 // Workaround for Bug 60970 [EditorMgmt] setActive() called on an editor when it does not have focus.
272
// Force focus on the shell so that when ctrl is hidden,
273
// SWT does not try to send focus elsewhere, which may cause
274
// some other part to be activated, which affects the part
275
// activation order and can cause flicker.
276
ctrl.getShell().forceFocus();
277             }
278
279             ctrl.setVisible(makeVisible);
280         }
281     }
282
283     /**
284      * Returns <code>true</code> if the given control or any of its descendents has focus.
285      */

286     private boolean isFocusAncestor(Control ctrl) {
287         Control f = ctrl.getDisplay().getFocusControl();
288         while (f != null && f != ctrl) {
289             f = f.getParent();
290         }
291         return f == ctrl;
292     }
293
294     /**
295      * Sets the presentation bounds.
296      */

297     public void setBounds(Rectangle r) {
298         Control ctrl = getControl();
299         if (!SwtUtil.isDisposed(ctrl)) {
300             ctrl.setBounds(r);
301         }
302     }
303
304     /**
305      * Sets the parent for this part.
306      */

307     public void setContainer(ILayoutContainer container) {
308         
309         this.container = container;
310         
311         if (container != null) {
312             setZoomed(container.childIsZoomed(this));
313         }
314     }
315
316     /**
317      * Sets focus to this part.
318      */

319     public void setFocus() {
320     }
321
322     /**
323      * Sets the part ID.
324      */

325     public void setID(String JavaDoc str) {
326         id = str;
327     }
328
329     /* (non-Javadoc)
330      * @see org.eclipse.ui.internal.IWorkbenchDragDropPart#getPart()
331      */

332     public LayoutPart getPart() {
333         return this;
334     }
335
336     public void childRequestZoomIn(LayoutPart toZoom) {
337         
338     }
339     
340     public void childRequestZoomOut() {
341         
342     }
343     
344     public final void requestZoomOut() {
345         ILayoutContainer container = getContainer();
346         if (container != null) {
347             container.childRequestZoomOut();
348         }
349     }
350     
351     public final void requestZoomIn() {
352         ILayoutContainer container = getContainer();
353         if (container != null) {
354             container.childRequestZoomIn(this);
355         }
356     }
357     
358     public final boolean isObscuredByZoom() {
359         ILayoutContainer container = getContainer();
360         
361         if (container != null) {
362             return container.childObscuredByZoom(this);
363         }
364         
365         return false;
366     }
367     
368     public boolean childObscuredByZoom(LayoutPart toTest) {
369         return false;
370     }
371     
372     public boolean childIsZoomed(LayoutPart childToTest) {
373         return false;
374     }
375     
376     public void setZoomed(boolean isZoomed) {
377
378     }
379     
380     /**
381      * deferUpdates(true) disables widget updates until a corresponding call to
382      * deferUpdates(false). Exactly what gets deferred is the decision
383      * of each LayoutPart, however the part may only defer operations in a manner
384      * that does not affect the final result.
385      * That is, the state of the receiver after the final call to deferUpdates(false)
386      * must be exactly the same as it would have been if nothing had been deferred.
387      *
388      * @param shouldDefer true iff events should be deferred
389      */

390     public final void deferUpdates(boolean shouldDefer) {
391         if (shouldDefer) {
392             if (deferCount == 0) {
393                 startDeferringEvents();
394             }
395             deferCount++;
396         } else {
397             if (deferCount > 0) {
398                 deferCount--;
399                 if (deferCount == 0) {
400                     handleDeferredEvents();
401                 }
402             }
403         }
404     }
405     
406     /**
407      * This is called when deferUpdates(true) causes UI events for this
408      * part to be deferred. Subclasses can overload to initialize any data
409      * structures that they will use to collect deferred events.
410      */

411     protected void startDeferringEvents() {
412         
413     }
414     
415     /**
416      * Immediately processes all UI events which were deferred due to a call to
417      * deferUpdates(true). This is called when the last call is made to
418      * deferUpdates(false). Subclasses should overload this method if they
419      * defer some or all UI processing during deferUpdates.
420      */

421     protected void handleDeferredEvents() {
422         
423     }
424     
425     /**
426      * Subclasses can call this method to determine whether UI updates should
427      * be deferred. Returns true iff there have been any calls to deferUpdates(true)
428      * without a corresponding call to deferUpdates(false). Any operation which is
429      * deferred based on the result of this method should be performed later within
430      * handleDeferredEvents().
431      *
432      * @return true iff updates should be deferred.
433      */

434     protected final boolean isDeferred() {
435         return deferCount > 0;
436     }
437
438     /**
439      * Writes a description of the layout to the given string buffer.
440      * This is used for drag-drop test suites to determine if two layouts are the
441      * same. Like a hash code, the description should compare as equal iff the
442      * layouts are the same. However, it should be user-readable in order to
443      * help debug failed tests. Although these are english readable strings,
444      * they do not need to be translated.
445      *
446      * @param buf
447      */

448     public void describeLayout(StringBuffer JavaDoc buf) {
449
450     }
451
452     /**
453      * Returns an id representing this part, suitable for use in a placeholder.
454      *
455      * @since 3.0
456      */

457     public String JavaDoc getPlaceHolderId() {
458         return getID();
459     }
460
461     public void resizeChild(LayoutPart childThatChanged) {
462
463     }
464
465     public void flushLayout() {
466         ILayoutContainer container = getContainer();
467         if (getContainer() != null) {
468             container.resizeChild(this);
469         }
470     }
471
472     /**
473      * Returns true iff the given part can be added to this ILayoutContainer
474      * @param toAdd
475      * @return
476      * @since 3.1
477      */

478     public boolean allowsAdd(LayoutPart toAdd) {
479         return false;
480     }
481     
482     /**
483      * Tests the integrity of this object. Throws an exception if the object's state
484      * is not internally consistent. For use in test suites.
485      */

486     public void testInvariants() {
487     }
488 }
489
Popular Tags