Beispiel #1
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;
 }
Beispiel #2
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;
  }