Skip to content

Instantly share code, notes, and snippets.

@ghostandthemachine
Created February 27, 2010 16:03
Show Gist options
  • Save ghostandthemachine/316787 to your computer and use it in GitHub Desktop.
Save ghostandthemachine/316787 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ofvisual.node;
import org.netbeans.api.visual.action.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import ofvisual.node.PortGroup.PortType;
import ofvisual.scene.VisualScene;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.anchor.AnchorFactory;
import org.netbeans.api.visual.border.Border;
import org.netbeans.api.visual.border.BorderFactory;
import org.netbeans.api.visual.widget.ConnectionWidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;
/**
*
* @author Jon
*/
public class PortInteractor extends Widget {
VisualScene parentScene;
private int size = 0;
private Border lineBorder = BorderFactory.createLineBorder(1, Color.BLACK);
private WidgetAction hoverAction = ActionFactory.createHoverAction(new MyHoverProvider());
private WidgetAction moveAction = ActionFactory.createMoveAction();
private WidgetAction zoomAction = ActionFactory.createZoomAction();
private WidgetAction panAction = ActionFactory.createPanAction();
private PortType portType;
public PortInteractor(VisualScene p, int s, PortType type) {
super(p);
portType = type;
size = s;
parentScene = p;
if (portType == PortType.OUTPUT) {
getActions().addAction(ActionFactory.createConnectAction(parentScene.getInteractionLayer(), new ConnectProvider() {
private String source = null;
private String target = null;
public boolean isSourceWidget(Widget sourceWidget) {
Object object = parentScene.findObject(sourceWidget);
System.out.println(sourceWidget);
return true;
}
public ConnectorState isTargetWidget(Widget sourceWidget, Widget targetWidget) {
Object object = parentScene.findObject(targetWidget);
System.out.println(sourceWidget);
return ConnectorState.ACCEPT;
}
public boolean hasCustomTargetWidgetResolver(Scene scene) {
if(VisualScene.currentPort.getPortType() == PortType.INPUT){
return false;
}
return true;
}
public Widget resolveTargetWidget(Scene scene, Point sceneLocation) {
return null;
}
public void createConnection(Widget sourceWidget, Widget targetWidget) {
ConnectionWidget conn = new ConnectionWidget(parentScene);
conn.setSourceAnchor(AnchorFactory.createRectangularAnchor(sourceWidget));
conn.setTargetAnchor(AnchorFactory.createRectangularAnchor(targetWidget));
parentScene.getConnectionLayer().addChild(conn);
}
}));
} else {
getActions().addAction(ActionFactory.createConnectAction(parentScene.getInteractionLayer(), new ConnectProvider() {
private String source = null;
private String target = null;
public boolean isSourceWidget(Widget sourceWidget) {
Object object = parentScene.findObject(sourceWidget);
source = parentScene.isNode(object) ? (String) object : null;
return source != null;
}
public ConnectorState isTargetWidget(Widget sourceWidget, Widget targetWidget) {
Object object = parentScene.findObject(targetWidget);
target = parentScene.isNode(object) ? (String) object : null;
if (target != null) {
return !source.equals(target) ? ConnectorState.ACCEPT : ConnectorState.REJECT_AND_STOP;
}
return object != null ? ConnectorState.REJECT_AND_STOP : ConnectorState.REJECT;
}
public boolean hasCustomTargetWidgetResolver(Scene scene) {
VisualScene s = (VisualScene) scene;
Port current = s.getCurrentPort();
return false;
}
public Widget resolveTargetWidget(Scene scene, Point sceneLocation) {
return null;
}
public void createConnection(Widget sourceWidget, Widget targetWidget) {
ConnectionWidget conn = new ConnectionWidget(parentScene);
conn.setSourceAnchor(AnchorFactory.createRectangularAnchor(sourceWidget));
conn.setTargetAnchor(AnchorFactory.createRectangularAnchor(targetWidget));
parentScene.getConnectionLayer().addChild(conn);
}
}));
}
getActions().addAction(hoverAction);
}
@Override
protected Rectangle calculateClientArea() {
return new Rectangle(-size, -size, 2 * size + 1, 2 * size + 1);
}
@Override
protected void paintWidget() {
Graphics2D g = getGraphics();
g.setColor(this.getForeground());
g.setStroke(new BasicStroke(2f));
g.drawOval(-size, -size, 2 * size, 2 * size);
g.setStroke(new BasicStroke(1f));
}
// public Color getHighlightPaint() {
// return highlightPaint;
// }
//
// public void setHighlightPaint(Color highlightPaint) {
// this.highlightPaint = highlightPaint;
// }
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
private PortType getPortType() {
return portType;
}
private static class MyHoverProvider implements TwoStateHoverProvider {
public void unsetHovering(Widget widget) {
widget.setBackground(new Color(0, 0, 0, 0));
widget.setForeground(new Color(0, 0, 0, 0));
VisualScene scene = (VisualScene) widget.getParentWidget().getParentWidget().getParentWidget().getParentWidget();
scene.setCurrentPort(null);
System.out.println("sjberr");
}
public void setHovering(Widget widget) {
widget.setBackground(new Color(0, 0, 0, 0));
widget.setForeground(new Color(200, 255, 200));
VisualScene.setCurrentPort(widget.getParentWidget());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment