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

📄 defaultcontextmenulistener.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.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import javax.swing.AbstractAction;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import net.sourceforge.jpowergraph.DefaultGraph;
import net.sourceforge.jpowergraph.Edge;
import net.sourceforge.jpowergraph.Legend;
import net.sourceforge.jpowergraph.Node;
import net.sourceforge.jpowergraph.lens.LegendLens;
import net.sourceforge.jpowergraph.lens.RotateLens;
import net.sourceforge.jpowergraph.lens.ZoomLens;

/**
 * @author Mick Kerrigan
 *
 * Created on 03-Aug-2005
 * Committed by $Author: morcen $
 *
 * $Source: /cvsroot/jpowergraph/swing/src/net/sourceforge/jpowergraph/manipulator/contextandtooltip/DefaultContextMenuListener.java,v $,
 * @version $Revision: 1.1 $ $Date: 2005/08/09 12:43:23 $
 */

public class DefaultContextMenuListener implements ContextMenuListener{

    private DefaultGraph graph;
    private LegendLens legendLens;
    private ZoomLens zoomLens;
    private Integer[] zoomLevels;
    private RotateLens rotateLens;
    private Integer[] rotateAngles;
    
    public DefaultContextMenuListener(DefaultGraph theGraph, LegendLens theLegendLens, ZoomLens theZoomLens, Integer[] theZoomLevels, RotateLens theRotateLens, Integer[] theRotateAngles){
        this.graph = theGraph;
        this.legendLens = theLegendLens;
        this.zoomLens = theZoomLens;
        this.zoomLevels = theZoomLevels;
        this.rotateLens = theRotateLens;
        this.rotateAngles = theRotateAngles;
    }
    
    public void fillNodeContextMenu(final Node theNode, JPopupMenu theMenu) {
        JMenuItem deleteNode = new JMenuItem(new AbstractAction("Delete " + theNode.getLabel()) {
            public void actionPerformed(ActionEvent e) {
                ArrayList nodes = new ArrayList();
                nodes.add(theNode);
                ArrayList edges = new ArrayList();
                for (Iterator i = graph.getEdges().iterator(); i.hasNext();) {
                    Edge edge = (Edge) i.next();
                    if (edge.getFrom().equals(theNode) || edge.getTo().equals(theNode)){
                        edges.add(edge);
                    }
                }
                graph.deleteElements(nodes, edges);
            }
        });
        theMenu.add(deleteNode);
    }

    public void fillEdgeContextMenu(final Edge theEdge, JPopupMenu theMenu) {
        JMenuItem deleteEdge = new JMenuItem(new AbstractAction("Delete Edge") {
            public void actionPerformed(ActionEvent e) {
                ArrayList edges = new ArrayList();
                edges.add(theEdge);
                graph.deleteElements(new ArrayList(), edges);
            }
        });
        theMenu.add(deleteEdge);
    }

    public void fillLegendContextMenu(Legend theLegend, JPopupMenu theMenu) {
        if (legendLens != null){
            JMenuItem hideLegend = new JMenuItem(new AbstractAction("Hide Legend") {
                public void actionPerformed(ActionEvent e) {
                    legendLens.setShowLegend(false);
                }
            });
            theMenu.add(hideLegend);
        }
    }
    
    public void fillBackgroundContextMenu(JPopupMenu theMenu) {
        if (zoomLens != null){
            final Integer[] zoom = zoomLevels;
            final int index = Arrays.binarySearch(zoom, (int) (zoomLens.getZoomFactor() * 100d));
            
            JMenuItem zoomIn = new JMenuItem(new AbstractAction("Zoom In") {
                public void actionPerformed(ActionEvent e) {
                    zoomLens.setZoomFactor(zoom[index + 1]/ 100d);
                }
            });
            zoomIn.setEnabled(index != zoom.length - 1);
            theMenu.add(zoomIn);
            JMenuItem zoomOut = new JMenuItem(new AbstractAction("Zoom Out") {
                public void actionPerformed(ActionEvent e) {
                    zoomLens.setZoomFactor(zoom[index - 1]/ 100d);
                }
            });
            zoomOut.setEnabled(index != 0);
            theMenu.add(zoomOut);
        }
        if (zoomLens != null && rotateLens != null){
            theMenu.addSeparator();
        }
        if (rotateLens != null){
            final Integer[] rotate = rotateAngles;
            int currentValue = (int) (360 - rotateLens.getRotationAngle());
            while (currentValue == 360){
                currentValue = 0;
            }
            final int index = Arrays.binarySearch(rotate, currentValue);
            
            JMenuItem rotateClockwise = new JMenuItem(new AbstractAction("Rotate ClockWise") {
                public void actionPerformed(ActionEvent e) {
                    if (index == rotate.length - 1){
                        rotateLens.setRotationAngle(360 - rotate[0]);
                    }
                    else{
                        rotateLens.setRotationAngle(360 - rotate[index + 1]);
                    }
                }
            });
            theMenu.add(rotateClockwise);
            
            JMenuItem rotateCounterClockwise = new JMenuItem(new AbstractAction("Rotate Counter ClockWise") {
                public void actionPerformed(ActionEvent e) {
                    if (index == 0){
                        rotateLens.setRotationAngle(360 - rotate[rotate.length - 1]);
                    }
                    else{
                        rotateLens.setRotationAngle(360 - rotate[index - 1]);
                    }
                }
            });
            theMenu.add(rotateCounterClockwise);
        }
    }
}

⌨️ 快捷键说明

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