package com.peterhi.ui.stripbar5;
import java.util.concurrent.TimeUnit;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Listener;
import org.jdesktop.core.animation.timing.Animator;
import org.jdesktop.core.animation.timing.Animator.Builder;
import org.jdesktop.core.animation.timing.TimingTarget;
import org.jdesktop.core.animation.timing.TimingTargetAdapter;
import org.jdesktop.core.animation.timing.interpolators.AccelerationInterpolator;
import com.peterhi.ui.UI;
import com.peterhi.ui.obsolete.Bounds;
public class StripItem extends Item implements Listener {
public static final int DEFAULT = 0;
public static final int FOCUSED = 1;
public static final int PRESSED = 2;
public static final int DRAGGED = 4;
public static final int OUTSIDE = 8;
public static final int SELECTED = 16;
private final StripBar parent;
private final Rectangle bounds;
private final Rectangle renderBounds;
private final RGB foreground;
private final RGB renderForeground;
private final RGB background;
private final RGB renderBackground;
private int state;
private Animator foregroundAnimator;
private Animator backgroundAnimator;
private Animator boundsAnimator;
public StripItem(StripBar parent, int style, int index) {
super(parent, style, index);
this.parent = parent;
this.bounds = new Rectangle(0, 0, 0, 0);
this.renderBounds = new Rectangle(0, 0, 0, 0);
this.foreground = UI.getRGB(SWT.COLOR_WIDGET_FOREGROUND);
this.renderForeground = UI.getRGB(SWT.COLOR_WIDGET_FOREGROUND);
this.background = UI.getRGB(SWT.COLOR_WIDGET_BACKGROUND);
this.renderBackground = UI.getRGB(SWT.COLOR_WIDGET_BACKGROUND);
addListener(SWT.Dispose, this);
Event redir = new Event();
redir.time = (int )(System.currentTimeMillis() & 0xffffffffL);
redir.widget = parent;
redir.item = this;
redir.index = index;
redir.detail = SWT.INSERT;
parent.notifyListeners(SWT.Modify, redir);
}
public StripItem(StripBar parent, int style) {
this(parent, style, -1);
}
@Override
public void handleEvent(Event event) {
if (equals(event.widget)) {
if (event.type == SWT.Dispose) {
Event redir = new Event();
redir.time = (int )(System.currentTimeMillis() & 0xffffffffL);
redir.widget = parent;
redir.item = this;
redir.detail = SWT.DEL;
parent.notifyListeners(SWT.Modify, redir);
}
}
}
public StripBar getParent() {
return parent;
}
void redraw() {
redraw(null, renderBounds);
}
boolean isState(int value, boolean exact) {
if (exact) {
return state == value;
} else {
return ((state & value) == value);
}
}
int getState() {
return state;
}
void setState(int value, boolean animate) {
if (value == state) {
return;
}
state = value;
setForeground(getForeground(state), animate, null);
setBackground(getBackground(state), animate, null);
}
boolean hasText() {
if (getText() == null) {
return false;
}
if (getText().isEmpty()) {
return false;
}
return true;
}
boolean hasImage() {
if (getImage() == null) {
return false;
}
if (getImage().isDisposed()) {
return false;
}
return true;
}
Point getTextSize() {
Point size = new Point(0, 0);
if (hasText()) {
GC gc = new GC(parent);
size = gc.stringExtent(getText());
gc.dispose();
}
return size;
}
Point getImageSize() {
Point size = new Point(0, 0);
if (hasImage()) {
Rectangle imageBounds = getImage().getBounds();
size.x = imageBounds.width;
size.y = imageBounds.height;
}
return size;
}
Rectangle getBounds() {
return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
}
Rectangle getRenderBounds() {
return new Rectangle(renderBounds.x, renderBounds.y, renderBounds.width, renderBounds.height);
}
void setBounds(Rectangle value, boolean animate, TimingTarget listener) {
if (value == null) {
throw new NullPointerException();
}
setBounds(value.x, value.y, value.width, value.height, animate, listener);
}
void setBounds(int x, int y, int width, int height, boolean animate, final TimingTarget listener) {
if (bounds.x == x && bounds.y == y && bounds.width == width && bounds.height == height) {
return;
}
bounds.x = x;
bounds.y = y;
bounds.width = width;
bounds.height = height;
if (animate) {
if (isBoundsAnimating()) {
boundsAnimator.cancel();
boundsAnimator = null;
}
final Bounds from = Bounds.fromRectangle(getRenderBounds());
final Bounds to = Bounds.fromXYWidthHeight(x, y, width, height);
TimingTarget timingTarget = new TimingTargetAdapter() {
@Override
public void timingEvent(Animator source, double fraction) {
int left = (int )Math.round(from.getLeft() + (to.getLeft() - from.getLeft()) * fraction);
int top = (int )Math.round(from.getTop() + (to.getTop() - from.getTop()) * fraction);
int right = (int )Math.round(from.getRight() + (to.getRight() - from.getRight()) * fraction);
int bottom = (int )Math.round(from.getBottom() + (to.getBottom() - from.getBottom()) * fraction);
setRenderBounds(Bounds.fromLeftTopRightBottom(left, top, right, bottom).toRectangle());
if (listener != null) {
listener.timingEvent(source, fraction);
}
}
@Override
public void end(Animator source) {
setRenderBounds(to.toRectangle());
boundsAnimator = null;
if (listener != null) {
listener.end(source);
}
}
};
Builder builder = new Builder();
builder.addTarget(timingTarget);
builder.setInterpolator(new AccelerationInterpolator(0.0, 0.9));
builder.setDuration(160, TimeUnit.MILLISECONDS);
boundsAnimator = builder.build();
boundsAnimator.start();
} else {
setRenderBounds(x, y, width, height);
}
}
RGB getForeground() {
return new RGB(foreground.red, foreground.green, foreground.blue);
}
RGB getBackground() {
return new RGB(background.red, background.green, background.blue);
}
RGB getRenderForeground() {
return new RGB(renderForeground.red, renderForeground.green, renderForeground.blue);
}
RGB getRenderBackground() {
return new RGB(renderBackground.red, renderBackground.green, renderBackground.blue);
}
boolean isForegroundAnimating() {
return foregroundAnimator != null;
}
boolean isBackgroundAnimating() {
return backgroundAnimator != null;
}
boolean isBoundsAnimating() {
return boundsAnimator != null;
}
private void setForeground(RGB value, boolean animate, TimingTarget listener) {
if (value == null) {
throw new NullPointerException();
}
setForeground(value.red, value.green, value.blue, animate, listener);
}
private void setForeground(int red, int green, int blue, boolean animate, final TimingTarget listener) {
if (foreground.red == red && foreground.green == green && foreground.blue == blue) {
return;
}
foreground.red = red;
foreground.green = green;
foreground.blue = blue;
if (animate) {
if (isForegroundAnimating()) {
foregroundAnimator.cancel();
foregroundAnimator = null;
}
final RGB from = getRenderForeground();
final RGB to = new RGB(red, green, blue);
TimingTarget timingTarget = new TimingTargetAdapter() {
@Override
public void timingEvent(Animator source, double fraction) {
int red = (int )Math.round(from.red + (to.red - from.red) * fraction);
int green = (int )Math.round(from.green + (to.green - from.green) * fraction);
int blue = (int )Math.round(from.blue + (to.blue - from.blue) * fraction);
setRenderForeground(red, green, blue);
if (listener != null) {
listener.timingEvent(source, fraction);
}
}
@Override
public void end(Animator source) {
setRenderForeground(to);
foregroundAnimator = null;
if (listener != null) {
listener.end(source);
}
}
};
Builder builder = new Builder();
builder.addTarget(timingTarget);
builder.setInterpolator(new AccelerationInterpolator(0.0, 0.9));
builder.setDuration(160, TimeUnit.MILLISECONDS);
foregroundAnimator = builder.build();
foregroundAnimator.start();
} else {
setRenderForeground(red, green, blue);
}
}
private void setBackground(RGB value, boolean animate, TimingTarget listener) {
if (value == null) {
throw new NullPointerException();
}
setBackground(value.red, value.green, value.blue, animate, listener);
}
private void setBackground(int red, int green, int blue, boolean animate, final TimingTarget listener) {
if (background.red == red && background.green == green && background.blue == blue) {
return;
}
background.red = red;
background.green = green;
background.blue = blue;
if (animate) {
if (isBackgroundAnimating()) {
backgroundAnimator.cancel();
backgroundAnimator = null;
}
final RGB from = getRenderBackground();
final RGB to = new RGB(red, green, blue);
TimingTarget timingTarget = new TimingTargetAdapter() {
@Override
public void timingEvent(Animator source, double fraction) {
int red = (int )Math.round(from.red + (to.red - from.red) * fraction);
int green = (int )Math.round(from.green + (to.green - from.green) * fraction);
int blue = (int )Math.round(from.blue + (to.blue - from.blue) * fraction);
setRenderBackground(red, green, blue);
if (listener != null) {
listener.timingEvent(source, fraction);
}
}
@Override
public void end(Animator source) {
setRenderBackground(to);
backgroundAnimator = null;
if (listener != null) {
listener.end(source);
}
}
};
Builder builder = new Builder();
builder.addTarget(timingTarget);
builder.setInterpolator(new AccelerationInterpolator(0.0, 0.9));
builder.setDuration(160, TimeUnit.MILLISECONDS);
backgroundAnimator = builder.build();
backgroundAnimator.start();
} else {
setRenderBackground(red, green, blue);
}
}
private void setRenderBounds(Rectangle value) {
if (value == null) {
throw new NullPointerException();
}
setRenderBounds(value.x, value.y, value.width, value.height);
}
private void setRenderForeground(RGB value) {
if (value == null) {
throw new NullPointerException();
}
setRenderForeground(value.red, value.green, value.blue);
}
private void setRenderBackground(RGB value) {
if (value == null) {
throw new NullPointerException();
}
setRenderBackground(value.red, value.green, value.blue);
}
private void setRenderBounds(int x, int y, int width, int height) {
if (renderBounds.x == x && renderBounds.y == y && renderBounds.width == width && renderBounds.height == height) {
return;
}
Rectangle oldRenderBounds = new Rectangle(renderBounds.x, renderBounds.y, renderBounds.width, renderBounds.height);
renderBounds.x = x;
renderBounds.y = y;
renderBounds.width = width;
renderBounds.height = height;
redraw(oldRenderBounds, renderBounds);
}
private void setRenderForeground(int red, int green, int blue) {
if (renderForeground.red == red && renderForeground.green == green && renderForeground.blue == blue) {
return;
}
renderForeground.red = red;
renderForeground.green = green;
renderForeground.blue = blue;
redraw(null, renderBounds);
}
private void setRenderBackground(int red, int green, int blue) {
if (renderBackground.red == red && renderBackground.green == green && renderBackground.blue == blue) {
return;
}
renderBackground.red = red;
renderBackground.green = green;
renderBackground.blue = blue;
redraw(null, renderBounds);
}
private void redraw(Rectangle oldBounds, Rectangle newBounds) {
if (oldBounds != null) {
if (!oldBounds.equals(newBounds)) {
parent.redraw(oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height, true);
}
}
parent.redraw(newBounds.x, newBounds.y, newBounds.width, newBounds.height, true);
}
private RGB getForeground(int state) {
if ((state & DRAGGED) == DRAGGED) {
return UI.getRGB(SWT.COLOR_WIDGET_FOREGROUND);
}
if ((state & PRESSED) == PRESSED) {
return UI.getRGB(SWT.COLOR_WIDGET_LIGHT_SHADOW);
}
if ((state & SELECTED) == SELECTED) {
return UI.getRGB(SWT.COLOR_LIST_BACKGROUND);
}
return UI.getRGB(SWT.COLOR_WIDGET_FOREGROUND);
}
private RGB getBackground(int state) {
if ((state & DRAGGED) == DRAGGED) {
return new RGB(65, 105, 225);
}
if ((state & PRESSED) == PRESSED) {
return new RGB(65, 105, 225);
}
if ((state & SELECTED) == SELECTED) {
return new RGB(67, 110, 238);
}
if ((state & FOCUSED) == FOCUSED) {
return new RGB(72, 118, 255);
}
return UI.getRGB(SWT.COLOR_WIDGET_BACKGROUND);
}
}