1 11 package org.eclipse.ui.internal.progress; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 41 public class ProgressCanvasViewer extends AbstractProgressViewer { 42 Canvas canvas; 43 44 Object [] displayedItems = new Object [0]; 45 46 private final static List EMPTY_LIST = new ArrayList (); 47 48 51 private FontMetrics fontMetrics; 52 53 private int numShowItems = 1; 54 55 private int maxCharacterWidth; 56 57 private int orientation = SWT.HORIZONTAL; 58 59 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 GC gc = new GC(canvas); 77 gc.setFont(JFaceResources.getDefaultFont()); 78 fontMetrics = gc.getFontMetrics(); 79 gc.dispose(); 80 initializeListeners(); 81 } 82 83 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 103 protected Widget doFindInputItem(Object element) { 104 return null; } 106 107 112 protected Widget doFindItem(Object element) { 113 return null; } 115 116 122 protected void doUpdateItem(Widget item, Object element, boolean fullMap) { 123 canvas.redraw(); 124 } 125 126 131 protected List getSelectionFromWidget() { 132 return EMPTY_LIST; 134 } 135 136 141 protected void internalRefresh(Object element) { 142 displayedItems = getSortedChildren(getRoot()); 143 canvas.redraw(); 144 } 145 146 151 public void reveal(Object element) { 152 } 154 155 161 protected void setSelectionToWidget(List l, boolean reveal) { 162 } 164 165 170 public Control getControl() { 171 return canvas; 172 } 173 174 private void initializeListeners() { 175 canvas.addPaintListener(new PaintListener() { 176 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) { 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 string = labelProvider.getText(displayedItems[i]); 211 if(string == null) { 212 string = ""; } 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 230 public void setLabelProvider(IBaseLabelProvider labelProvider) { 231 Assert.isTrue(labelProvider instanceof ILabelProvider); 232 super.setLabelProvider(labelProvider); 233 } 234 235 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 265 public void add(Object [] elements) { 266 refresh(true); 267 268 } 269 270 273 public void remove(Object [] elements) { 274 refresh(true); 275 276 } 277 278 279 } 280 | Popular Tags |