Package com.peterhi.ui.stripbar

Source Code of com.peterhi.ui.stripbar.StripItem

package com.peterhi.ui.stripbar;

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.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.TimingTarget;

public final class StripItem extends Item implements Listener {

  private final StripBar parent;
  private final Rectangle bounds;
  private final RGB foreground;
  private final RGB background;
  private boolean close;
 
  public StripItem(StripBar parent, int style) {
    this(parent, style, -1);
  }

  public StripItem(StripBar parent, int style, int index) {
    super(parent, style, index);
   
    Display display = getDisplay();
   
    this.parent = parent;
    this.bounds = new Rectangle(0, 0, 0, 0);
    this.foreground = display.getSystemColor(SWT.COLOR_WIDGET_FOREGROUND).getRGB();
    this.background = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND).getRGB();
   
    addListener(SWT.Dispose, this);
   
    parent.add(this, index);
  }

  @Override
  public void handleEvent(Event event) {
    if (equals(event.widget)) {
      if (event.type == SWT.Dispose) {
        onDispose(event);
      }
    }
  }
 
  public boolean hasImage() {
    return getImage() != null;
  }
 
  public boolean hasText() {
    if (getText() == null) {
      return false;
    }
   
    if (getText().isEmpty()) {
      return false;
    }
   
    return true;
  }
 
  public Point getImageSize() {
    Point imageSize = new Point(0, 0);
   
    if (hasImage()) {
      Rectangle imageBounds = getImage().getBounds();
      imageSize.x = imageBounds.width;
      imageSize.y = imageBounds.height;
    }
   
    return imageSize;
  }
 
  public Point getTextSize() {
    Point textSize = new Point(0, 0);
   
    if (hasText()) {
      GC gc = new GC(parent);
      textSize = gc.stringExtent(getText());
      gc.dispose();
    }
   
    return textSize;
  }
 
  public Rectangle getBounds() {
    return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
  }
 
  public RGB getForeground() {
    return new RGB(foreground.red, foreground.green, foreground.blue);
  }
 
  public RGB getBackground() {
    return new RGB(background.red, background.green, background.blue);
  }
 
  public boolean isClose() {
    return close;
  }
 
  protected void onDispose(Event event) {
    parent.remove(this);
  }
 
  void setBounds(Rectangle value, boolean animate, TimingTarget callback) {
    if (value == null) {
      throw new NullPointerException();
    }
   
    setBounds(value.x, value.y, value.width, value.height, animate, callback);
  }
 
  void setBounds(int x, int y, int width, int height, boolean animate, TimingTarget callback) {
    Rectangle oldBounds = new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
   
    bounds.x = x;
    bounds.y = y;
    bounds.width = width;
    bounds.height = height;
   
    repaint(oldBounds);
  }
 
  void setForeground(RGB value, boolean animate, TimingTarget callback) {
    if (value == null) {
      throw new NullPointerException();
    }
   
    setForeground(value.red, value.green, value.blue, animate, callback);
  }
 
  void setForeground(int red, int green, int blue, boolean animate, TimingTarget callback) {
    foreground.red = red;
    foreground.green = green;
    foreground.blue = blue;
   
    repaint(null);
  }
 
  void setBackground(RGB value, boolean animate, TimingTarget callback) {
    if (value == null) {
      throw new NullPointerException();
    }
   
    setBackground(value.red, value.green, value.blue, animate, callback);
  }
 
  void setBackground(int red, int green, int blue, boolean animate, TimingTarget callback) {
    background.red = red;
    background.green = green;
    background.blue = blue;
   
    repaint(null);
  }
 
  void setClose(boolean value) {
    if (close == value) {
      return;
    }
   
    close = value;
    repaint(null);
  }
 
  private void repaint(Rectangle oldBounds) {
    if (oldBounds != null && !bounds.equals(oldBounds)) {
      parent.redraw(oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height, true);
    }
   
    parent.redraw(bounds.x, bounds.y, bounds.width, bounds.height, true);
  }
}
TOP

Related Classes of com.peterhi.ui.stripbar.StripItem

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.