1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| class MyMarqueeHandler extends BasicMarqueeHandler {
protected Point2D start, current;
protected PortView port, firstPort;
public boolean isForceMarqueeEvent(MouseEvent e) {
port = getSourcePortAt(e.getPoint());
if (port != null && classPanel.graph.isPortsVisible())
return true;
return super.isForceMarqueeEvent(e);
}
public void mousePressed(MouseEvent e) {
// If Right Mouse Button
if (SwingUtilities.isRightMouseButton(e)) {
// Find Cell in Model Coordinates
Object cell = classPanel.graph.getFirstCellForLocation(e.getX(), e.getY());
// Create PopupMenu for the Cell
JPopupMenu menu = createPopupMenu(e.getPoint(), cell);
// Display PopupMenu
menu.show(classPanel.graph, e.getX(), e.getY());
// Else if in ConnectMode and Remembered Port is Valid
} else if (port != null && classPanel.graph.isPortsVisible()) {
// Remember Start Location
start = classPanel.graph.toScreen(port.getLocation());
// Remember First Port
firstPort = port;
} else {
// Call Superclass
super.mousePressed(e);
}
}
public void mouseDragged(MouseEvent e) {
if (start != null) {
Graphics g = classPanel.graph.getGraphics();
PortView newPort = getTargetPortAt(e.getPoint());
if (newPort == null || newPort != port) {
paintConnector(Color.black, classPanel.graph.getBackground(), g);
port = newPort;
if (port != null)
current = classPanel.graph.toScreen(port.getLocation());
else
current = classPanel.graph.snap(e.getPoint());
paintConnector(classPanel.graph.getBackground(), Color.black, g);
}
}
super.mouseDragged(e);
}
public void mouseReleased(MouseEvent e) {
if (e != null && port != null && firstPort != null && firstPort != port) {
DefaultEdge myEdge = new DefaultEdge();
int arrow = GraphConstants.ARROW_CLASSIC;
GraphConstants.setLineEnd(myEdge.getAttributes(), arrow);
GraphConstants.setEndFill(myEdge.getAttributes(), true);
classPanel.graph.getGraphLayoutCache().insertEdge(myEdge,
(DefaultPort)firstPort.getCell(), (DefaultPort)port.getCell());
e.consume();
} else {
classPanel.graph.repaint();
}
firstPort = port = null;
start = current = null;
super.mouseReleased(e);
}
public void mouseMoved(MouseEvent e) {
// Check Mode and Find Port
if (e != null && getSourcePortAt(e.getPoint()) != null
&& classPanel.graph.isPortsVisible()) {
// Set Cusor on Graph (Automatically Reset)
classPanel.graph.setCursor(new Cursor(Cursor.HAND_CURSOR));
// Consume Event
// Note: This is to signal the BasicGraphUI's
// MouseHandle to stop further event processing.
e.consume();
} else
// Call Superclass
super.mouseMoved(e);
}
protected PortView getSourcePortAt(Point point) {
//throw new UnsupportedOperationException("Not yet implemented");
// Disable jumping
classPanel.graph.setJumpToDefaultPort(false);
PortView result;
try {
// Find a Port View in Model Coordinates and Remember
result = classPanel.graph.getPortViewAt(point.getX(), point.getY());
} finally {
classPanel.graph.setJumpToDefaultPort(true);
}
return result;
}
protected PortView getTargetPortAt(Point point) {
// throw new UnsupportedOperationException("Not yet implemented");
return classPanel.graph.getPortViewAt(point.getX(), point.getY());
}
protected void paintConnector(Color fg, Color bg, Graphics g) {
// throw new UnsupportedOperationException("Not yet implemented");
// Set Foreground
g.setColor(fg);
// Set Xor-Mode Color
g.setXORMode(bg);
// Highlight the Current Port
paintPort(classPanel.graph.getGraphics());
// If Valid First Port, Start and Current Point
if (firstPort != null && start != null && current != null)
// Then Draw A Line From Start to Current Point
g.drawLine((int) start.getX(), (int) start.getY(),
(int) current.getX(), (int) current.getY());
}
protected void paintPort(Graphics g) {
//throw new UnsupportedOperationException("Not yet implemented");
// If Current Port is Valid
if (port != null) {
// If Not Floating Port...
boolean o = (GraphConstants.getOffset(port.getAllAttributes()) != null);
// ...Then use Parent's Bounds
Rectangle2D r = (o) ? port.getBounds() : port.getParentView()
.getBounds();
// Scale from Model to Screen
r = classPanel.graph.toScreen((Rectangle2D) r.clone());
// Add Space For the Highlight Border
r.setFrame(r.getX() - 3, r.getY() - 3, r.getWidth() + 6, r
.getHeight() + 6);
// Paint Port in Preview (=Highlight) Mode
classPanel.graph.getUI().paintCell(g, port, r, true);
}
}
} |