/* * TouchGraph LLC. Apache-Style Software License * * * Copyright (c) 2002 Alexander Shapiro. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by * TouchGraph LLC (http://www.touchgraph.com/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "TouchGraph" or "TouchGraph LLC" must not be used to endorse * or promote products derived from this software without prior written * permission. For written permission, please contact * alex@touchgraph.com * * 5. Products derived from this software may not be called "TouchGraph", * nor may "TouchGraph" appear in their name, without prior written * permission of alex@touchgraph.com. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL TOUCHGRAPH OR ITS CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * */ /* * Modifications by Miika Nurminen (minurmin@cc.jyu.fi) for KeyGraph. * Added keyboard handling. * 31.12.2003. */ /** LBNavigateUI. User interface for moving around the graph, as opposed * to editing. * * @author Alexander Shapiro * @version 1.20 */ package com.touchgraph.linkbrowser; import com.touchgraph.graphlayout.interaction.*; import com.touchgraph.graphlayout.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; public class LBNavigateUI extends TGUserInterface { TGPanel tgPanel; TGLinkBrowser tgLinkBrowser; LBNavigateMouseListener ml; TGAbstractDragUI hvDragUI; TGAbstractClickUI hvScrollToCenterUI; LBNodeHintUI lbNodeHintUI; DragNodeUI dragNodeUI; TGAbstractDragUI rotateDragUI; LocalityScroll localityScroll; JPopupMenu nodePopup; JPopupMenu edgePopup; LBNode popupNode; LBEdge popupEdge; public LBNavigateUI(TGLinkBrowser tglb) { tgLinkBrowser = tglb; tgPanel = tgLinkBrowser.getTGPanel(); localityScroll=tgLinkBrowser.localityScroll; hvDragUI = tgLinkBrowser.hvScroll.getHVDragUI(); rotateDragUI = tgLinkBrowser.rotateScroll.getRotateDragUI(); hvScrollToCenterUI = tgLinkBrowser.hvScroll.getHVScrollToCenterUI(); dragNodeUI = new DragNodeUI(tgPanel); lbNodeHintUI = new LBNodeHintUI(tglb); ml = new LBNavigateMouseListener(); /* for unknown reason, in seems that focuslistener is required to get focus * on mozilla 1.0.2 /mn,1.12.2003 */ tgPanel.addFocusListener(new FocusAdapter() { public void focusGained(FocusEvent evt) { System.out.println("got focus"); } public void focusLost(FocusEvent evt) { System.out.println("lost focus"); } }); setUpNodePopup(); setUpEdgePopup(); } KeyListener kl = new LBNavigateKeyListener(); public void activate() { tgPanel.addMouseListener(ml); tgPanel.addKeyListener(kl); // added requestFocus to receive keybouad events tgPanel.requestFocus(); lbNodeHintUI.activate(); } public void deactivate() { tgPanel.removeMouseListener(ml); tgPanel.removeKeyListener(kl); lbNodeHintUI.deactivate(); } class LBNavigateKeyListener extends KeyAdapter { private class SumPoint extends java.awt.Point { public SumPoint(java.awt.Point base, double factor) { super(); this.setLocation(base.getX()*factor,base.getY()*factor); } public void translate(java.awt.Point p) { this.setLocation(getX()+p.getX(),getY()+p.getY()); } } private class WeightTreeSet extends java.util.TreeSet { int weight=0; java.awt.Point pos; public WeightTreeSet(java.awt.Point p) { super(); setPosition(p); } void resetWeight() { weight = 0; } void testWeight(Object obj) { if (this.contains(obj)) { weight=1; } } void setPosition(java.awt.Point p) { pos = p; } SumPoint getCalculatedPosition() { return new SumPoint(pos,weight); } }; private SumPoint reduceCalculatedPosition() { SumPoint result = trees[0].getCalculatedPosition(); for (int i=1; i=0)) { Integer code = new Integer(e.getKeyCode()); testWeights(code); } } public void keyReleased(KeyEvent e) { // System.out.println("released "+e.getKeyCode()); Edge edge = null; LBNode select = (LBNode) tgPanel.getSelect(); if ((select!=null) && (select.edgeNum()>=0)) { SumPoint p = reduceCalculatedPosition(); if (p==null) { // not applicable position - open hint? open link? // try opening link if (e.getKeyCode()==e.VK_ENTER) { tgLinkBrowser.processNodeUrl(select); } return; // not applicable key - exit } edge = select.getRadialEdge(java.lang.Math.atan2(p.getY(),p.getX())); } resetWeights(); if (edge==null) return; Node n = edge.getOtherEndpt(select); tgPanel.setSelect(n); tgPanel.setLocale(n, localityScroll.getRadius()); tgPanel.centerNode(select,n); } }; class LBNavigateMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { LBNode mouseOverN = (LBNode) tgPanel.getMouseOverN(); if (e.getModifiers() == MouseEvent.BUTTON1_MASK) { if (mouseOverN == null) hvDragUI.activate(e); else dragNodeUI.activate(e); } // if (own!=tgPanel) java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner(); // System.out.println("focus:"+java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()); // System.out.println("panel:"+tgPanel); tgPanel.grabFocus(); /* boolean b = tgPanel.requestFocus(false); if (b) System.out.println("focus request likely to succeed"); else System.out.println("focus request failed!");*/ //tgPanel.requestFocus(); // added..*/ } public void mouseClicked(MouseEvent e) { LBNode mouseOverN = (LBNode) tgPanel.getMouseOverN(); LBNode select = (LBNode) tgPanel.getSelect(); if ((e.getModifiers() & MouseEvent.BUTTON1_MASK)!=0) { if (mouseOverN != null) { if (mouseOverN!=select) { tgPanel.setSelect(mouseOverN); tgPanel.setLocale(mouseOverN, localityScroll.getRadius()); //hvScrollToCenterUI.activate(e); } else { tgLinkBrowser.processNodeUrl(mouseOverN); } } } } public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { popupNode = (LBNode) tgPanel.getMouseOverN(); popupEdge = (LBEdge) tgPanel.getMouseOverE(); if (popupNode!=null) { tgPanel.setMaintainMouseOver(true); lbNodeHintUI.deactivate(); nodePopup.show(e.getComponent(), e.getX(), e.getY()); } else if (popupEdge!=null) { tgPanel.setMaintainMouseOver(true); lbNodeHintUI.deactivate(); edgePopup.show(e.getComponent(), e.getX(), e.getY()); } else { tgLinkBrowser.lbPopup.show(e.getComponent(), e.getX(), e.getY()); } } } } private void setUpNodePopup() { nodePopup = new JPopupMenu(); JMenuItem menuItem; menuItem = new JMenuItem("Expand Node"); ActionListener expandAction = new ActionListener() { public void actionPerformed(ActionEvent e) { if(popupNode!=null) { tgPanel.expandNode(popupNode); } } }; menuItem.addActionListener(expandAction); nodePopup.add(menuItem); menuItem = new JMenuItem("Hide Node"); ActionListener hideAction = new ActionListener() { public void actionPerformed(ActionEvent e) { Node select = tgPanel.getSelect(); if(popupNode!=null) { tgPanel.hideNode(popupNode, select); } } }; menuItem.addActionListener(hideAction); nodePopup.add(menuItem); menuItem = new JMenuItem("Select Node"); ActionListener selectAction = new ActionListener() { public void actionPerformed(ActionEvent e) { if(popupNode!=null) { tgPanel.setSelect(popupNode); } } }; menuItem.addActionListener(selectAction); nodePopup.add(menuItem); nodePopup.addPopupMenuListener(new PopupMenuListener() { public void popupMenuCanceled(PopupMenuEvent e) {} public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { tgPanel.setMaintainMouseOver(false); tgPanel.setMouseOverN(null); tgPanel.repaint(); lbNodeHintUI.activate(); } public void popupMenuWillBecomeVisible(PopupMenuEvent e) {} }); } private void setUpEdgePopup() { edgePopup = new JPopupMenu(); JMenuItem menuItem; menuItem = new JMenuItem("Hide Edge"); ActionListener hideAction = new ActionListener() { public void actionPerformed(ActionEvent e) { if(popupEdge!=null) { tgPanel.hideEdge(popupEdge); } } }; menuItem.addActionListener(hideAction); edgePopup.add(menuItem); edgePopup.addPopupMenuListener(new PopupMenuListener() { public void popupMenuCanceled(PopupMenuEvent e) {} public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { tgPanel.setMaintainMouseOver(false); tgPanel.setMouseOverE(null); tgPanel.repaint(); lbNodeHintUI.activate(); } public void popupMenuWillBecomeVisible(PopupMenuEvent e) {} }); } }