/**
   * Loads all the block genuses, properties, and link rules of a language specified in the
   * pre-defined language def file.
   *
   * @param root Loads the language specified in the Element root
   */
  public void loadBlockLanguage(final Element root) {
    /* MUST load shapes before genuses in order to initialize
    connectors within each block correctly */
    BlockConnectorShape.loadBlockConnectorShapes(root);

    // load genuses
    BlockGenus.loadBlockGenera(workspace, root);

    // load rules
    BlockLinkChecker.addRule(workspace, new CommandRule(workspace));
    BlockLinkChecker.addRule(workspace, new SocketRule());

    // set the dirty flag for the language definition file
    // to false now that the lang file has been loaded
    langDefDirty = false;
  }
  /**
   * @param childBlock
   * @param widget
   * @requires widget != null
   * @modifies
   * @effects Does nothing if: childBlock is invalid (null) Otherwise, remove childBlock from it's
   *     parent block if the childBlock has a parent. If it does not have a parent, do nothing.
   */
  private void disconnectBlock(Block childBlock, WorkspaceWidget widget) {
    if (childBlock == null || invalidBlockID(childBlock.getBlockID())) {
      return;
    }
    BlockConnector childPlug = BlockLinkChecker.getPlugEquivalent(childBlock);
    if (childPlug == null || !childPlug.hasBlock() || isNullBlockInstance(childPlug.getBlockID())) {
      return;
    }
    Block parentBlock = workspace.getEnv().getBlock(childPlug.getBlockID());
    BlockConnector parentSocket = parentBlock.getConnectorTo(childBlock.getBlockID());
    if (parentSocket == null) {
      return;
    }
    // disconector if child connector exists and has a block connected to it
    BlockLink link =
        BlockLink.getBlockLink(workspace, childBlock, parentBlock, childPlug, parentSocket);
    if (link == null) {
      return;
    }

    link.disconnect();

    RenderableBlock parentRenderable =
        workspace.getEnv().getRenderableBlock(parentBlock.getBlockID());
    if (parentRenderable == null) {
      throw new RuntimeException(
          "INCONSISTANCY VIOLATION: "
              + "parent block was valid, non-null, and existed.\n\tBut yet, when we get it's renderable"
              + "representation, we recieve a null instance.\n\tIf the Block instance of an ID is non-null"
              + "then its graphical RenderableBlock should be non-null as well");
    }
    parentRenderable.blockDisconnected(parentSocket);
    workspace.notifyListeners(
        new WorkspaceEvent(workspace, widget, link, WorkspaceEvent.BLOCKS_DISCONNECTED));
  }
 /**
  * @param renderable
  * @param widget
  * @param container
  * @requires renderable != null && renderable.blockID != null && renderable.blockID != Block.NULL
  *     && widget != null && container != null
  * @modifies renderable && children blocks connected to renderable
  * @effects removes renderable from container and widget and re-renders renderable block, widget,
  *     and container appropriately. Repeats for all of renderable's children.
  */
 private void removeBlock(
     RenderableBlock renderable, WorkspaceWidget widget, Container container) {
   widget.removeBlock(renderable);
   container.remove(renderable);
   container.validate();
   container.repaint();
   renderable.setParentWidget(null);
   // Workspace.getInstance().notifyListeners(new WorkspaceEvent(widget, renderable.getBlockID(),
   // WorkspaceEvent.BLOCK_REMOVED));
   for (BlockConnector child :
       BlockLinkChecker.getSocketEquivalents(
           workspace.getEnv().getBlock(renderable.getBlockID()))) {
     if (child == null || child.getBlockID().equals(Block.NULL)) {
       continue;
     }
     RenderableBlock childRenderable = workspace.getEnv().getRenderableBlock(child.getBlockID());
     if (childRenderable == null) {
       continue;
     }
     removeBlock(childRenderable, widget, container);
   }
   if (renderable.hasComment()) {
     renderable.removeComment();
   }
   workspace.notifyListeners(
       new WorkspaceEvent(
           workspace, widget, renderable.getBlockID(), WorkspaceEvent.BLOCK_REMOVED));
 }
Beispiel #4
0
  public static RenderableBlock makeRenderable(
      Workspace workspace, BlockNode node, WorkspaceWidget widget) {
    String genusName = node.getGenusName(); // genusName may not be null
    RenderableBlock renderable = BlockUtilities.getBlock(workspace, genusName, node.getLabel());
    if (renderable == null) {
      throw new RuntimeException("No children block exists for this genus: " + genusName);
    }
    Block block = Block.getBlock(renderable.getBlockID()); // assume not null
    widget.blockDropped(renderable);
    for (int i = 0; i < node.getChildren().size(); i++) {
      BlockConnector socket = block.getSocketAt(i);
      BlockNode child = node.getChildren().get(i);
      RenderableBlock childRenderable = makeRenderable(workspace, child, widget);
      Block childBlock = Block.getBlock(childRenderable.getBlockID());

      // link blocks
      BlockLink link;
      if (childBlock.hasPlug()) {
        link = BlockLinkChecker.canLink(workspace, block, childBlock, socket, childBlock.getPlug());
      } else if (childBlock.hasBeforeConnector()) {
        link =
            BlockLinkChecker.canLink(
                workspace, block, childBlock, socket, childBlock.getBeforeConnector());
      } else {
        link = null;
      } // assume link is not null
      link.connect();
      workspace.notifyListeners(
          new WorkspaceEvent(
              workspace,
              RenderableBlock.getRenderableBlock(link.getPlugBlockID()).getParentWidget(),
              link,
              WorkspaceEvent.BLOCKS_CONNECTED));
    }
    if (node.getAfterNode() != null) {
      BlockConnector socket = block.getAfterConnector(); // assume has after connector
      BlockNode child = node.getAfterNode();
      RenderableBlock childRenderable = makeRenderable(workspace, child, widget);
      Block childBlock = Block.getBlock(childRenderable.getBlockID());

      // link blocks
      BlockLink link;
      if (childBlock.hasPlug()) {
        link = BlockLinkChecker.canLink(workspace, block, childBlock, socket, childBlock.getPlug());
      } else if (childBlock.hasBeforeConnector()) {
        link =
            BlockLinkChecker.canLink(
                workspace, block, childBlock, socket, childBlock.getBeforeConnector());
      } else {
        link = null;
      } // assume link is not null
      link.connect();
      workspace.notifyListeners(
          new WorkspaceEvent(
              workspace,
              RenderableBlock.getRenderableBlock(link.getPlugBlockID()).getParentWidget(),
              link,
              WorkspaceEvent.BLOCKS_CONNECTED));
    }
    return renderable;
  }
 /** Resets the current language within the active Workspace. */
 public void resetLanguage() {
   BlockConnectorShape.resetConnectorShapeMappings();
   BlockGenus.resetAllGenuses();
   BlockLinkChecker.reset();
 }
  /**
   * @requires the current block with focus must exist with non-null ID in a non-null widget with a
   *     non-null parent
   * @modifies the current block with focus
   * @effects removes the current block with focus and children from the GUI and destroys the link
   *     between the block with focus and it's parent block if one exist and children blocks if it
   *     has childrens.
   */
  private void deleteBlockAndChildren() {
    //		====================>>>>>>>>>>>>>>>>>>>>>>>>>
    //		====================focus coming in>>>>>>>>>>TODO
    //		====================>>>>>>>>>>>>>>>>>>>>>>>>>

    // Do not delete null block references.  Otherwise, get Block and RenderableBlock instances.
    if (isNullBlockInstance(focusManager.getFocusBlockID())) {
      throw new RuntimeException("TypeBlockManager: deleting a null block references.");
    }
    Block block = workspace.getEnv().getBlock(focusManager.getFocusBlockID());
    RenderableBlock renderable = workspace.getEnv().getRenderableBlock(block.getBlockID());

    // get workspace widget associated with current focus
    WorkspaceWidget widget = renderable.getParentWidget();
    // do not delete block instances in null widgets
    if (widget == null) {
      throw new RuntimeException("TypeBlockManager: do not delete blocks with no parent widget.");
      // return;
    }
    // get parent container of this graphical representation
    Container container = renderable.getParent();
    // do not delete block instances in null parents
    if (container == null) {
      throw new RuntimeException(
          "TypeBlockManager: do not delete blocks with no parent container.");
      // return;
    }
    // get the Block's location on the canvas
    Point location =
        SwingUtilities.convertPoint(renderable, new Point(0, 0), this.blockCanvas.getCanvas());

    // for every valid and active connection, disconnect it.
    Long parentID = null;
    if (validConnection(block.getPlug())) {
      parentID = block.getPlugBlockID();
      this.disconnectBlock(block, widget);
      if (validConnection(block.getAfterConnector())) {
        disconnectBlock(workspace.getEnv().getBlock(block.getAfterBlockID()), widget);
      }
    } else if (validConnection(block.getBeforeConnector())) {
      parentID = block.getBeforeBlockID();
      BlockConnector parentConnectorToBlock =
          workspace.getEnv().getBlock(parentID).getConnectorTo(block.getBlockID());
      this.disconnectBlock(block, widget);
      if (validConnection(block.getAfterConnector())) {
        Long afterBlockID = block.getAfterBlockID();
        disconnectBlock(workspace.getEnv().getBlock(afterBlockID), widget);
        if (parentID != null) {
          BlockLink link =
              BlockLinkChecker.canLink(
                  workspace,
                  workspace.getEnv().getBlock(parentID),
                  workspace.getEnv().getBlock(afterBlockID),
                  parentConnectorToBlock,
                  workspace.getEnv().getBlock(afterBlockID).getBeforeConnector());
          if (link != null) {
            link.connect();
            workspace.notifyListeners(
                new WorkspaceEvent(
                    workspace,
                    workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).getParentWidget(),
                    link,
                    WorkspaceEvent.BLOCKS_CONNECTED));
            workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).repaintBlock();
            workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).repaint();
            workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).moveConnectedBlocks();
            workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).repaintBlock();
            workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).repaint();
          }
        }
      }
    } else if (validConnection(block.getAfterConnector())) {
      parentID = block.getAfterBlockID();
    }

    // remove form widget and container
    this.removeChildrenBlock(renderable, widget, container);

    //		<<<<<<<<<<<<<<<<<<<<<<<<<<==========================
    //		<<<<<<<<<<<<<<<<<<<<<<<<<<focus changing, coming out TODO
    //		<<<<<<<<<<<<<<<<<<<<<<<<<<==========================
    // If the deleted block had a parent, give the parent the focus,
    // Otherwise, give the focus to the canvas (NOT BLOCK CANVAS)
    if (invalidBlockID(parentID)) {
      this.focusManager.setFocus(location, Block.NULL);
      this.blockCanvas.getCanvas().requestFocus();
      return;
    } else {
      this.focusManager.setFocus(parentID);
      this.blockCanvas.getCanvas().requestFocus();
      return;
    }
  }