Package org.dcarew.halostatus.utils

Source Code of org.dcarew.halostatus.utils.LabelContributionItem

package org.dcarew.halostatus.utils;

import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.StatusLineLayoutData;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;

/**
* A ContributionItem for the application's status line. This is used to show the Halo icon and the
* number of players online.
*
* @author Devon Carew
*/
public class LabelContributionItem
  extends ContributionItem
{
  public final static int  DEFAULT_CHAR_WIDTH  = 40;
 
  private CLabel      countLabel;
  private CLabel      imageLabel;
 
  private int        charWidth;
 
  private Image      image;
  private String      text    = "";
  private String      tooltip;
  private int        widthHint  = -1;
 
 
  public LabelContributionItem(String id)
  {
    this(id, -1);
  }
 
  public LabelContributionItem(String id, int charWidth)
  {
    super(id);
   
    this.charWidth = charWidth;
  }
 
  public void fill(Composite parent)
  {
    // separator
    //new Label(parent, SWT.SEPARATOR);
   
    // count label
    countLabel = new CLabel(parent, SWT.SHADOW_NONE | SWT.RIGHT);
   
    if (widthHint < 0)
    {
      GC gc = new GC(parent);
      gc.setFont(parent.getFont());
      FontMetrics fm = gc.getFontMetrics();
     
      if (charWidth == -1)
        widthHint = fm.getAverageCharWidth() * text.length();
      else
        widthHint = fm.getAverageCharWidth() * charWidth;
     
      widthHint += image.getBounds().width + 10;
      gc.dispose();
    }
   
    countLabel.setLayoutData(createStatusLineLayoutData(widthHint, SWT.DEFAULT));
    countLabel.setText(text);
   
    if (tooltip != null)
      countLabel.setToolTipText(tooltip);
   
    // image label
    imageLabel = new CLabel(parent, SWT.SHADOW_NONE);
    imageLabel.setImage(image);
    imageLabel.setToolTipText("Halo 3 Status");
  }
 
  private static StatusLineLayoutData createStatusLineLayoutData(int widthHint, int heightHint)
  {
    StatusLineLayoutData data = new StatusLineLayoutData();
   
    data.widthHint = widthHint;
    data.heightHint = heightHint;
   
    return data;
  }
 
  public void setText(String text)
  {
    if (text == null)
      throw new IllegalArgumentException("text cannot be null");
   
    this.text = text;
   
    if (countLabel != null && !countLabel.isDisposed())
      countLabel.setText(this.text);
   
    if (getParent() != null)
      getParent().update(true);
  }
 
  public void setTooltip(String tooltip)
  {
    this.tooltip = tooltip;
   
    if (countLabel != null && !countLabel.isDisposed())
    {
      countLabel.setToolTipText(tooltip);
    }
  }
 
  public void setImage(Image image)
  {
    this.image = image;
   
    if (imageLabel != null && !imageLabel.isDisposed())
      imageLabel.setImage(this.image);
  }

}
TOP

Related Classes of org.dcarew.halostatus.utils.LabelContributionItem

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.