1 11 package org.eclipse.ui.internal.progress; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 38 public class ProgressViewer extends AbstractProgressViewer { 39 Canvas canvas; 40 41 Object [] displayedItems = new Object [0]; 42 43 private final static List EMPTY_LIST = new ArrayList (); 44 45 48 private FontMetrics fontMetrics; 49 50 private int numShowItems = 1; 51 52 private int maxCharacterWidth; 53 54 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 GC gc = new GC(canvas); 70 gc.setFont(JFaceResources.getDefaultFont()); 71 fontMetrics = gc.getFontMetrics(); 72 gc.dispose(); 73 initializeListeners(); 74 } 75 76 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 96 protected Widget doFindInputItem(Object element) { 97 return null; } 99 100 105 protected Widget doFindItem(Object element) { 106 return null; } 108 109 115 protected void doUpdateItem(Widget item, Object element, boolean fullMap) { 116 canvas.redraw(); 117 } 118 119 124 protected List getSelectionFromWidget() { 125 return EMPTY_LIST; 127 } 128 129 134 protected void internalRefresh(Object element) { 135 displayedItems = getSortedChildren(getRoot()); 136 canvas.redraw(); 137 } 138 139 144 public void reveal(Object element) { 145 } 147 148 154 protected void setSelectionToWidget(List l, boolean reveal) { 155 } 157 158 163 public Control getControl() { 164 return canvas; 165 } 166 167 private void initializeListeners() { 168 canvas.addPaintListener(new PaintListener() { 169 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) { 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 string = labelProvider.getText(displayedItems[i]); 191 if(string == null) 192 string = ""; gc.drawString(string, 2, yOffset 194 + (i * fontMetrics.getHeight()), true); 195 } 196 } 197 }); 198 } 199 200 203 public void setLabelProvider(IBaseLabelProvider labelProvider) { 204 Assert.isTrue(labelProvider instanceof ILabelProvider); 205 super.setLabelProvider(labelProvider); 206 } 207 208 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 |