Package com.peterhi.ui.stripbar8

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

package com.peterhi.ui.stripbar8;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Listener;

public final class StripItem extends Item implements Listener {

  private final StripBar parent;
  private final Rectangle bounds = new Rectangle(0, 0, 0, 0);
  private final Rectangle animBounds = new Rectangle(0, 0, 0, 0);
 
  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.parent.internalAdd(this, index);
    addListener(SWT.Dispose, this);
  }

  @Override
  public void handleEvent(Event event) {
    if (!equals(event.widget)) {
      return;
    }
   
    if (event.type == SWT.Dispose) {
      onDispose(event);
    }
  }
 
  public boolean hasImage() {
    Image image = getImage();
   
    if (image == null) {
      return false;
    }
   
    return true;
  }
 
  public boolean hasText() {
    String text = getText();
   
    if (text == null) {
      return false;
    }
   
    if (text.isEmpty()) {
      return false;
    }
   
    return true;
  }
 
  public Point getImageSize() {
    Point size = new Point(0, 0);
   
    if (hasImage()) {
      Image image = getImage();
      Rectangle imageBounds = image.getBounds();
      size.x = imageBounds.width;
      size.y = imageBounds.height;
    }
   
    return size;
  }
 
  public Point getTextSize() {
    Point size = new Point(0, 0);
   
    if (hasText()) {
      String text = getText();
      GC gc = new GC(parent);
      size = gc.stringExtent(text);
      gc.dispose();
    }
   
    return size;
  }
 
  protected void onDispose(Event event) {
    parent.internalRemove(this);
  }
 
  Rectangle getBounds(boolean anim) {
    if (anim) {
      return new Rectangle(animBounds.x, animBounds.y, animBounds.width, animBounds.height);
    } else {
      return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
    }
  }
 
  void setBounds(Rectangle value, boolean anim) {
    if (value == null) {
      throw new NullPointerException();
    }
   
    setBounds(value.x, value.y, value.width, value.height, anim);
  }
 
  void setBounds(int x, int y, int width, int height, boolean anim) {
    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 (anim) {
     
    } else {
      updateAnimBounds(x, y, width, height);
    }
  }
 
  private void updateAnimBounds(Rectangle value) {
    if (value == null) {
      throw new NullPointerException();
    }
   
    updateAnimBounds(value.x, value.y, value.width, value.height);
  }
 
  private void updateAnimBounds(int x, int y, int width, int height) {
    Rectangle oldAnimBounds = new Rectangle(animBounds.x, animBounds.y, animBounds.width, animBounds.height);
    animBounds.x = x;
    animBounds.y = y;
    animBounds.width = width;
    animBounds.height = height;
    update(animBounds, oldAnimBounds);
  }

  private void update(Rectangle bounds, Rectangle old) {
    if (bounds == null) {
      throw new NullPointerException();
    }
   
    if (old != null) {
      if (!old.equals(bounds)) {
        parent.redraw(old.x, old.y, old.width, old.height, true);
      }
    }
   
    parent.redraw(bounds.x, bounds.y, bounds.width, bounds.height, true);
  }
}
TOP

Related Classes of com.peterhi.ui.stripbar8.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.