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.select;
005    
006    import graphlab.graph.event.GraphEvent;
007    import graphlab.graph.event.VertexEvent;
008    import graphlab.graph.graph.*;
009    import graphlab.library.exceptions.InvalidVertexException;
010    import graphlab.platform.core.AbstractAction;
011    import graphlab.platform.core.BlackBoard;
012    import graphlab.plugins.commonplugin.undo.Undoable;
013    import graphlab.plugins.commonplugin.undo.UndoableActionOccuredData;
014    import graphlab.plugins.main.GraphData;
015    
016    /**
017     * @author azin azadi
018     */
019    public class MoveSelected extends AbstractAction implements Undoable {
020        public static final String SELECTION_MOVED = "Selection Moved";
021        public static final String SELECTION_MOVING = "Selection Moving";
022    
023        /**
024         * constructor
025         *
026         * @param bb the blackboard of the action
027         */
028        public MoveSelected(BlackBoard bb) {
029            super(bb);
030            listen4Event(VertexEvent.EVENT_KEY);
031            listen4Event(GraphEvent.EVENT_KEY);
032            gd = new GraphData(blackboard);
033        }
034    
035        GraphData gd;
036        private GraphPoint[] verticesPositionsBackUp;
037    
038        public void performAction(String eventName, Object value) {
039            gv = blackboard.getData(AbstractGraphRenderer.EVENT_KEY);
040            //while resizing the vertex it shouldn't move it
041    //resize
042    //        if (b == null || !b) {
043            if (eventName == VertexEvent.EVENT_KEY) {
044    
045                VertexEvent vdd = blackboard.getData(VertexEvent.EVENT_KEY);
046    
047    
048                SubGraph sd = Select.getSelection(blackboard);
049                if (sd.vertices.contains(vdd.v))   //start if the vertex selected
050                {
051                    if (vdd.eventType == VertexEvent.DRAGGING_STARTED) {
052                        verticesPositionsBackUp = new GraphPoint[gd.getGraph().getVerticesCount()];
053                        for (VertexModel _ : gd.getGraph()) {
054                            verticesPositionsBackUp[_.getId()] = _.getLocation();
055                        }
056                        startx = vdd.v.getLocation().x;
057                        starty = vdd.v.getLocation().y;
058                        drag();
059    //            listen4Event(GraphMouseMoveData.event);
060    //todo                        listen4Event(GraphDropData.event);
061    //                        listen4Event(VertexDropData.event);
062                    }
063                    if (vdd.eventType == VertexEvent.RELEASED || vdd.eventType == VertexEvent.DROPPED) {
064                        drop();
065                        //add undo data
066                        GraphPoint[] newPos = new GraphPoint[gd.getGraph().getVerticesCount()];
067                        for (VertexModel _ : gd.getGraph()) {
068                            newPos[_.getId()] = _.getLocation();
069                        }
070    
071                        UndoableActionOccuredData uaod = new UndoableActionOccuredData(this);
072                        uaod.properties.put("oldPositions", verticesPositionsBackUp);
073                        uaod.properties.put("newPositions", newPos);
074                        blackboard.setData(UndoableActionOccuredData.EVENT_KEY, uaod);
075                        blackboard.setData(SELECTION_MOVED, new GraphPoint(vdd.v.getLocation().x - startx, vdd.v.getLocation().y - starty));
076    
077    //                v.view.removeMouseMotionListener(mlistener);
078    //todo                    unListenEvent(VertexMouseDraggingData.event);
079    //                    unListenEvent(GraphDropData.event);
080    //                    unListenEvent(VertexDropData.event);
081                    }
082                    if (vdd.eventType == VertexEvent.DRAGGING) {
083    //                    VertexMouseDraggingData d = blackboard.get(VertexMouseDraggingData.name);
084                        xx = vdd.mousePos.x;
085                        yy = vdd.mousePos.y;
086                        moveVertices();
087                    }
088    
089                }
090            }
091    //        if (eventName ==GraphEvent.name)) {
092    //            drop();
093    //            unListenEvent(GraphMouseMoveData.event);
094    //                v.view.removeMouseMotionListener(mlistener);
095    //todo                unListenEvent(VertexMouseDraggingData.event);
096    //                unListenEvent(GraphDropData.event);
097    //                unListenEvent(VertexDropData.event);
098    //        }
099    
100    //        }
101        }
102    
103        private void mouseMove() {
104            GraphEvent ge = blackboard.getData(GraphEvent.EVENT_KEY);
105    //        GraphMouseMoveData gmmd = blackboard.get(GraphMouseMoveData.name);
106            xx = ge.mousePos.x;
107            yy = ge.mousePos.y;
108            moveVertices();
109        }
110    
111        private void moveVertices() {
112            blackboard.setData("MoveSelected.moving", "yes");
113    //        System.out.println("mm");
114            final SubGraph selection = Select.getSelection(blackboard);
115            final double dx = xx - x;
116            final double dy = yy - y;
117    //        System.out.println(xx + ", " + x + ", " + dx);
118            AbstractGraphRenderer ren = blackboard.getData(AbstractGraphRenderer.EVENT_KEY);
119            ren.ignoreRepaints(new Runnable() {
120                public void run() {
121                    for (VertexModel v : selection.vertices) {
122                        GraphPoint loc = v.getLocation();
123                        v.setLocation(new GraphPoint(loc.x + dx, loc.y + dy));
124                    }
125                }
126            });
127            blackboard.setData(SELECTION_MOVING, new GraphPoint(dx, dy));
128    //        blackboard.setData(SELECTION_MOVING, new GraphPoint(vdd.v.getLocation().x - startx, vdd.v.getLocation().y - starty));
129        }
130    
131        double x;
132        double startx;
133        double y;
134        double starty;
135        AbstractGraphRenderer gv;
136        VertexModel v;
137    //    MouseMotionAdapter mlistener = new MouseMotionAdapter() {
138    //        public synchronized void mouseDragging(MouseEvent e) {
139    //            xx = e.getX();
140    //            yy = e.getY();
141    //            moveVertices();
142    //        }
143    //    };
144    
145        private void drag() {
146            VertexEvent data = blackboard.getData(VertexEvent.EVENT_KEY);
147            x = data.mousePos.x;
148            y = data.mousePos.y;
149    //        startx = x;
150    //        starty = y;
151            v = data.v;
152    //todo        listen4Event(VertexMouseDraggingData.event);
153    //        data.v.view.addMouseMotionListener(mlistener);
154        }
155    
156        private void drop() {
157            blackboard.setData("MoveSelected.moving", "no");
158    //        v.view.removeMouseMotionListener(mlistener);
159    //        GraphDropData data = blackboard.get(GraphDropData.name);
160            AbstractGraphRenderer ren = blackboard.getData(AbstractGraphRenderer.EVENT_KEY);
161            ren.ignoreRepaints(new Runnable() {
162                public void run() {
163                    VertexEvent ve = blackboard.getData(VertexEvent.EVENT_KEY);
164                    int _x = (int) ve.mousePos.getX();
165                    int _y = (int) ve.mousePos.getY();
166                    int dx = (int) (_x - x);
167                    int dy = (int) (_y - y);
168                    x = x + dx;
169                    y = y + dy;
170                    SubGraph selection = Select.getSelection(blackboard);
171                    for (VertexModel v : selection.vertices) {
172                        try {
173                            v.setLocation(new GraphPoint(v.getLocation().x + dx, v.getLocation().y + dy));
174                        } catch (InvalidVertexException e) {
175                            selection.vertices.remove(v);     //as in java any thing is references, there is no need to update the log in the blackboard :D:D
176                            //exception occurs whenever a selected vertex, or edge was deleted by user
177    //                e.printStackTrace();
178                        }
179                    }
180    
181                }
182            });
183    
184    
185        }
186    
187        double xx;
188        double yy;
189    
190        public void undo(UndoableActionOccuredData uaod) {
191            GraphPoint verticesPositionsBackUp[] = (GraphPoint[]) uaod.properties.get("oldPositions");
192            GraphModel g = gd.getGraph();
193            if (g.getVerticesCount() != this.verticesPositionsBackUp.length) {
194                System.err.println("Graph has changed, Undo can not be done");
195                return;
196            }
197    
198            int i = 0;
199            for (VertexModel v : g) {
200                v.setLocation(verticesPositionsBackUp[i++]);
201            }
202    
203        }
204    
205        public void redo(UndoableActionOccuredData uaod) {
206            GraphPoint newPos[] = (GraphPoint[]) uaod.properties.get("newPositions");
207            GraphModel g = gd.getGraph();
208            if (g.getVerticesCount() != this.verticesPositionsBackUp.length) {
209                System.err.println("Graph has changed, Undo can not be done");
210                return;
211            }
212    
213            int i = 0;
214            for (VertexModel v : g) {
215                v.setLocation(newPos[i++]);
216            }
217    
218        }
219    }
220    //
221    //class SelectedVerticesCursorUpdator extends GraphAction {
222    //    /**
223    //     * constructor
224    //     *
225    //     * @param bb the blackboard of the action
226    //     */
227    //    public SelectedVerticesCursorUpdator(blackboard bb) {
228    //        super(bb);
229    //        listen4Event(SelectData.event);
230    //    }
231    //
232    //    /**
233    //     * determines the cursors that the vertices has before changing their cursors,
234    //     * this also determines the vertices whose cursors changed
235    //     */
236    //    HashMap<Vertex, Cursor> lastCursors = new HashMap<Vertex, Cursor>();
237    //
238    //    public void performJob(GraphData graphData) {
239    //        HashSet<Vertex> selectedVertices = graphData.select.getSelectedVertices();
240    //        for (Vertex v : lastCursors.keySet()) {
241    //            if (!selectedVertices.contains(v)) {
242    ////                v.view.setCursor(lastCursors.get(v));
243    //                // last cursors had some problems, so i didn;t use it temporarily
244    //                v.view.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
245    ////                System.out.println("cleared");
246    //            }
247    //        }
248    //        lastCursors.clear();
249    //        for (Vertex v : selectedVertices) {
250    ////            lastCursors.put(v, v.view.getCursor());
251    //            v.view.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
252    ////            System.out.println("setted");
253    //        }
254    //    }
255    //}