⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contextmenuandtooltipmanipulator.java

📁 JPowerGraph is a Java library for creating directed graphs for Swing. It supports graph movement, se
💻 JAVA
字号:
package net.sourceforge.jpowergraph.manipulator.contextandtooltip;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.JPopupMenu;

import net.sourceforge.jpowergraph.Edge;
import net.sourceforge.jpowergraph.Legend;
import net.sourceforge.jpowergraph.Node;
import net.sourceforge.jpowergraph.lens.TooltipLens;
import net.sourceforge.jpowergraph.manipulator.AbstractManipulator;
import net.sourceforge.jpowergraph.pane.JGraphPane;

/**
 * A manipulator providing the contextmenu & tooltip for nodes.
 */
public class ContextMenuAndToolTipManipulator extends AbstractManipulator {
    protected static final String CLASS_NAME = ContextMenuAndToolTipManipulator.class.toString();
    public static final String NAME = "ContextMenuManipulator";

    private JPopupMenu rightClick;
    private JPopupMenu tooltip;
    private Node lastNode = null;
    private Color toolTipBackground;
    
    private ContextMenuListener contextMenuListener;
    private ToolTipListener toolTipListener;
    private TooltipLens tooltipLens;
    
    public ContextMenuAndToolTipManipulator(JGraphPane theWSMLGraphPane, ContextMenuListener theContextMenuListener, ToolTipListener theToolTipListener, TooltipLens theTooltipLens) {
        setGraphPane(theWSMLGraphPane);
        this.contextMenuListener = theContextMenuListener;
        this.toolTipListener = theToolTipListener;
        this.tooltipLens = theTooltipLens;
        this.toolTipBackground = new Color(255, 255, 204);
    }

    public String getName() {
        return NAME;
    }

    public void mousePressed(MouseEvent e) {
        closeToolTipIfNeeded();
        doRightClickPopup(e);
    }
    
    public void mouseReleased(MouseEvent e) {
        doRightClickPopup(e);
    }
    
    public void mouseMoved(MouseEvent e) {
        if (showToolTips() && !isRightClickShowing()){
            doToolTipPopup(e);
        }
    }
    
    private boolean showToolTips() {
        return tooltipLens == null || tooltipLens.isShowToolTips();
    }

    protected void doRightClickPopup(MouseEvent e) {
        if (m_graphPane.isEnabled() && e.getButton() == MouseEvent.BUTTON3) {
            Point point = e.getPoint();
            Legend legend =  m_graphPane.getLegendAtPoint(point);
            Node node = m_graphPane.getNodeAtPoint(point);
            Edge edge = m_graphPane.getNearestEdge(point);
            
            closeRightClickIfNeeded();
            rightClick = new JPopupMenu();
            if (legend != null){
                if (contextMenuListener != null){
                    contextMenuListener.fillLegendContextMenu(legend, rightClick);
                }
            }
            else if (node != null){
                if (contextMenuListener != null){
                    contextMenuListener.fillNodeContextMenu(node, rightClick);
                }
            }
            else if (edge != null){
                if (contextMenuListener != null){
                    contextMenuListener.fillEdgeContextMenu(edge, rightClick);
                }
            }
            else{
                if (contextMenuListener != null){
                    contextMenuListener.fillBackgroundContextMenu(rightClick);
                }
            }
            
            if (rightClick.getComponentCount() > 0){
                closeToolTipIfNeeded();
                rightClick.show(m_graphPane, point.x, point.y);
                e.consume();
            }
        }
        else{
            closeRightClickIfNeeded();
        }
    }
    
    protected void doToolTipPopup(MouseEvent e) {
        if (m_graphPane.isEnabled()) {
            Point point = e.getPoint();
            Node node = m_graphPane.getNodeAtPoint(point);

            if (node != null && node != lastNode) {
                closeToolTipIfNeeded();
                tooltip = new JPopupMenu();
                tooltip.setBackground(toolTipBackground);
                if (toolTipListener != null){
                    boolean okay = toolTipListener.addNodeToolTipItems(node, tooltip, toolTipBackground);
                    if (okay){
                        tooltip.show(m_graphPane, point.x, point.y + 25);
                        e.consume();
                    }
                }
            }
            else if (node == null){
                closeToolTipIfNeeded();
            }
            lastNode = node;
        }
    }
    
    private boolean isRightClickShowing(){
        return rightClick != null && rightClick.isVisible();
    }
    
    private boolean isToolTipShowing(){
        return tooltip != null && tooltip.isVisible();
    }
    
    private void closeRightClickIfNeeded(){
        if (isRightClickShowing()) {
            rightClick.setVisible(false);
            rightClick = null;
        }
    }
    
    private void closeToolTipIfNeeded(){
        if (isToolTipShowing()) {
            tooltip.setVisible(false);
            tooltip = null;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -