KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.ui.internal.progress;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.jface.viewers.IBaseLabelProvider;
19 import org.eclipse.jface.viewers.ILabelProvider;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.DisposeEvent;
22 import org.eclipse.swt.events.DisposeListener;
23 import org.eclipse.swt.events.PaintEvent;
24 import org.eclipse.swt.events.PaintListener;
25 import org.eclipse.swt.graphics.FontMetrics;
26 import org.eclipse.swt.graphics.GC;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.graphics.Rectangle;
29 import org.eclipse.swt.graphics.Transform;
30 import org.eclipse.swt.widgets.Canvas;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Widget;
35 import org.eclipse.ui.internal.TrimUtil;
36
37 /**
38  * The ProgressCanvasViewer is the viewer used by progress windows. It displays text
39  * on the canvas.
40  */

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

51     private FontMetrics fontMetrics;
52
53     private int numShowItems = 1;
54
55     private int maxCharacterWidth;
56
57     private int orientation = SWT.HORIZONTAL;
58
59     /**
60      * Create a new instance of the receiver with the supplied
61      * parent and style bits.
62      * @param parent The composite the Canvas is created in
63      * @param style style bits for the canvas
64      * @param itemsToShow the number of items this will show
65      * @param numChars The number of characters for the width hint.
66      * @param side the side to display text, this helps determine horizontal vs vertical
67      */

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

90     protected void hookControl(Control control) {
91         control.addDisposeListener(new DisposeListener() {
92             public void widgetDisposed(DisposeEvent event) {
93                 handleDispose(event);
94             }
95         });
96     }
97
98     /*
99      * (non-Javadoc)
100      *
101      * @see org.eclipse.jface.viewers.StructuredViewer#doFindInputItem(java.lang.Object)
102      */

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

112     protected Widget doFindItem(Object JavaDoc element) {
113         return null; // No widgets associated with items
114
}
115
116     /*
117      * (non-Javadoc)
118      *
119      * @see org.eclipse.jface.viewers.StructuredViewer#doUpdateItem(org.eclipse.swt.widgets.Widget,
120      * java.lang.Object, boolean)
121      */

122     protected void doUpdateItem(Widget item, Object JavaDoc element, boolean fullMap) {
123         canvas.redraw();
124     }
125
126     /*
127      * (non-Javadoc)
128      *
129      * @see org.eclipse.jface.viewers.StructuredViewer#getSelectionFromWidget()
130      */

131     protected List JavaDoc getSelectionFromWidget() {
132         //No selection on a Canvas
133
return EMPTY_LIST;
134     }
135
136     /*
137      * (non-Javadoc)
138      *
139      * @see org.eclipse.jface.viewers.StructuredViewer#internalRefresh(java.lang.Object)
140      */

141     protected void internalRefresh(Object JavaDoc element) {
142         displayedItems = getSortedChildren(getRoot());
143         canvas.redraw();
144     }
145
146     /*
147      * (non-Javadoc)
148      *
149      * @see org.eclipse.jface.viewers.StructuredViewer#reveal(java.lang.Object)
150      */

151     public void reveal(Object JavaDoc element) {
152         //Nothing to do here as we do not scroll
153
}
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see org.eclipse.jface.viewers.StructuredViewer#setSelectionToWidget(java.util.List,
159      * boolean)
160      */

161     protected void setSelectionToWidget(List JavaDoc l, boolean reveal) {
162         //Do nothing as there is no selection
163
}
164
165     /*
166      * (non-Javadoc)
167      *
168      * @see org.eclipse.jface.viewers.Viewer#getControl()
169      */

170     public Control getControl() {
171         return canvas;
172     }
173
174     private void initializeListeners() {
175         canvas.addPaintListener(new PaintListener() {
176             /*
177              * (non-Javadoc)
178              *
179              * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
180              */

181             public void paintControl(PaintEvent event) {
182
183                 GC gc = event.gc;
184                 Transform transform = null;
185                 if (orientation == SWT.VERTICAL) {
186                     transform = new Transform(event.display);
187                     transform.translate(TrimUtil.TRIM_DEFAULT_HEIGHT, 0);
188                     transform.rotate(90);
189                 }
190                 ILabelProvider labelProvider = (ILabelProvider) getLabelProvider();
191
192                 int itemCount = Math.min(displayedItems.length, numShowItems);
193
194                 int yOffset = 0;
195                 int xOffset = 0;
196                 if (numShowItems == 1) {//If there is a single item try to center it
197
Rectangle clientArea = canvas.getParent().getClientArea();
198                     if (orientation == SWT.HORIZONTAL) {
199                         int size = clientArea.height;
200                         yOffset = size - (fontMetrics.getHeight());
201                         yOffset = yOffset / 2;
202                     } else {
203                         int size = clientArea.width;
204                         xOffset = size - (fontMetrics.getHeight());
205                         xOffset = xOffset / 2;
206                     }
207                 }
208
209                 for (int i = 0; i < itemCount; i++) {
210                     String JavaDoc string = labelProvider.getText(displayedItems[i]);
211                     if(string == null) {
212                         string = "";//$NON-NLS-1$
213
}
214                     if (orientation == SWT.HORIZONTAL) {
215                         gc.drawString(string, 2, yOffset + (i * fontMetrics.getHeight()), true);
216                     } else {
217                         gc.setTransform(transform);
218                         gc.drawString(string, xOffset + (i * fontMetrics.getHeight()), 2, true);
219                     }
220                 }
221                 if (transform != null)
222                     transform.dispose();
223             }
224         });
225     }
226
227     /* (non-Javadoc)
228      * @see org.eclipse.jface.viewers.ContentViewer#setLabelProvider(org.eclipse.jface.viewers.IBaseLabelProvider)
229      */

230     public void setLabelProvider(IBaseLabelProvider labelProvider) {
231         Assert.isTrue(labelProvider instanceof ILabelProvider);
232         super.setLabelProvider(labelProvider);
233     }
234
235     /**
236      * Get the size hints for the receiver. These are used for
237      * layout data.
238      * @return Point - the preferred x and y coordinates
239      */

240     public Point getSizeHints() {
241
242         Display display = canvas.getDisplay();
243
244         GC gc = new GC(canvas);
245         FontMetrics fm = gc.getFontMetrics();
246         int charWidth = fm.getAverageCharWidth();
247         int charHeight = fm.getHeight();
248         int maxWidth = display.getBounds().width / 2;
249         int maxHeight = display.getBounds().height / 6;
250         int fontWidth = charWidth * maxCharacterWidth;
251         int fontHeight = charHeight * numShowItems;
252         if (maxWidth < fontWidth) {
253             fontWidth = maxWidth;
254         }
255         if (maxHeight < fontHeight) {
256             fontHeight = maxHeight;
257         }
258         gc.dispose();
259         return new Point(fontWidth, fontHeight);
260     }
261
262     /* (non-Javadoc)
263      * @see org.eclipse.ui.internal.progress.AbstractProgressViewer#add(java.lang.Object[])
264      */

265     public void add(Object JavaDoc[] elements) {
266         refresh(true);
267         
268     }
269
270     /* (non-Javadoc)
271      * @see org.eclipse.ui.internal.progress.AbstractProgressViewer#remove(java.lang.Object[])
272      */

273     public void remove(Object JavaDoc[] elements) {
274         refresh(true);
275         
276     }
277
278
279 }
280
Popular Tags