/**
  * @param block - the textual block from which a new RenderableBlock will be constructed
  * @requires
  * @modifies nothing
  * @effects none
  * @return new RenderableBlock instance from the TextualFactoryBlock or null if not possible.
  */
 private RenderableBlock createRenderableBlock(TextualFactoryBlock block) {
   // if textual wrapper is null, return a null instance of RenderableBlock.
   if (block == null) {
     return null;
   }
   // if FactoryBlock wrapped in textual wrapper is invalid, return null RenderableBlock instance.
   if (block.getfactoryBlock() == null
       || block.getfactoryBlock().getBlockID().equals(Block.NULL)) {
     return null;
   }
   // create and get the RenderableBloc instance associated with the Textual wrapper's FactoryBlock
   RenderableBlock createdRB = block.getfactoryBlock().createNewInstance();
   // if the above instance of RenderableBlock is invalid (null or points to null)
   // then DO NOT insert a new block, DO NOT change the focus.
   if (createdRB == null || isNullBlockInstance(createdRB.getBlockID())) {
     throw new RuntimeException(
         "Invariant Violated:" + "May not drop null instances of Renderable Blocks");
   }
   // Please keep the above check rep because it does not
   // make any sense to have an exisitn valid
   // FactoryRenderableBlock point to some non-existing
   // block.  In other words, why would you have a factory
   // that churns out invalid products?
   return createdRB;
 }
示例#2
0
 public int compare(TextualFactoryBlock t1, TextualFactoryBlock t2) {
   if (t1.compareTo(t2) == 0) {
     return 0;
   }
   if (t1.toString().toLowerCase().indexOf(keyword)
       == t2.toString().toLowerCase().indexOf(keyword)) {
     return t1.compareTo(t2);
   }
   return t1.toString().toLowerCase().indexOf(keyword)
           > t2.toString().toLowerCase().indexOf(keyword)
       ? 1
       : -1;
 }
示例#3
0
  /**
   * @param workspace The workspace in use
   * @param keyword
   * @requires keyword != null
   * @return List of TextualFactoryBlocks, {T}, such that: T.toString contains keyword T == null if
   *     no matching blocks were found T.toString is unique for each T
   */
  public static List<TextualFactoryBlock> getAllMatchingBlocks(
      Workspace workspace, String keyword) {
    // Use Set such that we don't get any repeats
    Set<TextualFactoryBlock> matchingBlocks =
        new TreeSet<TextualFactoryBlock>(new MatchingComparator(keyword));

    // find all FactoryRenderableBlocks and check for a match
    for (RenderableBlock renderable : workspace.getFactoryManager().getBlocks()) {

      // TODO: don't assume they're all FactoryRenderableBlocks!  Collisions aren't...
      if (renderable == null
          || renderable.getBlockID().equals(Block.NULL)
          || !(renderable instanceof FactoryRenderableBlock)) {
        continue;
      }

      // first, check if query matches block keyword
      if (renderable.getKeyword().toLowerCase().contains(keyword.toLowerCase())) {
        matchingBlocks.add(
            new TextualFactoryBlock(
                (FactoryRenderableBlock) renderable, renderable.getBlock().getBlockLabel()));
      }

      // grabs the quote block needed TODO: needs to be independent!
      if (keyword.startsWith("\"")
          && renderable.getBlock().getGenusName().equalsIgnoreCase("string")) {
        String[] quote = keyword.split("\"");
        // makes sure that there is text after the " so that it can be placed onto the block
        if (quote.length > 1) {
          matchingBlocks.add(
              new TextualFactoryBlock((FactoryRenderableBlock) renderable, "\"" + quote[1] + "\""));
        }
      } // otherwise, if the keyword is too long, check to see if
      // the user is trying to type extra info for disambiguation
      else if (keyword.length() > renderable.getKeyword().length()) {
        if (disambiguousStringRep((FactoryRenderableBlock) renderable)
            .toLowerCase()
            .contains(keyword.toLowerCase())) {
          matchingBlocks.add(
              new TextualFactoryBlock(
                  (FactoryRenderableBlock) renderable,
                  disambiguousStringRep((FactoryRenderableBlock) renderable)));
        }
      }

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

    }

    /* if blocks have the same labels, the search results will be ambiguous.
     * the following expands the string representation of the TFB if needed
     * to disambiguate the blocks. */
    ArrayList<TextualFactoryBlock> disambiguatedMatches =
        new ArrayList<TextualFactoryBlock>(matchingBlocks);
    TextualFactoryBlock t1, t2;
    for (int i = 0; i < disambiguatedMatches.size(); i++) {
      t1 = disambiguatedMatches.get(i);
      if (i > 0) {
        t2 = disambiguatedMatches.get(i - 1);
        if (t1.toString().equals(t2.toString())) {
          disambiguatedMatches.set(
              i,
              new TextualFactoryBlock(
                  t1.getfactoryBlock(), disambiguousStringRep(t1.getfactoryBlock())));
          disambiguatedMatches.set(
              i - 1,
              new TextualFactoryBlock(
                  t2.getfactoryBlock(), disambiguousStringRep(t2.getfactoryBlock())));
        }
      }
      if (i < disambiguatedMatches.size() - 1) {
        t2 = disambiguatedMatches.get(i + 1);
        if (t1.toString().equals(t2.toString())) {
          disambiguatedMatches.set(
              i,
              new TextualFactoryBlock(
                  t1.getfactoryBlock(), disambiguousStringRep(t1.getfactoryBlock())));
          disambiguatedMatches.set(
              i + 1,
              new TextualFactoryBlock(
                  t2.getfactoryBlock(), disambiguousStringRep(t2.getfactoryBlock())));
        }
      }
    }
    // List<TextualFactoryBlock> f = new ArrayList<TextualFactoryBlock>();
    return disambiguatedMatches;
  }