KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006 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  ******************************************************************************/

11
12 package org.eclipse.ui.internal;
13
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.jface.dialogs.ControlAnimator;
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.Control;
22 import org.eclipse.ui.progress.UIJob;
23
24 /**
25  * Animates a control by sliding it up or down. The animation is
26  * achieved using the UI's job functionality and by incrementally
27  * decreasing or increasing the control's y coordinate.
28  *
29  * @since 3.2
30  */

31 public class WorkbenchControlAnimator extends ControlAnimator {
32     
33     /**
34      * A constant denoting the CLOSED animation state of
35      * a control (value is 0)
36      */

37     private static final int CLOSED = 0;
38
39     /**
40      * A constant denoting the OPENING animation state of
41      * a control (value is 1)
42      */

43     private static final int OPENING = 1;
44
45     /**
46      * A constant denoting the OPEN animation state of
47      * a control (value is 2)
48      */

49     private static final int OPEN = 2;
50
51     /**
52      * A constant denoting the CLOSING animation state of
53      * a control (value is 3)
54      */

55     private static final int CLOSING = 3;
56     
57     private UIJob slideJob;
58     
59     private int endY;
60     
61     private boolean finished;
62     
63     private boolean inTransition = false;
64     
65     private boolean animated = false;
66     
67     private int state = CLOSED;
68     
69     private int LONG_DELAY = 300;
70     
71     private int SHORT_DELAY = 25;
72     
73
74     /**
75      * Constructs a new WorbenchControlAnimator instance and passes along the
76      * control that will be animated.
77      *
78      * @param control the control that will be animated.
79      */

80     public WorkbenchControlAnimator(Control control) {
81         super(control);
82     }
83
84     /* (non-Javadoc)
85      * @see org.eclipse.jface.dialogs.ControlAnimator#setVisible(boolean)
86      */

87     public void setVisible(boolean visible) {
88 // // return immediately if already OPENING/OPEN and
89
// // visible is true or if CLOSING/CLOSED and visible
90
// // is false.
91
// switch (getAnimationState()) {
92
// case OPENING:
93
// case OPEN:
94
// if (visible)
95
// return;
96
// break;
97
// case CLOSING:
98
// case CLOSED:
99
// if (!visible)
100
// return;
101
// break;
102
// }
103

104         if(animated) {
105             super.setVisible(visible);
106             return;
107         }
108         
109         setAnimationState(visible ? OPENING: CLOSING);
110         finished = false;
111
112         control.setVisible(true);
113         
114         Rectangle parentBounds = control.getParent().getBounds();
115         int bottom = parentBounds.height;
116         
117         if (bottom <= 0) {
118             setAnimationState(OPEN);
119             return;
120         }
121         
122         endY = visible ? bottom - control.getBounds().height
123                 : bottom;
124         
125         if(slideJob != null)
126             slideJob.cancel();
127         
128         slideJob = getSlideJob();
129         control.addDisposeListener(new DisposeListener(){
130             public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
131                 slideJob = null;
132             }
133         });
134         
135         // Wait before displaying the control to allow for opening
136
// condition to change, but no waiting before closing the control.
137
if(getAnimationState() == OPENING && !inTransition){
138             slideJob.schedule(LONG_DELAY);
139         } else {
140             slideJob.schedule(SHORT_DELAY);
141         }
142         
143     }
144     
145     /**
146      * Creates a job in the UI thread to display or hide the control
147      * by increasing or decreasing the y coordinate and setting the new
148      * location. The job will continually re-schedule itself until the
149      * the control is no longer in transition and will also stop
150      * if the monitor is canceled or the control is disposed.
151      *
152      * @return the UIJob responsible for opening or closing the control
153      */

154     private UIJob getSlideJob(){
155         UIJob newSlideJob = new UIJob("Sliding Message") { //$NON-NLS-1$
156
public IStatus runInUIThread(IProgressMonitor monitor) {
157                 if(!monitor.isCanceled() && !control.isDisposed()){
158                     Point loc = control.getLocation();
159                     switch (getAnimationState()) {
160                     case OPENING:
161                         animated = true;
162                         loc.y--;
163                         if (loc.y >= endY) {
164                             control.setLocation(loc);
165                         } else {
166                             finished = true;
167                             setAnimationState(OPEN);
168                         }
169                         break;
170                     case CLOSING:
171                         loc.y++;
172                         if (loc.y <= endY) {
173                             control.setLocation(loc);
174                         } else {
175                             finished = true;
176                             setAnimationState(CLOSED);
177                             control.setVisible(false);
178                         }
179                         break;
180                     default:
181                         break;
182                     }
183                     if(!finished) {
184                         inTransition = true;
185                         slideJob.schedule(5);
186                     } else
187                         inTransition = false;
188                     return Status.OK_STATUS;
189                 }
190                 return Status.CANCEL_STATUS;
191             }
192         };
193         newSlideJob.setSystem(true);
194         return newSlideJob;
195     }
196     
197     /**
198      * Sets the state of the control and whether or not
199      * it should be visible. The value should be one of
200      * the following: {@link #OPENING}, {@link #OPEN},
201      * {@link #CLOSING}, or {@link #CLOSED}
202      *
203      * @param state the desired state of the control
204      */

205     private void setAnimationState(int state) {
206         this.state = state;
207     }
208
209     /**
210      * Returns the current state of the control.
211      *
212      * @return the current state of the control: {@link #OPENING},
213      * {@link #OPEN}, {@link #CLOSING}, or {@link #CLOSED}
214      */

215     private int getAnimationState() {
216         return state;
217     }
218
219 }
220
Popular Tags