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.graph.ui;
005    
006    
007    import graphlab.graph.GraphUtils;
008    import graphlab.graph.event.GraphEvent;
009    import graphlab.graph.graph.AbstractGraphRenderer;
010    import graphlab.graph.graph.PaintHandler;
011    import graphlab.graph.old.AcceleratedRenderer;
012    import graphlab.graph.old.GStroke;
013    import graphlab.platform.core.BlackBoard;
014    import graphlab.platform.core.Listener;
015    
016    import java.awt.*;
017    
018    /**
019     * @author azin azadi
020     * @email
021     */
022    public abstract class GraphRectRegionSelect implements Listener, PaintHandler<AbstractGraphRenderer> {
023        public Rectangle getCurrentRect() {
024            return rect;
025        }
026    
027        protected Rectangle rect = new Rectangle(0, 0, 0, 0);
028        protected int x;
029        protected int y;
030        protected AbstractGraphRenderer gv;
031        protected int xx;
032        protected int yy;
033        private BlackBoard blackboard;
034        public static boolean isSelecting = false;
035    
036        public GraphRectRegionSelect(BlackBoard bb) {
037            this.blackboard = bb;
038        }
039    
040        /**
041         * starts the process of selecting a0 rectangular region by the user on the graph
042         * this will listen for press the mouse button and drag it on the graph
043         * this will finished whenever the mouse released
044         */
045        public void startSelectingRegion() {
046            blackboard.addListener(GraphEvent.EVENT_KEY, this);
047            //other things will be done on doJob
048        }
049    
050        GraphEvent gdrag, gdrop, gmove;
051        boolean dragStarted = false;
052    
053        public void keyChanged(String eventKey, Object value) {
054            gv = blackboard.getData(AbstractGraphRenderer.EVENT_KEY);
055            if (eventKey.equals(GraphEvent.EVENT_KEY)) {
056                GraphEvent ge = blackboard.getData(GraphEvent.EVENT_KEY);
057                if (ge.eventType == GraphEvent.DRAGGING_STARTED) {
058                    gv.addPostPaintHandler(this);
059                    gdrag = blackboard.getData(GraphEvent.EVENT_KEY);
060                    drag();
061                    dragStarted = true;
062                    isSelecting = true;
063                }
064                if (dragStarted && ge.eventType == GraphEvent.DRAGGING) {
065                    gmove = blackboard.getData(GraphEvent.EVENT_KEY);
066                    mouseMove();
067                }
068                if (dragStarted && ge.eventType == GraphEvent.DROPPED) {
069                    isSelecting = false;
070                    gdrop = blackboard.getData(GraphEvent.EVENT_KEY);
071                    blackboard.removeListener(GraphEvent.EVENT_KEY, this);
072                    gv.removePaintHandler(this);
073    //                if (!dragStarted) {
074    //                    rect = new Rectangle((int) gdrop.mousePos.getX(), (int) gdrop.mousePos.getY(), 300, 300);
075    //                }
076                    dragStarted = false;
077                    drop();
078                }
079            }
080        }
081    
082        abstract public void onMouseMoved(GraphEvent data);
083    
084        abstract public void onDrop(GraphEvent data);
085    
086        private void mouseMove() {
087            xx = (int) gmove.mousePos.x;
088            yy = (int) gmove.mousePos.y;
089            int dx = xx - x;
090            int dy = yy - y;
091            int _x = x;
092            int _y = y;
093            if (dx < 0) {
094                dx *= -1;
095                _x -= dx;
096            }
097            if (dy < 0) {
098                dy *= -1;
099                _y -= dy;
100            }
101            rect.setBounds(_x, _y, dx, dy);
102            if (gv != null) {
103                if (!(gv instanceof AcceleratedRenderer))
104                    gv.repaint();
105            }
106            onMouseMoved(gmove);
107        }
108    
109        private void drag() {
110            x = (int) gdrag.mousePos.x;
111            y = (int) gdrag.mousePos.y;
112            rect = new Rectangle(x, y, 300, 300);
113        }
114    
115        private void drop() {
116            onDrop(gdrop);
117            gv.repaint();
118        }
119    
120        public void paint(Graphics g, Object destinationComponent, Boolean drawExtras) {
121            if (!drawExtras)
122                return;
123            double zoomFactor = gv.getGraph().getZoomFactor();
124            Graphics2D gg = (Graphics2D) g;
125            gg.setStroke(GStroke.dashed.stroke);
126            gg.setColor(Color.DARK_GRAY);
127            Rectangle _rect = GraphUtils.createViewRectangle(gv.getGraph(), rect);
128            g.drawRoundRect(_rect.x, _rect.y, _rect.width, _rect.height, 5, 5);
129        }
130    
131        public boolean isEnable() {
132            return true;
133        }
134    }