use of edu.mit.blocks.codeblocks.BlockConnector in project openblocks by mikaelhg.
the class RenderableBlock method linkDefArgs.
/**
* Links the default arguments of this block if it has any and if this block has not already linked its
* default args in this session. Re-linking this block's default args everytime it gets dropped/moved
* within the block canvas can get annoying.
*/
public void linkDefArgs() {
if (!linkedDefArgsBefore && getBlock().hasDefaultArgs()) {
Iterator<Long> ids = getBlock().linkAllDefaultArgs().iterator();
Iterator<BlockConnector> sockets = getBlock().getSockets().iterator();
Long id;
BlockConnector socket;
// Store the ids, sockets, and blocks we need to update.
List<Long> idList = new ArrayList<Long>();
List<BlockConnector> socketList = new ArrayList<BlockConnector>();
List<RenderableBlock> argList = new ArrayList<RenderableBlock>();
while (ids.hasNext() && sockets.hasNext()) {
id = ids.next();
socket = sockets.next();
if (id != Block.NULL) {
// for each block id, create a new RenderableBlock
RenderableBlock arg = new RenderableBlock(workspace, this.getParentWidget(), id);
arg.setZoomLevel(this.zoom);
// getParentWidget().addBlock(arg);
// arg.repaint();
// this.getParent().add(arg);
// set the location of the def arg at
Point myLocation = getLocation();
Point2D socketPt = getSocketPixelPoint(socket);
Point2D plugPt = arg.getSocketPixelPoint(arg.getBlock().getPlug());
arg.setLocation((int) (socketPt.getX() + myLocation.x - plugPt.getX()), (int) (socketPt.getY() + myLocation.y - plugPt.getY()));
// update the socket space of at this socket
this.getConnectorTag(socket).setDimension(new Dimension(arg.getBlockWidth() - (int) BlockConnectorShape.NORMAL_DATA_PLUG_WIDTH, arg.getBlockHeight()));
// drop each block to this parent's widget/component
// getParentWidget().blockDropped(arg);
getParentWidget().addBlock(arg);
idList.add(id);
socketList.add(socket);
argList.add(arg);
}
}
int size = idList.size();
for (int i = 0; i < size; i++) {
workspace.notifyListeners(new WorkspaceEvent(workspace, this.getParentWidget(), argList.get(i).getBlockID(), WorkspaceEvent.BLOCK_ADDED, true));
// must call this method to update the dimensions of this
// TODO ria in the future would be good to just link the default args
// but first creating a block link object and then connecting
// something like notifying the renderableblock to update its dimensions will be
// take care of
this.blockConnected(socketList.get(i), idList.get(i));
argList.get(i).repaint();
}
this.redrawFromTop();
linkedDefArgsBefore = true;
}
}
use of edu.mit.blocks.codeblocks.BlockConnector in project openblocks by mikaelhg.
the class RenderableBlock method drag.
private void drag(RenderableBlock renderable, int dx, int dy, WorkspaceWidget widget, boolean isTopLevelBlock) {
if (!renderable.pickedUp) {
throw new RuntimeException("dragging without prior pickup");
}
// mark this as being dragged
renderable.dragging = true;
// move the block by drag amount
if (!isTopLevelBlock) {
renderable.setLocation(renderable.getX() + dx, renderable.getY() + dy);
}
// send blockEntered/blockExited/blogDragged as appropriate
if (widget != null) {
if (!widget.equals(renderable.lastDragWidget)) {
widget.blockEntered(renderable);
if (renderable.lastDragWidget != null) {
renderable.lastDragWidget.blockExited(renderable);
}
}
widget.blockDragged(renderable);
renderable.lastDragWidget = widget;
}
// translate highlight along with the block - this would happen automatically,
// but putting the call here takes out any lag.
renderable.highlighter.repaint();
// Propagate the drag event to anything plugged into this block
for (BlockConnector socket : BlockLinkChecker.getSocketEquivalents(renderable.getBlock())) {
if (socket.hasBlock()) {
drag(workspace.getEnv().getRenderableBlock(socket.getBlockID()), dx, dy, widget, false);
}
}
}
use of edu.mit.blocks.codeblocks.BlockConnector 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.codeblocks.BlockConnector in project openblocks by mikaelhg.
the class RenderableBlock method reformBlockShape.
/**
* Reforms the blockShape of this renderableBlock and saves it into the blockArea while
* updating the bounds of this RenderableBlock. Used to update the shape and socket
* positions while avoiding a full updateBuffImg.
*/
private void reformBlockShape() {
abstractBlockArea = blockShape.reformArea();
// TODO for zooming, create an AffineTransform to scale the block shape
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
blockArea = abstractBlockArea.createTransformedArea(at);
// note: need to add twice the highlight stroke width so that the highlight does not get cut off
Rectangle updatedDimensionRect = new Rectangle(this.getX(), this.getY(), blockArea.getBounds().width, blockArea.getBounds().height);
if (!this.getBounds().equals(updatedDimensionRect)) {
// bounds have changed, so move connected blocks
moveConnectedBlocks();
}
this.setBounds(updatedDimensionRect);
// ////////////////////////////////////////
if (pageLabel != null && getBlock().hasPageLabel()) {
pageLabel.update();
}
if (blockLabel != null) {
blockLabel.update();
}
if (collapseLabel != null) {
collapseLabel.update();
}
if (comment != null) {
comment.update();
}
for (ConnectorTag tag : socketTags) {
BlockConnector socket = tag.getSocket();
SocketLabel label = tag.getLabel();
if (label == null || SocketLabel.ignoreSocket(socket)) {
continue;
}
label.update(getSocketAbstractPoint(socket));
}
}
use of edu.mit.blocks.codeblocks.BlockConnector in project openblocks by mikaelhg.
the class RenderableBlock method synchronizeSockets.
// /////////////////
// LABEL METHODS //
// /////////////////
/**
* Synchronizes this RenderableBlock's socket components (including tags, labels)
* with the associated Block's list of sockets.
* Complexity: Running time for n Block sockets
* and m Renderable tags: O(m+nm)=O(nm)
* @effects for every socket in Block:
* (1) check/add corresponding tag structure,
* (2) check/add block label
* for every tag in Renderable:
* (1) delete any sockets not in Block
*/
private boolean synchronizeSockets() {
boolean changed = false;
List<ConnectorTag> newSocketTags = new ArrayList<ConnectorTag>();
for (ConnectorTag tag : socketTags) {
if (tag.getLabel() != null) {
this.remove(tag.getLabel().getJComponent());
}
}
for (int i = 0; i < getBlock().getNumSockets(); i++) {
BlockConnector socket = getBlock().getSocketAt(i);
ConnectorTag tag = this.getConnectorTag(socket);
if (tag == null) {
tag = new ConnectorTag(socket);
if (SocketLabel.ignoreSocket(socket)) {
// ignored sockets have no labels
tag.setLabel(null);
} else {
SocketLabel label = new SocketLabel(workspace, socket, socket.getLabel(), BlockLabel.Type.PORT_LABEL, socket.isLabelEditable(), blockID);
String argumentToolTip = getBlock().getArgumentDescription(i);
if (argumentToolTip != null) {
label.setToolTipText(getBlock().getArgumentDescription(i).trim());
}
tag.setLabel(label);
label.setZoomLevel(this.getZoom());
label.setText(socket.getLabel());
this.add(label.getJComponent());
changed = true;
}
} else {
SocketLabel label = tag.getLabel();
if (!SocketLabel.ignoreSocket(socket)) {
// ignored bottom sockets or sockets with label == ""
if (label == null) {
label = new SocketLabel(workspace, socket, socket.getLabel(), BlockLabel.Type.PORT_LABEL, socket.isLabelEditable(), blockID);
String argumentToolTip = getBlock().getArgumentDescription(i);
if (argumentToolTip != null) {
label.setToolTipText(getBlock().getArgumentDescription(i).trim());
}
tag.setLabel(label);
label.setText(socket.getLabel());
this.add(label.getJComponent());
changed = true;
} else {
label.setText(socket.getLabel());
this.add(label.getJComponent());
changed = true;
}
label.setZoomLevel(this.getZoom());
}
}
newSocketTags.add(tag);
}
this.socketTags.clear();
this.socketTags = newSocketTags;
return changed;
}
Aggregations