Package com.ardublock.translator

Examples of com.ardublock.translator.Translator


 
  public void actionPerformed(ActionEvent e)
  {
    boolean success;
    success = true;
    Translator translator = new Translator(workspace);
    translator.reset();
   
    Iterable<RenderableBlock> renderableBlocks = workspace.getRenderableBlocks();
   
    Set<RenderableBlock> loopBlockSet = new HashSet<RenderableBlock>();
    Set<RenderableBlock> subroutineBlockSet = new HashSet<RenderableBlock>();
    Set<RenderableBlock> scoopBlockSet = new HashSet<RenderableBlock>();
    StringBuilder code = new StringBuilder();
   
   
    for (RenderableBlock renderableBlock:renderableBlocks)
    {
      Block block = renderableBlock.getBlock();
     
      if (!block.hasPlug() && (Block.NULL.equals(block.getBeforeBlockID())))
      {
       
        if(block.getGenusName().equals("loop"))
        {
          loopBlockSet.add(renderableBlock);
        }
        if(block.getGenusName().equals("loop1"))
        {
          loopBlockSet.add(renderableBlock);
        }
        if(block.getGenusName().equals("loop2"))
        {
          loopBlockSet.add(renderableBlock);
        }
        if(block.getGenusName().equals("loop3"))
        {
          loopBlockSet.add(renderableBlock);
        }
        if(block.getGenusName().equals("program"))
        {
          loopBlockSet.add(renderableBlock);
        }
        if(block.getGenusName().equals("setup"))
        {
          loopBlockSet.add(renderableBlock);
        }
        if (block.getGenusName().equals("subroutine"))
        {
          String functionName = block.getBlockLabel().trim();
          try
          {
            translator.addFunctionName(block.getBlockID(), functionName);
          }
          catch (SubroutineNameDuplicatedException e1)
          {
            context.highlightBlock(renderableBlock);
            //find the second subroutine whose name is defined, and make it highlight. though it cannot happen due to constraint of OpenBlocks -_-
            JOptionPane.showMessageDialog(parentFrame, uiMessageBundle.getString("ardublock.translator.exception.subroutineNameDuplicated"), "Error", JOptionPane.ERROR_MESSAGE);
            return ;
          }
          subroutineBlockSet.add(renderableBlock);
        }
        if (block.getGenusName().equals("scoop_task"))
        {
          translator.setScoopProgram(true);
          scoopBlockSet.add(renderableBlock);
        }
        if (block.getGenusName().equals("scoop_loop"))
        {
          translator.setScoopProgram(true);
          scoopBlockSet.add(renderableBlock);
        }
        if (block.getGenusName().equals("scoop_pin_event"))
        {
          translator.setScoopProgram(true);
          scoopBlockSet.add(renderableBlock);
        }
       
      }
    }
    if (loopBlockSet.size() == 0) {
      JOptionPane.showMessageDialog(parentFrame, uiMessageBundle.getString("ardublock.translator.exception.noLoopFound"), "Error", JOptionPane.ERROR_MESSAGE);
      return ;
    }
    if (loopBlockSet.size() > 1) {
      for (RenderableBlock rb : loopBlockSet)
      {
        context.highlightBlock(rb);
      }
      JOptionPane.showMessageDialog(parentFrame, uiMessageBundle.getString("ardublock.translator.exception.multipleLoopFound"), "Error", JOptionPane.ERROR_MESSAGE);
      return ;
    }

    try
    {
     
      for (RenderableBlock renderableBlock : loopBlockSet)
      {
        translator.setRootBlockName("loop");
        Block loopBlock = renderableBlock.getBlock();
        code.append(translator.translate(loopBlock.getBlockID()));
      }
     
      for (RenderableBlock renderableBlock : scoopBlockSet)
      {
        translator.setRootBlockName("scoop");
        Block scoopBlock = renderableBlock.getBlock();
        code.append(translator.translate(scoopBlock.getBlockID()));
      }
     
      for (RenderableBlock renderableBlock : subroutineBlockSet)
      {
        translator.setRootBlockName("subroutine");
        Block subroutineBlock = renderableBlock.getBlock();
        code.append(translator.translate(subroutineBlock.getBlockID()));
      }
     
      translator.beforeGenerateHeader();
      code.insert(0, translator.genreateHeaderCommand());
    }
    catch (SocketNullException e1)
    {
      e1.printStackTrace();
      success = false;
View Full Code Here

TOP

Related Classes of com.ardublock.translator.Translator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.