/** @overrides WorkspaceWidget.addBlock() */ @Override public void addBlock(RenderableBlock block) { // update parent widget if dropped block WorkspaceWidget oldParent = block.getParentWidget(); if (oldParent != this) { if (oldParent != null) { oldParent.removeBlock(block); if (block.hasComment()) { block.getComment().getParent().remove(block.getComment()); } } block.setParentWidget(this); if (block.hasComment()) { block.getComment().setParent(block.getParentWidget().getJComponent()); } } this.getRBParent().addToBlockLayer(block); block.setHighlightParent(this.getRBParent()); // if block has page labels enabled, in other words, if it can, then set page label to this if (workspace.getEnv().getBlock(block.getBlockID()).isPageLabelSetByPage()) { workspace.getEnv().getBlock(block.getBlockID()).setPageLabel(this.getPageName()); } // notify block to link default args if it has any block.linkDefArgs(); // fire to workspace that block was added to canvas if oldParent != this if (oldParent != this) { workspace.notifyListeners( new WorkspaceEvent(workspace, oldParent, block.getBlockID(), WorkspaceEvent.BLOCK_MOVED)); workspace.notifyListeners( new WorkspaceEvent( workspace, this, block.getBlockID(), WorkspaceEvent.BLOCK_ADDED, true)); } // if the block is off the edge, shift everything or grow as needed to fully show it this.reformBlockPosition(block); this.pageJComponent.setComponentZOrder(block, 0); }
/** * @modifies this.miniPixelWidth * @effects sets the minimumPixelWidth such that the following condition holds: * DEFAULT_MINIMUMWIDTH < new minimumPixelWidth && for each block, b, in this page's set of * blocks { b.x+b.width < new minimumPixelWidth} */ public void reformMinimumPixelWidth() { minimumPixelWidth = 0; // reset min to 0 // loop through blocks, growing min to fit each block for (RenderableBlock b : this.getBlocks()) { if (b.getX() + b.getWidth() + b.getHighlightStrokeWidth() / 2 > minimumPixelWidth) { // increase min width to fit this block minimumPixelWidth = b.getX() + b.getWidth() + b.getHighlightStrokeWidth() / 2 + 1; } if (b.hasComment()) { if (b.getComment().getX() + b.getComment().getWidth() > minimumPixelWidth) { // increase min width to fit this block minimumPixelWidth = b.getComment().getX() + b.getComment().getWidth() + 1; } } } if (this.minimumPixelWidth < Page.DEFAULT_MINUMUM_WIDTH * zoom) { this.minimumPixelWidth = (int) (Page.DEFAULT_MINUMUM_WIDTH * zoom); } }
/** * @param block - the new block being added whose position must be revalidated * @requires block != null * @modifies block.location or this page's abstract width * @effects shifts this block into the page or increases the width of this page to fit the new * block. It must then notify listeners that the page's size may have changed */ public void reformBlockPosition(RenderableBlock block) { // move blocks in Point p = SwingUtilities.convertPoint(block.getParent(), block.getLocation(), this.pageJComponent); if (p.x < block.getHighlightStrokeWidth() / 2 + 1) { block.setLocation(block.getHighlightStrokeWidth() / 2 + 1, p.y); block.moveConnectedBlocks(); // the block has moved, so update p p = SwingUtilities.convertPoint(block.getParent(), block.getLocation(), this.pageJComponent); } else if (p.x + block.getWidth() + block.getHighlightStrokeWidth() / 2 + 1 > this.pageJComponent.getWidth()) { this.setPixelWidth(p.x + block.getWidth() + block.getHighlightStrokeWidth() / 2 + 1); } if (p.y < block.getHighlightStrokeWidth() / 2 + 1) { block.setLocation(p.x, block.getHighlightStrokeWidth() / 2 + 1); block.moveConnectedBlocks(); } else if (p.y + block.getStackBounds().height + block.getHighlightStrokeWidth() / 2 + 1 > this.pageJComponent.getHeight()) { block.setLocation( p.x, this.pageJComponent.getHeight() - block.getStackBounds().height - block.getHighlightStrokeWidth() / 2 + 1); block.moveConnectedBlocks(); } if (block.hasComment()) { // p = SwingUtilities.convertPoint(block.getComment().getParent(), // block.getComment().getLocation(), this.pageJComponent); p = block.getComment().getLocation(); if (p.x + block.getComment().getWidth() + 1 > this.pageJComponent.getWidth()) { this.setPixelWidth(p.x + block.getComment().getWidth() + 1); } } // repaint all pages PageChangeEventManager.notifyListeners(); }