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

📄 defaultcontextmenulistener.java

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

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;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

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

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, Menu theMenu) {
        MenuItem it1 = new MenuItem(theMenu, SWT.CASCADE);
        it1.setText("Delete " + theNode.getLabel());
        it1.addSelectionListener(new SelectionListener() {
            public void widgetSelected(SelectionEvent arg0) {
                doStuff();
            }

            public void widgetDefaultSelected(SelectionEvent arg0) {
                doStuff();
            }
            
            public void doStuff(){
                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);
            }
        });
    }

    public void fillEdgeContextMenu(final Edge theEdge, Menu theMenu) {
        MenuItem it1 = new MenuItem(theMenu, SWT.CASCADE);
        it1.setText("Delete Edge");
        it1.addSelectionListener(new SelectionListener() {
            public void widgetSelected(SelectionEvent arg0) {
                doStuff();
            }

            public void widgetDefaultSelected(SelectionEvent arg0) {
                doStuff();
            }
            
            public void doStuff(){
                ArrayList edges = new ArrayList();
                edges.add(theEdge);
                graph.deleteElements(new ArrayList(), edges);
            }
        });
    }
    
    public void fillLegendContextMenu(Legend theLegend, Menu theMenu) {
        if (legendLens != null){
            MenuItem it1 = new MenuItem(theMenu, SWT.CASCADE);
            it1.setText("Hide Legend");
            it1.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent arg0) {
                    doStuff();
                }

                public void widgetDefaultSelected(SelectionEvent arg0) {
                    doStuff();
                }
                
                public void doStuff(){
                    legendLens.setShowLegend(false);
                }
            });
        }
    }

    public void fillBackgroundContextMenu(Menu theMenu) {
        if (zoomLens != null){
            final Integer[] zoom = zoomLevels;
            final int index = Arrays.binarySearch(zoom, (int) (zoomLens.getZoomFactor() * 100d));
            
            MenuItem zoomIn = new MenuItem(theMenu, SWT.CASCADE);
            zoomIn.setText("Zoom In");
            zoomIn.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent arg0) {
                    doStuff();
                }

                public void widgetDefaultSelected(SelectionEvent arg0) {
                    doStuff();
                }
                
                public void doStuff(){
                    zoomLens.setZoomFactor(zoom[index + 1]/ 100d);
                }
            });
            zoomIn.setEnabled(index != zoom.length - 1);
            
            MenuItem zoomOut = new MenuItem(theMenu, SWT.CASCADE);
            zoomOut.setText("Zoom Out");
            zoomOut.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent arg0) {
                    doStuff();
                }

                public void widgetDefaultSelected(SelectionEvent arg0) {
                    doStuff();
                }
                
                public void doStuff(){
                    zoomLens.setZoomFactor(zoom[index - 1]/ 100d);
                }
            });
            zoomOut.setEnabled(index != 0);
        }
        if (zoomLens != null && rotateLens != null){
            new MenuItem(theMenu, SWT.SEPARATOR);
        }
        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);
            
            MenuItem rotateClockwise = new MenuItem(theMenu, SWT.CASCADE);
            rotateClockwise.setText("Rotate ClockWise");
            rotateClockwise.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent arg0) {
                    doStuff();
                }

                public void widgetDefaultSelected(SelectionEvent arg0) {
                    doStuff();
                }
                
                public void doStuff(){
                    if (index == rotate.length - 1){
                        rotateLens.setRotationAngle(360 - rotate[0]);
                    }
                    else{
                        rotateLens.setRotationAngle(360 - rotate[index + 1]);
                    }
                }
            });
            
            MenuItem rotateCounterClockwise = new MenuItem(theMenu, SWT.CASCADE);
            rotateCounterClockwise.setText("Rotate Counter ClockWise");
            rotateCounterClockwise.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent arg0) {
                    doStuff();
                }

                public void widgetDefaultSelected(SelectionEvent arg0) {
                    doStuff();
                }
                
                public void doStuff(){
                    if (index == 0){
                        rotateLens.setRotationAngle(360 - rotate[rotate.length - 1]);
                    }
                    else{
                        rotateLens.setRotationAngle(360 - rotate[index - 1]);
                    }
                }
            });
        }
    }
}

⌨️ 快捷键说明

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