?? workflow_graphed.java
字號:
// Remove Groups from Model (Without Children)
graph.getGraphLayoutCache().remove(groups.toArray());
// Select Children
graph.setSelectionCells(children.toArray());
}
}
// Determines if a Cell is a Group
public boolean isGroup(Object cell) {
// Map the Cell to its View
CellView view = graph.getGraphLayoutCache().getMapping(cell, false);
if (view != null) {
return!view.isLeaf();
}
return false;
}
// Brings the Specified Cells to Front
public void toFront(Object[] c) {
graph.getGraphLayoutCache().toFront(c);
}
// Sends the Specified Cells to Back
public void toBack(Object[] c) {
graph.getGraphLayoutCache().toBack(c);
}
// Undo the last Change to the Model or the View
public void undo() {
try {
undoManager.undo(graph.getGraphLayoutCache());
}
catch (Exception ex) {
System.err.println(ex);
}
finally {
updateHistoryButtons();
}
}
// Redo the last Change to the Model or the View
public void redo() {
try {
undoManager.redo(graph.getGraphLayoutCache());
}
catch (Exception ex) {
System.err.println(ex);
}
finally {
updateHistoryButtons();
}
}
// Update Undo/Redo Button State based on Undo Manager
protected void updateHistoryButtons() {
// The View Argument Defines the Context
undo.setEnabled(undoManager.canUndo(graph.getGraphLayoutCache()));
redo.setEnabled(undoManager.canRedo(graph.getGraphLayoutCache()));
}
//
// Listeners
//
// From GraphSelectionListener Interface
public void valueChanged(GraphSelectionEvent e) {
// Group Button only Enabled if more than One Cell Selected
group.setEnabled(graph.getSelectionCount() > 1);
// Update Button States based on Current Selection
boolean enabled = !graph.isSelectionEmpty();
remove.setEnabled(enabled);
ungroup.setEnabled(enabled);
tofront.setEnabled(enabled);
toback.setEnabled(enabled);
copy.setEnabled(enabled);
cut.setEnabled(enabled);
}
//
// KeyListener for Delete KeyStroke
//
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
// Listen for Delete Key Press
if (e.getKeyCode() == KeyEvent.VK_DELETE) {
// Execute Remove Action on Delete Key Press
remove.actionPerformed(null);
}
}
//
// Custom Graph
//
// Defines a Graph that uses the Shift-Button (Instead of the Right
// Mouse Button, which is Default) to add/remove point to/from an edge.
public class MyGraph
extends JGraph {
// Construct the Graph using the Model as its Data Source
public MyGraph(GraphModel model) {
super(model);
// Use a Custom Marquee Handler
setMarqueeHandler(new MyMarqueeHandler());
// Tell the Graph to Select new Cells upon Insertion
setSelectNewCells(true);
// Make Ports Visible by Default
setPortsVisible(true);
// Use the Grid (but don't make it Visible)
setGridEnabled(true);
// Set the Grid Size to 10 Pixel
setGridSize(6);
// Set the Tolerance to 2 Pixel
setTolerance(2);
// Accept edits if click on background
setInvokesStopCellEditing(true);
}
// Override Superclass Method to Return Custom EdgeView
protected EdgeView createEdgeView(JGraph graph, CellMapper cm, Object cell) {
// Return Custom EdgeView
return new EdgeView(cell, graph, cm) {
/**
* Returns a cell handle for the view.
*/
public CellHandle getHandle(GraphContext context) {
return new MyEdgeHandle(this, context);
}
};
}
}
//
// Custom Edge Handle
//
// Defines a EdgeHandle that uses the Shift-Button (Instead of the Right
// Mouse Button, which is Default) to add/remove point to/from an edge.
public class MyEdgeHandle
extends EdgeView.EdgeHandle {
/**
* @param edge
* @param ctx
*/
public MyEdgeHandle(EdgeView edge, GraphContext ctx) {
super(edge, ctx);
}
// Override Superclass Method
public boolean isAddPointEvent(MouseEvent event) {
// Points are Added using Shift-Click
return event.isShiftDown();
}
// Override Superclass Method
public boolean isRemovePointEvent(MouseEvent event) {
// Points are Removed using Shift-Click
return event.isShiftDown();
}
}
//
// Custom Model
//
// A Custom Model that does not allow Self-References
public static class MyModel
extends DefaultGraphModel {
// Override Superclass Method
public boolean acceptsSource(Object edge, Object port) {
// Source only Valid if not Equal Target
return ( ( (Edge) edge).getTarget() != port);
}
// Override Superclass Method
public boolean acceptsTarget(Object edge, Object port) {
// Target only Valid if not Equal Source
return ( ( (Edge) edge).getSource() != port);
}
}
//
// Custom MarqueeHandler
// MarqueeHandler that Connects Vertices and Displays PopupMenus
public class MyMarqueeHandler
extends BasicMarqueeHandler {
// Holds the Start and the Current Point
protected Point2D start, current;
// Holds the First and the Current Port
protected PortView port, firstPort;
// Override to Gain Control (for PopupMenu and ConnectMode)
public boolean isForceMarqueeEvent(MouseEvent e) {
if (e.isShiftDown()) {
return false;
}
// If Right Mouse Button we want to Display the PopupMenu
if (SwingUtilities.isRightMouseButton(e)) {
// Return Immediately
return true;
}
// Find and Remember Port
port = getSourcePortAt(e.getPoint());
// If Port Found and in ConnectMode (=Ports Visible)
if (port != null && graph.isPortsVisible()) {
return true;
}
// Else Call Superclass
return super.isForceMarqueeEvent(e);
}
// Display PopupMenu or Remember Start Location and First Port
public void mousePressed(final MouseEvent e) {
// If Right Mouse Button
if (SwingUtilities.isRightMouseButton(e)) {
// Scale From Screen to Model
Point2D loc = graph.fromScreen( (Point2D) e.getPoint().clone());
// Find Cell in Model Coordinates
Object cell = graph.getFirstCellForLocation(e.getX(), e.getY());
// Create PopupMenu for the Cell
JPopupMenu menu = createPopupMenu(e.getPoint(), cell);
// Display PopupMenu
menu.show(graph, e.getX(), e.getY());
// Else if in ConnectMode and Remembered Port is Valid
}
else if (port != null && graph.isPortsVisible()) {
// Remember Start Location
start = graph.toScreen(port.getLocation(null));
// Remember First Port
firstPort = port;
}
else {
// Call Superclass
super.mousePressed(e);
}
}
// Find Port under Mouse and Repaint Connector
public void mouseDragged(MouseEvent e) {
// If remembered Start Point is Valid
if (start != null) {
// Fetch Graphics from Graph
Graphics g = graph.getGraphics();
// Xor-Paint the old Connector (Hide old Connector)
paintConnector(Color.black, graph.getBackground(), g);
// Reset Remembered Port
port = getTargetPortAt(e.getPoint());
// If Port was found then Point to Port Location
if (port != null) {
current = graph.toScreen(port.getLocation(null));
// Else If no Port was found then Point to Mouse Location
}
else {
current = graph.snap(e.getPoint());
// Xor-Paint the new Connector
}
paintConnector(graph.getBackground(), Color.black, g);
}
// Call Superclass
super.mouseDragged(e);
}
public PortView getSourcePortAt(Point2D point) {
// Scale from Screen to Model
Point2D tmp = graph.fromScreen( (Point2D) point.clone());
// Find a Port View in Model Coordinates and Remember
return graph.getPortViewAt(tmp.getX(), tmp.getY());
}
// Find a Cell at point and Return its first Port as a PortView
protected PortView getTargetPortAt(Point2D point) {
// Find Cell at point (No scaling needed here)
Object cell = graph.getFirstCellForLocation(point.getX(), point.getY());
// Loop Children to find PortView
for (int i = 0; i < graph.getModel().getChildCount(cell); i++) {
// Get Child from Model
Object tmp = graph.getModel().getChild(cell, i);
// Get View for Child using the Graph's View as a Cell Mapper
tmp = graph.getGraphLayoutCache().getMapping(tmp, false);
// If Child View is a Port View and not equal to First Port
if (tmp instanceof PortView && tmp != firstPort) {
// Return as PortView
return (PortView) tmp;
}
}
// No Port View found
return getSourcePortAt(point);
}
// Connect the First Port and the Current Port in the Graph or Repaint
public void mouseReleased(MouseEvent e) {
// If Valid Event, Current and First Port
if (e != null
&& port != null
&& firstPort != null
&& firstPort != port) {
// Then Establish Connection
connect( (Port) firstPort.getCell(), (Port) port.getCell());
// Else Repaint the Graph
}
else {
graph.repaint();
// Reset Global Vars
}
firstPort = port = null;
start = current = null;
// Call Superclass
super.mouseReleased(e);
}
// Show Special Cursor if Over Port
public void mouseMoved(MouseEvent e) {
// Check Mode and Find Port
if (e != null
&& getSourcePortAt(e.getPoint()) != null
&& graph.isPortsVisible()) {
// Set Cusor on Graph (Automatically Reset)
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);
}
}
// Use Xor-Mode on Graphics to Paint Connector
protected void paintConnector(Color fg, Color bg, Graphics g) {
// Set Foreground
g.setColor(fg);
// Set Xor-Mode Color
g.setXORMode(bg);
// Highlight the Current Port
paintPort(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());
}
}
// Use the Preview Flag to Draw a Highlighted Port
protected void paintPort(Graphics g) {
// If Current Port is Valid
if (port != null) {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -