View Javadoc

1   /*
2    * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3    * California All rights reserved.
4    *
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or http://jung.sourceforge.net/license.txt for a description.
7    *
8    * Created on Jul 7, 2005
9    */
10  
11  package edu.uci.ics.jung.visualization.control;
12  
13  import java.awt.event.MouseEvent;
14  import java.awt.event.MouseListener;
15  import java.awt.event.MouseMotionListener;
16  import java.awt.event.MouseWheelEvent;
17  import java.awt.event.MouseWheelListener;
18  import java.util.LinkedHashSet;
19  import java.util.Set;
20  
21  import edu.uci.ics.jung.visualization.VisualizationViewer;
22  
23  /**
24   * a GraphMouse that accepts plugins for various mouse events.
25   * 
26   * @author Tom Nelson 
27   *
28   *
29   */
30  public class PluggableGraphMouse implements VisualizationViewer.GraphMouse {
31  
32      MouseListener[] mouseListeners;
33      MouseMotionListener[] mouseMotionListeners;
34      MouseWheelListener[] mouseWheelListeners;
35      Set<GraphMousePlugin> mousePluginList = new LinkedHashSet<GraphMousePlugin>();
36      Set<MouseMotionListener> mouseMotionPluginList = new LinkedHashSet<MouseMotionListener>();
37      Set<MouseWheelListener> mouseWheelPluginList = new LinkedHashSet<MouseWheelListener>();
38  
39      public void add(GraphMousePlugin plugin) {
40          if(plugin instanceof MouseListener) {
41              mousePluginList.add(plugin);
42              mouseListeners = null;
43          }
44          if(plugin instanceof MouseMotionListener) {
45              mouseMotionPluginList.add((MouseMotionListener)plugin);
46              mouseMotionListeners = null;
47          }
48          if(plugin instanceof MouseWheelListener) {
49              mouseWheelPluginList.add((MouseWheelListener)plugin);
50              mouseWheelListeners = null;
51          }
52      }
53  
54      public void remove(GraphMousePlugin plugin) {
55          if(plugin instanceof MouseListener) {
56              boolean wasThere = mousePluginList.remove(plugin);
57              if(wasThere) mouseListeners = null;
58          }
59          if(plugin instanceof MouseMotionListener) {
60              boolean wasThere = mouseMotionPluginList.remove(plugin);
61              if(wasThere) mouseMotionListeners = null;
62          }
63          if(plugin instanceof MouseWheelListener) {
64              boolean wasThere = mouseWheelPluginList.remove(plugin);
65              if(wasThere) mouseWheelListeners = null;
66          }
67      }
68      
69      private void checkMouseListeners() {
70          if(mouseListeners == null) {
71              mouseListeners = (MouseListener[])
72              mousePluginList.toArray(new MouseListener[mousePluginList.size()]);
73          }
74      }
75      
76      private void checkMouseMotionListeners() {
77          if(mouseMotionListeners == null){
78              mouseMotionListeners = (MouseMotionListener[])
79              mouseMotionPluginList.toArray(new MouseMotionListener[mouseMotionPluginList.size()]);
80          } 
81      }
82      
83      private void checkMouseWheelListeners() {
84          if(mouseWheelListeners == null) {
85              mouseWheelListeners = (MouseWheelListener[])
86              mouseWheelPluginList.toArray(new MouseWheelListener[mouseWheelPluginList.size()]);
87          }
88      }
89  
90      public void mouseClicked(MouseEvent e) {
91          checkMouseListeners();
92          for(int i=0; i<mouseListeners.length; i++) {
93              mouseListeners[i].mouseClicked(e);
94              if(e.isConsumed()) break;
95          }
96      }
97      
98      public void mousePressed(MouseEvent e) {
99          checkMouseListeners();
100         for(int i=0; i<mouseListeners.length; i++) {
101             mouseListeners[i].mousePressed(e);
102             if(e.isConsumed()) break;
103         }
104     }
105     
106     public void mouseReleased(MouseEvent e) {
107         checkMouseListeners();
108         for(int i=0; i<mouseListeners.length; i++) {
109             mouseListeners[i].mouseReleased(e);
110             if(e.isConsumed()) break;
111         }
112     }
113     
114     public void mouseEntered(MouseEvent e) {
115         checkMouseListeners();
116         for(int i=0; i<mouseListeners.length; i++) {
117             mouseListeners[i].mouseEntered(e);
118             if(e.isConsumed()) break;
119         }
120     }
121     
122     public void mouseExited(MouseEvent e) {
123         checkMouseListeners();
124         for(int i=0; i<mouseListeners.length; i++) {
125             mouseListeners[i].mouseExited(e);
126             if(e.isConsumed()) break;
127         }
128     }
129     
130     public void mouseDragged(MouseEvent e) {
131         checkMouseMotionListeners();
132         for(int i=0; i<mouseMotionListeners.length; i++) {
133             mouseMotionListeners[i].mouseDragged(e);
134             if(e.isConsumed()) break;
135         }
136     }
137     
138     public void mouseMoved(MouseEvent e) {
139         checkMouseMotionListeners();
140         for(int i=0; i<mouseMotionListeners.length; i++) {
141             mouseMotionListeners[i].mouseMoved(e);
142             if(e.isConsumed()) break;
143         }
144     }
145     
146     public void mouseWheelMoved(MouseWheelEvent e) {
147         checkMouseWheelListeners();
148         for(int i=0; i<mouseWheelListeners.length; i++) {
149             mouseWheelListeners[i].mouseWheelMoved(e);
150             if(e.isConsumed()) break;
151         }
152     }
153 }