View Javadoc

1   /*
2    * TouchGraph LLC. Apache-Style Software License
3    *
4    *
5    * Copyright (c) 2001-2002 Alexander Shapiro. All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 1. Redistributions of source code must retain the above copyright
12   *    notice, this list of conditions and the following disclaimer. 
13   *
14   * 2. Redistributions in binary form must reproduce the above copyright
15   *    notice, this list of conditions and the following disclaimer in
16   *    the documentation and/or other materials provided with the
17   *    distribution.
18   *
19   * 3. The end-user documentation included with the redistribution,
20   *    if any, must include the following acknowledgment:  
21   *       "This product includes software developed by 
22   *        TouchGraph LLC (http://www.touchgraph.com/)."
23   *    Alternately, this acknowledgment may appear in the software itself,
24   *    if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The names "TouchGraph" or "TouchGraph LLC" must not be used to endorse 
27   *    or promote products derived from this software without prior written 
28   *    permission.  For written permission, please contact 
29   *    alex@touchgraph.com
30   *
31   * 5. Products derived from this software may not be called "TouchGraph",
32   *    nor may "TouchGraph" appear in their name, without prior written
33   *    permission of alex@touchgraph.com.
34   *
35   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38   * DISCLAIMED.  IN NO EVENT SHALL TOUCHGRAPH OR ITS CONTRIBUTORS BE 
39   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
40   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
41   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
42   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
43   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
44   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
45   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46   * ====================================================================
47   *
48   */
49  
50  package com.touchgraph.graphlayout.interaction;
51  
52  import java.awt.event.*;
53  
54  import javax.swing.*;
55  import javax.swing.event.*;
56  
57  import com.touchgraph.graphlayout.*;
58  
59  /*** GLNavigateUI. User interface for moving around the graph, as opposed
60    * to editing.
61    *   
62    * @author   Alexander Shapiro                                        
63    * @author   Murray Altheim (abstracted GLPanel to TGScrollPane interface)
64    * @version  1.21  $Id: GLNavigateUI.java,v 1.1.1.1 2004/02/06 08:44:07 keesj Exp $
65    */
66  public class GLNavigateUI extends TGUserInterface {
67      
68      GLPanel glPanel;
69      TGPanel tgPanel;    
70      
71      GLNavigateMouseListener ml;
72          
73      TGAbstractDragUI hvDragUI;
74      TGAbstractDragUI rotateDragUI;
75      //TGAbstractDragUI hvRotateDragUI;
76      
77      TGAbstractClickUI hvScrollToCenterUI;
78      DragNodeUI dragNodeUI;
79      LocalityScroll localityScroll;
80      JPopupMenu nodePopup;    
81      JPopupMenu edgePopup;    
82      Node popupNode;
83      Edge popupEdge;
84      
85      public GLNavigateUI( GLPanel glp ) {
86          glPanel = glp;
87          tgPanel = glPanel.getTGPanel();        
88          
89          localityScroll = glPanel.getLocalityScroll();
90          hvDragUI = glPanel.getHVScroll().getHVDragUI();
91          rotateDragUI = glPanel.getRotateScroll().getRotateDragUI();
92          //hvRotateDragUI = new HVRotateDragUI(tgPanel, 
93          //        glPanel.getHVScroll(), glPanel.getRotateScroll());
94          hvScrollToCenterUI = glPanel.getHVScroll().getHVScrollToCenterUI();
95          dragNodeUI = new DragNodeUI(tgPanel);                    
96  
97          ml = new GLNavigateMouseListener();
98          setUpNodePopup();
99          setUpEdgePopup();
100     }
101     
102     public void activate() {        
103         tgPanel.addMouseListener(ml);
104     }
105     
106     public void deactivate() {
107         tgPanel.removeMouseListener(ml);
108     }
109     
110     class GLNavigateMouseListener extends MouseAdapter {
111     
112         public void mousePressed(MouseEvent e) {
113         	if (e.isPopupTrigger()) {
114             	triggerPopup(e);
115             }
116             else {
117             	Node mouseOverN = tgPanel.getMouseOverN();            
118             	if (e.getModifiers() == MouseEvent.BUTTON1_MASK) { 
119                 	if (mouseOverN == null) 
120 	                    hvDragUI.activate(e);
121     	            else 
122         	            dragNodeUI.activate(e);                    
123             	}
124         	}
125         }
126 
127         public void mouseClicked(MouseEvent e) {
128             Node mouseOverN = tgPanel.getMouseOverN();
129             if (e.getModifiers() == MouseEvent.BUTTON1_MASK) { 
130                 if ( mouseOverN != null) {                        
131                         tgPanel.setSelect(mouseOverN);
132                         try {
133                             tgPanel.setLocale(mouseOverN, localityScroll.getLocalityRadius());                        
134                         }
135                         catch (TGException ex) {
136                            System.out.println("Error setting locale");
137                             ex.printStackTrace();
138                         }
139                         //hvScrollToCenterUI.activate(e);                        
140                 }
141             }    
142         }    
143         
144         public void mouseReleased(MouseEvent e) {
145             if (e.isPopupTrigger()) {
146             	triggerPopup(e);
147             }
148         }    
149 
150 		private void triggerPopup(MouseEvent e) {
151     		popupNode = tgPanel.getMouseOverN();
152         	popupEdge = tgPanel.getMouseOverE();
153 	        if (popupNode!=null) {
154         		tgPanel.setMaintainMouseOver(true);
155 				nodePopup.show(e.getComponent(), e.getX(), e.getY());
156 			}
157 			else if (popupEdge!=null) {
158 				tgPanel.setMaintainMouseOver(true);
159             	edgePopup.show(e.getComponent(), e.getX(), e.getY());
160         	}
161         	else {                    
162 	        	glPanel.glPopup.show(e.getComponent(), e.getX(), e.getY());
163         	}
164   		}
165     }
166 	
167 
168     private void setUpNodePopup() {        
169         nodePopup = new JPopupMenu();
170         JMenuItem menuItem;
171         
172         menuItem = new JMenuItem("Expand Node");
173         ActionListener expandAction = new ActionListener() {
174                 public void actionPerformed(ActionEvent e) {
175                     if(popupNode!=null) {
176                         tgPanel.expandNode(popupNode);
177                     }
178                 }
179             };
180             
181         menuItem.addActionListener(expandAction);
182         nodePopup.add(menuItem);
183          
184         menuItem = new JMenuItem("Collapse Node");
185         ActionListener collapseAction = new ActionListener() {
186                 public void actionPerformed(ActionEvent e) {                    
187                     if(popupNode!=null) {
188                         tgPanel.collapseNode(popupNode );
189                     }
190                 }
191             };
192             
193         menuItem.addActionListener(collapseAction);
194         nodePopup.add(menuItem);
195 
196         menuItem = new JMenuItem("Hide Node");
197         ActionListener hideAction = new ActionListener() {
198                 public void actionPerformed(ActionEvent e) {                    
199                     if(popupNode!=null) {
200                         tgPanel.hideNode(popupNode );
201                     }
202                 }
203             };
204             
205         menuItem.addActionListener(hideAction);
206         nodePopup.add(menuItem);
207 
208         nodePopup.addPopupMenuListener(new PopupMenuListener() {
209             public void popupMenuCanceled(PopupMenuEvent e) {}
210             public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
211                 tgPanel.setMaintainMouseOver(false);
212                 tgPanel.setMouseOverN(null);
213                 tgPanel.repaint();        
214             }
215             public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
216         });
217         
218     }
219 
220     private void setUpEdgePopup() {        
221         edgePopup = new JPopupMenu();
222         JMenuItem menuItem;
223                  
224         menuItem = new JMenuItem("Hide Edge");
225         ActionListener hideAction = new ActionListener() {
226                 public void actionPerformed(ActionEvent e) {
227                     if(popupEdge!=null) {
228                         tgPanel.hideEdge(popupEdge);
229                     }
230                 }
231             };
232             
233         menuItem.addActionListener(hideAction);
234         edgePopup.add(menuItem);        
235          
236         edgePopup.addPopupMenuListener(new PopupMenuListener() {
237             public void popupMenuCanceled(PopupMenuEvent e) {}
238             public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
239                 tgPanel.setMaintainMouseOver(false);
240                 tgPanel.setMouseOverE(null);
241                 tgPanel.repaint();        
242             }
243             public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
244         });
245     }
246 
247 } // end com.touchgraph.graphlayout.interaction.GLNavigateUI