Package com.peterhi.ui.stripbar7

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

package com.peterhi.ui.stripbar7;

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

public final class StripItem extends Item implements Listener {

  private final StripBar parent;
  private final RGB foreground;
  private final RGB animateForeground;
  private final RGB background;
  private final RGB animateBackground;
  private final Rectangle bounds;
  private final Rectangle animateBounds;
 
  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.parent.addStripItemInternal(this, index);
   
    Color foreground = display.getSystemColor(SWT.COLOR_WIDGET_FOREGROUND);
    Color background = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
   
    this.foreground = foreground.getRGB();
    this.animateForeground = foreground.getRGB();
   
    this.background = background.getRGB();
    this.animateBackground = background.getRGB();
   
    this.bounds = new Rectangle(0, 0, 0, 0);
    this.animateBounds = new Rectangle(0, 0, 0, 0);
   
    addListener(SWT.Dispose, this);
  }
 
  public StripBar getParent() {
    return parent;
  }

  @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.removeStripItemInternal(this);
  }
 
  RGB getForeground(boolean animate) {
    if (animate) {
      return new RGB(animateForeground.red, animateForeground.green, animateForeground.blue);
    } else {
      return new RGB(foreground.red, foreground.green, foreground.blue);
    }
  }
 
  RGB getBackground(boolean animate) {
    if (animate) {
      return new RGB(animateBackground.red, animateBackground.green, animateBackground.blue);
    } else {
      return new RGB(background.red, background.green, background.blue);
    }
  }
 
  Rectangle getBounds(boolean animate) {
    if (animate) {
      return new Rectangle(animateBounds.x, animateBounds.y, animateBounds.width, animateBounds.height);
    } else {
      return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
    }
  }
 
  void setBounds(Rectangle value, boolean animate) {
    if (value == null) {
      throw new NullPointerException();
    }
   
    setBounds(value.x, value.y, value.width, value.height, animate);
  }

  void setBounds(int x, int y, int width, int height, boolean animate) {
    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) {
     
    } else {
      updateAnimateBounds(x, y, width, height);
    }
  }
 
  private void updateAnimateForeground(RGB value) {
    if (value == null) {
      throw new NullPointerException();
    }
   
    updateAnimateForeground(value.red, value.green, value.blue);
  }
 
  private void updateAnimateForeground(int red, int green, int blue) {
    animateForeground.red = red;
    animateForeground.green = green;
    animateForeground.blue = blue;
    repaint();
  }
 
  private void updateAnimateBackground(RGB value) {
    if (value == null) {
      throw new NullPointerException();
    }
   
    updateAnimateBackground(value.red, value.green, value.blue);
  }
 
  private void updateAnimateBackground(int red, int green, int blue) {
    animateBackground.red = red;
    animateBackground.green = green;
    animateBackground.blue = blue;
    repaint();
  }
 
  private void updateAnimateBounds(Rectangle value) {
    if (value == null) {
      throw new NullPointerException();
    }
   
    updateAnimateBounds(value.x, value.y, value.width, value.height);
  }
 
  private void updateAnimateBounds(int x, int y, int width, int height) {
    Rectangle oldAnimateBounds = new Rectangle(animateBounds.x, animateBounds.y, animateBounds.width, animateBounds.height);
    animateBounds.x = x;
    animateBounds.y = y;
    animateBounds.width = width;
    animateBounds.height = height;
    repaint(oldAnimateBounds, animateBounds);
  }
 
  private void repaint() {
    repaint(null, bounds);
  }
 
  private void repaint(Rectangle oldBounds, Rectangle newBounds) {
    if (newBounds == null) {
      throw new NullPointerException();
    }
   
    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);
  }
}
TOP

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