Exemple #1
0
  /**
   * Returns a new RenderableBlock instance with the matching genusName. New block will also have
   * matching label is label is not-null. May return null.
   *
   * @param workspace The workspace to use
   * @param genusName
   * @param label
   * @requires if block associated with genusName has a non editable or unique block label, then
   *     "label" MUST BE NULL.
   * @return A new RenderableBlock with matching genusName and label (if label is not-null). If no
   *     matching blocks were found, return null.
   */
  public static RenderableBlock getBlock(Workspace workspace, String genusName, String label) {
    if (genusName == null) {
      return null;
    }

    //		find all blocks on the page and look for any match
    for (Block block : workspace.getBlocks()) {
      // make sure we're not dealing with null blocks
      if (block == null || block.getBlockID() == null || block.getBlockID().equals(Block.NULL)) {
        continue;
      }
      // find the block with matching genus and either a matching label or an editable label
      if (block.getGenusName().equals(genusName)
          && (block.isLabelEditable() || block.getBlockLabel().equals(label) || block.isInfix())) {
        // for block stubs, need to make sure that the label matches because stubs of the same kind
        // (i.e. global var getters, agent var setters, etc.) have the same genusName
        // but stubs of different parents do not share the same label
        if (block instanceof BlockStub && !block.getBlockLabel().equals(label)) {
          continue;
        }
        // create new renderable block instance
        RenderableBlock renderable = BlockUtilities.cloneBlock(block);
        // make sure renderable block is not a null instance of a block
        if (renderable == null || renderable.getBlockID().equals(Block.NULL)) {
          throw new RuntimeException(
              "Invariant Violated: a valid non null blockID just"
                  + "returned a null instance of RenderableBlock");
          // please throw an exception here because it wouldn't make any sense
          // if the Block is valid but it's associated RenderableBlock is not
        }
        // do not drop down default arguments
        renderable.ignoreDefaultArguments();
        // get corresponding block
        Block newblock = Block.getBlock(renderable.getBlockID());
        // make sure corresponding block is not a null instance of block
        if (newblock == null || newblock.getBlockID().equals(Block.NULL)) {
          throw new RuntimeException(
              "Invariant Violated: a valid non null blockID just"
                  + "returned a null instance of Block");
          // please throw an exception here because it wouldn't make any sense
          // if the Block is valid but it's associated RenderableBlock is not
        }
        // attempt to set the label text if possible as defined by the specs
        // should not set the labels of block stubs because their labels are determined by their
        // parent
        if ((block.isLabelEditable() || block.getBlockLabel().equals(label))) {
          if (label != null && !(block instanceof BlockStub)) {
            if (newblock.isLabelEditable() && !newblock.labelMustBeUnique()) {
              newblock.setBlockLabel(label);
            }
          }
        }
        // return renderable block
        return renderable;
      }

      /////////////////////////////////////
      // TODO: Add code here for nicknames//
      /////////////////////////////////////

    }
    // TODO: the part below is a hack. If there are other types of blocks, we need to account for
    // them
    return null;
  }