FileDocCategorySizeDatePackage
SWTSkinObjectSlider.javaAPI DocAzureus 3.0.3.410947Tue Sep 18 15:01:46 BST 2007com.aelitis.azureus.ui.swt.skin

SWTSkinObjectSlider

public class SWTSkinObjectSlider extends SWTSkinObjectBasic implements MouseMoveListener, MouseListener, PaintListener
author
TuxPaper
created
Aug 28, 2007

Fields Summary
private org.eclipse.swt.graphics.Image
imageFG
private Object
imageFGLeft
private Object
imageFGRight
private org.eclipse.swt.widgets.Canvas
canvas
private org.eclipse.swt.graphics.Image
imageThumbRight
private org.eclipse.swt.graphics.Image
imageThumb
private org.eclipse.swt.graphics.Image
imageThumbLeft
private org.eclipse.swt.graphics.Image
imageBGRight
private org.eclipse.swt.graphics.Image
imageBG
private org.eclipse.swt.graphics.Image
imageBGLeft
private double
percent
private org.eclipse.swt.graphics.Rectangle
imageFGbounds
private org.eclipse.swt.graphics.Rectangle
imageBGbounds
private org.eclipse.swt.graphics.Rectangle
imageThumbBounds
private org.eclipse.swt.graphics.Point
maxSize
private boolean
mouseDown
private boolean
mouseMoveAdjusts
private ArrayList
listeners
private double
draggingPercent
private boolean
disabled
Constructors Summary
public SWTSkinObjectSlider(SWTSkin skin, SWTSkinProperties skinProperties, String sID, String sConfigID, String[] typeParams, SWTSkinObject parent)


	    
			        
		super(skin, skinProperties, sID, sConfigID, "slider", parent);

		String sSuffix = ".complete";
		ImageLoader imageLoader = skin.getImageLoader(properties);
		Image[] images = imageLoader.getImages(sConfigID + sSuffix);
		if (images.length == 1 && ImageLoader.isRealImage(images[0])) {
			imageFG = images[0];
			imageFGLeft = imageLoader.getImage(sConfigID + sSuffix + "-left");
			imageFGRight = imageLoader.getImage(sConfigID + sSuffix + "-right");
		} else if (images.length == 3 && ImageLoader.isRealImage(images[2])) {
			imageFGLeft = images[0];
			imageFG = images[1];
			imageFGRight = images[2];
		}

		if (imageFG != null) {
			imageFGbounds = imageFG.getBounds();
		}

		sSuffix = ".incomplete";
		images = imageLoader.getImages(sConfigID + sSuffix);
		if (images.length == 1 && ImageLoader.isRealImage(images[0])) {
			imageBG = images[0];
			imageBGLeft = imageLoader.getImage(sConfigID + sSuffix + "-left");
			imageBGRight = imageLoader.getImage(sConfigID + sSuffix + "-right");
		} else if (images.length == 3 && ImageLoader.isRealImage(images[2])) {
			imageBGLeft = images[0];
			imageBG = images[1];
			imageBGRight = images[2];
		}

		if (imageBG != null) {
			imageBGbounds = imageBG.getBounds();
		}

		sSuffix = ".thumb";
		images = imageLoader.getImages(sConfigID + sSuffix);
		if (images.length == 1) {
			imageThumb = images[0];
			imageThumbLeft = imageLoader.getImage(sConfigID + sSuffix + "-left");
			imageThumbRight = imageLoader.getImage(sConfigID + sSuffix + "-right");
		} else if (images.length == 3 && ImageLoader.isRealImage(images[2])) {
			imageThumbLeft = images[0];
			imageThumb = images[1];
			imageThumbRight = images[2];
		}

		if (imageThumb != null) {
			imageThumbBounds = imageThumb.getBounds();
		}

		maxSize = buildMaxSize(new Rectangle[] {
			imageThumbBounds,
			imageBGbounds,
			imageFGbounds
		});

		if (skinProperties.getStringValue(sConfigID + ".width", "").equalsIgnoreCase(
				"auto")) {
			maxSize.x = 0;
		}

		int style = SWT.NONE;

		if (skinProperties.getIntValue(sConfigID + ".border", 0) == 1) {
			style |= SWT.BORDER;
		}

		Composite createOn;
		if (parent == null) {
			createOn = skin.getShell();
		} else {
			createOn = (Composite) parent.getControl();
		}

		canvas = new Canvas(createOn, style);
		canvas.setLayoutData(new FormData(maxSize.x == 0 ? SWT.DEFAULT
				: maxSize.x, maxSize.y));
		canvas.setSize(SWT.DEFAULT, maxSize.y);
		setControl(canvas);

		canvas.addPaintListener(this);
		canvas.addMouseListener(this);
		canvas.addMouseMoveListener(this);
	
Methods Summary
public voidaddListener(com.aelitis.azureus.ui.swt.skin.SWTSkinObjectSlider$SWTSkinListenerSliderSelection listener)

		listeners.add(listener);
	
private booleanareDoublesEqual(double a, double b)

		return Math.abs(a - b) < 0.000001;
	
private org.eclipse.swt.graphics.PointbuildMaxSize(org.eclipse.swt.graphics.Rectangle[] bounds)

		Point maxSize = new Point(0, 0);
		for (int i = 0; i < bounds.length; i++) {
			if (bounds[i] == null) {
				continue;
			}

			if (bounds[i].width > maxSize.x) {
				maxSize.x = bounds[i].width;
			}
			if (bounds[i].height > maxSize.y) {
				maxSize.y = bounds[i].height;
			}
		}
		return maxSize;
	
public booleangetMouseMoveAdjusts()

		return mouseMoveAdjusts;
	
public doublegetPercent()

		return percent;
	
public booleanisDisabled()

		return disabled;
	
public voidmouseDoubleClick(MouseEvent e)

	
public voidmouseDown(MouseEvent e)

		if (disabled) {
			return;
		}
		mouseDown = true;

		int offset = imageThumbBounds.width / 2;
		int sizeX = maxSize.x;
		if (maxSize.x == 0) {
			sizeX = canvas.getClientArea().width;
		}
		float newPercent = (e.x - offset)
				/ (float) (sizeX - imageThumbBounds.width);
		
		if (mouseMoveAdjusts) {
			setPercent(newPercent, true);
		} else {
			draggingPercent = validatePercent(newPercent, true);
			
			Utils.execSWTThread(new AERunnable() {
				public void runSupport() {
					if (canvas != null && !canvas.isDisposed()) {
						canvas.redraw();
						canvas.update();
					}
				}
			});

		}
	
public voidmouseMove(MouseEvent e)

		if (disabled) {
			return;
		}
		if (mouseDown) {
			int offset = imageThumbBounds.width / 2;
			int sizeX = maxSize.x;
			if (maxSize.x == 0) {
				sizeX = canvas.getClientArea().width;
			}
			float newPercent = (e.x - offset)
					/ (float) (sizeX - imageThumbBounds.width);

			if (mouseMoveAdjusts) {
				setPercent(newPercent, true);
			} else {
				draggingPercent = validatePercent(newPercent, true);
				
				Utils.execSWTThread(new AERunnable() {
					public void runSupport() {
						if (canvas != null && !canvas.isDisposed()) {
							canvas.redraw();
							canvas.update();
						}
					}
				});

			}
		}
	
public voidmouseUp(MouseEvent e)

		if (disabled) {
			return;
		}
		mouseDown = false;
		if (!mouseMoveAdjusts) {
			int offset = imageThumbBounds.width / 2;
			int sizeX = maxSize.x;
			if (maxSize.x == 0) {
				sizeX = canvas.getClientArea().width;
			}
			float newPercent = (e.x - offset)
					/ (float) (sizeX - imageThumbBounds.width);
			setPercent(newPercent, true);
		}
	
public voidpaintControl(PaintEvent e)

		int fullWidth = maxSize.x == 0 || imageFGbounds == null
				? canvas.getClientArea().width : imageFGbounds.width;

		if (percent > 0 && imageFG != null) {
			int xDrawTo = (int) (fullWidth * percent);
			int xDrawToSrc = xDrawTo > imageFGbounds.width ? imageFGbounds.width
					: xDrawTo;
			int y = (maxSize.y - imageFGbounds.height) / 2;
			e.gc.drawImage(imageFG, 0, 0, xDrawToSrc, imageFGbounds.height, 0, y,
					xDrawTo, imageFGbounds.height);
		}
		if (percent < 100 && imageBG != null) {
			int xDrawFrom = (int) (imageBGbounds.width * percent);
			int xDrawWidth = imageBGbounds.width - xDrawFrom;
			e.gc.drawImage(imageBG, xDrawFrom, 0, xDrawWidth, imageFGbounds.height,
					xDrawFrom, 0, xDrawWidth, imageFGbounds.height);
		}

		int drawWidth = fullWidth - imageThumbBounds.width;
		int xThumbPos = (int) ((mouseDown && !mouseMoveAdjusts ? draggingPercent : percent) * drawWidth);
		e.gc.drawImage(imageThumb, xThumbPos, 0);

	
public voidsetDisabled(boolean disabled)

		if (disabled == this.disabled) {
			return;
		}
		this.disabled = disabled;
		if (disabled) {
			mouseDown = false;
		}
	
public voidsetMouseMoveAdjusts(boolean mouseMoveAdjusts)

		this.mouseMoveAdjusts = mouseMoveAdjusts;
	
public voidsetPercent(double percent)

		setPercent(percent, false);
	
private voidsetPercent(double newPercent, boolean triggerListeners)

		if (areDoublesEqual(percent, newPercent)) {
			return;
		}

		newPercent = validatePercent(newPercent, triggerListeners);

		if (areDoublesEqual(percent, newPercent)) {
			return;
		}
		
		this.percent = newPercent;

		Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
				if (canvas != null && !canvas.isDisposed()) {
					canvas.redraw();
					canvas.update();
				}
			}
		});

		if (triggerListeners) {
			Object[] listenersArray = listeners.toArray();
			for (int i = 0; i < listenersArray.length; i++) {
				SWTSkinListenerSliderSelection l = (SWTSkinListenerSliderSelection) listenersArray[i];
				l.selectionChanged(this.percent);
			}
		}
	
private doublevalidatePercent(double percent, boolean triggerListeners)

return
since
3.0.2.3

		if (triggerListeners) {
			Object[] listenersArray = listeners.toArray();
			for (int i = 0; i < listenersArray.length; i++) {
				SWTSkinListenerSliderSelection l = (SWTSkinListenerSliderSelection) listenersArray[i];
				Double changedPercent = l.selectionChanging(this.percent, percent);
				if (changedPercent != null) {
					return changedPercent.floatValue();
				}
			}
		}
		
		if (percent < 0) {
			return 0;
		} else if (percent > 1) {
			return 1;
		}
		return percent;