[graphl-cvs] graphl/src/org/mediavirus/graphl/interaction Manipulator.java GraphlManipulator.java
Status: Pre-Alpha
Brought to you by:
flo1
From: Flo L. <fl...@us...> - 2004-08-02 12:36:59
|
Update of /cvsroot/graphl/graphl/src/org/mediavirus/graphl/interaction In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31344/src/org/mediavirus/graphl/interaction Added Files: Manipulator.java GraphlManipulator.java Log Message: big refactoring: * AssignableNodeController has been renamed to Facet * many methods renamed accordingly * new package for painters * moved around some classes to match new package structure --- NEW FILE: GraphlManipulator.java --- /* * Created on 31.10.2003 * */ package org.mediavirus.graphl.interaction; import java.awt.Cursor; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.geom.Point2D; import java.util.Collections; import org.mediavirus.graphl.GraphlPane; import org.mediavirus.graphl.graph.*; import org.mediavirus.graphl.graph.rdf.RDFEdge; import org.mediavirus.graphl.graph.rdf.RDFNode; import org.mediavirus.graphl.gui.EdgeContextMenu; import org.mediavirus.graphl.gui.NodeContextMenu; import org.mediavirus.graphl.gui.NodeEditingController; import org.mediavirus.graphl.gui.TextFieldInPlaceNodeEditor; import org.mediavirus.graphl.painter.ArrowEdgePainter; import org.mediavirus.graphl.painter.NodePainter; import org.mediavirus.graphl.view.AbstractManipulator; /** * @author flo * */ public class GraphlManipulator extends AbstractManipulator implements NodeEditingController{ private boolean moved = false; /** The position of the last mouse event. */ protected Point lastPosition; /** The edge that is being dragged. */ protected Point grabPoint; protected boolean oldFixedState; protected Cursor graphCursor; private long lastClick; private Point mousePos; private Node edgeNode; private Edge dragEdge = null; private Node dragNode = null; private double oldLength = 2; Point dragStart = null; private NodeContextMenu nodeMenu = null; private EdgeContextMenu edgeMenu = null; int sequence = 0; private Point2D oldTranslation; boolean doDragNode = true; public GraphlManipulator(){ } public void mousePressed(MouseEvent e) { Point p = new Point(); graphPane.screenToGraphPoint(e.getPoint(),p); Node overNode = graphPane.getNodeAtPoint(p); Edge overEdge = graphPane.getNearestEdge(p); if (e.getModifiers() == MouseEvent.BUTTON1_MASK) { dragStart = e.getPoint(); if (overNode != null) { NodePainter painter = overNode.getCurrentPainter(); if (painter.isEdgeDragPoint(graphPane, overNode, p)) { edgeNode = overNode; e.consume(); } else { doDragNode = true; if (overNode.getCurrentLayouter().isDraggable()) { dragNode = overNode; moved = false; dragNode.setDragging(true); lastPosition=e.getPoint(); Point nodeScreenPoint=graphPane.getScreenPointForNode(dragNode); grabPoint=new Point(p.x-nodeScreenPoint.x,p.y-nodeScreenPoint.y); graphCursor=graphPane.getCursor(); graphPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } } } else if (overEdge != null) { dragEdge = (RDFEdge)overEdge; oldLength = dragEdge.getLength(); e.consume(); } else { oldTranslation = ((GraphlPane)graphPane).getTranslation(); } } else if (e.getModifiers() == MouseEvent.BUTTON3_MASK) { if (overNode != null) { nodeMenu.show(e.getX(), e.getY(), overNode); } else if(overEdge != null) { // TODO (2): stop layout and resume later. (move layouter to graphlpane) (0.5h) edgeMenu.show(e.getX(), e.getY(), overEdge); } } } public void mouseClicked(MouseEvent e) { Point p = new Point(); graphPane.screenToGraphPoint(e.getPoint(),p); Node clickNode = graphPane.getNodeAtPoint(p); RDFEdge clickEdge = (RDFEdge)graphPane.getNearestEdge(p); if ((e.getWhen() - lastClick) < 300) { if ((clickNode == null) && (clickEdge == null)) { clickNode = graphPane.getGraph().createNode(); clickNode.setLocation(p.getX(), p.getY()); ((DefaultGraph)graphPane.getGraph()).addElements(Collections.singleton(clickNode), null); graphPane.getGraph().notifyLayoutUpdated(); } else if (clickNode != null) { TextFieldInPlaceNodeEditor editor = new TextFieldInPlaceNodeEditor(graphPane, clickNode, this); editor.startEditing(); } else if (clickEdge != null) { if ( clickEdge.getCurrentPainter().isPointInLabel(graphPane, clickEdge, p)){ //TODO (2): show in-place editor for edge } else { clickEdge.invert(); graphPane.getGraph().notifyLayoutUpdated(); } } } else { if ((e.getModifiers() & MouseEvent.CTRL_MASK) == 0) { graphPane.getSelection().clear(); graphPane.getGraph().notifyLayoutUpdated(); } if (clickNode != null) { if (graphPane.getSelection().isNodeSelected(clickNode)) { graphPane.getSelection().removeNode(clickNode); } else { graphPane.getSelection().addNode(clickNode); } graphPane.getGraph().notifyLayoutUpdated(); } else if (clickEdge != null) { if (graphPane.getSelection().isEdgeSelected(clickEdge)) { graphPane.getSelection().removeEdge(clickEdge); } else { graphPane.getSelection().addEdge(clickEdge); } graphPane.getGraph().notifyLayoutUpdated(); } } lastClick = e.getWhen(); } public void mouseReleased(MouseEvent e) { if (dragNode!=null) { moveDraggedNode(e.getPoint()); dragNode.setDragging(false); graphPane.setCursor(graphCursor); graphCursor=null; grabPoint=null; lastPosition=null; if (moved) e.consume(); } else if (edgeNode != null) { Point p = new Point(); graphPane.screenToGraphPoint(e.getPoint(),p); Node overNode = graphPane.getNodeAtPoint(p); if (overNode!=null && overNode!=edgeNode) { // we also allow multiple edges between two nodes! RDFEdge edge = new RDFEdge(edgeNode, overNode); ((DefaultGraph)graphPane.getGraph()).addElements(null, Collections.singleton(edge)); } else if ( overNode!=edgeNode ) { Node newNode = graphPane.getGraph().createNode(); newNode.setLocation(p.getX(), p.getY()); Edge edge = graphPane.getGraph().createEdge(edgeNode, newNode); Point d = new Point(); graphPane.screenToGraphPoint(dragStart,d); ((DefaultEdge)edge).setLength(dragStart.distance(d)); ((DefaultGraph)graphPane.getGraph()).addElements(Collections.singleton(newNode), Collections.singleton(edge)); TextFieldInPlaceNodeEditor editor = new TextFieldInPlaceNodeEditor(graphPane, newNode, this); editor.startEditing(); } } dragEdge = null; dragNode = null; edgeNode = null; doDragNode = false; } public void mouseDragged(MouseEvent e) { mousePos = e.getPoint(); Point p = new Point(); graphPane.screenToGraphPoint(mousePos, p); if (doDragNode && dragNode!=null) { autoscroll(e); moveDraggedNode(e.getPoint()); e.consume(); moved = true; } else if (dragEdge != null) { int delta = (int)(mousePos.getY() - dragStart.getY()); // TODO (2) setLength is not in Node interface! -> Edge Manipulators if (oldLength-delta > 2) ((DefaultEdge)dragEdge).setLength(oldLength-delta); else ((DefaultEdge)dragEdge).setLength(2); graphPane.repaint(); e.consume(); } else if (edgeNode != null) { graphPane.repaint(); e.consume(); } else if (!doDragNode && oldTranslation != null){ GraphlPane pane = (GraphlPane)graphPane; Point2D diff = new Point2D.Double(mousePos.getX() - dragStart.getX(),mousePos.getY() - dragStart.getY()); pane.setTranslation(diff.getX()+oldTranslation.getX(), diff.getY()+oldTranslation.getY()); } graphPane.getGraph().notifyLayoutUpdated(); } public void mouseMoved(MouseEvent e) { mousePos = e.getPoint(); } protected void moveDraggedNode(Point point) { if (!point.equals(lastPosition)) { lastPosition.setLocation(point.x,point.y); point.x-=grabPoint.x; point.y-=grabPoint.y; Point2D graphPoint=new Point2D.Double(); graphPane.screenToGraphPoint(point,graphPoint); dragNode.setLocation(graphPoint.getX(),graphPoint.getY()); graphPane.getGraph().notifyLayoutUpdated(); } } public String getName() { return "GraphlManipulator"; } public void paint(Graphics2D g2) { if (edgeNode != null) { if(mousePos==null) return; Point p = new Point((int)edgeNode.getX(), (int)edgeNode.getY()); Point sp = new Point(); graphPane.screenToGraphPoint(mousePos,sp); //TODO (3): use default edge painter (0.5h) //((GraphlPane)m_graphPane).getDefaultEdgePainter().paintEdge() ArrowEdgePainter.paintArrow(g2, (int)p.getX(), (int)p.getY(), (int)sp.getX(), (int)sp.getY()); } } /** * @see de.fzi.wim.guibase.graphview.inplaceedit.NodeEditingController#editingStarted(de.fzi.wim.guibase.graphview.graph.Node) */ public Object editingStarted(Node node) { return ((RDFNode)node).getValue(); } /** * @see de.fzi.wim.guibase.graphview.inplaceedit.NodeEditingController#editingFinished(de.fzi.wim.guibase.graphview.graph.Node, java.lang.Object) */ public boolean editingFinished(Node node, Object value) { if (!((String)value).equals("")) { ((RDFNode)node).setValue((String)value); } return true; } /** * @see de.fzi.wim.guibase.graphview.inplaceedit.NodeEditingController#editingAborted(de.fzi.wim.guibase.graphview.graph.Node) */ public void editingAborted(Node node) { } /* * Overrides @see de.fzi.wim.guibase.graphview.view.Manipulator#setGraphPane(de.fzi.wim.guibase.graphview.view.JGraphPane) */ public void setGraphPane(GraphlPane graphPane) { super.setGraphPane(graphPane); GraphlPane parent = (GraphlPane)graphPane; nodeMenu = new NodeContextMenu(parent); edgeMenu = new EdgeContextMenu(parent); } } --- NEW FILE: Manipulator.java --- package org.mediavirus.graphl.interaction; import java.awt.Graphics2D; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.KeyListener; import java.awt.event.FocusListener; import org.mediavirus.graphl.GraphlPane; /** * A manipulator gets to process UI events in the graph pane. */ public interface Manipulator extends MouseListener,MouseMotionListener,KeyListener,FocusListener { /** * Sets the graph pane to the manipulator. * * @param graphPane the graph pane */ void setGraphPane(GraphlPane graphPane); /** * Returns the manipulator name. This is used to better search manipulators. * * @return the name of the manipulator */ String getName(); /** * Gives the manipulator an opportunity to update the screen. * * @param g the graphics where drawing should be performed */ void paint(Graphics2D g); /** * Notifies the manipulator that the graph pane has been scrolled. */ void notifyGraphPaneScrolled(); } |