use of edu.mit.blocks.workspace.WorkspaceWidget in project openblocks by mikaelhg.
the class RenderableBlock method mouseReleased.
public void mouseReleased(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (pickedUp) {
dragHandler.mouseReleased(e);
// if the block was dragged before...then
if (dragging) {
// look for nearby link opportunities
BlockLink link = getNearbyLink();
WorkspaceWidget widget = null;
// if a suitable link wasn't found, just drop the block
if (link == null) {
widget = lastDragWidget;
stopDragging(this, widget);
} else // otherwise, if a link WAS found...
{
/* Make sure that no matter who's connecting to whom, the block
* that's being dragged gets dropped on the parent widget of the
* block that's already on the canvas.
*/
if (blockID.equals(link.getSocketBlockID())) {
// dragged block is the socket block, so take plug's parent.
widget = workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).getParentWidget();
} else {
// dragged block is the plug block, so take the socket block's parent.
widget = workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).getParentWidget();
}
// drop the block and connect its link
stopDragging(this, widget);
link.connect();
workspace.notifyListeners(new WorkspaceEvent(workspace, widget, link, WorkspaceEvent.BLOCKS_CONNECTED));
workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).moveConnectedBlocks();
}
// set the locations for X and Y based on zoom at 1.0
this.unzoomedX = this.calculateUnzoomedX(this.getX());
this.unzoomedY = this.calculateUnzoomedY(this.getY());
workspace.notifyListeners(new WorkspaceEvent(workspace, widget, link, WorkspaceEvent.BLOCK_MOVED, true));
if (widget instanceof MiniMap) {
workspace.getMiniMap().animateAutoCenter(this);
}
}
}
}
pickedUp = false;
if (e.isPopupTrigger() || SwingUtilities.isRightMouseButton(e) || e.isControlDown()) {
// add context menu at right click location to provide functionality
// for adding new comments and removing comments
PopupMenu popup = ContextMenu.getContextMenuFor(this);
add(popup);
popup.show(this, e.getX(), e.getY());
}
workspace.getMiniMap().repaint();
}
use of edu.mit.blocks.workspace.WorkspaceWidget in project openblocks by mikaelhg.
the class TypeBlockManager method deleteBlockAndChildren.
/**
* @requires the current block with focus must exist with non-null
* ID in a non-null widget with a non-null parent
* @modifies the current block with focus
* @effects removes the current block with focus and children
* from the GUI and destroys the link
* between the block with focus and it's parent
* block if one exist and children blocks
* if it has childrens.
*/
private void deleteBlockAndChildren() {
// Do not delete null block references. Otherwise, get Block and RenderableBlock instances.
if (isNullBlockInstance(focusManager.getFocusBlockID())) {
throw new RuntimeException("TypeBlockManager: deleting a null block references.");
}
Block block = workspace.getEnv().getBlock(focusManager.getFocusBlockID());
RenderableBlock renderable = workspace.getEnv().getRenderableBlock(block.getBlockID());
// get workspace widget associated with current focus
WorkspaceWidget widget = renderable.getParentWidget();
// do not delete block instances in null widgets
if (widget == null) {
throw new RuntimeException("TypeBlockManager: do not delete blocks with no parent widget.");
// return;
}
// get parent container of this graphical representation
Container container = renderable.getParent();
// do not delete block instances in null parents
if (container == null) {
throw new RuntimeException("TypeBlockManager: do not delete blocks with no parent container.");
// return;
}
// get the Block's location on the canvas
Point location = SwingUtilities.convertPoint(renderable, new Point(0, 0), this.blockCanvas.getCanvas());
// for every valid and active connection, disconnect it.
Long parentID = null;
if (validConnection(block.getPlug())) {
parentID = block.getPlugBlockID();
this.disconnectBlock(block, widget);
if (validConnection(block.getAfterConnector())) {
disconnectBlock(workspace.getEnv().getBlock(block.getAfterBlockID()), widget);
}
} else if (validConnection(block.getBeforeConnector())) {
parentID = block.getBeforeBlockID();
BlockConnector parentConnectorToBlock = workspace.getEnv().getBlock(parentID).getConnectorTo(block.getBlockID());
this.disconnectBlock(block, widget);
if (validConnection(block.getAfterConnector())) {
Long afterBlockID = block.getAfterBlockID();
disconnectBlock(workspace.getEnv().getBlock(afterBlockID), widget);
if (parentID != null) {
BlockLink link = BlockLinkChecker.canLink(workspace, workspace.getEnv().getBlock(parentID), workspace.getEnv().getBlock(afterBlockID), parentConnectorToBlock, workspace.getEnv().getBlock(afterBlockID).getBeforeConnector());
if (link != null) {
link.connect();
workspace.notifyListeners(new WorkspaceEvent(workspace, workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).getParentWidget(), link, WorkspaceEvent.BLOCKS_CONNECTED));
workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).repaintBlock();
workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).repaint();
workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).moveConnectedBlocks();
workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).repaintBlock();
workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).repaint();
}
}
}
} else if (validConnection(block.getAfterConnector())) {
parentID = block.getAfterBlockID();
}
// remove form widget and container
this.removeChildrenBlock(renderable, widget, container);
// Otherwise, give the focus to the canvas (NOT BLOCK CANVAS)
if (invalidBlockID(parentID)) {
this.focusManager.setFocus(location, Block.NULL);
this.blockCanvas.getCanvas().requestFocus();
return;
} else {
this.focusManager.setFocus(parentID);
this.blockCanvas.getCanvas().requestFocus();
return;
}
}
use of edu.mit.blocks.workspace.WorkspaceWidget in project openblocks by mikaelhg.
the class TypeBlockManager method pasteStack.
private void pasteStack(BlockNode node) {
// ====================>>>>>>>>>>>>>>>>>>>>>>>>>
if (node == null) {
return;
}
WorkspaceWidget widget = null;
Iterable<WorkspaceWidget> widgets = null;
Point spot = null;
if (invalidBlockID(focusManager.getFocusBlockID())) {
// canvas has focus
Point location = SwingUtilities.convertPoint(this.blockCanvas.getCanvas(), this.focusManager.getCanvasPoint(), workspace);
widget = workspace.getWidgetAt(location);
spot = SwingUtilities.convertPoint(this.blockCanvas.getCanvas(), this.focusManager.getCanvasPoint(), widget.getJComponent());
} else {
RenderableBlock focusRenderable = workspace.getEnv().getRenderableBlock(focusManager.getFocusBlockID());
widget = focusRenderable.getParentWidget();
spot = focusRenderable.getLocation();
}
if (widget == null) {
// TODO: To be examined and fixed, occurs on macs
JOptionPane.showMessageDialog(frame, "Please click somewhere on the canvas first.", "Error", JOptionPane.PLAIN_MESSAGE);
// throw new RuntimeException("Why are we adding a block to a null widget?");
} else {
// checks to see if the copied block still exists
if (BlockUtilities.blockExists(workspace, node)) {
// create mirror block and mirror childrens
spot.translate(10, 10);
RenderableBlock mirror = BlockUtilities.makeRenderable(workspace, node, widget);
mirror.setLocation(spot);
// make sure the childrens are placed correctly
mirror.moveConnectedBlocks();
} else {
// TODO: future version, allow them to paste
JOptionPane.showMessageDialog(frame, "You cannot paste blocks that are currently NOT on the canvas." + "\nThis function will be available in a future version.\n", "Error", JOptionPane.PLAIN_MESSAGE);
}
}
}
use of edu.mit.blocks.workspace.WorkspaceWidget in project openblocks by mikaelhg.
the class RenderableBlock method mouseDragged.
public void mouseDragged(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (pickedUp) {
Point pp = SwingUtilities.convertPoint(this, e.getPoint(), workspace.getMiniMap());
if (workspace.getMiniMap().contains(pp)) {
workspace.getMiniMap().blockDragged(this, e.getPoint());
lastDragWidget = workspace.getMiniMap();
return;
}
// drag this block if appropriate (checks bounds first)
dragHandler.mouseDragged(e);
// Find the widget under the mouse
dragHandler.myLoc.move(getX() + dragHandler.mPressedX, getY() + dragHandler.mPressedY);
Point p = SwingUtilities.convertPoint(this.getParent(), dragHandler.myLoc, workspace);
WorkspaceWidget widget = workspace.getWidgetAt(p);
if (widget == null) {
// not on a workspace widget, cancel dragging
return;
}
// if this is the first call to mouseDragged
if (!dragging) {
Block block = getBlock();
BlockConnector plug = BlockLinkChecker.getPlugEquivalent(block);
if (plug != null && plug.hasBlock()) {
Block parent = workspace.getEnv().getBlock(plug.getBlockID());
BlockConnector socket = parent.getConnectorTo(blockID);
BlockLink link = BlockLink.getBlockLink(workspace, block, parent, plug, socket);
link.disconnect();
// socket is removed internally from block's socket list if socket is expandable
workspace.getEnv().getRenderableBlock(parent.getBlockID()).blockDisconnected(socket);
// NOTIFY WORKSPACE LISTENERS OF DISCONNECTION
workspace.notifyListeners(new WorkspaceEvent(workspace, widget, link, WorkspaceEvent.BLOCKS_DISCONNECTED));
}
startDragging(this, widget);
}
// drag this block and all attached to it
drag(this, dragHandler.dragDX, dragHandler.dragDY, widget, true);
workspace.getMiniMap().repaint();
}
}
}
use of edu.mit.blocks.workspace.WorkspaceWidget in project openblocks by mikaelhg.
the class BlockUtilities method deleteBlock.
public static void deleteBlock(RenderableBlock block) {
block.setLocation(0, 0);
WorkspaceWidget widget = block.getParentWidget();
if (widget != null) {
widget.removeBlock(block);
}
Container parent = block.getParent();
if (parent != null) {
parent.remove(block);
parent.validate();
}
block.setParentWidget(null);
Workspace workspace = block.getWorkspace();
workspace.notifyListeners(new WorkspaceEvent(workspace, widget, block.getBlockID(), WorkspaceEvent.BLOCK_REMOVED));
}
Aggregations