genericgraph.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 416 行 · 第 1/2 页

JAVA
416
字号
        controlPanel.add(zoomPanel);
        controlPanel.add(layoutPanel);
        
        return controlPanel;
    }

    public void actionPerformed(ActionEvent e)
    {
        String cmd = e.getActionCommand();
        if( cmd.equals("label") ) {
            VertexStringer vs = ((JCheckBox) e.getSource()).isSelected()
            	? SHORT_LABEL
            	: NO_LABEL; 
            pr.setVertexStringer( vs );
        }
        else if( cmd.equals("inverseEdge") ) {
            EdgeShapeFunction es = ((JCheckBox) e.getSource()).isSelected()
            	? CURVED_LINE
            	: LINE; 
            pr.setEdgeShapeFunction( es );
        }        
        else if( cmd.equals("scale") ) {
            vSize.setScaling( ((JCheckBox) e.getSource()).isSelected() );
        }
        else if( cmd.equals("font") ) {
            vFont.setBold( ((JCheckBox) e.getSource()).isSelected() );
        }
        else if( cmd.equals("layout") ) {
            String layoutName = ((JComboBox) e.getSource()).getSelectedItem().toString();
            if( layoutName.startsWith("KK") )
                vv.setGraphLayout( new KKLayout( graph ) );
            else if( layoutName.startsWith("Spring") )
                vv.setGraphLayout( new SpringLayout( graph ) );
            else if( layoutName.startsWith("Circle") )
                vv.setGraphLayout( new CircleLayout( graph ) );
            else if( layoutName.startsWith("FR") )
                vv.setGraphLayout( new FRLayout( graph ) );
            else if( layoutName.startsWith("ISOM") )
                vv.setGraphLayout( new ISOMLayout( graph ) );
            else if( layoutName.startsWith("DAG") )
                vv.setGraphLayout( new DAGLayout( graph ) );
            else
                throw new RuntimeException("Unknown layout");
        }

        vv.repaint();
    }
    
    protected DirectedSparseVertex addToGraph(DirectedSparseGraph graph, Object obj) 
    {
        DirectedSparseVertex node = (DirectedSparseVertex) graph.getUserDatum( obj );
        if(node == null) 
        {
            node = new DirectedSparseVertex();
            node.setUserDatum( DATA, obj, SHARE );
            graph.setUserDatum( obj, node, SHARE );
            graph.addVertex( node );
        
	        Collection linkedOnts = props.getLinkedElements( obj );
	        for(Iterator i = linkedOnts.iterator(); i.hasNext();) {
	            Object linkedObj = i.next();
	            DirectedSparseVertex linkedNode = addToGraph(graph, linkedObj);
	            DirectedSparseEdge edge = new DirectedSparseEdge(node, linkedNode);
	            graph.addEdge(edge);
	        }
        }
        return node;
    }

    private final class VertexSize implements VertexSizeFunction {        
        boolean scale = true;
        int maxSize = Integer.MIN_VALUE;
        int minSize = Integer.MAX_VALUE;
        
        double factor = 1.0;
            
        public VertexSize() {
        }
        
        public void setGraph( Graph g ) {
            for(Iterator i = g.getVertices().iterator(); i.hasNext();) {
                Vertex vertex = (Vertex) i.next();
                int size = props.getSize( vertex.getUserDatum( DATA ) );
                maxSize = Math.max( maxSize, size );
                minSize = Math.min( minSize, size );
            }
            
            if( maxSize == minSize )
                factor = 0.0;
            else 
                factor = (double) (MAX - MIN) / (maxSize - minSize);
        }
        
        public void setScaling(boolean scale) {
            this.scale = scale;
        }
        
        public boolean getScaling() {
            return scale;
        }
        
        public int getSize( Vertex vertex ) {
            if( scale ) {
                int size = props.getSize( vertex.getUserDatum( DATA ) );
                
                return ((int) ((size - minSize) * factor)) + MIN;
            }
            else
                return MIN;
        }
	}
    
    private class VertexLabel implements VertexStringer {
        private boolean shortLabel;
        
        public VertexLabel(boolean qname) {
            this.shortLabel = qname;
        }
        
        public String getLabel(Vertex vertex) {
            Object obj = vertex.getUserDatum( DATA );
            
            if( shortLabel )                    
                return props.getShortName( obj );
            else
                return props.getLongName( obj );
        }
    }   
    
    public class VertexTips implements VisualizationViewer.ToolTipListener {
        public VertexTips() {
        }
    
        public String getToolTipText(MouseEvent e) {
	        PickSupport pickSupport = vv.getPickSupport();
	        Point2D p = vv.transform(e.getPoint());

            Vertex v = pickSupport.getVertex(p.getX(), p.getY());
            if (v != null) {
                return LONG_LABEL.getLabel( v );                
            } else {
                Edge edge = pickSupport.getEdge(p.getX(), p.getY());
                if(edge != null) {
                    return edge.toString();
                }
                return "<html><center>Use the mouse wheel to zoom<p>Click and Drag the mouse to pan</center></html>";
            }
        }
    }
    
    private final class VertexColor implements VertexPaintFunction {
        protected PickedInfo pi;
        
        public VertexColor(VisualizationViewer vv) {
            this.pi = vv.getPickedState();
        }
        
        public Paint getDrawPaint(Vertex v) {
            return pi.isPicked(v)
            	? Color.YELLOW
            	: Color.BLACK;
        }
        
        public Paint getFillPaint(Vertex v) {
            if( v.getOutEdges().isEmpty() ) {                
                if( v.getInEdges().isEmpty() )
                    return Color.GREEN;
                else
                    return Color.BLUE;
            }
            else
                return Color.RED;
        }
    }

    private final static class VertexFont implements VertexFontFunction {
        protected boolean bold = false;
        Font f = new Font("Helvetica", Font.PLAIN, 12);
        Font b = new Font("Helvetica", Font.BOLD, 12);
        
        public void setBold(boolean bold) {
            this.bold = bold;
        }
        
        public Font getFont(Vertex v) {
            return bold ? b : f;
        }        
    }    
    
    private class EdgeShapeFn implements EdgeShapeFunction {
        public Shape getShape(Edge edge) {
            Pair pair = edge.getEndpoints();
            Vertex from = (Vertex) pair.getFirst();
            Vertex to = (Vertex) pair.getSecond();
            if( to.findEdge( from ) == null )
                return LINE.getShape( edge );
            else
                return CURVE.getShape( edge );
        }

        public void setControlOffsetIncrement(float inc) {
            LINE.setControlOffsetIncrement( inc );
            CURVE.setControlOffsetIncrement( inc );
        }    
    
    }
}

⌨️ 快捷键说明

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