Package com.peterhi.ui.bar2

Source Code of com.peterhi.ui.bar2.StripBar3$StripItem

package com.peterhi.ui.bar2;

import java.util.ArrayList;
import java.util.List;

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.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
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.eclipse.swt.widgets.Shell;

import com.peterhi.ui.UIImageResource;
import com.peterhi.ui.UI;

@Deprecated
public class StripBar3 extends Canvas implements Listener {

  public static class StripItem extends Item implements Listener {
   
    private StripBar3 parent;
   
    public StripItem(StripBar3 parent, int style) {
      super(parent, style);
      init(parent, style, -1);
    }
   
    public StripItem(StripBar3 parent, int style, int index) {
      super(parent, style, index);
      init(parent, style, index);
    }
   
    public int getIndex() {
      checkWidget();
      return parent.indexOf(this);
    }

    @Override
    public void handleEvent(Event event) {
      if (event.widget == this && event.type == SWT.Dispose) {
        Event redirEvent = new Event();
        redirEvent.widget = parent;
        redirEvent.item = this;
        redirEvent.time = (int )(System.currentTimeMillis() & 0xffffffffL);
        redirEvent.index = getIndex();
      }
    }
   
    public StripBar3 getParent() {
      checkWidget();
      return parent;
    }
   
    public Rectangle getBounds() {
      checkWidget();
      return parent.getBounds(this);
    }
   
    public Rectangle getImageBounds() {
      checkWidget();
      return parent.getImageBounds(this);
    }
   
    public Rectangle getTextBounds() {
      checkWidget();
      return parent.getTextBounds(this);
    }
   
    public Rectangle getCloseButtonBounds() {
      checkWidget();
      return parent.getCloseButtonBounds(this);
    }
   
    private void init(StripBar3 parent, int style, int index) {
      checkWidget();
     
      if (parent == null) {
        throw new NullPointerException();
      }
     
      if (index < -1) {
        throw new IllegalArgumentException();
      }
     
      this.parent = parent;
     
      addListener(SWT.Dispose, this);
     
      Event redirEvent = new Event();
      redirEvent.widget = parent;
      redirEvent.item = this;
      redirEvent.time = (int )(System.currentTimeMillis() & 0xffffffffL);
      redirEvent.index = index;
    }
  }
 
  private final int SPACING = 6;
  private final List<StripItem> items = new ArrayList<StripItem>();
 
  public StripBar3(Composite parent, int style) {
    super(parent, style);
    addListener(SWT.Dispose, this);
    addListener(SWT.Paint, this);
  }

  @Override
  public void handleEvent(Event event) {
    if (event.widget == this && event.type == SWT.Dispose) {
      StripItem[] items = getItems();
     
      for (int i = items.length - 1; i >= 0; i--) {
        StripItem item = items[i];
        item.dispose();
      }
    }
   
    if (event.widget == this && event.type == SWT.Paint) {
      for (int i = 0; i < items.size(); i++) {
        StripItem item = items.get(i);
        paintItem(event.gc, item);
      }
    }
  }
 
  @Override
  public Point computeSize(int wHint, int hHint, boolean changed) {
    Point size = new Point(0, 0);
   
    for (int i = 0; i < items.size(); i++) {
      StripItem item = items.get(i);
      Rectangle bounds = item.getBounds();
      size.x += SPACING;
      size.x += bounds.width;
      size.y = Math.max(size.y, bounds.height);
    }
   
    size.x += SPACING;
    size.y += SPACING * 2;
   
    Rectangle sizeWithTrim = computeTrim(0, 0, size.x, size.y);
    size.x = sizeWithTrim.width;
    size.y = sizeWithTrim.height;
   
    return size;
  }

  public int getItemCount() {
    checkWidget();
    return items.size();
  }
 
  public StripItem getItem(int index) {
    checkWidget();
    return items.get(index);
  }
 
  public int indexOf(StripItem item) {
    checkWidget();
    return items.indexOf(item);
  }
 
  public boolean contains(StripItem item) {
    checkWidget();
    return indexOf(item) != -1;
  }
 
  public StripItem[] getItems() {
    checkWidget();
    return items.toArray(new StripItem[items.size()]);
  }
 
  public Rectangle getBounds(StripItem srcItem) {
    checkWidget();
   
    if (!contains(srcItem)) {
      return null;
    }
   
    GC gc = new GC(this);
    Rectangle bounds = new Rectangle(SPACING, SPACING, 0, 0);
   
    for (int i = 0; i < items.size(); i++) {
      StripItem destItem = items.get(i);
     
      if (destItem.getImage() != null) {
        Rectangle imageBounds = destItem.getImage().getBounds();
        bounds.height = Math.max(bounds.height, imageBounds.height);
      }
     
      if (destItem.getText() != null && !destItem.getText().isEmpty()) {
        Point textSize = gc.stringExtent(destItem.getText());
        bounds.height = Math.max(bounds.height, textSize.y);
      }
     
      Image closeButtonImage = UIImageResource.T.closeNormal10();
      Rectangle closeButtonImageBounds = closeButtonImage.getBounds();
      bounds.height = Math.max(bounds.height, closeButtonImageBounds.height);
    }
   
    bounds.height += SPACING * 2;

    for (int i = 0; i < items.size(); i++) {
      StripItem destItem = items.get(i);
     
      int extent = SPACING;
     
      if (destItem.getImage() != null) {
        Rectangle imageBounds = destItem.getImage().getBounds();
        extent += imageBounds.width;
        extent += SPACING;
      }
     
      if (destItem.getText() != null && !destItem.getText().isEmpty()) {
        Point textSize = gc.stringExtent(destItem.getText());
        extent += textSize.x;
        extent += SPACING;
      }
     
      Image closeButtonImage = UIImageResource.T.closeNormal10();
      Rectangle closeButtonImageBounds = closeButtonImage.getBounds();
      extent += closeButtonImageBounds.width;
      extent += SPACING;
     
      if (srcItem.equals(destItem)) {
        bounds.width = extent;
        break;
      } else {
        bounds.x += extent;
        bounds.x += SPACING;
      }
    }
   
    gc.dispose();
    return bounds;
  }
 
  public Rectangle getImageBounds(StripItem srcItem) {
    checkWidget();
   
    if (!contains(srcItem)) {
      return null;
    }
   
    Rectangle bounds = getBounds(srcItem);
   
    if (bounds == null) {
      return null;
    }
   
    bounds.y += bounds.height / 2;
    bounds.width = 0;
    bounds.height = 0;
   
    if (srcItem.getImage() != null) {
      Rectangle imageBounds = srcItem.getImage().getBounds();
      bounds.x += SPACING;
      bounds.y -= imageBounds.height / 2;
      bounds.width = imageBounds.width;
      bounds.height = imageBounds.height;
    }
   
    return bounds;
  }
 
  public Rectangle getTextBounds(StripItem srcItem) {
    checkWidget();
   
    if (!contains(srcItem)) {
      return null;
    }
   
    Rectangle bounds = getImageBounds(srcItem);
   
    if (bounds == null) {
      return null;
    }
   
    bounds.x += bounds.width;
    bounds.y += bounds.height / 2;
    bounds.width = 0;
    bounds.height = 0;
   
    if (srcItem.getText() != null && !srcItem.getText().isEmpty()) {
      GC gc = new GC(this);
      Point textSize = gc.stringExtent(srcItem.getText());
      bounds.x += SPACING;
      bounds.y -= textSize.y / 2;
      bounds.width = textSize.x;
      bounds.height = textSize.y;
      gc.dispose();
    }
   
    return bounds;
  }
 
  public Rectangle getCloseButtonBounds(StripItem srcItem) {
    checkWidget();
   
    if (!contains(srcItem)) {
      return null;
    }
   
    Rectangle bounds = getTextBounds(srcItem);
   
    if (bounds == null) {
      return null;
    }
   
    bounds.x += bounds.width;
    bounds.y += bounds.height / 2;
    bounds.width = 0;
    bounds.height = 0;
   
    Image closeButtonImage = UIImageResource.T.closeNormal10();
    Rectangle closeButtonImageBounds = closeButtonImage.getBounds();
    bounds.x += SPACING;
    bounds.y -= closeButtonImageBounds.height / 2;
    bounds.width = closeButtonImageBounds.width;
    bounds.height = closeButtonImageBounds.height;
    return bounds;
  }
 
  private void paintItem(GC gc, StripItem item) {
    paintItemBackground(gc, item);
    paintItemImage(gc, item);
    paintItemText(gc, item);
    paintItemCloseButton(gc, item);
    paintItemFrame(gc, item);
  }
 
  private void paintItemBackground(GC gc, StripItem item) {
  }
 
  private void paintItemImage(GC gc, StripItem item) {
    if (item.getImage() != null) {
      Rectangle imageBounds = item.getImageBounds();
      gc.drawImage(item.getImage(), imageBounds.x, imageBounds.y);
    }
  }
 
  private void paintItemText(GC gc, StripItem item) {
    if (item.getText() != null && !item.getText().isEmpty()) {
      Rectangle textBounds = item.getTextBounds();
      gc.drawText(item.getText(), textBounds.x, textBounds.y, true);
    }
  }
 
  private void paintItemCloseButton(GC gc, StripItem item) {
    Image closeButtonImage = UIImageResource.T.closeNormal10();
    Rectangle closeButtonBounds = item.getCloseButtonBounds();
    gc.drawImage(closeButtonImage, closeButtonBounds.x, closeButtonBounds.y);
  }
 
  private void paintItemFrame(GC gc, StripItem item) {
    Color oldForeground = gc.getForeground();
    gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
    Rectangle bounds = item.getBounds();
    gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1);
    gc.setForeground(oldForeground);
  }
 
  public static void main(String[] args) {
    UI.initialize();
    Display display = Display.getDefault();
    Shell shell = new Shell(display);
    GridLayout layout = new GridLayout();
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    shell.setLayout(layout);
    StripBar3 bar = new StripBar3(shell, SWT.DOUBLE_BUFFERED);
    GridData data = new GridData();
    data.horizontalAlignment = SWT.FILL;
    data.verticalAlignment = SWT.FILL;
    data.grabExcessHorizontalSpace = true;
    bar.setLayoutData(data);
    {
      StripItem item0 = new StripItem(bar, SWT.NONE);
      item0.setImage(UIImageResource.T.about16());
      item0.setText("Item 0");
     
      StripItem item1 = new StripItem(bar, SWT.NONE);
      item1.setText("Item 1");
     
      StripItem item2 = new StripItem(bar, SWT.NONE);
      item2.setImage(UIImageResource.T.addContact16());
     
      StripItem item3 = new StripItem(bar, SWT.NONE);
    }
   
    shell.open();
   
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
   
    UI.destroy();
  }

}
TOP

Related Classes of com.peterhi.ui.bar2.StripBar3$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.