001package org.vishia.gral.cfg;
002
003import java.text.ParseException;
004
005import org.vishia.gral.base.GralMng;
006import org.vishia.gral.base.GralPos;
007import org.vishia.gral.cfg.GralCfgData.GuiCfgButton;
008import org.vishia.gral.cfg.GralCfgData.GuiCfgCurveview;
009import org.vishia.gral.cfg.GralCfgData.GuiCfgImage;
010import org.vishia.gral.cfg.GralCfgData.GuiCfgInputFile;
011import org.vishia.gral.cfg.GralCfgData.GuiCfgLed;
012import org.vishia.gral.cfg.GralCfgData.GuiCfgLine;
013import org.vishia.gral.cfg.GralCfgData.GuiCfgShowField;
014import org.vishia.gral.cfg.GralCfgData.GuiCfgTable;
015import org.vishia.gral.cfg.GralCfgData.GuiCfgText;
016import org.vishia.gral.cfg.GralCfgData.GuiCfgWidget;
017import org.vishia.gral.ifc.GralMngBuild_ifc;
018import org.vishia.gral.ifc.GralWidgetCfg_ifc;
019
020
021/**ZBNF: Element::= ... ;
022 * Class for instance to capture and store one element. Any widget type is an Element.
023 * The distinction between different types are given with the {@link #itsCfgData}. 
024 * <pre>
025 * 
026 * 
027 * 
028 * */
029public class GralCfgElement implements Cloneable, GralWidgetCfg_ifc
030{ 
031  /**Version and history
032   * <ul>
033   * <li>2012-04-22 Hartmut chg {@link #setPos(GralMngBuild_ifc)} moved from {@link GralCfgBuilder#buildWidget(GralCfgElement)}
034   * <li>2012-04-22 Hartmut new {@link #new_ValueBar()}
035   * <li>2011-05-00 Hartmut created, the old ZbnfCfg.. class is obsolte now.
036   * </ul>
037   *
038   * <b>Copyright/Copyleft</b>:<br>
039   * For this source the LGPL Lesser General Public License,
040   * published by the Free Software Foundation is valid.
041   * It means:
042   * <ol>
043   * <li> You can use this source without any restriction for any desired purpose.
044   * <li> You can redistribute copies of this source to everybody.
045   * <li> Every user of this source, also the user of redistribute copies
046   *    with or without payment, must accept this license for further using.
047   * <li> But the LPGL is not appropriate for a whole software product,
048   *    if this source is only a part of them. It means, the user
049   *    must publish this part of source,
050   *    but doesn't need to publish the whole source of the own product.
051   * <li> You can study and modify (improve) this source
052   *    for own using or for redistribution, but you have to license the
053   *    modified sources likewise under this LGPL Lesser General Public License.
054   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
055   * </ol>
056   * If you intent to use this source without publishing its usage, you can get
057   * a second license subscribing a special contract with the author. 
058   * 
059   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
060   */
061  public static final int version = 20120422;
062
063  /**The previous element is necessary because non complete coordinates are resolved with previous.
064   * The next element is need to build a queue in order of the text. */
065  GralCfgElement previous, next;
066  
067  //final GralCfgData itsCfgData;
068  
069  
070  /**The content is set in textual form too. It is because [<?Element>...] was written */
071  String content;
072  
073  /**The position is set in textual form too. It is because [<?Element>...] was written */
074  String positionString;
075  
076  /**ZBNF: Position coordinates will be filled from [<?position>.... 
077   * The instance contains only that position data, which are found in the textual config file. 
078   * It is important to rewrite only that data, which are contained in the originally one. */
079  final GralCfgPosition positionInput = new GralCfgPosition();
080  
081  /**This obj contains all position data. Missed position data in the {@link #positionInput}
082   * are completed by knowledge of the position of the previous elements. 
083   * The position is filled only in {@link #setPos(GralMngBuild_ifc)} and used nearly only temporary
084   * */
085  private final GralCfgPosition position = new GralCfgPosition();
086  
087  //private final GralPos posInput = new GralPos();
088  
089  GuiCfgWidget widgetType;
090  
091  GralCfgElement() //GralCfgData itsCfgData)
092  { //this.itsCfgData = itsCfgData;
093  }
094  
095  @Override
096  public GralCfgElement clone(){ 
097    GralCfgElement newObj = new GralCfgElement();  //this.itsCfgData);
098    newObj.widgetType = widgetType.clone(); ///use a new cloned instance (use data).
099    newObj.positionInput.set(this.positionInput);
100    /*NOTE: don't use super.clone() because it copies the references to final elements. We need cloned new elements
101    GralCfgElement newObj = null;
102    try{ newObj = (GralCfgElement)super.clone(); 
103      newObj.position = new GralCfgPosition(); //use a new empty instance in cloned object, empty data.
104      newObj.positionInput = positionInput.clone(); ///use a new cloned instance (use data).
105      newObj.widgetType = widgetType.clone(); ///use a new cloned instance (use data).
106    } catch(CloneNotSupportedException exc){ assert(false); }
107    */
108    newObj.previous = this;  //link it in queue after this.
109    newObj.next = next;
110    next = newObj;
111    return newObj; 
112  }
113  
114  
115  
116  
117  /**Builds the position.
118   * @param cfge deprecated, it is this.
119   * @param guiMng to set the position.
120   * @return
121   * @throws ParseException 
122   */
123  void setPos(GralMngBuild_ifc guiMng) 
124  throws ParseException 
125  { GralMng mng = GralMng.get();
126    if(positionString !=null) {
127      mng.setPos(positionString);
128    } else {
129      GralCfgPosition p = positionInput;
130      if(p.yPos >=0 || p.xPos >=0 || p.ySizeDown !=0 || p.xWidth !=0) {
131          
132        int ySize = p.ySizeDown == GralPos.useNatSize ? p.ySizeDown :
133          p.ySizeDown == 0 ? GralPos.samesize : 
134          (p.ySizeFrac > 0 && p.ySizeDown < 0 ? p.ySizeDown -1 : p.ySizeDown) + GralPos.size;
135        int xSize = p.xWidth == GralPos.useNatSize ? p.xWidth : 
136          p.xWidth == 0 ? GralPos.samesize :
137          (p.xSizeFrac > 0 && p.xWidth < 0 ? p.xWidth -1 : p.xWidth) + GralPos.size;
138        mng.setFinePosition(p.yPos, p.yPosFrac, ySize, p.ySizeFrac, p.xPos, p.xPosFrac, xSize, p.xSizeFrac, 0, 'r', 0, 0, null);  
139      }
140      else {
141        stop();
142        //no position given. The new position will be set relative to the old one on widget.setToPanel.
143      }
144      //  setPosOld(guiMng);  //use inputPos
145    }
146  }  
147  
148  
149  
150  
151  public String getPanel(){ return positionInput.panel; }
152
153  void setPanel(String panel){ position.panel = positionInput.panel = panel; }
154  
155  public int get_xPos(){ return positionInput.xPos; }
156  
157  public int get_yPos(){ return positionInput.yPos; }
158  
159  
160  /**ZBNF: <?position> */
161  public GralCfgPosition new_position(){ return positionInput; }  
162  
163  /**ZBNF: <*:?position> */
164  public void set_positionString(String val) { this.positionString = val; }
165 
166  /**ZBNF: [<?position>   ] it is unnecessary*/
167  public void set_position(String val) {  }
168 
169  /**ZBNF: <?position> */
170  public void set_position(GralCfgPosition val) {  } //is set only
171 
172  /**ZBNF: Text::= */
173  public GuiCfgText new_Text()
174  { GuiCfgText widgetType1 = new GuiCfgText(this); 
175    this.widgetType = widgetType1; 
176    return widgetType1;
177  }
178  
179  /**ZBNF: Text::= */
180  public void set_Text(GuiCfgText data){  }
181  
182  public GuiCfgWidget new_InputTextbox()
183  { GralCfgData.GuiCfgWidget widgt = new GralCfgData.GuiCfgWidget(this, 't');
184    this.widgetType = widgt;
185    return widgt;
186  }
187  
188  public void set_InputTextbox(GuiCfgWidget data) {  }
189  
190  public GuiCfgWidget new_OutputTextbox()
191  { GralCfgData.GuiCfgWidget widgt = new GralCfgData.GuiCfgWidget(this, 't');
192    this.widgetType = widgt;
193    return widgt;
194  }
195  
196  public void set_OutputTextbox(GuiCfgWidget data) {  }
197  
198  public GuiCfgWidget new_InputTextline()
199  { GralCfgData.GuiCfgWidget widgt = new GralCfgData.GuiCfgWidget(this, 'T');
200    this.widgetType = widgt;
201    return widgt;
202  }
203  
204  public void set_InputTextline(GuiCfgWidget data) {  }
205  
206  public GuiCfgWidget new_InputFile()
207  { GuiCfgInputFile widgt = new GuiCfgInputFile(this);
208    this.widgetType = widgt;
209    return widgt;
210  }
211  
212  public void set_InputFile(GuiCfgWidget data) {  }
213  
214  public GuiCfgWidget new_Button()
215  { GuiCfgButton widgt = new GuiCfgButton(this);
216    this.widgetType = widgt;
217    return widgt;
218  }
219  
220  public void set_Button(GuiCfgWidget data) {  }
221  
222  public GuiCfgWidget new_SwitchButton()
223  { GuiCfgButton widgt = new GuiCfgButton(this, true);
224    this.widgetType = widgt;
225    return widgt;
226  }
227  
228  public void set_SwitchButton(GuiCfgWidget data) {  }
229  
230  /**ZBNF: Led::= */
231  public GuiCfgLed new_Led()
232  { GuiCfgLed widgetType1 = new GuiCfgLed(this); 
233    this.widgetType = widgetType1; 
234    return widgetType1;
235  }
236  
237  /**ZBNF: Led::= */
238  public void set_Led(GuiCfgLed data){  }
239  
240  /**ZBNF: ValueBar::= */
241  public GralCfgData.GuiCfgWidget new_ValueBar()
242  { GralCfgData.GuiCfgWidget widgt = new GralCfgData.GuiCfgWidget(this, 'U');
243    this.widgetType = widgt;
244    return widgt;
245  }
246
247  
248  /**ZBNF: ValueBar::= */
249  public void set_ValueBar(GralCfgData.GuiCfgWidget data){  }
250  
251  /**ZBNF: Line::= */
252  public GuiCfgLine new_Line()
253  { GuiCfgLine widgetType1 = new GuiCfgLine(this); 
254    this.widgetType = widgetType1; 
255    return widgetType1;
256  }
257  
258  /**ZBNF: Line::= */
259  public void set_Line(GuiCfgLine data){  }
260  
261  /**ZBNF: Imagefile::= */
262  public GuiCfgImage new_Imagefile()
263  { GuiCfgImage widgetType1 = new GuiCfgImage(this); 
264    this.widgetType = widgetType1; 
265    return widgetType1;
266  }
267  
268  /**ZBNF: ShowField::= */
269  public void set_Imagefile(GuiCfgImage data){  }
270  
271  /**ZBNF: ShowField::= */
272  public GuiCfgShowField new_ShowField()
273  { GuiCfgShowField widgetType1 = new GuiCfgShowField(this); 
274    this.widgetType = widgetType1; 
275    return widgetType1;
276  }
277  
278  /**ZBNF: ShowField::= */
279  public void set_ShowField(GuiCfgShowField data){  }
280  
281  /**ZBNF: Table::= */
282  public GuiCfgTable new_Table()
283  { GuiCfgTable widgetType1 = new GuiCfgTable(this); 
284    this.widgetType = widgetType1; 
285    return widgetType1;
286  }
287  
288  /**ZBNF: Table::= */
289  public void set_Table(GuiCfgTable data){  }
290  
291  /**ZBNF: Curveview::= */
292  public GuiCfgCurveview new_Curveview()
293  { GuiCfgCurveview widgetType1 = new GuiCfgCurveview(this); 
294    this.widgetType = widgetType1; 
295    return widgetType1;
296  }
297  
298  /**ZBNF: Curveview::= */
299  public void set_Curveview(GuiCfgCurveview data){  }
300
301  
302  private void stop(){}
303  
304}//class GuiCfgElement
305