KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > progress > ProgressViewer


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

11 package org.eclipse.ui.internal.progress;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.jface.viewers.IBaseLabelProvider;
18 import org.eclipse.jface.viewers.ILabelProvider;
19 import org.eclipse.swt.events.DisposeEvent;
20 import org.eclipse.swt.events.DisposeListener;
21 import org.eclipse.swt.events.PaintEvent;
22 import org.eclipse.swt.events.PaintListener;
23 import org.eclipse.swt.graphics.FontMetrics;
24 import org.eclipse.swt.graphics.GC;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.swt.graphics.Rectangle;
27 import org.eclipse.swt.widgets.Canvas;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.widgets.Widget;
32 import org.eclipse.ui.internal.misc.Assert;
33
34 /**
35  * The ProgressViewer is the viewer used by progress windows. It displays text
36  * on the canvas.
37  */

38 public class ProgressViewer extends AbstractProgressViewer {
39     Canvas canvas;
40
41     Object JavaDoc[] displayedItems = new Object JavaDoc[0];
42
43     private final static List JavaDoc EMPTY_LIST = new ArrayList JavaDoc();
44
45     /**
46      * Font metrics to use for determining pixel sizes.
47      */

48     private FontMetrics fontMetrics;
49
50     private int numShowItems = 1;
51
52     private int maxCharacterWidth;
53
54     /**
55      * Create a new instance of the receiver with the supplied
56      * parent and style bits.
57      * @param parent The composite the Canvas is created in
58      * @param style style bits for the canvas
59      * @param itemsToShow the number of items this will show
60      * @param numChars The number of characters for the width hint.
61      */

62     ProgressViewer(Composite parent, int style, int itemsToShow, int numChars) {
63         super();
64         numShowItems = itemsToShow;
65         maxCharacterWidth = numChars;
66         canvas = new Canvas(parent, style);
67         hookControl(canvas);
68         // Compute and store a font metric
69
GC gc = new GC(canvas);
70         gc.setFont(JFaceResources.getDefaultFont());
71         fontMetrics = gc.getFontMetrics();
72         gc.dispose();
73         initializeListeners();
74     }
75
76     /**
77      * NE: Copied from ContentViewer. We don't want the OpenStrategy hooked
78      * in StructuredViewer.hookControl otherwise the canvas will take focus
79      * since it has a key listener. We don't want this included in the window's
80      * tab traversal order. Defeating it here is more self-contained then
81      * setting the tab list on the shell or other parent composite.
82      */

83     protected void hookControl(Control control) {
84         control.addDisposeListener(new DisposeListener() {
85             public void widgetDisposed(DisposeEvent event) {
86                 handleDispose(event);
87             }
88         });
89     }
90
91     /*
92      * (non-Javadoc)
93      *
94      * @see org.eclipse.jface.viewers.StructuredViewer#doFindInputItem(java.lang.Object)
95      */

96     protected Widget doFindInputItem(Object JavaDoc element) {
97         return null; // No widgets associated with items
98
}
99
100     /*
101      * (non-Javadoc)
102      *
103      * @see org.eclipse.jface.viewers.StructuredViewer#doFindItem(java.lang.Object)
104      */

105     protected Widget doFindItem(Object JavaDoc element) {
106         return null; // No widgets associated with items
107
}
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see org.eclipse.jface.viewers.StructuredViewer#doUpdateItem(org.eclipse.swt.widgets.Widget,
113      * java.lang.Object, boolean)
114      */

115     protected void doUpdateItem(Widget item, Object JavaDoc element, boolean fullMap) {
116         canvas.redraw();
117     }
118
119     /*
120      * (non-Javadoc)
121      *
122      * @see org.eclipse.jface.viewers.StructuredViewer#getSelectionFromWidget()
123      */

124     protected List JavaDoc getSelectionFromWidget() {
125         //No selection on a Canvas
126
return EMPTY_LIST;
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see org.eclipse.jface.viewers.StructuredViewer#internalRefresh(java.lang.Object)
133      */

134     protected void internalRefresh(Object JavaDoc element) {
135         displayedItems = getSortedChildren(getRoot());
136         canvas.redraw();
137     }
138
139     /*
140      * (non-Javadoc)
141      *
142      * @see org.eclipse.jface.viewers.StructuredViewer#reveal(java.lang.Object)
143      */

144     public void reveal(Object JavaDoc element) {
145         //Nothing to do here as we do not scroll
146
}
147
148     /*
149      * (non-Javadoc)
150      *
151      * @see org.eclipse.jface.viewers.StructuredViewer#setSelectionToWidget(java.util.List,
152      * boolean)
153      */

154     protected void setSelectionToWidget(List JavaDoc l, boolean reveal) {
155         //Do nothing as there is no selection
156
}
157
158     /*
159      * (non-Javadoc)
160      *
161      * @see org.eclipse.jface.viewers.Viewer#getControl()
162      */

163     public Control getControl() {
164         return canvas;
165     }
166
167     private void initializeListeners() {
168         canvas.addPaintListener(new PaintListener() {
169             /*
170              * (non-Javadoc)
171              *
172              * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
173              */

174             public void paintControl(PaintEvent event) {
175
176                 GC gc = event.gc;
177                 ILabelProvider labelProvider = (ILabelProvider) getLabelProvider();
178
179                 int itemCount = Math.min(displayedItems.length, numShowItems);
180
181                 int yOffset = 0;
182                 if (numShowItems == 1) {//If there is a single item try to center it
183
Rectangle clientArea = canvas.getParent().getClientArea();
184                     int size = clientArea.height;
185                     yOffset = size - (fontMetrics.getHeight());
186                     yOffset = yOffset / 2;
187                 }
188
189                 for (int i = 0; i < itemCount; i++) {
190                     String JavaDoc string = labelProvider.getText(displayedItems[i]);
191                     if(string == null)
192                         string = "";//$NON-NLS-1$
193
gc.drawString(string, 2, yOffset
194                             + (i * fontMetrics.getHeight()), true);
195                 }
196             }
197         });
198     }
199
200     /* (non-Javadoc)
201      * @see org.eclipse.jface.viewers.ContentViewer#setLabelProvider(org.eclipse.jface.viewers.IBaseLabelProvider)
202      */

203     public void setLabelProvider(IBaseLabelProvider labelProvider) {
204         Assert.isTrue(labelProvider instanceof ILabelProvider);
205         super.setLabelProvider(labelProvider);
206     }
207
208     /**
209      * Get the size hints for the receiver. These are used for
210      * layout data.
211      * @return Point - the preferred x and y coordinates
212      */

213     public Point getSizeHints() {
214
215         Display display = canvas.getDisplay();
216
217         GC gc = new GC(display);
218         FontMetrics fm = gc.getFontMetrics();
219         int charWidth = fm.getAverageCharWidth();
220         int charHeight = fm.getHeight();
221         int maxWidth = display.getBounds().width / 2;
222         int maxHeight = display.getBounds().height / 6;
223         int fontWidth = charWidth * maxCharacterWidth;
224         int fontHeight = charHeight * numShowItems;
225         if (maxWidth < fontWidth)
226             fontWidth = maxWidth;
227         if (maxHeight < fontHeight)
228             fontHeight = maxHeight;
229         gc.dispose();
230         return new Point(fontWidth, fontHeight);
231     }
232
233 }
234
Popular Tags