001package org.vishia.gral.cfg;
002
003import org.vishia.gral.base.GralPos;
004
005/**ZBNF: position::= ... ;
006 * Class for instance to capture and store the position in an element. */
007public final class GralCfgPosition implements Cloneable
008{
009  /**Version and history
010   * <ul>
011   * </ul>
012   *
013   * <b>Copyright/Copyleft</b>:<br>
014   * For this source the LGPL Lesser General Public License,
015   * published by the Free Software Foundation is valid.
016   * It means:
017   * <ol>
018   * <li> You can use this source without any restriction for any desired purpose.
019   * <li> You can redistribute copies of this source to everybody.
020   * <li> Every user of this source, also the user of redistribute copies
021   *    with or without payment, must accept this license for further using.
022   * <li> But the LPGL is not appropriate for a whole software product,
023   *    if this source is only a part of them. It means, the user
024   *    must publish this part of source,
025   *    but doesn't need to publish the whole source of the own product.
026   * <li> You can study and modify (improve) this source
027   *    for own using or for redistribution, but you have to license the
028   *    modified sources likewise under this LGPL Lesser General Public License.
029   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
030   * </ol>
031   * If you intent to use this source without publishing its usage, you can get
032   * a second license subscribing a special contract with the author. 
033   * 
034   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
035   */
036  public static final int version = 20120303;
037
038  
039  public String panel;
040  public boolean yPosRelative;
041  public int yPos = -1, yPosFrac;
042  public int ySizeDown, ySizeFrac;
043  boolean yIncr_;
044  
045  public boolean xPosRelative;
046  public int xPos = -1, xPosFrac;
047  public int xWidth, xSizeFrac;
048  boolean xIncr_ = false;
049  
050  public void set_xIncr(){ xIncr_ = true; yIncr_ = false; }
051  public void set_yIncr(){ yIncr_ = true; xIncr_ = false; }
052  public void set_xOwnSize(){ xWidth = GralPos.useNatSize; } //Integer.MAX_VALUE; }
053  public void set_yOwnSize(){ ySizeDown = GralPos.useNatSize; } //Integer.MAX_VALUE; }
054  
055  @Override protected GralCfgPosition clone()
056  { GralCfgPosition clone = null;
057    try{ clone = (GralCfgPosition)super.clone(); } 
058    catch(CloneNotSupportedException exc){ assert(false); }
059    return clone;
060  }
061  
062  /**Sets all data from src. It is similar as {@link #clone()} but it uses a given instance.
063   * @param src of data
064   */
065  public void set(GralCfgPosition src){
066    panel = src.panel;
067    yPosRelative = src.yPosRelative;
068    yPos = src.yPos;
069    yPosFrac = src.yPosFrac;
070    ySizeDown = src.ySizeDown;
071    ySizeFrac = src.ySizeFrac;
072    yIncr_ = src.yIncr_;
073    xPosRelative = src.xPosRelative;
074    xPos = src.xPos;
075    xPosFrac = src.xPosFrac;
076    xWidth = src.xWidth;
077    xSizeFrac = src.xSizeFrac;
078    xIncr_ = src.yIncr_;
079    
080  }
081  
082  
083  /**Sets a position element. It is able to call from a configuration input or gui input.
084   * @param what use y, x, h, w for pos-y, pos-x, height, width. All other chars causes an IllegalArgumentException.
085   * @param sVal String given Value in ZBNF-syntax-form ::=[< ?posRelative> &+]< #?val>[ \. <#?frac> ]. Fault inputs causes return false.
086   *        It should not have leeding or trailing spaces! Trim outside. 
087   *        It is admissible that the string is empty, then no action is done.
088   * @return true on success. False if the sVal contains numberFormat errors. True on empty sVal
089   */
090  public boolean setPosElement(char what, String sVal)
091  { boolean ok = true;
092    final int val; final int frac;
093    if(sVal.length() >0){
094      boolean posRelativ = sVal.charAt(0)=='&';
095      int pos1 = posRelativ ? 1: 0;
096      if(sVal.charAt(pos1) == '+'){
097        pos1 +=1;   //skip over a '+', it disturbs Integer.parseInt
098      }
099      int posPoint = sVal.indexOf('.');
100      try{
101        if(posPoint >=0){
102          val = Integer.parseInt(sVal.substring(pos1, posPoint));   
103          frac = Integer.parseInt(sVal.substring(posPoint +1));   
104        } else {
105          val = Integer.parseInt(sVal.substring(pos1));
106          frac = 0;
107        }
108        switch(what){
109          case 'y': yPos = val; yPosFrac = frac; yPosRelative = posRelativ; break;
110          case 'x': xPos = val; xPosFrac = frac; xPosRelative = posRelativ; break;
111          case 'h': ySizeDown = val; ySizeFrac = frac; break;
112          case 'w': xWidth = val; xSizeFrac = frac; break;
113        }
114      } catch(NumberFormatException exc){ ok = false; }
115    }
116    return ok;
117  }
118  
119
120  
121}