001/*
002 * Copyright 2006 - 2013
003 *     Stefan Balev     <stefan.balev@graphstream-project.org>
004 *     Julien Baudry    <julien.baudry@graphstream-project.org>
005 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
006 *     Yoann Pigné      <yoann.pigne@graphstream-project.org>
007 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
008 * 
009 * This file is part of GraphStream <http://graphstream-project.org>.
010 * 
011 * GraphStream is a library whose purpose is to handle static or dynamic
012 * graph, create them from scratch, file or any source and display them.
013 * 
014 * This program is free software distributed under the terms of two licenses, the
015 * CeCILL-C license that fits European law, and the GNU Lesser General Public
016 * License. You can  use, modify and/ or redistribute the software under the terms
017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
019 * the Free Software Foundation, either version 3 of the License, or (at your
020 * option) any later version.
021 * 
022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
024 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Lesser General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * 
029 * The fact that you are presently reading this means that you have had
030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
031 */
032package org.graphstream.ui.swingViewer.util;
033
034import java.awt.event.MouseEvent;
035import java.util.ArrayList;
036
037import org.graphstream.graph.Node;
038import org.graphstream.ui.graphicGraph.GraphicElement;
039import org.graphstream.ui.graphicGraph.GraphicGraph;
040import org.graphstream.ui.graphicGraph.GraphicSprite;
041import org.graphstream.ui.swingViewer.View;
042
043public class DefaultMouseManager implements MouseManager {
044        // Attribute
045
046        /**
047         * The view this manager operates upon.
048         */
049        protected View view;
050
051        /**
052         * The graph to modify according to the view actions.
053         */
054        protected GraphicGraph graph;
055
056        // Construction
057
058        public void init(GraphicGraph graph, View view) {
059                this.view = view;
060                this.graph = graph;
061                view.addMouseListener(this);
062                view.addMouseMotionListener(this);
063        }
064        
065        public void release() {
066                view.removeMouseListener(this);
067                view.removeMouseMotionListener(this);
068        }
069
070        // Command
071
072        protected void mouseButtonPress(MouseEvent event) {
073                view.requestFocus();
074
075                // Unselect all.
076
077                if (!event.isShiftDown()) {
078                        for (Node node : graph) {
079                                if (node.hasAttribute("ui.selected"))
080                                        node.removeAttribute("ui.selected");
081                        }
082
083                        for (GraphicSprite sprite : graph.spriteSet()) {
084                                if (sprite.hasAttribute("ui.selected"))
085                                        sprite.removeAttribute("ui.selected");
086                        }
087                }
088        }
089
090        protected void mouseButtonRelease(MouseEvent event,
091                        ArrayList<GraphicElement> elementsInArea) {
092                for (GraphicElement element : elementsInArea) {
093                        if (!element.hasAttribute("ui.selected"))
094                                element.addAttribute("ui.selected");
095                }
096        }
097
098        protected void mouseButtonPressOnElement(GraphicElement element,
099                        MouseEvent event) {
100                view.freezeElement(element, true);
101                if (event.getButton() == 3) {
102                        element.addAttribute("ui.selected");
103                } else {
104                        element.addAttribute("ui.clicked");
105                }
106        }
107
108        protected void elementMoving(GraphicElement element, MouseEvent event) {
109                view.moveElementAtPx(element, event.getX(), event.getY());
110        }
111
112        protected void mouseButtonReleaseOffElement(GraphicElement element,
113                        MouseEvent event) {
114                view.freezeElement(element, false);
115                if (event.getButton() != 3) {
116                        element.removeAttribute("ui.clicked");
117                } else {
118                }
119        }
120
121        // Mouse Listener
122
123        protected GraphicElement curElement;
124
125        protected float x1, y1;
126
127        public void mouseClicked(MouseEvent event) {
128                // NOP
129        }
130
131        public void mousePressed(MouseEvent event) {
132                curElement = view.findNodeOrSpriteAt(event.getX(), event.getY());
133
134                if (curElement != null) {
135                        mouseButtonPressOnElement(curElement, event);
136                } else {
137                        x1 = event.getX();
138                        y1 = event.getY();
139                        mouseButtonPress(event);
140                        view.beginSelectionAt(x1, y1);
141                }
142        }
143
144        public void mouseDragged(MouseEvent event) {
145                if (curElement != null) {
146                        elementMoving(curElement, event);
147                } else {
148                        view.selectionGrowsAt(event.getX(), event.getY());
149                }
150        }
151
152        public void mouseReleased(MouseEvent event) {
153                if (curElement != null) {
154                        mouseButtonReleaseOffElement(curElement, event);
155                        curElement = null;
156                } else {
157                        float x2 = event.getX();
158                        float y2 = event.getY();
159                        float t;
160
161                        if (x1 > x2) {
162                                t = x1;
163                                x1 = x2;
164                                x2 = t;
165                        }
166                        if (y1 > y2) {
167                                t = y1;
168                                y1 = y2;
169                                y2 = t;
170                        }
171
172                        mouseButtonRelease(event, view.allNodesOrSpritesIn(x1, y1, x2, y2));
173                        view.endSelectionAt(x2, y2);
174                }
175        }
176
177        public void mouseEntered(MouseEvent event) {
178                // NOP
179        }
180
181        public void mouseExited(MouseEvent event) {
182                // NOP
183        }
184
185        public void mouseMoved(MouseEvent e) {
186        }
187}