    001package org.vishia.gral.swt;
    002
    003import org.eclipse.swt.SWT;
    004import org.eclipse.swt.events.PaintEvent;
    005import org.eclipse.swt.events.PaintListener;
    006import org.eclipse.swt.graphics.Color;
    007import org.eclipse.swt.graphics.Device;
    008import org.eclipse.swt.graphics.Font;
    009import org.eclipse.swt.graphics.GC;
    010import org.eclipse.swt.widgets.Canvas;
    011import org.eclipse.swt.widgets.Composite;
    012
    013/**This class supports drawing a Label without background.
    014 * The Problem is: A swt.Color doesn't support the transparent/opaque/alpha property of colors.
    015 * Therefore setBackground(Color) with an non-opaque color can't be used in SWT (in the current version).
    016 * But a swt.widgets.Composite support drawing without background.
    017 * This class isn't offer in the Gral interface. It is used for Prompt labels which overlay an input field.
    018 *
    019 * @author Hartmut Schorrig
    020 *
    021 */
    022public class SwtTransparentLabel extends Canvas
    023{
    024  String text = "";
    025
    026  Font font = null;
    027
    028  public SwtTransparentLabel(Composite parent, int style)
    029  {
    030    super(parent, style | SWT.TRANSPARENT);
    031    addPaintListener(paintListener);
    032  }
    033
    034
    035  public void setText (String string) {
    036    text = string;
    037  }
    038
    039  public void setFont (Font font) {
    040    this.font = font;
    041  }
    042
    043
    044  public void setBackground (Color color) {
    045    if (color != null) {
    046    }
    047  }
    048
    049
    050  public String getText(){ return text; }
    051
    052
    053
    054  /**The listener for paint events. It is called whenever the window is shown newly. */
    055  protected PaintListener paintListener = new PaintListener()
    056  {
    057
    058    @Override
    059    public void paintControl(PaintEvent e) {
    060      // TODO Auto-generated method stub
    061      GC gc = e.gc;
    062      if(font !=null){ gc.setFont(font); }
    063      //gc.setAlpha(100);
    064      gc.drawString(text, 0, 0, true);
    065    }
    066
    067  };
    068
    069
    070
    071}