001package org.vishia.gral.base;
002
003import org.vishia.gral.ifc.GralColor;
004import org.vishia.util.ObjectValue;
005
006public class GralLed extends GralWidget
007{
008  /**Version and history
009   * <ul>
010   * <li>2016-11-22 bugfix: {@link #setValue(Object[])} with negative input causes an exception. Now a 3. color magenta was shown to see that failure. 
011   * <li>2014-10-12 chg: now instantiable. New concept. 
012   * <li>2012-04-01 new: {@link #colorBorder}, {@link #colorBorderSelectable}, {@link #setValue(Object[])}
013   * <li>2011-12-03 new Baseclass for LED visualization.
014   *   It is the concept of specialized {@link GralWidget}.
015   * </ul>
016   * 
017   * <b>Copyright/Copyleft</b>:
018   * For this source the LGPL Lesser General Public License,
019   * published by the Free Software Foundation is valid.
020   * It means:
021   * <ol>
022   * <li> You can use this source without any restriction for any desired purpose.
023   * <li> You can redistribute copies of this source to everybody.
024   * <li> Every user of this source, also the user of redistribute copies
025   *    with or without payment, must accept this license for further using.
026   * <li> But the LPGL ist not appropriate for a whole software product,
027   *    if this source is only a part of them. It means, the user
028   *    must publish this part of source,
029   *    but don't need to publish the whole source of the own product.
030   * <li> You can study and modify (improve) this source
031   *    for own using or for redistribution, but you have to license the
032   *    modified sources likewise under this LGPL Lesser General Public License.
033   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
034   * </ol>
035   * If you are intent to use this sources without publishing its usage, you can get
036   * a second license subscribing a special contract with the author. 
037   * 
038   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
039   * 
040   * 
041   */
042  @SuppressWarnings("hiding")
043  static final public int version = 0x20111203;
044  
045  /**Some given colors which can be selected by value calling {@link #setValue(Object[])}
046   * or {@link #setValue(float)}.
047   * 
048   */
049  private final GralColor[] colorBorderSelectable, colorInnerSelectable;
050  
051  
052  /**The colors which are used for repaint (if not null). */
053  //protected GralColor colorBorder, colorInner;
054  
055  /**
056   * @param name
057   * @param mng
058   * @deprecated use {@link #GralLed(String)}
059   */
060  protected GralLed(String name, GralMng mng)
061  { super(name, 'D', mng);
062    colorBorderSelectable = new GralColor[3];
063    colorBorderSelectable[0] = GralColor.getColor("ye");
064    colorBorderSelectable[1] = GralColor.getColor("gn");
065    colorBorderSelectable[2] = GralColor.getColor("ma");  //faulty showing color
066    colorInnerSelectable = new GralColor[3];
067    colorInnerSelectable[0] = GralColor.getColor("wh");
068    colorInnerSelectable[1] = GralColor.getColor("gn");
069    colorInnerSelectable[2] = GralColor.getColor("ma");   //faulty showing color
070    setValue(0);  //initializes dyda.colors
071  }
072
073  
074  public GralLed(String name)
075  { this(name, null);
076  }
077  
078  
079  /**Sets the LED's color. The border can be another than the inner color.
080   * The effect of {@link #setValue(Object[])} is independent from this method.
081   * @param colorBorder assign to {@link GralWidget#setLineColor(GralColor, int)} or {@link GralWidget.DynamicData#lineColor}. 
082   * @param colorInner assign to {@link GralWidget#setBackColor(GralColor, int)} {@link GralWidget.DynamicData#backColor}.
083   */
084  public void setColor(GralColor colorBorder, GralColor colorInner){
085    dyda.lineColor = colorBorder;
086    dyda.backColor = colorInner;
087    repaint(repaintDelay, repaintDelayMax);
088  }
089
090  /**Sets the color both inner and border with the given value
091   * using the {@link #colorInnerSelectable}
092   * @see org.vishia.gral.base.GralWidget#setValue(float)
093   */
094  @Override public void setValue(float value){
095    int ival = (int)value;
096    if(ival < 0){ ival = 0; }
097    else if(ival >= colorInnerSelectable.length){ ival = colorInnerSelectable.length -1;}
098    dyda.lineColor = dyda.backColor = colorInnerSelectable[ival];
099    dyda.setChanged(ImplAccess.chgColorBack | ImplAccess.chgColorLine);
100    super.setValue(value);  //stores and calls repaint.
101  }
102  
103  
104  /**This method is invoked if more as one variable is assigned to the widget.
105   * It sets the line and the back color which is the inner color (back) and border (line).
106   * The values should be integer-readable. 
107   * @see org.vishia.gral.base.GralWidget#setValue(java.lang.Object[])
108   */
109  @Override public void setValue(Object[] values){
110    if(values.length >=2){
111      int val1 = ObjectValue.getInt(values[0]); //The index to the color
112      int val2 = ObjectValue.getInt(values[1]);
113      if(val1 >= colorBorderSelectable.length || val1 <0){ val1 = colorBorderSelectable.length -1; } //faulty index, use faulty showing color.
114      if(val2 >= colorInnerSelectable.length || val2 <0){ val2 = colorInnerSelectable.length -1; }
115      dyda.lineColor = colorBorderSelectable[val1];
116      dyda.backColor = colorInnerSelectable[val2];
117      dyda.setChanged(ImplAccess.chgColorBack | ImplAccess.chgColorLine);
118      super.setValue(values);  //stores and calls repaint.
119      //repaint(repaintDelay, repaintDelayMax);
120    }
121  }
122  
123  
124  
125  public abstract class GraphicImplAccess extends GralWidget.ImplAccess
126  implements GralWidgImpl_ifc
127  {
128
129    
130    protected GraphicImplAccess(GralWidget widgg, GralMng mng)
131    {
132      super(widgg, mng);
133    }
134    
135    protected GralWidget.DynamicData dyda(){ return dyda; }
136  }    
137
138}