001    // Java tool interface class ConsoleTool
002    // This file is generated automatically, please do not edit!
003    // generation time: Feb 22, 2007 3:24:38 PM
004    
005    package nl.cwi.sen1.gui.plugin;
006    
007    import java.util.HashSet;
008    import java.util.List;
009    import java.util.Set;
010    
011    import toolbus.SwingTool;
012    
013    import aterm.ATerm;
014    import aterm.ATermAppl;
015    import aterm.ATermFactory;
016    import aterm.ATermList;
017    
018    abstract public class ConsoleTool
019      extends SwingTool
020      implements ConsoleTif
021    {
022      // This table will hold the complete input signature
023      private Set<ATerm> sigTable = new HashSet<ATerm>();
024    
025      // Patterns that are used to match against incoming terms
026      private ATerm PaddMessage0;
027      private ATerm PrecTerminate0;
028    
029      // Mimic the constructor from the AbstractTool class
030      protected ConsoleTool(ATermFactory factory)
031      {
032        super(factory);
033        initSigTable();
034        initPatterns();
035      }
036    
037      // This method initializes the table with input signatures
038      private void initSigTable()
039      {
040        sigTable.add(factory.parse("rec-do(<console>,add-message(<str>))"));
041        sigTable.add(factory.parse("rec-terminate(<console>,<term>)"));
042      }
043    
044      // Initialize the patterns that are used to match against incoming terms
045      private void initPatterns()
046      {
047        PaddMessage0 = factory.parse("rec-do(add-message(<str>))");
048        PrecTerminate0 = factory.parse("rec-terminate(<term>)");
049      }
050    
051      // The generic handler calls the specific handlers
052      public ATerm handler(ATerm term)
053      {
054        List<?> result;
055    
056        result = term.match(PaddMessage0);
057        if (result != null) {
058          addMessage((String)result.get(0));
059          return null;
060        }
061        result = term.match(PrecTerminate0);
062        if (result != null) {
063          recTerminate((ATerm)result.get(0));
064          return null;
065        }
066    
067        notInInputSignature(term);
068        return null;
069      }
070    
071      // Check the input signature
072      public void checkInputSignature(ATermList sigs)
073      {
074        while(!sigs.isEmpty()) {
075          ATermAppl sig = (ATermAppl)sigs.getFirst();
076          sigs = sigs.getNext();
077          if (!sigTable.contains(sig)) {
078            // Sorry, but the term is not in the input signature!
079            notInInputSignature(sig);
080          }
081        }
082      }
083    
084      // This function is called when an input term
085      // was not in the input signature.
086      void notInInputSignature(ATerm t)
087      {
088        throw new RuntimeException("term not in input signature: " + t);
089      }
090    }