    001package org.vishia.gral.awt;
    002
    003import java.awt.Container;
    004import java.awt.Dimension;
    005import java.awt.Font;
    006import java.awt.FontMetrics;
    007import java.awt.Label;
    008
    009import org.vishia.gral.base.GralWidget;
    010import org.vishia.gral.ifc.GralRectangle;
    011import org.vishia.gral.widget.GralLabel;
    012
    013public class AwtLabel extends GralLabel.GraphicImplAccess
    014{
    015  /**Version, history and license.
    016   * <ul>
    017   * <li>2013-12-23 Hartmut new: Uses the new concept of widget implementation.
    018   * </ul>
    019   *
    020   * <b>Copyright/Copyleft</b>:
    021   * For this source the LGPL Lesser General Public License,
    022   * published by the Free Software Foundation is valid.
    023   * It means:
    024   * <ol>
    025   * <li> You can use this source without any restriction for any desired purpose.
    026   * <li> You can redistribute copies of this source to everybody.
    027   * <li> Every user of this source, also the user of redistribute copies
    028   *    with or without payment, must accept this license for further using.
    029   * <li> But the LPGL is not appropriate for a whole software product,
    030   *    if this source is only a part of them. It means, the user
    031   *    must publish this part of source,
    032   *    but doesn't need to publish the whole source of the own product.
    033   * <li> You can study and modify (improve) this source
    034   *    for own using or for redistribution, but you have to license the
    035   *    modified sources likewise under this LGPL Lesser General Public License.
    036   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
    037   * </ol>
    038   * If you intent to use this source without publishing its usage, you can get
    039   * a second license subscribing a special contract with the author.
    040   *
    041   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
    042   *
    043   *
    044   */
    045  @SuppressWarnings("hiding")
    046  public static final int version = 20120317;
    047
    048  /**It contains the association to the swt widget (Control) and the {@link SwtMng}
    049   * and implements some methods of {@link GralWidgImpl_ifc} which are delegate from this.
    050   */
    051  private final AwtWidgetHelper widgHelper;
    052
    053  protected Label labelAwt;
    054
    055  private Font fontAwt;
    056
    057
    058  AwtLabel(GralLabel widgg, AwtWidgetMng mng)
    059  {
    060    widgg.super(widgg, mng.mng);
    061    //Container panelSwt = mng.getCurrentPanel();
    062    //int styleSwt = 0;
    063    int mode;
    064    switch(origin()){
    065    case 1: mode = Label.LEFT; break;
    066    case 2: mode = Label.CENTER; break;
    067    case 3: mode = Label.RIGHT; break;
    068    case 4: mode = Label.LEFT; break;
    069    case 5: mode = Label.CENTER; break;
    070    case 6: mode = Label.RIGHT; break;
    071    case 7: mode = Label.LEFT; break;
    072    case 8: mode = Label.CENTER; break;
    073    case 9: mode = Label.RIGHT; break;
    074    default: mode = 0;
    075    }
    076    labelAwt = new Label(dyda().displayedText, mode);
    077    Container panelAwt = (Container)widgg.pos().panel.getWidgetImplementation();
    078    panelAwt.add(labelAwt);
    079    widgHelper = new AwtWidgetHelper(labelAwt, mng);
    080    labelAwt.setAlignment(mode);
    081    mng.setBounds_(widgg.pos(), labelAwt);
    082    mng.mng.registerWidget(widgg);
    083    repaintGthread();  //to set text etc.
    084  }
    085
    086
    087  @Override
    088  public GralRectangle getPixelPositionSize(){ return widgHelper.getPixelPositionSize(); }
    089
    090
    091  @Override
    092  public Object getWidgetImplementation(){ return labelAwt; }
    093
    094
    095  @Override public void removeWidgetImplementation()
    096  { widgHelper.removeWidgetImplementation();
    097    labelAwt = null;
    098  }
    099
    100
    101  @Override
    102  public void repaintGthread()
    103  {
    104    if(labelAwt !=null){ //do nothing if the graphic implementation widget is removed.
    105      GralWidget.DynamicData dyda = dyda();
    106      int chg, catastrophicalCount =0;
    107      while( (chg = getChanged()) !=0){ //widgg.dyda.whatIsChanged.get();
    108        if(++catastrophicalCount > 10000)
    109          throw new RuntimeException("atomic failed");
    110        if((chg & chgColorText)!=0) {  //firstly set the font.
    111          AwtProperties props = widgHelper.mng.propertiesGuiAwt;
    112          if(dyda.textFont !=null){
    113            fontAwt = props.fontAwt(dyda.textFont);
    114            labelAwt.setFont(fontAwt);
    115          }
    116          if(dyda.textColor !=null){
    117            labelAwt.setForeground(props.colorAwt(dyda.textColor));
    118          }
    119          if(dyda.backColor !=null){
    120            labelAwt.setBackground(props.colorAwt(dyda.backColor));
    121          }
    122        }
    123        if((chg & chgText) !=0 && dyda.displayedText !=null){
    124          labelAwt.setText(dyda.displayedText);
    125          //font.getFontData()[0].
    126          //FontMetrics ff; ff.
    127          //labelAwt.getFontMetrics(font)
    128          Dimension textSize = new Dimension(10* dyda.displayedText.length(), 20);
    129              //labelSwt.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
    130          //int width = sText.length();
    131          //widget.setSize(sizePixel);
    132
    133          Dimension widgetSize = labelAwt.getSize();
    134          if(widgetSize.width < textSize.width){
    135            labelAwt.setSize(textSize);
    136          }
    137          //labelAwt.setSize(textSize);
    138        }
    139        acknChanged(chg);
    140      }
    141      labelAwt.repaint();
    142    }
    143  }
    144
    145
    146  @Override public void setBoundsPixel(int x, int y, int dx, int dy)
    147  { widgHelper.setBoundsPixel(x, y, dx, dy);
    148  }
    149
    150
    151  @Override public boolean setFocusGThread()
    152  { return widgHelper.setFocusGThread(); }
    153
    154
    155  /**Sets the implementation widget vible or not.
    156   * @see org.vishia.gral.base.GralWidgImpl_ifc#setVisibleGThread(boolean)
    157   */
    158  @Override public void setVisibleGThread(boolean bVisible){ super.setVisibleState(bVisible); widgHelper.setVisibleGThread(bVisible); }
    159
    160
    161}