    001package org.vishia.gral.ifc;
    002
    003import org.vishia.gral.base.GralGraphicThread;
    004import org.vishia.gral.base.GralMng;
    005import org.vishia.gral.base.GralWindow;
    006import org.vishia.msgDispatch.LogMessage;
    007
    008/**This is the super class for a factory class which allows usage of any graphical base system
    009 * such as AWT, Swing, SWT or other for the GRAL GRaphic Adaption Layer
    010 * @author Hartmut Schorrig
    011 *
    012 */
    013public abstract class GralFactory
    014{
    015
    016  /**Version, history and license.
    017   * <ul>
    018   * <li>2017-01-15 Hartmut chg in deprecated createWindow, see comment there.
    019   * <li>2016-07-16 Hartmut new: {@link #createGraphic(GralWindow, char, LogMessage, String)} as non abstract static method
    020   *   creates the graphic, for several implementation platforms by "AWT", "SWT" or special factory class.
    021   * <li>2015-05-01 Hartmut now a abstract class instead interface.
    022   * <li>2011-06-00 Hartmut created
    023   * </ul>
    024   *
    025   * <b>Copyright/Copyleft</b>:<br>
    026   * For this source the LGPL Lesser General Public License,
    027   * published by the Free Software Foundation is valid.
    028   * It means:
    029   * <ol>
    030   * <li> You can use this source without any restriction for any desired purpose.
    031   * <li> You can redistribute copies of this source to everybody.
    032   * <li> Every user of this source, also the user of redistribute copies
    033   *    with or without payment, must accept this license for further using.
    034   * <li> But the LPGL is not appropriate for a whole software product,
    035   *    if this source is only a part of them. It means, the user
    036   *    must publish this part of source,
    037   *    but doesn't need to publish the whole source of the own product.
    038   * <li> You can study and modify (improve) this source
    039   *    for own using or for redistribution, but you have to license the
    040   *    modified sources likewise under this LGPL Lesser General Public License.
    041   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
    042   * </ol>
    043   * If you intent to use this source without publishing its usage, you can get
    044   * a second license subscribing a special contract with the author.
    045   *
    046   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
    047   */
    048  public static final int version = 20150501;
    049
    050
    051
    052  /**Creates and opens the primary window of the application.
    053   * @param log
    054   * @param sTitle
    055   * @param sizeShow
    056   * @param left
    057   * @param top
    058   * @param xSize
    059   * @param ySize
    060   * @return the window
    061   * @deprecated use {@link #createGraphic(GralWindow, char, int, int, int, int, LogMessage)}
    062   */
    063  public final GralWindow createWindow(LogMessage log, String sTitle, char sizeShow, int left, int top, int xSize, int ySize)
    064  {
    065    GralMng.create(log);
    066    int windProps = GralWindow_ifc.windResizeable;
    067    //@date 2017-01-15 a "!" is faulty yet, use a sensible position string
    068    GralWindow window = new GralWindow("@10+100,30+150", "primaryWindow", sTitle, windProps);
    069    createWindow(window, sizeShow, left, top, xSize, ySize);
    070    return window;
    071  }
    072
    073
    074  /**Creates and opens the primary window of the application with a given GralWindow instance.
    075   * @param sizeShow
    076   * @param left
    077   * @param top
    078   * @param xSize
    079   * @param ySize
    080   * @return the window
    081   * @deprecated use {@link #createGraphic(GralWindow, char, int, int, int, int, LogMessage)}
    082   */
    083  @Deprecated public final void createWindow(GralWindow windowg, char sizeShow, int left, int top, int xSize, int ySize)
    084  {
    085    GralMng mng = GralMng.get();
    086    mng.setFirstlyThePrimaryWindow(windowg); //checks whether called firstly.
    087    LogMessage log = mng.log;
    088    //The graphicthread creates the Swt Window.
    089    //SwtPrimaryWindow swtWindow = SwtPrimaryWindow.create(log, sTitle, sizeShow, left, top, xSize, ySize);
    090    GralGraphicThread gralGraphicThread = createGraphic(windowg, sizeShow, log);
    091    synchronized(gralGraphicThread){
    092      while(gralGraphicThread.getThreadIdGui() == 0){
    093        try{ gralGraphicThread.wait(1000);} catch(InterruptedException exc){}
    094      }
    095    }
    096  }
    097
    098  /**This method should intitialize the implementation layer manager, primary window and the graphic thread.
    099   * @param windowg
    100   * @param sizeShow
    101   * @param left
    102   * @param top
    103   * @param xSize
    104   * @param ySize
    105   * @param log
    106   * @return
    107   */
    108  protected abstract GralGraphicThread createGraphic(GralWindow windowg, char sizeShow, LogMessage log);
    109
    110
    111
    112
    113
    114
    115  public static GralGraphicThread createGraphic(GralWindow windowg, char sizeShow, LogMessage log, String implementor) {
    116    GralMng mng = GralMng.get();
    117    mng.setFirstlyThePrimaryWindow(windowg); //checks whether called firstly.
    118    GralGraphicThread gralThread = null;
    119    final String sNameFactoryClass;
    120    if(implementor == null) { implementor = "SWT"; }
    121    if(implementor.equals("SWT")) { sNameFactoryClass = "org.vishia.gral.swt.SwtFactory"; }
    122    else if(implementor.equals("AWT")) { sNameFactoryClass = "org.vishia.gral.awt.AwtFactory"; }
    123    else { sNameFactoryClass = implementor; }
    124    GralFactory factory;
    125    try{
    126      //Class<GralFactory> classfactory = ClassLoader.getSystemClassLoader().loadClass(sNameFactoryClass);
    127      Class<?> classfactory = Class.forName(sNameFactoryClass);
    128      Object oFactory = classfactory.newInstance();
    129      factory = (GralFactory)oFactory;           //Exception if faulty type.
    130    }catch(Exception exc){
    131      String sError = "class not found or faulty: " + sNameFactoryClass;
    132      System.err.println(sError);
    133      throw new RuntimeException(sError, exc);
    134    }
    135    try { //this start the GralGraphicThread, see run(). There the primaryWindow will be created.
    136      gralThread = factory.createGraphic(windowg, sizeShow, log);
    137    } catch(Exception exc) {
    138      String sError = "Exception initializing graphic: " + exc.getMessage();
    139      System.err.println(sError);
    140      throw new RuntimeException(sError, exc);
    141    }
    142    return gralThread;
    143  }
    144
    145
    146
    147
    148
    149
    150
    151
    152}