    001package org.vishia.gral.swt;
    002
    003import java.util.Map;
    004import java.util.TreeMap;
    005
    006import org.eclipse.swt.SWT;
    007import org.eclipse.swt.graphics.Color;
    008import org.eclipse.swt.graphics.Device;
    009import org.eclipse.swt.graphics.Font;
    010import org.eclipse.swt.graphics.RGB;
    011import org.vishia.gral.base.GralGridProperties;
    012import org.vishia.gral.ifc.GralColor;
    013import org.vishia.gral.ifc.GralFont;
    014
    015
    016public class SwtProperties extends GralGridProperties
    017{
    018        private final Device guiDevice;
    019
    020  public final Font smallPromptFont;
    021
    022  public final Font stdInputFont;
    023
    024  public final Font stdButtonFont;
    025
    026  Map<Integer, Color> colorsSwt = new TreeMap<Integer, Color>();
    027
    028  private final Color colorBlack;
    029
    030  public final Color colorGrid, colorGridStrong;
    031
    032  /**A common background color for all widgets which are paint at the background. */
    033  public Color colorBackground;
    034
    035  /**Initializes a properties object.
    036   * <br><br>
    037   * @param size number between 1..5 to determine the size of the content (font size, pixel per cell)
    038   */
    039  public SwtProperties(Device device, char sizeC)
    040  { super(sizeC);
    041        this.guiDevice = device;
    042    this.colorBlack = new Color(guiDevice, 0,0,0);
    043    this.colorGrid = colorSwt(0xe0e0e0);
    044    this.colorGridStrong = colorSwt(0xc0c0c0);
    045    this.colorBackground = colorSwt(colorBackground_);
    046    this.smallPromptFont = new Font(device, "Arial", smallPromptFontSize[size], SWT.NORMAL);
    047    this.stdInputFont = new Font(device, "Arial", stdInputFontSize[size], SWT.NORMAL);
    048    this.stdButtonFont = new Font(device, "Arial", stdButtonFontSize[size], SWT.NORMAL);
    049  }
    050
    051  /**Returns a color with given Gui-independent color.
    052   * The SWT-Color instance is taken from a pool if the color is used already.
    053   * Elsewhere it is created newly and put into the pool.
    054   * @param color The given color in system-indpending form.
    055   * @return An instance of SWT-color
    056   */
    057  public Color colorSwt(GralColor color)
    058  {
    059    if(color.colorGuimpl == null){
    060      int colorValue = color.getColorValue();
    061      color.colorGuimpl = colorSwt(colorValue);
    062    } else if(!(color.colorGuimpl instanceof Color)){
    063      throw new IllegalArgumentException("unauthorized color setting");
    064    }
    065    return (Color)color.colorGuimpl;
    066  }
    067
    068
    069  /**Returns a color with given numeric color value.
    070   * The SWT-Color instance is taken from a pool if the color is used already.
    071   * Elsewhere it is created newly and put into the pool.
    072   * @param colorValue red, green and blue
    073   * @return An instance of SWT-color
    074   */
    075  public Color colorSwt(int colorValue){
    076        Color color;
    077        if(colorValue >=0 && colorValue < 0x1000000){
    078                color = colorsSwt.get(colorValue);
    079                if(color==null){
    080                        color = new Color(guiDevice, (colorValue >>16)&0xff, (colorValue >>8)&0xff, (colorValue)&0xff );
    081              //colorsSwt.put(colorValue, color);  //store it to reuse.
    082                }
    083        } else {
    084                color = colorBlack;  //The values -1... may be used for palettes.
    085        }
    086        return color;
    087  }
    088
    089  /**Returns a color with given Gui-independent color name.
    090   * The SWT-Color instance is taken from a pool if the color is used already.
    091   * Elsewhere it is created newly and put into the pool.
    092   * @param sColorName One of the registered color names.
    093   * @return An instance of SWT-color
    094   */
    095
    096  public Color color(String sColorname)
    097  { int nColor = getColorValue(sColorname);
    098    return colorSwt(nColor);
    099  }
    100
    101  public Color colorBackgroundSwt(){ return colorSwt(colorBackground_); }
    102
    103  /**Creates an instance of a GUI-system independent color with given SWT color.
    104   * @param color The SWT-color
    105   * @return a new instance.
    106   */
    107  public static GralColor createColorGui(Color color)
    108  { RGB rgb = color.getRGB();
    109    GralColor ret = new GralColor(rgb.red, rgb.green, rgb.blue);
    110    return ret;
    111  }
    112
    113  public Font getSwtFont(float fontSize){ return fontSwt(super.getTextFont(fontSize)); }
    114
    115
    116  /*
    117   * JLabel   Trace-GUI           56 x 16
    118   * FileInputField              237 x 34
    119   * JButton                      73 x 26
    120   */
    121
    122
    123
    124  /**Returns a implementation font with given Gui-independent font.
    125   * The SWT-Font instance is taken from a pool if the font is used already.
    126   * Elsewhere it is created newly and put into the pool.
    127   * @param forn The given Gral font in system-independent form.
    128   * @return An instance of SWT-Font
    129   */
    130  public Font fontSwt(GralFont font)
    131  {
    132    if(font.fontImpl == null){
    133      int styleSwt = 0;
    134      switch(font.style){
    135        case 'b': styleSwt |= SWT.BOLD; break;
    136        case 'B': styleSwt |= SWT.BOLD; break;
    137        case 'i': styleSwt |= SWT.ITALIC; break;
    138        case 'I': styleSwt |= SWT.BOLD | SWT.ITALIC; break;
    139        default: styleSwt = SWT.NORMAL;
    140      }
    141      String fontName;
    142      //NOTE: on SWT there are not Java standardfonts, there are platform-depending.
    143      if(font.fontName.equals(GralFont.fontMonospacedSansSerif)){ fontName = "Courier"; }
    144      else if(font.fontName.equals(GralFont.fontMonospacedSmall)){ fontName = "Courier"; }
    145      else if(font.fontName.equals(GralFont.fontMonospacedSerif)){ fontName = "Courier"; }
    146      else if(font.fontName.equals(GralFont.fontSansSerif)){ fontName = "Arial"; }
    147      else if(font.fontName.equals(GralFont.fontSerif)){ fontName = "Serif"; }
    148      else {fontName = font.fontName; }
    149      font.fontImpl = new Font(guiDevice, fontName, font.size, styleSwt);
    150    } else if(!(font.fontImpl instanceof Font)){
    151      throw new IllegalArgumentException("unauthorized font setting");
    152    }
    153    return (Font)font.fontImpl;
    154  }
    155
    156
    157
    158  Font getTextFontSwt(float fontSize, char type, char style){
    159    return fontSwt(getTextFont(fontSize, type, style));
    160  }
    161
    162
    163}