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

📄 selectionmanipulator.java

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

import java.awt.event.ActionEvent;
import java.awt.geom.Point2D;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;

import javax.swing.Action;

import net.sourceforge.jpowergraph.Edge;
import net.sourceforge.jpowergraph.Legend;
import net.sourceforge.jpowergraph.Node;
import net.sourceforge.jpowergraph.manipulator.AbstractManipulator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

/**
 * A manipulator that provides the selection user interface.
 */
public class SelectionManipulator extends AbstractManipulator {
    /** The name of this manipulator. */
    public static final String NAME="SelectionManipulator";

    /** The node selection model.*/
    protected NodeSelectionModel m_nodeSelectionModel;
    /** The graph position of the fixed point. */
    protected Point2D m_selectionRectangleFixedGraph;
    /** The fixed point of the selection rectangle. */
    protected Point m_selectionRectangleFixed;
    /** The moving point of the selection rectangle. */
    protected Point m_selectionRectangleMoving;

    private Color selectionColor;
    
    private int selectionMouseEventKeyMask;
    private int toggleMouseEventKeyMask;

    private boolean disposed = false;
    
    /**
     * Creates an instance of this class.
     * 
     * Selection is done by holding shift and left clicking
     * Toggling is done by holding ctrl and left clicking
     *
     * @param nodeSelectionModel                the model for node selection
     */
    public SelectionManipulator(Display theDisplay, NodeSelectionModel nodeSelectionModel) {
        this(theDisplay, nodeSelectionModel, SWT.SHIFT, SWT.CTRL);
    }
    
    /**
     * Creates an instance of this class.
     * 
     * Selection is done by holding the theSelectionMouseEventKeyMask key and left clicking
     * Toggling is done by holding the theToggleMouseEventKeyMask and left clicking
     * 
     * The key masks can be one of:
     * 
     * SWT.SHIFT
     * SWT.CTRL
     * SWT.ALT
     * SWT.NONE (for no key mask)
     *
     * @param nodeSelectionModel                the model for node selection
     * @param theSelectionMouseEventKeyMask     The Mouse MASK
     * @param theToggleMouseEventKeyMask        The Mouse MASK
     */
    public SelectionManipulator(Display theDisplay, NodeSelectionModel nodeSelectionModel,  int theSelectionMouseEventKeyMask, int theToggleMouseEventKeyMask) {
        m_nodeSelectionModel=nodeSelectionModel;
        selectionMouseEventKeyMask = theSelectionMouseEventKeyMask;
        toggleMouseEventKeyMask = theToggleMouseEventKeyMask;
        m_nodeSelectionModel.addNodeSelectionListener(new NodeSelectionHandler());
        m_selectionRectangleFixedGraph=new Point2D.Double();
        selectionColor = theDisplay.getSystemColor(SWT.COLOR_GRAY);
    }
    protected boolean shouldSelectNode(Node node) {
        return node != null;
    }
    public String getName() {
        return NAME;
    }
    public NodeSelectionModel getNodeSelectionModel() {
        return m_nodeSelectionModel;
    }
    
    public void paint(GC g) {
        if (m_selectionRectangleFixed!=null) {
            Color oldColor=g.getBackground();
            g.setBackground(selectionColor);
            int x=Math.min(m_selectionRectangleFixed.x,m_selectionRectangleMoving.x);
            int y=Math.min(m_selectionRectangleFixed.y,m_selectionRectangleMoving.y);
            int width=Math.abs(m_selectionRectangleFixed.x-m_selectionRectangleMoving.x);
            int height=Math.abs(m_selectionRectangleFixed.y-m_selectionRectangleMoving.y);
            g.drawRectangle(x,y,width,height);
            g.setBackground(oldColor);
        }
    }
    
    public void mouseDown(MouseEvent e) {
        if (m_graphPane.isEnabled() && e.button == 1 && isStartSelectionEvent(e)) {
            Legend legend = m_graphPane.getLegendAtPoint(new Point(e.x, e.y));
            Node node=m_graphPane.getNodeAtPoint(new Point(e.x, e.y));
            if (legend != null){
                Action a = legend.getActionAtPoint(new Point(e.x, e.y));
                if (a != null){
                    a.actionPerformed(new ActionEvent(e.getSource(), 0, "do Action"));
                }
            }
            else if (node!=null) {
                Collection rawSelection=Collections.singletonList(node);
                changeSelection(rawSelection,e);
            }
            else {
                Edge edge=m_graphPane.getNearestEdge(new Point(e.x, e.y));
                if (edge==null) {
                    updateLastMouseEventScreenPoint(e);
                    m_selectionRectangleFixed = new Point(e.x, e.y);
                    m_graphPane.screenToGraphPoint(m_selectionRectangleFixed,m_selectionRectangleFixedGraph);
                    m_selectionRectangleMoving=m_selectionRectangleFixed;
                }
            }
        }
    }
    public void mouseReleased(MouseEvent e) {
        if (m_selectionRectangleFixed!=null && e.button == 1) {
            m_selectionRectangleMoving = new Point(e.x, e.y);
            int x=Math.min(m_selectionRectangleFixed.x,m_selectionRectangleMoving.x);
            int y=Math.min(m_selectionRectangleFixed.y,m_selectionRectangleMoving.y);
            int width=Math.abs(m_selectionRectangleFixed.x-m_selectionRectangleMoving.x);
            int height=Math.abs(m_selectionRectangleFixed.y-m_selectionRectangleMoving.y);
            Rectangle rectangle = new Rectangle(x,y,width,height);
            Collection nodesInRectangle=m_graphPane.getNodesInRectangle(rectangle);
            changeSelection(nodesInRectangle,e);
            m_selectionRectangleFixed=null;
            m_selectionRectangleMoving=null;
            m_lastMouseEventScreenPoint=null;
            m_graphPane.redraw();
        }
    }
    public void mouseDragged(MouseEvent e) {
        if (m_selectionRectangleFixed!=null) {
            autoscroll(e);
            updateLastMouseEventScreenPoint(e);
            m_selectionRectangleMoving = new Point(e.x, e.y);
            m_graphPane.redraw();
        }
    }
    public void notifyGraphPaneScrolled() {
        if (m_selectionRectangleFixed!=null) {
            m_graphPane.graphToScreenPoint(m_selectionRectangleFixedGraph,m_selectionRectangleFixed);
            m_selectionRectangleMoving=getLastMouseEventPoint();
            m_graphPane.redraw();
        }
    }
    protected void changeSelection(Collection rawSelection,MouseEvent e) {
        Collection newSelection=new HashSet();
        Iterator iterator=rawSelection.iterator();
        while (iterator.hasNext()) {
            Node node=(Node)iterator.next();
            if (shouldSelectNode(node))
                newSelection.add(node);
        }
        if (!newSelection.isEmpty()) {
            Collection existingSelection=m_nodeSelectionModel.getSelectedNodes();
            if (isToggleSelectionEvent(e)) {
                Collection addElements=new HashSet();
                Collection removeElements=new HashSet();
                iterator=newSelection.iterator();
                while (iterator.hasNext()) {
                    Node node=(Node)iterator.next();
                    if (existingSelection.contains(node))
                        removeElements.add(node);
                    else
                        addElements.add(node);
                }
                if (!removeElements.isEmpty())
                    m_nodeSelectionModel.removeNodes(removeElements);
                if (!addElements.isEmpty())
                    m_nodeSelectionModel.addNodes(addElements);
            }
            else {
                if (!existingSelection.containsAll(newSelection) || existingSelection.size()!=newSelection.size()) {
                    m_nodeSelectionModel.clear();
                    m_nodeSelectionModel.addNodes(newSelection);
                }
            }
        }
    }

    protected boolean isStartSelectionEvent(MouseEvent e) {
        if (selectionMouseEventKeyMask == SWT.NONE){
            return true;
        }
        return (e.stateMask & selectionMouseEventKeyMask) != 0;
    }

    protected boolean isToggleSelectionEvent(MouseEvent e) {
        if (toggleMouseEventKeyMask == SWT.NONE){
            return true;
        }
        return (e.stateMask & toggleMouseEventKeyMask) != 0;
    }

    /**
     * The handles of the node selection events.
     */
    protected class NodeSelectionHandler implements NodeSelectionListener {
        public void nodesAddedToSelection(NodeSelectionModel nodeSelectionModel,Collection nodes) {
            m_graphPane.redraw();
        }
        public void nodesRemovedFromSelection(NodeSelectionModel nodeSelectionModel,Collection nodes) {
            m_graphPane.redraw();
        }
        public void selectionCleared(NodeSelectionModel nodeSelectionModel) {
            m_graphPane.redraw();
        }
    }
    
    public void dispose(){
        if (!selectionColor.isDisposed()){
            selectionColor.dispose();
        }
        disposed = true;
    }
    
    public boolean isDisposed(){
        return disposed ;
    }
}

⌨️ 快捷键说明

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