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    
005    package graphlab.plugins.main.core.actions.edge;
006    
007    import graphlab.graph.atributeset.GraphAttrSet;
008    import graphlab.graph.event.EdgeEvent;
009    import graphlab.graph.event.GraphEvent;
010    import graphlab.graph.graph.EdgeModel;
011    import graphlab.graph.graph.GraphModel;
012    import graphlab.graph.graph.GraphPoint;
013    import graphlab.graph.graph.PaintHandler;
014    import graphlab.platform.core.AbstractAction;
015    import graphlab.platform.core.BlackBoard;
016    import graphlab.plugins.commonplugin.undo.Undoable;
017    import graphlab.plugins.commonplugin.undo.UndoableActionOccuredData;
018    
019    import java.awt.*;
020    
021    /**
022     * @author Rouzbeh Ebrahimi, Azin Azadi
023     *         Email: ruzbehus@gmail.com
024     */
025    public class DragEdge extends AbstractAction implements PaintHandler, Undoable {
026        /**
027         * constructor
028         *
029         * @param bb the blackboard of the action
030         */
031        public DragEdge(BlackBoard bb) {
032            super(bb);
033            listen4Event(EdgeEvent.EVENT_KEY);
034            listen4Event(GraphEvent.EVENT_KEY);
035    
036        }
037    
038        protected EdgeModel edge;
039        protected GraphModel g;
040        //    protected AbstractGraphRenderer gv;
041        //    protected boolean isClick = false;
042        //    protected boolean isDrag = false;
043        //    protected int x1;
044        //    protected int y1;
045        //    protected int cx;
046        //    protected int cy;
047        private GraphPoint lastPos, firstPos, cpfirstPos;
048        private GraphPoint clickPos;
049    
050    
051        public void performAction(String eventName, Object value) {
052            g = blackboard.getData(GraphAttrSet.name);
053            if (!g.isEdgesCurved())
054                return;
055    //        gv = blackboard.getData(AbstractGraphRenderer.name);
056            if (eventName == EdgeEvent.EVENT_KEY) {
057                EdgeEvent ee = (EdgeEvent) value;
058                edge = ee.e;
059                if (ee.eventType == EdgeEvent.DRAGGING_STARTED) {
060    //                if (ee.mouseBtn == MouseEvent.BUTTON3) {
061                    lastPos = ee.mousePos;
062                    firstPos = ee.mousePos;
063                    cpfirstPos = edge.getCurveControlPoint();
064    //                startPainting(ee.e);
065    //                }
066    //                else {
067    //                    isDrag = false;
068    //                    clickPos = ee.mousePos;
069    //                    isClick = true;
070    //                    startPainting(ee.e);
071    ////                    gv.repaint();
072    //                }
073    
074    
075                } else if (ee.eventType == EdgeEvent.DRAGGING) {
076                    GraphPoint ctrlPnt = edge.getCurveControlPoint();
077                    edge.setCurveControlPoint(
078                            new GraphPoint(ctrlPnt.x - lastPos.x + ee.mousePos.x,
079                                    ctrlPnt.y - lastPos.y + ee.mousePos.y));
080                    lastPos = ee.mousePos;
081    //                gv.repaint();
082    //                isDrag = true;
083    //                isClick = false;
084                } else if (ee.eventType == EdgeEvent.RELEASED) {
085                    GraphPoint ctrlPnt = edge.getCurveControlPoint();
086                    lastPos = ee.mousePos;
087                    GraphPoint newpos = new GraphPoint(ctrlPnt.x + lastPos.x - ee.mousePos.x,
088                            ctrlPnt.y + lastPos.y - ee.mousePos.y);
089                    edge.setCurveControlPoint(
090                            newpos);
091                    addUndoData(edge, cpfirstPos, newpos);
092                }
093    
094            }
095    //        else if (eventName ==GraphEvent.name.getName())) {
096    //            GraphEvent ge = (GraphEvent) value;
097    //            if (ge.eventType == GraphEvent.DROPPED) {
098    //                stopPainting();
099    //            }
100    //
101    //        }
102        }
103    
104        protected void stopPainting() {
105    //        gv.removePaintHandler(this);
106        }
107    
108        protected void startPainting(EdgeModel e) {
109    //
110    //        GraphPoint location = new GraphPoint(e.curve.getCtrlX(), e.curve.getCtrlY());
111    //        Point viewPoint = GraphPoint.createViewPoint(g, location);
112    //        x1 = viewPoint.x;
113    //        y1 = viewPoint.y;
114    ////        listen4Event(VertexMouseDraggingData.event);
115    //        gv.addPostPaintHandler(this);
116        }
117    
118    
119        public void paint(Graphics g, Object destinationComponent, Boolean drawExtras) {
120    //        if (g != null) {
121    //            if (isDrag) {
122    ////                double yDiff = (lastPos.getY() - firstPos.getY());
123    ////                double xDiff = (lastPos.getX() - firstPos.getX());
124    ////                GraphPoint difference=new GraphPoint(xDiff, yDiff);
125    //                Point viewPoint = GraphPoint.createViewPoint(this.g, lastPos);
126    //                Point2D p1 = e1.curve.getP1();
127    //                Point2D p2 = e1.curve.getP2();
128    //                Point2D pMouse = viewPoint;
129    //                Point2D ctrP = e1.curve.getCtrlPt();
130    //                Point2D newCtrlP = new Point2D.Double((Math.signum(pMouse.getX()) * Math.pow(pMouse.getX(), 1) + ctrP.getX()) / 2, (Math.signum(pMouse.getY()) * Math.pow(pMouse.getY(), 1) + ctrP.getY()) / 2);
131    //                e1.curve.setCurve(p1, newCtrlP, p2);
132    //            } else if (isClick) {
133    //                Point viewPoint = GraphPoint.createViewPoint(this.g, clickPos);
134    //                Point2D p1 = e1.curve.getP1();
135    //                Point2D p2 = e1.curve.getP2();
136    //                int ctrlX1 = (int) (p1.getX() + viewPoint.getX()) / 2 + 30;
137    //                int ctrlY1 = 30 + (int) (p1.getY() + viewPoint.getY()) / 2;
138    //                double dividingPointX = viewPoint.getX();
139    //                double dividingPointY = viewPoint.getY();
140    ////                QuadCurve2D leftCurve = new QuadCurve2D.Double(p1.getX(), p1.getY(), ctrlX1, ctrlY1, dividingPointX, dividingPointY);
141    //                int ctrlX2 = (int) (p2.getX() + dividingPointX) / 2 - 30;
142    //                int ctrlY2 = (int) (p2.getY() + dividingPointY) / 2 - 30;
143    ////                QuadCurve2D rightCurve = new QuadCurve2D.Double(dividingPointX, dividingPointY, ctrlX2, ctrlY2,p2.getX(),p2.getY() );
144    //                QuadCurve2D rightCurve = new QuadCurve2D.Double(), leftCurve = new QuadCurve2D.Double();
145    //                e1.curve.subdivide(leftCurve, rightCurve);
146    //                Point2D p = leftCurve.getCtrlPt();
147    //                int gh1 = (int) (p.getX() + 30);
148    //                int gh2 = (int) (p.getY() + 30);
149    //                p = new Point2D.Double(gh1, gh2);
150    //                leftCurve.setCurve(leftCurve.getP1(), p, leftCurve.getP2());
151    //                Point2D p4 = rightCurve.getCtrlPt();
152    //                int fh1 = (int) (p4.getX() - 30);
153    //                int fh2 = (int) (p4.getY() - 30);
154    //                p = new Point2D.Double(fh1, fh2);
155    //                rightCurve.setCurve(rightCurve.getP1(), p, rightCurve.getP2());
156    //                Graphics2D gg = (Graphics2D) g;
157    //                gg.draw(leftCurve);
158    //                g.drawString("*", (int) dividingPointX, (int) dividingPointY);
159    //                gg.draw(rightCurve);
160    ////                stopPainting();
161    //            }
162    //
163    ////                gg.draw(e1.curve);
164    //        }
165    //
166        }
167    
168        protected void addUndoData(EdgeModel e, GraphPoint oldPos, GraphPoint newPos) {
169            UndoableActionOccuredData uaod = new UndoableActionOccuredData(this);
170            uaod.properties.put("Moved Control Point old pos", oldPos);
171            uaod.properties.put("Moved Control Point new pos", newPos);
172            uaod.properties.put("Edge", e);
173            blackboard.setData(UndoableActionOccuredData.EVENT_KEY, uaod);
174        }
175    
176        public void undo(UndoableActionOccuredData uaod) {
177            EdgeModel e = (EdgeModel) uaod.properties.get("Edge");
178            GraphPoint op = (GraphPoint) uaod.properties.get("Moved Control Point old pos");
179            e.setCurveControlPoint(op);
180        }
181    
182        public void redo(UndoableActionOccuredData uaod) {
183            EdgeModel e = (EdgeModel) uaod.properties.get("Edge");
184            GraphPoint np = (GraphPoint) uaod.properties.get("Moved Control Point new pos");
185            e.setCurveControlPoint(np);
186        }
187    }