001package org.vishia.gral.widget;
002
003import java.text.SimpleDateFormat;
004import java.util.IllegalFormatConversionException;
005import java.util.IllegalFormatPrecisionException;
006import java.util.Locale;
007import java.util.MissingFormatArgumentException;
008import java.util.TimeZone;
009
010import org.vishia.bridgeC.OS_TimeStamp;
011import org.vishia.bridgeC.Va_list;
012import org.vishia.gral.base.GralTable;
013import org.vishia.gral.base.GralWidget;
014import org.vishia.gral.ifc.GralMng_ifc;
015import org.vishia.gral.ifc.GralTableLine_ifc;
016import org.vishia.gral.ifc.GralTable_ifc;
017import org.vishia.msgDispatch.LogMessage;
018import org.vishia.util.Assert;
019
020/**This class supports output of messages in a GralTable to view and scroll.
021 * The table contains only a defined maximum of messages, older messages will be removed if the capacity is used.
022 * It is recommended to write messages in a persistent medium additionally 
023 * (usual a {@link org.vishia.msgDispatch.LogMessageFile}). 
024 *   
025 * @author Hartmut Schorrig
026 *
027 */
028public class GralMsgOutputList  implements LogMessage
029{
030
031  /**Version, history and license.
032   * <ul>
033   * <li>2013-01-26 Hartmut:fine tuning while adapt message system in component javaSrc_vishiaRun.
034   * <li>2011-04-05 Hartmut creation
035   * </ul>
036   * <br><br>
037   * <b>Copyright/Copyleft</b>:
038   * For this source the LGPL Lesser General Public License,
039   * published by the Free Software Foundation is valid.
040   * It means:
041   * <ol>
042   * <li> You can use this source without any restriction for any desired purpose.
043   * <li> You can redistribute copies of this source to everybody.
044   * <li> Every user of this source, also the user of redistribute copies
045   *    with or without payment, must accept this license for further using.
046   * <li> But the LPGL ist not appropriate for a whole software product,
047   *    if this source is only a part of them. It means, the user
048   *    must publish this part of source,
049   *    but don't need to publish the whole source of the own product.
050   * <li> You can study and modify (improve) this source
051   *    for own using or for redistribution, but you have to license the
052   *    modified sources likewise under this LGPL Lesser General Public License.
053   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
054   * </ol>
055   * If you are intent to use this sources without publishing its usage, you can get
056   * a second license subscribing a special contract with the author. 
057   * 
058   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
059   * 
060   */
061  public static final int version = 20130126;
062
063  /**The access to the gui, to change data to show. */
064  private final GralMng_ifc guiAccess;
065  
066  private final SimpleDateFormat dateFormat;
067  
068  private final Locale localization;
069  
070
071  public GralMsgOutputList(GralMng_ifc guiAccess, String sTimeZoneShow, String sTimeFormat){
072    this.localization = Locale.ROOT;
073    this.guiAccess = guiAccess;
074    this.dateFormat = new SimpleDateFormat(sTimeFormat, localization);
075    this.dateFormat.setTimeZone(TimeZone.getTimeZone(sTimeZoneShow));
076
077
078  }
079  
080  @Override public void close() {
081  }
082
083  @Override public void flush() {
084  }
085
086  @Override public boolean isOnline() {
087    return true;
088  }
089
090  @Override public boolean sendMsg(int identNumber, String text, Object... args) {
091    // TODO Auto-generated method stub
092    return false;
093  }
094
095  @Override public boolean sendMsgTime(int identNumber, OS_TimeStamp creationTime,
096      String text, Object... args) {
097    // TODO Auto-generated method stub
098    return false;
099  }
100
101  /**This is the only one method, which is called from the message dispatcher. Only this is implemented.
102   * @see org.vishia.msgDispatch.LogMessage#sendMsgVaList(int, org.vishia.bridgeC.OS_TimeStamp, java.lang.String, org.vishia.bridgeC.Va_list)
103   */
104  @Override public boolean sendMsgVaList(int identNumber, OS_TimeStamp creationTime,
105    
106    String text, Va_list args) {
107    String sTime = dateFormat.format(creationTime);
108    String state = identNumber <0 ? "-" : "+'";  //going/coming
109    int identNumber1 = identNumber < 0 ? -identNumber :identNumber;
110    //The configuration for this msg ident.
111    String sText;
112    try{ sText = String.format(localization, text,args.get());
113    }catch(IllegalFormatPrecisionException exc){
114      sText = "error-precision in text format: " + text;
115    } catch(IllegalFormatConversionException exc){
116      sText = "error in text format: " + text;
117    } catch(MissingFormatArgumentException exc){
118      sText = "missing value: "+ text;
119    }
120    //String sInfoLine = sTime + '\t' + identNumber + '\t' + state + '\t' + sText;
121    String[] sInfoLine = {sTime, "" + identNumber1, state, "" + sText};
122
123    GralWidget oTable = guiAccess.getWidget("msgOfDay");
124    if(oTable == null){
125      Assert.consoleErr("GuiMainDialog:insertInfo: unknown widget; %s; message:%d;%s;\n", "msgOfDay", new Integer(identNumber), sText);
126    } else {
127      @SuppressWarnings("unchecked")
128      GralTable_ifc<Object> table = (GralTable<Object>)oTable;
129      GralTableLine_ifc<Object> line = table.addLine(null, sInfoLine, null);
130      table.setCurrentLine(line, -1, 0);
131    }
132    //guiAccess.insertInfo("msgOfDay", Integer.MAX_VALUE, sInfoLine);
133    return true;
134  }
135  
136}
137