    001package org.vishia.gral.swt;
    002
    003import org.eclipse.swt.events.PaintEvent;
    004import org.eclipse.swt.events.PaintListener;
    005import org.eclipse.swt.graphics.Color;
    006import org.eclipse.swt.graphics.GC;
    007import org.eclipse.swt.widgets.Canvas;
    008import org.eclipse.swt.widgets.Composite;
    009import org.vishia.gral.base.GralLed;
    010import org.vishia.gral.base.GralLed.GraphicImplAccess;
    011import org.vishia.gral.base.GralWidget;
    012import org.vishia.gral.ifc.GralColor;
    013import org.vishia.gral.ifc.GralRectangle;
    014
    015/**This class represents a LED, which is able to show a state with its color.
    016 * The LED may have a inner light and a border light. In that case to state,
    017 * which may have any association, can presented.
    018 */
    019public class SwtLed extends GralLed.GraphicImplAccess
    020{
    021
    022  /**Version and history
    023   * <ul>
    024   * <li>2011-12-03 chg now it is the implementation class for the new class {@link GralLed}.
    025   *   It is the concept of specialized {@link GralWidget}.
    026   *   The ctor creates the swt Control.
    027   *   The content before is the inner class {@link SwtLedImpl} up to now.
    028   * <li>2010-05-00 created.
    029   * </ul>
    030   *
    031   */
    032  private SwtLedImpl widgSwt;
    033
    034
    035  /**It contains the association to the swt widget (Control) and the {@link SwtMng}
    036   * and implements some methods of {@link GralWidgImpl_ifc} which are delegate from this.
    037   */
    038  private final SwtWidgetHelper wdgh;
    039
    040
    041  Color borderColor, innerColor;
    042
    043  final SwtMng mng;
    044
    045  boolean round;
    046
    047
    048  SwtLed(String name, SwtMng mng){
    049    this(new GralLed(name), mng);
    050  }
    051
    052  SwtLed(GralLed widgg, SwtMng mng){
    053    widgg.super(widgg, mng.mng);
    054    //super(name, mng);
    055    this.mng = mng;
    056    switch('r'){
    057    case 'r': round = true; break;
    058    case 'q': round = false; break;
    059    default: throw new IllegalArgumentException("param size must be r or q");
    060    }
    061    Composite panel = mng.getCurrentPanel();
    062    widgSwt = new SwtLedImpl(panel);
    063    super.wdgimpl = this.wdgh = new SwtWidgetHelper(widgSwt, mng);
    064    widgSwt.setBackground(mng.propertiesGuiSwt.colorBackground);
    065    widgSwt.addFocusListener(mng.focusListener);
    066    widgSwt.setForeground(mng.propertiesGuiSwt.colorSwt(0xff00));
    067    //widget.setSize(propertiesGui.xPixelUnit() * xSize -2, propertiesGui.yPixelUnit() * ySize -2);
    068    mng.setBounds_(widgg.pos(), widgSwt);
    069    widgSwt.setData(this);
    070    widgSwt.addMouseListener(mng.mouseClickForInfo);
    071
    072  }
    073
    074
    075
    076
    077  /**Called in the paint routine, corrects the colors for SWT depending on {@link GralWidget.DynamicData#backColor}
    078   * and {@link GralWidget.DynamicData#lineColor}.
    079   * <ul>
    080   * <li>lineColor is the border color
    081   * <li>backColor is the inner color.
    082   * </ul>
    083   */
    084  private void setColors(){
    085    int changedAckn = 0;
    086    GralWidget.DynamicData dyda = dyda(); //GralLed.GraphicImplAccess.this.dyda();
    087    int chg = dyda.getChanged();
    088    if(dyda.backColor !=null
    089      && ( (chg & GralWidget.ImplAccess.chgColorBack)!=0
    090         ||innerColor == null   //uninitialized: start with dyda.backColor
    091      )  ){
    092      innerColor = mng.getColorImpl(dyda.backColor);
    093      changedAckn |= GralWidget.ImplAccess.chgColorBack;
    094    }
    095    if(dyda.lineColor !=null){  //use backColor if lineColor is not set!
    096      if( (chg & GralWidget.ImplAccess.chgColorLine)!=0
    097        || borderColor == null
    098        ){
    099        borderColor = mng.getColorImpl(dyda.lineColor);
    100        changedAckn |= GralWidget.ImplAccess.chgColorLine;
    101      }
    102    } else {
    103      borderColor = innerColor;
    104    }
    105    if(changedAckn !=0) {
    106      dyda.acknChanged(changedAckn);
    107    }
    108  }
    109
    110
    111
    112
    113
    114private class SwtLedImpl extends Canvas
    115  {
    116                /**Creates a LED.
    117         * @param mng The Gui-panel-manager contains information about the graphic frame and properties.
    118         * @param kind Use 'r' or 'q' for a round or a square LED.
    119         */
    120        public SwtLedImpl(Composite panel)
    121        {
    122
    123      super(panel, 0);
    124          addPaintListener(paintListener);
    125        }
    126
    127    PaintListener paintListener = new PaintListener(){
    128                @Override
    129                public void paintControl(PaintEvent e) {
    130                        // TODO Auto-generated method stub
    131                        GC gc = e.gc;
    132                        //gc.d
    133                        //drawBackground(e.gc, e.x, e.y, e.width, e.height);
    134                        setColors();
    135        gc.setBackground(borderColor);
    136        gc.fillOval(2,2,e.width-3,e.height-3);
    137                        gc.setBackground(innerColor);
    138                        int dx = (e.width+1) / 2;  //size of inner point, calculate 1 if width = 1,2
    139                        int dy = (e.height+1) / 2;  //size of inner point, calculate 1 if height = 1,2
    140        int x1 = (e.width - dx) /2 +1;
    141        int y1 = (e.height - dy) /2 +1;
    142        gc.fillOval(x1,y1, dx, dy);
    143                        //gc.setForeground(borderColor);
    144                        //gc.setLineWidth(3);
    145                        //gc.drawOval(2,2,e.width-3,e.height-3);
    146                }
    147    };
    148
    149
    150    void XXXsetBorderColor(int color)
    151    {
    152      borderColor = mng.propertiesGuiSwt.colorSwt(color);
    153      redraw();
    154    }
    155
    156    void XXXsetInnerColor(int color)
    157    {
    158      innerColor = mng.propertiesGuiSwt.colorSwt(color);
    159      redraw();
    160    }
    161
    162    void XXXsetColor(int color)
    163    {
    164      borderColor = innerColor = mng.propertiesGuiSwt.colorSwt(color);
    165      redraw();
    166    }
    167
    168        void XXXsetColor(String sColor)
    169        {
    170                borderColor = innerColor = mng.propertiesGuiSwt.color(sColor);
    171                redraw();
    172        }
    173
    174
    175        void XXXsetColor(int nBorderColor, int nInnerColor)
    176        {
    177                borderColor = mng.propertiesGuiSwt.colorSwt(nBorderColor);
    178                innerColor = mng.propertiesGuiSwt.colorSwt(nInnerColor);
    179                redraw();
    180        }
    181
    182
    183  }
    184
    185  @Override public GralRectangle getPixelPositionSize(){ return wdgh.getPixelPositionSize(); }
    186
    187
    188  @Override
    189  public void removeWidgetImplementation()
    190  { if(widgSwt !=null){
    191      widgSwt.dispose();
    192      widgSwt = null;
    193    }
    194  }
    195
    196  @Override
    197  public boolean setFocusGThread()
    198  { return widgSwt.setFocus();
    199  }
    200
    201  @Override public void setVisibleGThread(boolean bVisible) { super.setVisibleState(bVisible); wdgh.setVisibleGThread(bVisible); }
    202
    203
    204  @Override public void repaintGthread(){
    205    widgSwt.redraw();
    206  }
    207
    208
    209  @Override
    210  public Object getWidgetImplementation()
    211  {
    212    return widgSwt;
    213  }
    214
    215
    216
    217  @Override
    218  public void setBoundsPixel(int x, int y, int dx, int dy)
    219  {
    220    // TODO Auto-generated method stub
    221
    222  }
    223
    224  //@Override
    225  public GralColor XXXsetForegroundColor(GralColor color)
    226  {
    227    // TODO Auto-generated method stub
    228    return null;
    229  }
    230}