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