package com.peterhi.ui.newbars;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
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 com.peterhi.ui.UIImageResource;
public final class StripItem extends Item implements Listener {
private final StripBar parent;
private final StripItemContext context;
protected int archSize = 6;
protected int marginLeft = 8;
protected int marginRight = 8;
protected int marginTop = 5;
protected int marginBottom = 5;
protected int textSpaceBefore = 5;
protected int closeButtonSpaceBefore = 10;
public StripItem(StripBar parent, int style) {
this(parent, style, -1);
}
public StripItem(StripBar parent, int style, int index) {
super(parent, style, index);
this.parent = parent;
this.context = new StripItemContext(this);
fireSetData(true, index);
addListener(SWT.Paint, this);
addListener(SWT.Dispose, this);
}
public StripBar getParent() {
return parent;
}
@Override
public void handleEvent(Event event) {
if (equals(event.widget)) {
if (event.type == SWT.Paint) {
onPaint(event);
} else if (event.type == SWT.Dispose) {
onDispose(event);
}
}
}
public Rectangle getBounds() {
GC gc = new GC(parent);
Image closeButtonImage = UIImageResource.T.closeNormal10();
Rectangle bounds = new Rectangle(parent.marginLeft, parent.marginTop, 0, 0);
StripItem[] items = parent.getItems();
for (StripItem item : items) {
if (item.getImage() != null) {
bounds.height = Math.max(bounds.height, item.getImage().getBounds().height);
}
if (item.getText() != null && !item.getText().isEmpty()) {
bounds.height = Math.max(bounds.height, gc.stringExtent(item.getText()).y);
}
bounds.height = Math.max(bounds.height, closeButtonImage.getBounds().height);
}
bounds.height += marginTop;
bounds.height += marginBottom;
for (StripItem item : items) {
int extent = marginLeft;
if (item.getImage() != null) {
extent += item.getImage().getBounds().width;
}
if (item.getText() != null && !item.getText().isEmpty()) {
if (item.getImage() != null) {
extent += textSpaceBefore;
}
extent += gc.stringExtent(item.getText()).x;
}
if ((item.getImage() != null) || (item.getText() != null && !item.getText().isEmpty())) {
extent += closeButtonSpaceBefore;
}
extent += closeButtonImage.getBounds().width;
extent += marginRight;
if (equals(item)) {
bounds.width = extent;
break;
} else {
bounds.x += extent;
bounds.x += parent.itemSpacing;
}
}
gc.dispose();
return bounds;
}
public StripItemState getState() {
return context.getState();
}
void setState(StripItemState value, boolean animate) {
context.setState(value, animate);
}
int getOffset() {
return context.getOffset();
}
void setOffset(int value, boolean animate, Runnable end) {
context.setOffset(value, animate, end);
}
Animator getOffsetAnimator() {
return context.getOffsetAnimator();
}
void setOffsetAnimator(Animator value) {
context.setOffsetAnimator(value);
}
protected void onPaint(Event event) {
paintBackground(event.gc, event.x, event.y, event.width, event.height);
paintImage(event.gc, event.x, event.y, event.width, event.height);
paintText(event.gc, event.x, event.y, event.width, event.height);
paintCloseButton(event.gc, event.x, event.y, event.width, event.height);
paintFrame(event.gc, event.x, event.y, event.width, event.height);
}
protected void onDispose(Event event) {
fireSetData(false, -1);
}
protected void paintBackground(GC gc, int x, int y, int width, int height) {
Color oldBackground = gc.getBackground();
Color newBackground = newColor(context.getBackground());
gc.setBackground(newBackground);
gc.fillRoundRectangle(x, y, width, height, archSize, archSize);
gc.setBackground(oldBackground);
newBackground.dispose();
}
protected void paintImage(GC gc, int x, int y, int width, int height) {
x += marginLeft;
y += height / 2;
if (getImage() != null) {
y -= getImage().getBounds().height / 2;
gc.drawImage(getImage(), x, y);
}
}
protected void paintText(GC gc, int x, int y, int width, int height) {
Color oldForeground = gc.getForeground();
Color newForeground = newColor(context.getForeground());
gc.setForeground(newForeground);
x += marginLeft;
y += height / 2;
if (getText() != null && !getText().isEmpty()) {
if (getImage() != null) {
x += getImage().getBounds().width;
x += textSpaceBefore;
}
y -= gc.stringExtent(getText()).y / 2;
gc.drawText(getText(), x, y, true);
}
gc.setForeground(oldForeground);
newForeground.dispose();
}
protected void paintCloseButton(GC gc, int x, int y, int width, int height) {
x += marginLeft;
y += height / 2;
boolean hasText = getText() != null && !getText().isEmpty();
if (getImage() != null) {
x += getImage().getBounds().width;
}
if (hasText) {
if (getImage() != null) {
x += textSpaceBefore;
}
x += gc.stringExtent(getText()).x;
}
if ((getImage() != null) || (getText() != null && !getText().isEmpty())) {
x += closeButtonSpaceBefore;
}
Image closeButtonImage = UIImageResource.T.closeNormal10();
y -= closeButtonImage.getBounds().height / 2;
gc.drawImage(closeButtonImage, x, y);
}
protected void paintFrame(GC gc, int x, int y, int width, int height) {
Color oldForeground = gc.getForeground();
Color newForeground = getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
gc.setForeground(newForeground);
gc.drawRoundRectangle(x, y, width - 1, height - 1, archSize, archSize);
gc.setForeground(oldForeground);
}
private void fireSetData(boolean addOrDel, int index) {
Event event = new Event();
event.time = (int )(System.currentTimeMillis() & 0xffffffffL);
event.widget = parent;
event.item = this;
event.detail = addOrDel ? SWT.INSERT : SWT.DEL;
event.index = index;
parent.notifyListeners(SWT.SetData, event);
}
private Color getColor(int id) {
Display display = getDisplay();
Color color = display.getSystemColor(id);
return color;
}
private Color newColor(RGB rgb) {
Display display = getDisplay();
Color color = new Color(display, rgb);
return color;
}
}