hierarchical layout algorithm added release60_beta1_base
authorthomas_wuerthinger
Fri, 31 Aug 2007 14:57:17 +0000
changeset 88628da174d3445f
parent 8861 11eb1e0ec1c2
child 8863 e9f6528d2a8a
hierarchical layout algorithm added
visual.experimental/src/org/netbeans/modules/visual/experimental/graph/layout/GraphLayoutFactory.java
visual.experimental/src/org/netbeans/modules/visual/experimental/graph/layout/hierarchical/HierarchicalGraphLayout.java
visual.experimental/src/org/netbeans/modules/visual/experimental/graph/layout/hierarchical/package.html
visual.experimental/test/SceneSupport.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/visual.experimental/src/org/netbeans/modules/visual/experimental/graph/layout/GraphLayoutFactory.java	Fri Aug 31 14:57:17 2007 +0000
     1.3 @@ -0,0 +1,37 @@
     1.4 +/*
     1.5 + * The contents of this file are subject to the terms of the Common Development
     1.6 + * and Distribution License (the License). You may not use this file except in
     1.7 + * compliance with the License.
     1.8 + * 
     1.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
    1.10 + * or http://www.netbeans.org/cddl.txt.
    1.11 + * 
    1.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
    1.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
    1.14 + * If applicable, add the following below the CDDL Header, with the fields
    1.15 + * enclosed by brackets [] replaced by your own identifying information:
    1.16 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.17 + * 
    1.18 + * The Original Software is NetBeans. The Initial Developer of the Original
    1.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
    1.20 + * Microsystems, Inc. All Rights Reserved.
    1.21 + */
    1.22 +package org.netbeans.modules.visual.experimental.graph.layout;
    1.23 +
    1.24 +import org.netbeans.api.visual.graph.layout.GraphLayout;
    1.25 +import org.netbeans.modules.visual.experimental.graph.layout.hierarchical.HierarchicalGraphLayout;
    1.26 +
    1.27 +/**
    1.28 + *
    1.29 + * @author Thomas
    1.30 + */
    1.31 +public final class GraphLayoutFactory {
    1.32 +
    1.33 +	public static <N, E> GraphLayout<N, E> createHierarchicalGraphLayout() {
    1.34 +		return new HierarchicalGraphLayout<N, E>();
    1.35 +	}
    1.36 +
    1.37 +	public static <N, E> GraphLayout<N, E> createHierarchicalGraphLayout(ConnectionManager<E> connectionManager) {
    1.38 +		return new HierarchicalGraphLayout<N, E>(connectionManager);
    1.39 +	}
    1.40 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/visual.experimental/src/org/netbeans/modules/visual/experimental/graph/layout/hierarchical/HierarchicalGraphLayout.java	Fri Aug 31 14:57:17 2007 +0000
     2.3 @@ -0,0 +1,193 @@
     2.4 +/*
     2.5 + * The contents of this file are subject to the terms of the Common Development
     2.6 + * and Distribution License (the License). You may not use this file except in
     2.7 + * compliance with the License.
     2.8 + * 
     2.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
    2.10 + * or http://www.netbeans.org/cddl.txt.
    2.11 + * 
    2.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
    2.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
    2.14 + * If applicable, add the following below the CDDL Header, with the fields
    2.15 + * enclosed by brackets [] replaced by your own identifying information:
    2.16 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.17 + * 
    2.18 + * The Original Software is NetBeans. The Initial Developer of the Original
    2.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
    2.20 + * Microsystems, Inc. All Rights Reserved.
    2.21 + */
    2.22 +
    2.23 +package org.netbeans.modules.visual.experimental.graph.layout.hierarchical;
    2.24 +
    2.25 +import java.awt.Dimension;
    2.26 +import java.awt.Point;
    2.27 +import java.util.ArrayList;
    2.28 +import java.util.Collection;
    2.29 +import java.util.HashMap;
    2.30 +import java.util.HashSet;
    2.31 +import java.util.List;
    2.32 +import java.util.Map;
    2.33 +import java.util.Set;
    2.34 +import org.netbeans.api.visual.graph.layout.GraphLayout;
    2.35 +import org.netbeans.api.visual.graph.layout.UniversalGraph;
    2.36 +import org.netbeans.api.visual.widget.Widget;
    2.37 +import org.netbeans.modules.visual.experimental.graph.layout.ConnectionManager;
    2.38 +
    2.39 +/**
    2.40 + *
    2.41 + * @author Thomas
    2.42 + */
    2.43 +public class HierarchicalGraphLayout<N, E> extends GraphLayout<N, E>{
    2.44 +    
    2.45 +	
    2.46 +	private ConnectionManager<E> connectionManager;
    2.47 +	
    2.48 +	
    2.49 +    public HierarchicalGraphLayout() {
    2.50 +	    this(null);
    2.51 +    }
    2.52 +    
    2.53 +    public HierarchicalGraphLayout(ConnectionManager<E> connectionManager) {
    2.54 +	    this.connectionManager = connectionManager;
    2.55 +    }
    2.56 +    
    2.57 +    private class LinkWrapper implements Link {
    2.58 +        
    2.59 +        private VertexWrapper from;
    2.60 +        private VertexWrapper to;
    2.61 +	private E edge;
    2.62 +        
    2.63 +        public LinkWrapper(E edge, VertexWrapper from, VertexWrapper to) {
    2.64 +	    this.edge = edge;	
    2.65 +            this.from = from;
    2.66 +            this.to = to;
    2.67 +        }
    2.68 +        
    2.69 +        public Port getFrom() {
    2.70 +            return from.getSlot();
    2.71 +        }
    2.72 +
    2.73 +        public Port getTo() {
    2.74 +            return to.getSlot();
    2.75 +        }
    2.76 +
    2.77 +        public List<Point> getControlPoints() {
    2.78 +            return new ArrayList<Point>();
    2.79 +        }
    2.80 +
    2.81 +        public void setControlPoints(List<Point> list) {
    2.82 +		if(connectionManager != null) {
    2.83 +			connectionManager.setControlPoints(edge, list);
    2.84 +		}
    2.85 +        }
    2.86 +    
    2.87 +    }
    2.88 +    
    2.89 +    private class VertexWrapper implements Vertex {
    2.90 +        
    2.91 +        private N node;
    2.92 +        private UniversalGraph<N, E> graph;
    2.93 +        private Port slot;
    2.94 +        private Point position;
    2.95 +        
    2.96 +        public VertexWrapper(N node, UniversalGraph<N, E> graph) {
    2.97 +            this.node = node;
    2.98 +            this.graph = graph;
    2.99 +            final VertexWrapper vertex = this;
   2.100 +            this.slot = new Port() {
   2.101 +                public Vertex getVertex() {
   2.102 +                    return vertex;
   2.103 +                }
   2.104 +
   2.105 +                public Point getRelativePosition() {
   2.106 +                    return new Point((int)(vertex.getSize().getWidth()/2), (int)(vertex.getSize().getHeight()/2));
   2.107 +                }
   2.108 +                
   2.109 +            };
   2.110 +            
   2.111 +            Widget w = graph.getScene().findWidget(node);
   2.112 +            this.position = w.getPreferredLocation();
   2.113 +        }
   2.114 +        
   2.115 +        public Cluster getCluster() {
   2.116 +            return null;
   2.117 +        }
   2.118 +
   2.119 +        public Dimension getSize() {
   2.120 +            Widget w = graph.getScene().findWidget(node);
   2.121 +            return w.getBounds().getSize();
   2.122 +        }
   2.123 +
   2.124 +        public Point getPosition() {
   2.125 +            return position;
   2.126 +        }
   2.127 +
   2.128 +        public void setPosition(Point p) {
   2.129 +            HierarchicalGraphLayout.this.setResolvedNodeLocation(graph, node, p);
   2.130 +            position = p;
   2.131 +	    System.out.println("Setting position of " + node + " to " + p);
   2.132 +        }
   2.133 +
   2.134 +        public boolean isDirty() {
   2.135 +            return false;
   2.136 +        }
   2.137 +
   2.138 +        public boolean isRoot() {
   2.139 +            return false;
   2.140 +        }
   2.141 +
   2.142 +        public int compareTo(Vertex o) {
   2.143 +            VertexWrapper vw = (VertexWrapper)o;
   2.144 +            return node.toString().compareTo(vw.node.toString());
   2.145 +        }
   2.146 +        
   2.147 +        public Port getSlot() {
   2.148 +            return slot;
   2.149 +        }
   2.150 +
   2.151 +        public boolean isExpanded() {
   2.152 +            return false;
   2.153 +        }
   2.154 +
   2.155 +        public boolean isFixed() {
   2.156 +            return false;
   2.157 +        }
   2.158 +
   2.159 +        public boolean isMarked() {
   2.160 +            return false;
   2.161 +        }
   2.162 +    
   2.163 +    }
   2.164 +    
   2.165 +    protected void performGraphLayout(UniversalGraph<N, E> graph) {
   2.166 +
   2.167 +        
   2.168 +        Set<LinkWrapper> links = new HashSet<LinkWrapper>();
   2.169 +        Set<VertexWrapper> vertices = new HashSet<VertexWrapper>();
   2.170 +        Map<N, VertexWrapper> vertexMap = new HashMap<N, VertexWrapper>();
   2.171 +        
   2.172 +        for(N node : graph.getNodes()) {
   2.173 +            VertexWrapper v = new VertexWrapper(node, graph);
   2.174 +            vertexMap.put(node, v);
   2.175 +            vertices.add(v);
   2.176 +        }
   2.177 +        
   2.178 +        for(E edge : graph.getEdges()) {
   2.179 +            N source = graph.getEdgeSource(edge);
   2.180 +            N target = graph.getEdgeTarget(edge);
   2.181 +            LinkWrapper l = new LinkWrapper(edge, vertexMap.get(source), vertexMap.get(target));
   2.182 +            links.add(l);
   2.183 +        }
   2.184 +        
   2.185 +        HierarchicalLayoutManager m = new HierarchicalLayoutManager(HierarchicalLayoutManager.Combine.NONE);
   2.186 +        
   2.187 +        LayoutGraph layoutGraph = new LayoutGraph(links, vertices);
   2.188 +        m.doLayout(layoutGraph);
   2.189 +        
   2.190 +    }
   2.191 +
   2.192 +    protected void performNodesLayout(UniversalGraph<N, E> graph, Collection<N> nodes) {
   2.193 +          //throw new UnsupportedOperationException();
   2.194 +    }
   2.195 +    
   2.196 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/visual.experimental/src/org/netbeans/modules/visual/experimental/graph/layout/hierarchical/package.html	Fri Aug 31 14:57:17 2007 +0000
     3.3 @@ -0,0 +1,1 @@
     3.4 +<h1>Documentation for hierarchical layout</h1>
     3.5 \ No newline at end of file
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/visual.experimental/test/SceneSupport.java	Fri Aug 31 14:57:17 2007 +0000
     4.3 @@ -0,0 +1,70 @@
     4.4 +/*
     4.5 + * The contents of this file are subject to the terms of the Common Development
     4.6 + * and Distribution License (the License). You may not use this file except in
     4.7 + * compliance with the License.
     4.8 + *
     4.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
    4.10 + * or http://www.netbeans.org/cddl.txt.
    4.11 + *
    4.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
    4.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
    4.14 + * If applicable, add the following below the CDDL Header, with the fields
    4.15 + * enclosed by brackets [] replaced by your own identifying information:
    4.16 + * "Portions Copyrighted [year] [name of copyright owner]"
    4.17 + *
    4.18 + * The Original Software is NetBeans. The Initial Developer of the Original
    4.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    4.20 + * Microsystems, Inc. All Rights Reserved.
    4.21 + */
    4.22 +package test;
    4.23 +
    4.24 +import org.netbeans.api.visual.widget.Scene;
    4.25 +import org.openide.util.RequestProcessor;
    4.26 +
    4.27 +import javax.swing.*;
    4.28 +import java.awt.*;
    4.29 +
    4.30 +/**
    4.31 + * @author David Kaspar
    4.32 + */
    4.33 +public class SceneSupport {
    4.34 +
    4.35 +    public static void show (final Scene scene) {
    4.36 +        JComponent sceneView = scene.getView ();
    4.37 +        if (sceneView == null)
    4.38 +            sceneView = scene.createView ();
    4.39 +        show (sceneView);
    4.40 +    }
    4.41 +
    4.42 +    public static void show (final JComponent sceneView) {
    4.43 +        JScrollPane panel = new JScrollPane (sceneView);
    4.44 +        panel.getHorizontalScrollBar ().setUnitIncrement (32);
    4.45 +        panel.getHorizontalScrollBar ().setBlockIncrement (256);
    4.46 +        panel.getVerticalScrollBar ().setUnitIncrement (32);
    4.47 +        panel.getVerticalScrollBar ().setBlockIncrement (256);
    4.48 +        showCore (panel);
    4.49 +    }
    4.50 +
    4.51 +    public static void showCore (final JComponent view) {
    4.52 +        int width=800,height=600;
    4.53 +        JFrame frame = new JFrame ();//new JDialog (), true);
    4.54 +        frame.add (view, BorderLayout.CENTER);
    4.55 +        frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
    4.56 +        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    4.57 +        frame.setBounds((screenSize.width-width)/2, (screenSize.height-height)/2, width, height);
    4.58 +        frame.setVisible (true);
    4.59 +    }
    4.60 +
    4.61 +    public static int randInt (int max) {
    4.62 +        return (int) (Math.random () * max);
    4.63 +    }
    4.64 +
    4.65 +    public static void invokeLater (final Runnable runnable, int delay) {
    4.66 +        RequestProcessor.getDefault ().post (new Runnable() {
    4.67 +            public void run () {
    4.68 +                SwingUtilities.invokeLater (runnable);
    4.69 +            }
    4.70 +        }, delay);
    4.71 +    }
    4.72 +
    4.73 +}