/** * @param workspace * @param digits * @requires digits != null * @return List containing a number TextualFactoryBlock and any other blocks containing the * numbers */ public static List<TextualFactoryBlock> getDigits(Workspace workspace, String digits) { Set<TextualFactoryBlock> matchingBlocks = new TreeSet<TextualFactoryBlock>(new MatchingComparator(digits)); // looks through the factory blocks for (RenderableBlock renderable : workspace.getFactoryManager().getBlocks()) { if (renderable == null || renderable.getBlockID().equals(Block.NULL) || !(renderable instanceof FactoryRenderableBlock)) { continue; } // TODO genus name are based from TNG, need to figure out a workaround // selects the number block if (renderable.getBlock().getGenusName().equalsIgnoreCase("number")) { matchingBlocks.add(new TextualFactoryBlock((FactoryRenderableBlock) renderable, digits)); } // selects any other block that contains the number (for variables that contains the number) if (renderable.getKeyword().toLowerCase().contains(digits.toLowerCase())) { matchingBlocks.add( new TextualFactoryBlock( (FactoryRenderableBlock) renderable, renderable.getBlock().getBlockLabel())); } } return new ArrayList<TextualFactoryBlock>(matchingBlocks); }
/** * @param plus * @requires plus != null * @return List containing the two "+" TextualFactoryBlocks and any other blocks containing "+" */ public static List<TextualFactoryBlock> getPlusBlocks(Workspace workspace, String plus) { Set<TextualFactoryBlock> matchingBlocks = new HashSet<TextualFactoryBlock>(); // looks through the factory blocks for (RenderableBlock renderable : workspace.getFactoryManager().getBlocks()) { if (renderable == null || renderable.getBlockID().equals(Block.NULL) || !(renderable instanceof FactoryRenderableBlock)) { continue; } // TODO genus names are based from TNG, need to figure out a workaround // grabs the "+" number and text blocks if (renderable.getBlock().getGenusName().equalsIgnoreCase("sum")) { // changes the label so that the search result will not be ambiguous matchingBlocks.add( new TextualFactoryBlock((FactoryRenderableBlock) renderable, "+ [number]")); } if (renderable.getBlock().getGenusName().equalsIgnoreCase("string-append")) { // changes the label so that the search result will not be ambiguous matchingBlocks.add( new TextualFactoryBlock((FactoryRenderableBlock) renderable, "+ [text]")); } // selects any other block that contains the number (for variables that contains the number) if (renderable.getKeyword().toLowerCase().contains(plus.toLowerCase())) { matchingBlocks.add( new TextualFactoryBlock( (FactoryRenderableBlock) renderable, renderable.getBlock().getBlockLabel())); } } return new ArrayList<TextualFactoryBlock>(matchingBlocks); }
/** setup current visual state of button */ public void update() { RenderableBlock rb = workspace.getEnv().getRenderableBlock(getBlockID()); if (rb != null) { int x = 5; int y = 7; if (rb.getBlock().isCommandBlock()) { y -= 2; x -= 3; } if (rb.getBlock().isDataBlock() || rb.getBlock().isFunctionBlock()) { x += 6; y -= 2; } if (rb.getBlock().isInfix() && rb.getBlock().getSocketAt(0) != null) { if (!rb.getBlock().getSocketAt(0).hasBlock()) { x += 30; } else { if (rb.getSocketSpaceDimension(rb.getBlock().getSocketAt(0)) != null) { x += rb.getSocketSpaceDimension(rb.getBlock().getSocketAt(0)).width + 2; } } y += 2; x += 1; } x = rb.rescale(x); y = rb.rescale(y); setLocation(x, y); setSize(rb.rescale(14), rb.rescale(14)); if (isActive()) { setText("?"); this.setForeground(new Color(255, 255, 0)); } else { setText("?"); this.setForeground(Color.lightGray); } rb.setComponentZOrder(this, 0); } }
public static boolean isLabelValid(Block block, String label) { if (block == null || label == null) { return false; } else if (block.labelMustBeUnique()) { Workspace workspace = block.getWorkspace(); // search through the current block instances active in the workspace for (RenderableBlock rb : workspace.getRenderableBlocksFromGenus(block.getGenusName())) { if (label.equals(rb.getBlock().getBlockLabel())) { return false; } } } // either label doesn't have to be unique or // label was found to be unique in the search return true; }
/** * Creates a string representation for the given RenderableBlock that is disambiguated from string * representations for blocks with the same label by appending socket information to the end of * the block's label. The created string is in the form: BlockLabel [socketLabel1, ..., * socketLabelN] * * @param block the FactoryRenderableBlock to create a string representation of * @return a String containing the given block's keyword with a list of its socket labels appended * to the end. */ public static String disambiguousStringRep(RenderableBlock block) { String rep = block.getKeyword(); String genus = block.getGenus(); Iterator<BlockConnector> sockets = block.getBlock().getSockets().iterator(); if (sockets.hasNext()) { String socketLabels = " ["; while (sockets.hasNext()) { if (socketLabels.length() > 2) { socketLabels += ", "; } socketLabels += sockets.next().getLabel(); } socketLabels += "]"; // HACK!!! TODO: rewriting typeblocking if (genus.equals("sum")) { return rep + " [number]"; } else if (genus.equals("string-append")) { return rep + " [text]"; } return rep + socketLabels; } return rep; }
/** * @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; }