001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 package graphlab.plugins.main.core.actions.vertex; 005 006 import graphlab.graph.atributeset.GraphAttrSet; 007 import graphlab.graph.event.VertexEvent; 008 import graphlab.graph.graph.GraphModel; 009 import graphlab.graph.graph.GraphPoint; 010 import graphlab.graph.graph.SubGraph; 011 import graphlab.graph.graph.VertexModel; 012 import graphlab.platform.core.AbstractAction; 013 import graphlab.platform.core.BlackBoard; 014 import graphlab.plugins.commonplugin.undo.Undoable; 015 import graphlab.plugins.commonplugin.undo.UndoableActionOccuredData; 016 import graphlab.plugins.main.select.Select; 017 018 /** 019 * @author Ruzbeh 020 */ 021 public class VertexMoveEvent extends AbstractAction implements Undoable { 022 public VertexMoveEvent(BlackBoard bb) { 023 super(bb); 024 // listen4Event(VertexDropData.event); 025 // listen4Event(VertexMouseDraggingData.event); 026 listen4Event(VertexEvent.EVENT_KEY); 027 } 028 029 GraphPoint oldPos; 030 double x1; 031 double y1; 032 GraphModel g; 033 034 public void performAction(String eventName, Object value) { 035 g = blackboard.getData(GraphAttrSet.name); 036 VertexEvent ve = blackboard.getData(VertexEvent.EVENT_KEY); 037 //while resizing the vertex it shouldn't move it 038 if (ve.eventType == VertexEvent.DRAGGING_STARTED) { 039 vdd = blackboard.getData(VertexEvent.EVENT_KEY); 040 drag(); 041 } 042 if (ve.eventType == VertexEvent.DROPPED) { 043 vdrod = blackboard.getData(VertexEvent.EVENT_KEY); 044 drop(); 045 } else if (ve.eventType == VertexEvent.DRAGGING) { 046 dragging(); 047 } 048 } 049 050 //it preserves the vertex selection consistant. if the vertex was selected before move it should be after and vice versa 051 private boolean isVertexSelected; 052 053 private void dragging() { 054 VertexEvent vmd = blackboard.getData(VertexEvent.EVENT_KEY); 055 GraphPoint _ = new GraphPoint(vmd.mousePos); 056 GraphPoint loc = vmd.v.getLocation(); 057 _.x += loc.x - x1; 058 _.y += loc.y - y1; 059 vmd.v.setLocation(_); 060 //System.out.println(vmd.me.getX() - x1); 061 } 062 063 VertexEvent vdrod; 064 065 private void drop() { 066 VertexMoveData vmd = new VertexMoveData(); 067 vmd.v = vdd.v; 068 GraphPoint _ = new GraphPoint(vdrod.mousePos); 069 GraphPoint loc = vmd.v.getLocation(); 070 _.x += vdrod.mousePos.x + loc.x - x1; 071 _.y += vdrod.mousePos.y + loc.y - y1; 072 vmd.newPosition = _; 073 blackboard.setData(VertexMoveData.EVENT_KEY, vmd); 074 addUndoData(g, vmd.v, oldPos, vmd); 075 //it negatives the status of the vertex so , it will be negatived cause a vertex click will be fired after and the selection status will be negatived again, it's just a hack! 076 //todo: the hack may cause problems for some one working with selections 077 SubGraph SubGraph = Select.getSelection(blackboard); 078 if (vmd.newPosition.x != oldPos.x && vmd.newPosition.y != oldPos.y) { 079 if (isVertexSelected) { 080 SubGraph.vertices.remove(vmd.v); 081 } else 082 SubGraph.vertices.add(vmd.v); 083 blackboard.setData(Select.EVENT_KEY, SubGraph); 084 } 085 086 } 087 088 VertexEvent vdd; 089 090 private void drag() { 091 oldPos = vdd.v.getLocation();// new Point(vdd.v.view.getX(), vdd.v.view.getY()); 092 //store the original position 093 x1 = vdd.mousePos.x; 094 y1 = vdd.mousePos.y; 095 isVertexSelected = ((SubGraph) Select.getSelection(blackboard)).vertices.contains(vdd.v); 096 } 097 098 private void addUndoData(GraphModel g, VertexModel v1, GraphPoint oldPos, VertexMoveData vmd) { 099 UndoableActionOccuredData uaod = new UndoableActionOccuredData(this); 100 uaod.properties.put("MovedVertex", v1); 101 uaod.properties.put("OldLocation", oldPos); 102 // uaod.properties.put("OldX", oldPos.x); 103 // uaod.properties.put("OldY", oldPos.y); 104 uaod.properties.put("NewPosition", vmd.newPosition); 105 // uaod.properties.put("NewY", vmd.newY); 106 uaod.properties.put("Graph", g); 107 blackboard.setData(UndoableActionOccuredData.EVENT_KEY, uaod); 108 } 109 110 public void undo(UndoableActionOccuredData uaod) { 111 //To change body of implemented methods use File | Settings | File Templates. 112 } 113 114 public void redo(UndoableActionOccuredData uaod) { 115 //To change body of implemented methods use File | Settings | File Templates. 116 } 117 } 118