KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > action > StatusLineManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.jface.action;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.IProgressMonitorWithBlocking;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20
21 /**
22  * A status line manager is a contribution manager which realizes itself and its items
23  * in a status line control.
24  * <p>
25  * This class may be instantiated; it may also be subclassed if a more
26  * sophisticated layout is required.
27  * </p>
28  */

29 public class StatusLineManager extends ContributionManager implements
30         IStatusLineManager {
31
32     /**
33      * Identifier of group marker used to position contributions at the beginning
34      * of the status line.
35      *
36      * @since 3.0
37      */

38     public static final String JavaDoc BEGIN_GROUP = "BEGIN_GROUP"; //$NON-NLS-1$
39

40     /**
41      * Identifier of group marker used to position contributions in the middle
42      * of the status line.
43      *
44      * @since 3.0
45      */

46     public static final String JavaDoc MIDDLE_GROUP = "MIDDLE_GROUP"; //$NON-NLS-1$
47

48     /**
49      * Identifier of group marker used to position contributions at the end
50      * of the status line.
51      *
52      * @since 3.0
53      */

54     public static final String JavaDoc END_GROUP = "END_GROUP"; //$NON-NLS-1$
55

56     /**
57      * The status line control; <code>null</code> before
58      * creation and after disposal.
59      */

60     private Composite statusLine = null;
61
62     /**
63      * Creates a new status line manager.
64      * Use the <code>createControl</code> method to create the
65      * status line control.
66      */

67     public StatusLineManager() {
68         add(new GroupMarker(BEGIN_GROUP));
69         add(new GroupMarker(MIDDLE_GROUP));
70         add(new GroupMarker(END_GROUP));
71     }
72
73     /**
74      * Creates and returns this manager's status line control.
75      * Does not create a new control if one already exists.
76      * <p>
77      * Note: Since 3.0 the return type is <code>Control</code>. Before 3.0, the return type was
78      * the package-private class <code>StatusLine</code>.
79      * </p>
80      *
81      * @param parent the parent control
82      * @return the status line control
83      */

84     public Control createControl(Composite parent) {
85         return createControl(parent, SWT.NONE);
86     }
87
88     /**
89      * Creates and returns this manager's status line control.
90      * Does not create a new control if one already exists.
91      *
92      * @param parent the parent control
93      * @param style the style for the control
94      * @return the status line control
95      * @since 3.0
96      */

97     public Control createControl(Composite parent, int style) {
98         if (!statusLineExist() && parent != null) {
99             statusLine = new StatusLine(parent, style);
100             update(false);
101         }
102         return statusLine;
103     }
104
105     /**
106      * Disposes of this status line manager and frees all allocated SWT resources.
107      * Notifies all contribution items of the dispose. Note that this method does
108      * not clean up references between this status line manager and its associated
109      * contribution items. Use <code>removeAll</code> for that purpose.
110      */

111     public void dispose() {
112         if (statusLineExist()) {
113             statusLine.dispose();
114         }
115         statusLine = null;
116
117         IContributionItem items[] = getItems();
118         for (int i = 0; i < items.length; i++) {
119             items[i].dispose();
120         }
121     }
122
123     /**
124      * Returns the control used by this StatusLineManager.
125      *
126      * @return the control used by this manager
127      */

128     public Control getControl() {
129         return statusLine;
130     }
131
132     /**
133      * Returns the progress monitor delegate. Override this method
134      * to provide your own object used to handle progress.
135      *
136      * @return the IProgressMonitor delegate
137      * @since 3.0
138      */

139     protected IProgressMonitor getProgressMonitorDelegate() {
140         return (IProgressMonitor) getControl();
141     }
142
143     /*
144      * (non-Javadoc)
145      * Method declared on IStatusLineManager
146      */

147     public IProgressMonitor getProgressMonitor() {
148
149         return new IProgressMonitorWithBlocking() {
150
151             IProgressMonitor progressDelegate = getProgressMonitorDelegate();
152
153             /* (non-Javadoc)
154              * @see org.eclipse.core.runtime.IProgressMonitor#beginTask(java.lang.String, int)
155              */

156             public void beginTask(String JavaDoc name, int totalWork) {
157                 progressDelegate.beginTask(name, totalWork);
158
159             }
160
161             /* (non-Javadoc)
162              * @see org.eclipse.core.runtime.IProgressMonitor#done()
163              */

164             public void done() {
165                 progressDelegate.done();
166             }
167
168             /* (non-Javadoc)
169              * @see org.eclipse.core.runtime.IProgressMonitor#internalWorked(double)
170              */

171             public void internalWorked(double work) {
172                 progressDelegate.internalWorked(work);
173
174             }
175
176             /* (non-Javadoc)
177              * @see org.eclipse.core.runtime.IProgressMonitor#isCanceled()
178              */

179             public boolean isCanceled() {
180                 return progressDelegate.isCanceled();
181             }
182
183             /* (non-Javadoc)
184              * @see org.eclipse.core.runtime.IProgressMonitor#setCanceled(boolean)
185              */

186             public void setCanceled(boolean value) {
187                 //Don't bother updating for disposed status
188
if (statusLine.isDisposed()) {
189                     return;
190                 }
191                 progressDelegate.setCanceled(value);
192
193             }
194
195             /* (non-Javadoc)
196              * @see org.eclipse.core.runtime.IProgressMonitor#setTaskName(java.lang.String)
197              */

198             public void setTaskName(String JavaDoc name) {
199                 progressDelegate.setTaskName(name);
200
201             }
202
203             /* (non-Javadoc)
204              * @see org.eclipse.core.runtime.IProgressMonitor#subTask(java.lang.String)
205              */

206             public void subTask(String JavaDoc name) {
207                 progressDelegate.subTask(name);
208
209             }
210
211             /* (non-Javadoc)
212              * @see org.eclipse.core.runtime.IProgressMonitor#worked(int)
213              */

214             public void worked(int work) {
215                 progressDelegate.worked(work);
216             }
217
218             /* (non-Javadoc)
219              * @see org.eclipse.core.runtime.IProgressMonitorWithBlocking#clearBlocked()
220              */

221             public void clearBlocked() {
222                 //Do nothing here as we let the modal context handle it
223
}
224
225             /* (non-Javadoc)
226              * @see org.eclipse.core.runtime.IProgressMonitorWithBlocking#setBlocked(org.eclipse.core.runtime.IStatus)
227              */

228             public void setBlocked(IStatus reason) {
229                 // Do nothing here as we let the modal context handle it
230
}
231         };
232     }
233
234     /* (non-Javadoc)
235      * Method declared on IStatueLineManager
236      */

237     public boolean isCancelEnabled() {
238         return statusLineExist() && ((StatusLine) statusLine).isCancelEnabled();
239     }
240
241     /* (non-Javadoc)
242      * Method declared on IStatueLineManager
243      */

244     public void setCancelEnabled(boolean enabled) {
245         if (statusLineExist()) {
246             ((StatusLine) statusLine).setCancelEnabled(enabled);
247         }
248     }
249
250     /* (non-Javadoc)
251      * Method declared on IStatusLineManager.
252      */

253     public void setErrorMessage(String JavaDoc message) {
254         if (statusLineExist()) {
255             ((StatusLine) statusLine).setErrorMessage(message);
256         }
257     }
258
259     /* (non-Javadoc)
260      * Method declared on IStatusLineManager.
261      */

262     public void setErrorMessage(Image image, String JavaDoc message) {
263         if (statusLineExist()) {
264             ((StatusLine) statusLine).setErrorMessage(image, message);
265         }
266     }
267
268     /* (non-Javadoc)
269      * Method declared on IStatusLineManager.
270      */

271     public void setMessage(String JavaDoc message) {
272         if (statusLineExist()) {
273             ((StatusLine) statusLine).setMessage(message);
274         }
275     }
276
277     /* (non-Javadoc)
278      * Method declared on IStatusLineManager.
279      */

280     public void setMessage(Image image, String JavaDoc message) {
281         if (statusLineExist()) {
282             ((StatusLine) statusLine).setMessage(image, message);
283         }
284     }
285
286     /**
287      * Returns whether the status line control is created
288      * and not disposed.
289      *
290      * @return <code>true</code> if the control is created
291      * and not disposed, <code>false</code> otherwise
292      */

293     private boolean statusLineExist() {
294         return statusLine != null && !statusLine.isDisposed();
295     }
296
297     /* (non-Javadoc)
298      * Method declared on IContributionManager.
299      */

300     public void update(boolean force) {
301
302         //boolean DEBUG= false;
303

304         if (isDirty() || force) {
305
306             if (statusLineExist()) {
307                 statusLine.setRedraw(false);
308
309                 // NOTE: the update algorithm is non-incremental.
310
// An incremental algorithm requires that SWT items can be created in the middle of the list
311
// but the ContributionItem.fill(Composite) method used here does not take an index, so this
312
// is not possible.
313

314                 Control ws[] = statusLine.getChildren();
315                 for (int i = 0; i < ws.length; i++) {
316                     Control w = ws[i];
317                     Object JavaDoc data = w.getData();
318                     if (data instanceof IContributionItem) {
319                         w.dispose();
320                     }
321                 }
322
323                 int oldChildCount = statusLine.getChildren().length;
324                 IContributionItem[] items = getItems();
325                 for (int i = 0; i < items.length; ++i) {
326                     IContributionItem ci = items[i];
327                     if (ci.isVisible()) {
328                         ci.fill(statusLine);
329                         // associate controls with contribution item
330
Control[] newChildren = statusLine.getChildren();
331                         for (int j = oldChildCount; j < newChildren.length; j++) {
332                             newChildren[j].setData(ci);
333                         }
334                         oldChildCount = newChildren.length;
335                     }
336                 }
337
338                 setDirty(false);
339
340                 statusLine.layout();
341                 statusLine.setRedraw(true);
342             }
343         }
344     }
345
346 }
347
Popular Tags