001package org.vishia.gral.area9;
002
003import java.io.File;
004import java.util.ArrayList;
005
006import org.vishia.gral.awt.AwtFactory;
007import org.vishia.gral.base.GralGridProperties;
008import org.vishia.gral.base.GralWindow;
009import org.vishia.gral.ifc.GralFactory;
010import org.vishia.gral.base.GralMng;
011import org.vishia.gral.swt.SwtFactory;
012import org.vishia.inspectorTarget.Inspector;
013import org.vishia.mainCmd.MainCmd;
014import org.vishia.msgDispatch.LogMessage;
015import org.vishia.msgDispatch.MsgDispatchSystemOutErr;
016import org.vishia.msgDispatch.MsgPrintStream;
017
018/**This class is an extension of the {@link MainCmd} for graphic applications.
019 * It provides up to 9 areas for panels. With them a basic partitioning of panels can be done.
020 * See {@link GralArea9_ifc}. 
021 * This instance can used for graphical output without any inheritance in an user application.
022 * The user application may inherit from {@link GuiCfg} instead or use GuiCfg with a special plugin
023 * and text-given graphic configuration. GuiCfg aggregates this class.
024 * Follow the pattern in {@link GuiCfg#main(String[])} to build a users application.
025 * 
026 * @author Hartmut Schorrig
027 *
028 */
029public class GralArea9MainCmd extends MainCmd
030{
031  
032  /**Version, history and license.
033   * <ul>
034   * <li>2016-07-10 Hartmut chg: Positioning of Windows on open, especiallythe top level window: with pos-String, it is the new concept of opsitioning of a GralWidget
035   * <li>2015-05-16 Hartmut new "-help:" for help base dir in argument check, "-msgcfg:" in argument check. 
036   *   It is moved from Fcmd to this base class of Fcmd, available generally.  
037   * <li>2013-11-22 Hartmut chg: title of main window as argument -title= 
038   * <li>2013-01-26 Hartmut chg: The MsgDispatchSystemOutErr.create(file) was invoked in this constructor. Therefore
039   *   all Applications redirect the System.out and System.err outputs to the message system. The outputs were completed
040   *   with the time stamp and a number, the {@link org.vishia.msgDispatch.MsgDispatcher} was prepared to use. 
041   *   This capability is removed from here yet. It should be a part of the application. See {@link GuiCfg#main(String[])}. 
042   * <li>2012-07-09 Hartmut new: The {@link Inspector} will be initialized only if the command line argument 
043   *   "-inspectorPort=" is given. That parameter in form "UDP:ip:port" is used.
044   * <li>2012-04-22 Hartmut new: {@link #parseArgumentsAndInitGraphic(String, String, int, int, int, int)}
045   *   for determination of display coordinates in the users application.
046   *   Getting size arguments from main-args. 
047   * <li>2012-03-30 MsgPrintStream systemErrAdapter used here.
048   *   Now System.err.println can be used to generate an dispatch-able message with numeric identifier.
049   * </ul>
050   * <b>Copyright/Copyleft</b>:
051   * For this source the LGPL Lesser General Public License,
052   * published by the Free Software Foundation is valid.
053   * It means:
054   * <ol>
055   * <li> You can use this source without any restriction for any desired purpose.
056   * <li> You can redistribute copies of this source to everybody.
057   * <li> Every user of this source, also the user of redistribute copies
058   *    with or without payment, must accept this license for further using.
059   * <li> But the LPGL ist not appropriate for a whole software product,
060   *    if this source is only a part of them. It means, the user
061   *    must publish this part of source,
062   *    but don't need to publish the whole source of the own product.
063   * <li> You can study and modify (improve) this source
064   *    for own using or for redistribution, but you have to license the
065   *    modified sources likewise under this LGPL Lesser General Public License.
066   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
067   * </ol>
068   * If you are intent to use this sources without publishing its usage, you can get
069   * a second license subscribing a special contract with the author. 
070   * 
071   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
072   * 
073   * 
074   */
075  public final static int version = 20130126;
076  
077  public GralArea9_ifc gui;
078  
079  //final MsgPrintStream systemErrAdapter;
080  //final MsgDispatchSystemOutErr msgDisp;
081  
082  
083  final LogMessage log;
084
085  /**Aggregation to given instance for the command-line-argument. The instance can be arranged anywhere else.
086   * It is given as ctor-parameter.
087   */
088  protected final GuiCallingArgs cargs;
089  
090  public GralMng gralMng;
091  
092  String sArgError = null;
093  
094  /**ctor called in static main.
095   * @param cargs aggregation to command-line-argument data, it will be filled here.
096   * @param args The command-line-calling arguments from static main
097   * @param sTitle Title in title line
098   * @param sOutputArea area for output, for example "3A3C".
099   */
100  public GralArea9MainCmd(GuiCallingArgs cargs, String[] args)
101  { 
102    super(args);
103    this.cargs = cargs;
104    this.log = getLogMessageErrorConsole();
105    //this.msgDisp = MsgDispatchSystemOutErr.create("D:/DATA/msg/log$yyyy-MMM-dd-HH_mm$.log");
106    
107    
108  }
109
110  
111  /**Builds the graphic and parses the command line parameters. Possible command line argument errors
112   * or help texts are outputted to the window in the output box.
113   * @param sTitle Title for window
114   * @param sOutputArea Use 1..3 for row and A..C for column in form for example "3A3C".
115   *   In this example the output box occupies all 3 columns (A to C) from the 3. (= bottom) row
116   *   of the 9 areas.  If null is used, the default selection is "3A3C".
117   * @return true if it is successfully.
118   */
119  public boolean parseArgumentsAndInitGraphic(String sTitle, String sOutputArea)
120  {
121    return parseArgumentsAndInitGraphic(sTitle, sOutputArea, '.', null);
122  }
123
124  
125  /**Builds the graphic and parses the command line parameters. Possible command line argument errors
126   * or help texts are outputted to the window in the output box.
127   * @param sTitle Title for window as default if -title= is not given as arg
128   * @param sOutputArea Use 1..3 for row and A..C for column in form for example "3A3C".
129   *   In this example the output box occupies all 3 columns (A to C) from the 3. (= bottom) row
130   *   of the 9 areas.  If null is used, the default selection is "3A3C".
131   * @return true if it is successfully.
132   */
133  public boolean parseArgumentsAndInitGraphic(String sTitle, String sOutputArea, char sizeShow, String position)
134  {
135    boolean bOk = true;
136    try{ parseArguments(); }
137    catch(Exception exception)
138    { sArgError = "Cmdline argument error:" + exception.getLocalizedMessage();
139      setExitErrorLevel(exitWithArgumentError);
140      bOk = false;
141    }
142    GralMng.create(log);
143    gralMng = GralMng.get();
144    if(cargs.graphicFactory == null){
145      cargs.graphicFactory = new SwtFactory();
146    }
147
148    if(sOutputArea == null){ sOutputArea = "A3C3"; }
149    if("\0 .".indexOf(sizeShow) >=0){ //undefined per parameter, use args  
150      sizeShow = cargs.sizeShow; 
151    }   
152    String posWindow1 = cargs.positionWindow !=null ? cargs.positionWindow : position !=null ? position : "0+80, 10+120";
153    
154    String sTitle1 = cargs.sTitle !=null ? cargs.sTitle : sTitle != null ? sTitle: cargs.fileGuiCfg !=null ? cargs.fileGuiCfg.getName() : " no -cfg given";
155    GralWindow primaryWindow = new GralWindow(posWindow1, "primaryWindow", sTitle1, GralWindow.windResizeable + GralWindow.windHasMenu);
156    GralFactory.createGraphic(primaryWindow, sizeShow, log, "SWT");
157    //cargs.graphicFactory.createWindow(primaryWindow, sizeShow);
158    gui = new GralArea9Window(this, primaryWindow);
159    gui.getGralMng().setApplicationAdapter(gui);
160    if(cargs.dirHtmlHelp !=null) {
161      try{
162        gui.setHelpBase(cargs.dirHtmlHelp.getAbsolutePath());
163      } catch(Exception exc) {
164        System.err.println("GralArea9MainCmd - help faulty, " + cargs.dirHtmlHelp.toString());
165      }
166    }
167    gui.initGraphic(sOutputArea);
168    if(sArgError !=null){
169      writeError(sArgError);
170    }
171    return bOk;
172  }
173
174  //public void setFullScreen(boolean full){ gui.setFullScreen(full); }
175
176  /*---------------------------------------------------------------------------------------------*/
177  /** Tests one argument. This method is invoked from parseArgument. It is abstract in the superclass MainCmd
178      and must be overwritten from the user.
179      :TODO: user, test and evaluate the content of the argument string
180      or test the number of the argument and evaluate the content in dependence of the number.
181
182      @param argc String of the actual parsed argument from cmd line
183      @param nArg number of the argument in order of the command line, the first argument is number 1.
184      @return true is okay,
185              false if the argument doesn't match. The parseArgument method in MainCmd throws an exception,
186              the application should be aborted.
187  */
188  @Override protected boolean testArgument(String arg, int nArg)
189  { boolean bOk = true;  //set to false if the argc is not passed
190    try {
191      if(arg.startsWith("-title="))      
192      { cargs.sTitle = getArgument(7);  //the graphic GUI-appearance
193      }
194      else if(arg.startsWith("-gui="))      
195      { cargs.fileGuiCfg = new File(getArgument(5));  //the graphic GUI-appearance
196      
197      }
198      else if(arg.startsWith("-cfg="))      
199      { String sCfg = getArgument(5);  //the graphic GUI-appearance
200        if(cargs.cfgConditions == null){
201          cargs.cfgConditions = new ArrayList<String>();
202        }
203        cargs.cfgConditions.add(sCfg);
204      }
205      else if(arg.startsWith("-ownIpc=")) 
206      { cargs.sOwnIpcAddr = getArgument(8);   //an example for default output
207      }
208      else if(arg.startsWith("-inspectorPort=")) 
209      { cargs.sInspectorOwnPort = getArgument(15);   //an example for default output
210      }
211      else if(arg.startsWith("-timeZone=")) 
212      { cargs.sTimeZone = getArgument(10);   //an example for default output
213      }
214      else if(arg.startsWith("-size=")) 
215      { String sValue = getArgument(6);
216        if(sValue.length() >=1){
217          cargs.sizeShow = sValue.charAt(0);   
218        } else {
219          bOk = false;
220        }
221      }
222      else if(arg.startsWith("-pos=")) 
223      { cargs.positionWindow = getArgument(5);
224      }
225      else if(arg.startsWith("-fullscreen")) 
226      { cargs.positionWindow = "0..0,0..0";
227      }
228      else if (arg.startsWith("-help:") || arg.startsWith("help=")) {
229        File file1 = new File(arg.substring(6));
230        String sPathHelpAbs = file1.getAbsolutePath();
231        cargs.dirHtmlHelp = new File(sPathHelpAbs);  //should be absolute because browser.
232      } else if (arg.startsWith("-msgcfg:") || arg.startsWith("-msgcfg=")) {
233        cargs.msgConfig = new File(arg.substring(8));
234      }
235      else if(arg.startsWith("-syntax=")) 
236      { cargs.sPathZbnf = getArgument(8);   //an example for default output
237      }
238      else if(arg.startsWith("-plugin=")) 
239      { cargs.sPluginClass = getArgument(8);   //an example for default output
240      }
241      
242      else if(arg.startsWith("-SWT")) 
243      { cargs.graphicFactory = new SwtFactory();   //an example for default output
244      }
245      
246      else if(arg.startsWith("-AWT")) 
247      { cargs.graphicFactory = new AwtFactory();   //an example for default output
248      }
249      
250      else if(arg.startsWith("-_")) 
251      { //accept but ignore it. Commented calling arguments.
252      }
253      else 
254      { bOk=false;
255      }
256    } catch(Exception exc){
257    }
258    return bOk;
259  }
260
261
262  /** Invoked from parseArguments if no argument is given. In the default implementation a help info is written
263   * and the application is terminated. The user should overwrite this method if the call without comand line arguments
264   * is meaningfull.
265   *
266   */
267  @Override protected void callWithoutArguments()
268  { //overwrite with empty method - if the calling without arguments
269    //having equal rights than the calling with arguments - no special action.
270  }
271
272  /*---------------------------------------------------------------------------------------------*/
273  /**Checks the cmdline arguments relation together.
274     If there is an inconsistents, a message should be written. It may be also a warning.
275     :TODO: the user only should determine the specific checks, this is a sample.
276     @return true if successfull, false if failed.
277  */
278  @Override protected boolean checkArguments()
279  { boolean bOk = true;
280    return bOk;
281  
282  }
283
284  
285  @Override public void exit()
286  { gui.mainWindow().closeWindow();
287  }
288  
289}