1 11 package org.eclipse.ui.internal.browser; 12 13 import org.eclipse.swt.events.PaintEvent; 14 import org.eclipse.swt.events.PaintListener; 15 import org.eclipse.swt.graphics.GC; 16 import org.eclipse.swt.graphics.Image; 17 import org.eclipse.swt.graphics.Point; 18 import org.eclipse.swt.graphics.Rectangle; 19 import org.eclipse.swt.widgets.Canvas; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Display; 22 25 public class BusyIndicator extends Canvas { 26 protected Image[] images; 27 protected Image image; 28 29 protected Thread busyThread; 30 protected boolean stop; 31 32 37 public BusyIndicator(Composite parent, int style) { 38 super(parent, style); 39 40 images = ImageResource.getBusyImages(); 41 42 addPaintListener(new PaintListener() { 43 public void paintControl(PaintEvent event) { 44 onPaint(event); 45 } 46 }); 47 48 image = images[0]; 49 } 50 51 public Point computeSize(int wHint, int hHint, boolean changed) { 52 return new Point(25, 25); 53 } 54 55 58 protected synchronized void createBusyThread() { 59 if (busyThread != null) 60 return; 61 62 stop = false; 63 busyThread = new Thread () { 64 protected int count; 65 public void run() { 66 try { 67 count = 1; 68 while (!stop) { 69 Display.getDefault().syncExec(new Runnable () { 70 public void run() { 71 if (!stop) { 72 if (count < 13) 73 setImage(images[count]); 74 count++; 75 if (count > 12) 76 count = 1; 77 } 78 } 79 }); 80 try { 81 sleep(125); 82 } catch (Exception e) { 83 } 85 } 86 if (busyThread == null) 87 Display.getDefault().syncExec(new Thread () { 88 public void run() { 89 setImage(images[0]); 90 } 91 }); 92 } catch (Exception e) { 93 Trace.trace(Trace.WARNING, "Busy error", e); } 95 } 96 }; 97 98 busyThread.setPriority(Thread.NORM_PRIORITY + 2); 99 busyThread.setDaemon(true); 100 busyThread.start(); 101 } 102 103 public void dispose() { 104 stop = true; 105 busyThread = null; 106 super.dispose(); 107 } 108 109 112 public Image getImage() { 113 return image; 114 } 115 116 121 public boolean isBusy() { 122 return (busyThread != null); 123 } 124 125 128 protected void onPaint(PaintEvent event) { 129 Rectangle rect = getClientArea(); 130 if (rect.width == 0 || rect.height == 0) 131 return; 132 133 GC gc = event.gc; 134 if (image != null) 135 gc.drawImage(image, 2, 2); 136 } 137 138 143 public synchronized void setBusy(boolean busy) { 144 if (busy) { 145 if (busyThread == null) 146 createBusyThread(); 147 } else { 148 if (busyThread != null) { 149 stop = true; 150 busyThread = null; 151 } 152 } 153 } 154 155 159 public void setImage(Image image) { 160 if (image != this.image && !isDisposed()) { 161 this.image = image; 162 redraw(); 163 } 164 } 165 } | Popular Tags |