001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.progress;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.core.runtime.Assert;
016: import org.eclipse.jface.resource.JFaceResources;
017: import org.eclipse.jface.viewers.IBaseLabelProvider;
018: import org.eclipse.jface.viewers.ILabelProvider;
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.events.DisposeEvent;
021: import org.eclipse.swt.events.DisposeListener;
022: import org.eclipse.swt.events.PaintEvent;
023: import org.eclipse.swt.events.PaintListener;
024: import org.eclipse.swt.graphics.FontMetrics;
025: import org.eclipse.swt.graphics.GC;
026: import org.eclipse.swt.graphics.Point;
027: import org.eclipse.swt.graphics.Rectangle;
028: import org.eclipse.swt.graphics.Transform;
029: import org.eclipse.swt.widgets.Canvas;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.swt.widgets.Control;
032: import org.eclipse.swt.widgets.Display;
033: import org.eclipse.swt.widgets.Widget;
034: import org.eclipse.ui.internal.TrimUtil;
035:
036: /**
037: * The ProgressCanvasViewer is the viewer used by progress windows. It displays text
038: * on the canvas.
039: */
040: public class ProgressCanvasViewer extends AbstractProgressViewer {
041: Canvas canvas;
042:
043: Object[] displayedItems = new Object[0];
044:
045: private final static List EMPTY_LIST = new ArrayList();
046:
047: /**
048: * Font metrics to use for determining pixel sizes.
049: */
050: private FontMetrics fontMetrics;
051:
052: private int numShowItems = 1;
053:
054: private int maxCharacterWidth;
055:
056: private int orientation = SWT.HORIZONTAL;
057:
058: /**
059: * Create a new instance of the receiver with the supplied
060: * parent and style bits.
061: * @param parent The composite the Canvas is created in
062: * @param style style bits for the canvas
063: * @param itemsToShow the number of items this will show
064: * @param numChars The number of characters for the width hint.
065: * @param side the side to display text, this helps determine horizontal vs vertical
066: */
067: ProgressCanvasViewer(Composite parent, int style, int itemsToShow,
068: int numChars, int orientation) {
069: super ();
070: this .orientation = orientation;
071: numShowItems = itemsToShow;
072: maxCharacterWidth = numChars;
073: canvas = new Canvas(parent, style);
074: hookControl(canvas);
075: // Compute and store a font metric
076: GC gc = new GC(canvas);
077: gc.setFont(JFaceResources.getDefaultFont());
078: fontMetrics = gc.getFontMetrics();
079: gc.dispose();
080: initializeListeners();
081: }
082:
083: /**
084: * NE: Copied from ContentViewer. We don't want the OpenStrategy hooked
085: * in StructuredViewer.hookControl otherwise the canvas will take focus
086: * since it has a key listener. We don't want this included in the window's
087: * tab traversal order. Defeating it here is more self-contained then
088: * setting the tab list on the shell or other parent composite.
089: */
090: protected void hookControl(Control control) {
091: control.addDisposeListener(new DisposeListener() {
092: public void widgetDisposed(DisposeEvent event) {
093: handleDispose(event);
094: }
095: });
096: }
097:
098: /*
099: * (non-Javadoc)
100: *
101: * @see org.eclipse.jface.viewers.StructuredViewer#doFindInputItem(java.lang.Object)
102: */
103: protected Widget doFindInputItem(Object 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 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 element,
123: boolean fullMap) {
124: canvas.redraw();
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see org.eclipse.jface.viewers.StructuredViewer#getSelectionFromWidget()
131: */
132: protected List getSelectionFromWidget() {
133: //No selection on a Canvas
134: return EMPTY_LIST;
135: }
136:
137: /*
138: * (non-Javadoc)
139: *
140: * @see org.eclipse.jface.viewers.StructuredViewer#internalRefresh(java.lang.Object)
141: */
142: protected void internalRefresh(Object element) {
143: displayedItems = getSortedChildren(getRoot());
144: canvas.redraw();
145: }
146:
147: /*
148: * (non-Javadoc)
149: *
150: * @see org.eclipse.jface.viewers.StructuredViewer#reveal(java.lang.Object)
151: */
152: public void reveal(Object element) {
153: //Nothing to do here as we do not scroll
154: }
155:
156: /*
157: * (non-Javadoc)
158: *
159: * @see org.eclipse.jface.viewers.StructuredViewer#setSelectionToWidget(java.util.List,
160: * boolean)
161: */
162: protected void setSelectionToWidget(List l, boolean reveal) {
163: //Do nothing as there is no selection
164: }
165:
166: /*
167: * (non-Javadoc)
168: *
169: * @see org.eclipse.jface.viewers.Viewer#getControl()
170: */
171: public Control getControl() {
172: return canvas;
173: }
174:
175: private void initializeListeners() {
176: canvas.addPaintListener(new PaintListener() {
177: /*
178: * (non-Javadoc)
179: *
180: * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
181: */
182: public void paintControl(PaintEvent event) {
183:
184: GC gc = event.gc;
185: Transform transform = null;
186: if (orientation == SWT.VERTICAL) {
187: transform = new Transform(event.display);
188: transform
189: .translate(TrimUtil.TRIM_DEFAULT_HEIGHT, 0);
190: transform.rotate(90);
191: }
192: ILabelProvider labelProvider = (ILabelProvider) getLabelProvider();
193:
194: int itemCount = Math.min(displayedItems.length,
195: numShowItems);
196:
197: int yOffset = 0;
198: int xOffset = 0;
199: if (numShowItems == 1) {//If there is a single item try to center it
200: Rectangle clientArea = canvas.getParent()
201: .getClientArea();
202: if (orientation == SWT.HORIZONTAL) {
203: int size = clientArea.height;
204: yOffset = size - (fontMetrics.getHeight());
205: yOffset = yOffset / 2;
206: } else {
207: int size = clientArea.width;
208: xOffset = size - (fontMetrics.getHeight());
209: xOffset = xOffset / 2;
210: }
211: }
212:
213: for (int i = 0; i < itemCount; i++) {
214: String string = labelProvider
215: .getText(displayedItems[i]);
216: if (string == null) {
217: string = "";//$NON-NLS-1$
218: }
219: if (orientation == SWT.HORIZONTAL) {
220: gc.drawString(string, 2, yOffset
221: + (i * fontMetrics.getHeight()), true);
222: } else {
223: gc.setTransform(transform);
224: gc.drawString(string, xOffset
225: + (i * fontMetrics.getHeight()), 2,
226: true);
227: }
228: }
229: if (transform != null)
230: transform.dispose();
231: }
232: });
233: }
234:
235: /* (non-Javadoc)
236: * @see org.eclipse.jface.viewers.ContentViewer#setLabelProvider(org.eclipse.jface.viewers.IBaseLabelProvider)
237: */
238: public void setLabelProvider(IBaseLabelProvider labelProvider) {
239: Assert.isTrue(labelProvider instanceof ILabelProvider);
240: super .setLabelProvider(labelProvider);
241: }
242:
243: /**
244: * Get the size hints for the receiver. These are used for
245: * layout data.
246: * @return Point - the preferred x and y coordinates
247: */
248: public Point getSizeHints() {
249:
250: Display display = canvas.getDisplay();
251:
252: GC gc = new GC(canvas);
253: FontMetrics fm = gc.getFontMetrics();
254: int charWidth = fm.getAverageCharWidth();
255: int charHeight = fm.getHeight();
256: int maxWidth = display.getBounds().width / 2;
257: int maxHeight = display.getBounds().height / 6;
258: int fontWidth = charWidth * maxCharacterWidth;
259: int fontHeight = charHeight * numShowItems;
260: if (maxWidth < fontWidth) {
261: fontWidth = maxWidth;
262: }
263: if (maxHeight < fontHeight) {
264: fontHeight = maxHeight;
265: }
266: gc.dispose();
267: return new Point(fontWidth, fontHeight);
268: }
269:
270: /* (non-Javadoc)
271: * @see org.eclipse.ui.internal.progress.AbstractProgressViewer#add(java.lang.Object[])
272: */
273: public void add(Object[] elements) {
274: refresh(true);
275:
276: }
277:
278: /* (non-Javadoc)
279: * @see org.eclipse.ui.internal.progress.AbstractProgressViewer#remove(java.lang.Object[])
280: */
281: public void remove(Object[] elements) {
282: refresh(true);
283:
284: }
285:
286: }
|