    001package org.vishia.gral.awt;
    002
    003import java.awt.Color;
    004import java.awt.Component;
    005import java.util.LinkedList;
    006import java.util.List;
    007import java.awt.Container;
    008import java.awt.Frame;
    009import java.awt.Rectangle;
    010
    011import org.vishia.gral.ifc.GralColor;
    012import org.vishia.gral.ifc.GralRectangle;
    013
    014/**The static methods of this class are called in some situations, where same functionality is need in some classes.
    015 * @author Hartmut Schorrig
    016 *
    017 */
    018public class AwtWidgetHelper
    019{
    020
    021  protected AwtWidgetMng mng;
    022
    023  protected Component widga;
    024
    025  protected AwtWidgetHelper(Component widga, AwtWidgetMng mng)
    026  { this.widga = widga;
    027    this.mng = mng;
    028  }
    029
    030
    031  public void setBoundsPixel(int x, int y, int dx, int dy)
    032  { widga.setBounds(x,y,dx,dy);
    033  }
    034
    035
    036
    037  public GralRectangle getPixelPositionSize(){
    038    return getPixelPositionSize(widga);
    039  }
    040
    041
    042  public static GralRectangle getPixelPositionSize(Component widga){
    043    int posx = 0, posy = 0;
    044    Rectangle r = widga.getBounds();
    045    Container parent;
    046    if(widga instanceof Container){
    047      parent = (Container) widga; //start with them, maybe the shell itself
    048    } else {
    049      parent = widga.getParent();
    050    }
    051    Rectangle pos;
    052    while( !( parent instanceof Frame ) ){
    053      pos = parent.getBounds();
    054      posx += pos.x; posy += pos.y;
    055      parent = parent.getParent();
    056    }
    057    assert(parent instanceof Frame);
    058    Rectangle s = parent.getBounds();
    059    pos = parent.getBounds();
    060    int dframe = (pos.width - s.width) /2;   //width of the frame line.
    061    posx += r.x + dframe;               //absolute position of the client area!
    062    posy += r.y + (pos.height - s.height) - dframe;
    063    int dx, dy;
    064    if(parent == widga){
    065      dx = s.width;
    066      dy = s.height;
    067    } else {
    068      dx = r.width;
    069      dy = r.height;
    070    }
    071    GralRectangle posSize = new GralRectangle(posx, posy, dx, dy);
    072    return posSize;
    073  }
    074
    075
    076
    077
    078
    079
    080  public static GralColor getColor(Color awtColor)
    081  {
    082    int colorValue = awtColor.getBlue() << 16 + awtColor.getGreen() << 8 + awtColor.getRed();
    083    return GralColor.getColor(colorValue);
    084  }
    085
    086
    087  public static GralColor setBackgroundColor(GralColor color, Component awtWidget)
    088  { Color colorAwt = (Color)color.colorGuimpl;
    089    Color colorAwtOld = awtWidget.getBackground();
    090    awtWidget.setBackground(colorAwt);
    091    return getColor(colorAwtOld);
    092  }
    093
    094
    095  public static GralColor setForegroundColor(GralColor color, Component awtWidget)
    096  { Color colorAwt = (Color)color.colorGuimpl;
    097    Color colorAwtOld = awtWidget.getForeground();
    098    awtWidget.setForeground(colorAwt);
    099    return getColor(colorAwtOld);
    100  }
    101
    102
    103
    104  /**Sets the correct TabItem if any widget at this TabItem is focused. That is not done by swt graphic
    105   * on Control.setFocus().
    106   * @param control
    107   */
    108  static boolean setFocusOfTabSwt(Component control)
    109  {
    110    List<Component> parents = new LinkedList<Component>();
    111    Component parent = control;
    112    while( (parent = parent.getParent())!=null){
    113      parents.add(parent);
    114    }
    115    for(Component parent1: parents){
    116    }
    117    return false; //TODOcontrol.setf();
    118
    119
    120  }
    121
    122
    123  public boolean setFocusGThread(){ widga.setFocusable(true); return widga.isFocusOwner(); }
    124
    125  public void setVisibleGThread(boolean bVisible){ widga.setVisible(bVisible); }
    126
    127
    128  public void removeWidgetImplementation()
    129  {
    130    if(widga !=null){
    131      widga.removeNotify();
    132      widga = null;
    133    }
    134  }
    135
    136
    137
    138}