1 11 package org.eclipse.swt.custom; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.graphics.*; 15 import org.eclipse.swt.widgets.*; 16 import org.eclipse.swt.events.*; 17 18 27 public class AnimatedProgress extends Canvas { 28 29 static final int SLEEP = 70; 30 static final int DEFAULT_WIDTH = 160; 31 static final int DEFAULT_HEIGHT = 18; 32 boolean active = false; 33 boolean showStripes = false; 34 int value; 35 int orientation = SWT.HORIZONTAL; 36 boolean showBorder = false; 37 38 66 public AnimatedProgress(Composite parent, int style) { 67 super(parent, checkStyle(style)); 68 69 if ((style & SWT.VERTICAL) != 0) { 70 orientation = SWT.VERTICAL; 71 } 72 showBorder = (style & SWT.BORDER) != 0; 73 74 addControlListener(new ControlAdapter() { 75 public void controlResized(ControlEvent e) { 76 redraw(); 77 } 78 }); 79 addPaintListener(new PaintListener() { 80 public void paintControl(PaintEvent e) { 81 paint(e); 82 } 83 }); 84 addDisposeListener(new DisposeListener() { 85 public void widgetDisposed(DisposeEvent e){ 86 stop(); 87 } 88 }); 89 } 90 private static int checkStyle (int style) { 91 int mask = SWT.NONE; 92 return style & mask; 93 } 94 103 public synchronized void clear(){ 104 checkWidget(); 105 if (active) stop(); 106 showStripes = false; 107 redraw(); 108 } 109 public Point computeSize(int wHint, int hHint, boolean changed) { 110 checkWidget(); 111 Point size = null; 112 if (orientation == SWT.HORIZONTAL) { 113 size = new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT); 114 } else { 115 size = new Point(DEFAULT_HEIGHT, DEFAULT_WIDTH); 116 } 117 if (wHint != SWT.DEFAULT) size.x = wHint; 118 if (hHint != SWT.DEFAULT) size.y = hHint; 119 120 return size; 121 } 122 private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright) { 123 gc.setForeground(topleft); 124 gc.drawLine(x, y, x+w-1, y); 125 gc.drawLine(x, y, x, y+h-1); 126 127 gc.setForeground(bottomright); 128 gc.drawLine(x+w, y, x+w, y+h); 129 gc.drawLine(x, y+h, x+w, y+h); 130 } 131 void paint(PaintEvent event) { 132 GC gc = event.gc; 133 Display disp= getDisplay(); 134 135 Rectangle rect= getClientArea(); 136 gc.fillRectangle(rect); 137 if (showBorder) { 138 drawBevelRect(gc, rect.x, rect.y, rect.width-1, rect.height-1, 139 disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW), 140 disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW)); 141 } 142 143 paintStripes(gc); 144 } 145 void paintStripes(GC gc) { 146 147 if (!showStripes) return; 148 149 Rectangle rect= getClientArea(); 150 rect = new Rectangle(rect.x+2, rect.y+2, rect.width-4, rect.height-4); 152 153 gc.setLineWidth(2); 154 gc.setClipping(rect); 155 Color color = getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION); 156 gc.setBackground(color); 157 gc.fillRectangle(rect); 158 gc.setForeground(this.getBackground()); 159 int step = 12; 160 int foregroundValue = value == 0 ? step - 2 : value - 2; 161 if (orientation == SWT.HORIZONTAL) { 162 int y = rect.y - 1; 163 int w = rect.width; 164 int h = rect.height + 2; 165 for (int i= 0; i < w; i+= step) { 166 int x = i + foregroundValue; 167 gc.drawLine(x, y, x, h); 168 } 169 } else { 170 int x = rect.x - 1; 171 int w = rect.width + 2; 172 int h = rect.height; 173 174 for (int i= 0; i < h; i+= step) { 175 int y = i + foregroundValue; 176 gc.drawLine(x, y, w, y); 177 } 178 } 179 180 if (active) { 181 value = (value + 2) % step; 182 } 183 } 184 192 public synchronized void start() { 193 checkWidget(); 194 if (active) return; 195 196 active = true; 197 showStripes = true; 198 199 final Display display = getDisplay(); 200 final Runnable [] timer = new Runnable [1]; 201 timer [0] = new Runnable () { 202 public void run () { 203 if (!active) return; 204 GC gc = new GC(AnimatedProgress.this); 205 paintStripes(gc); 206 gc.dispose(); 207 display.timerExec (SLEEP, timer [0]); 208 } 209 }; 210 display.timerExec (SLEEP, timer [0]); 211 } 212 215 public synchronized void stop() { 216 active = false; 218 } 219 } 220 | Popular Tags |