This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 105278
Collapse All | Expand All

(-)graph/examples/src/test/graphlayout/TreeGraphLayoutTest.java (-2 / +14 lines)
Lines 20-27 Link Here
20
20
21
import org.netbeans.api.visual.action.ActionFactory;
21
import org.netbeans.api.visual.action.ActionFactory;
22
import org.netbeans.api.visual.action.EditProvider;
22
import org.netbeans.api.visual.action.EditProvider;
23
import org.netbeans.api.visual.graph.layout.TreeGraphLayout;
23
import org.netbeans.api.visual.graph.layout.GraphLayout;
24
import org.netbeans.api.visual.graph.layout.GraphLayoutFactory;
25
import org.netbeans.api.visual.graph.layout.GraphLayoutSupport;
24
import org.netbeans.api.visual.widget.Widget;
26
import org.netbeans.api.visual.widget.Widget;
27
import org.netbeans.api.visual.layout.LayoutFactory;
28
import org.netbeans.api.visual.layout.SceneLayout;
25
import test.SceneSupport;
29
import test.SceneSupport;
26
import test.general.StringGraphScene;
30
import test.general.StringGraphScene;
27
31
Lines 31-39 Link Here
31
public class TreeGraphLayoutTest extends StringGraphScene {
35
public class TreeGraphLayoutTest extends StringGraphScene {
32
36
33
    public TreeGraphLayoutTest () {
37
    public TreeGraphLayoutTest () {
38
        // new implementation
39
        GraphLayout<String,String> graphLayout = GraphLayoutFactory.createTreeGraphLayout (100, 100, 50, 50, true);
40
        GraphLayoutSupport.setTreeGraphLayoutRootNode (graphLayout, "root");
41
        final SceneLayout sceneGraphLayout = LayoutFactory.createSceneGraphLayout (this, graphLayout);
42
34
        getActions ().addAction (ActionFactory.createEditAction (new EditProvider() {
43
        getActions ().addAction (ActionFactory.createEditAction (new EditProvider() {
35
            public void edit (Widget widget) {
44
            public void edit (Widget widget) {
36
                new TreeGraphLayout<String, String> (TreeGraphLayoutTest.this, 100, 100, 50, 50, true).layout ("root");
45
                // new implementation
46
                sceneGraphLayout.invokeLayoutImmediately ();
47
                // old implementation
48
//                new TreeGraphLayout<String, String> (TreeGraphLayoutTest.this, 100, 100, 50, 50, true).layout ("root");
37
            }
49
            }
38
        }));
50
        }));
39
    }
51
    }
(-)graph/lib/apichanges.xml (+14 lines)
Lines 168-173 Link Here
168
            <class package="org.netbeans.api.visual.router" name="ConnectionWidgetCollisionsCollector" link="yes"/>
168
            <class package="org.netbeans.api.visual.router" name="ConnectionWidgetCollisionsCollector" link="yes"/>
169
            <issue number="99054"/>
169
            <issue number="99054"/>
170
        </change>
170
        </change>
171
172
        <change>
173
            <api name="general"/>
174
            <summary>GraphLayoutFactory.createTreeGraphLayout added</summary>
175
            <version major="2" minor="4"/>
176
            <date day="1" month="6" year="2007"/>
177
            <author login="dkaspar"/>
178
            <compatibility addition="yes"/>
179
            <description>
180
                TreeGraphLayout added.
181
            </description>
182
            <class package="org.netbeans.api.visual.graph.layout" name="GraphLayoutFactory" link="yes"/>
183
            <issue number="105278"/>
184
        </change>
171
    </changes>
185
    </changes>
172
186
173
    <htmlcontents>
187
    <htmlcontents>
(-)graph/lib/manifest.mf (-1 / +1 lines)
Lines 1-4 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.api.visual
2
OpenIDE-Module: org.netbeans.api.visual
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/visual/resources/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/visual/resources/Bundle.properties
4
OpenIDE-Module-Specification-Version: 2.2
4
OpenIDE-Module-Specification-Version: 2.4
(-)graph/lib/src/org/netbeans/api/visual/graph/layout/GraphLayoutFactory.java (+47 lines)
Added Link Here
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
19
package org.netbeans.api.visual.graph.layout;
20
21
import org.netbeans.modules.visual.graph.layout.TreeGraphLayout;
22
23
/**
24
 * The factory class of all built-in GraphLayout based implementations.
25
 * 
26
 * @author David Kaspar
27
 * @since 2.4
28
 */
29
public class GraphLayoutFactory {
30
31
    /**
32
     * Creates a tree graph layout.
33
     * Use GraphLayoutSupport.setTreeGraphLayoutRootNode method to set the root node of the graph.
34
     * If not set/found, then layout is not executed.
35
     * @param originX the x-axis origin
36
     * @param originY the y-axis origin
37
     * @param verticalGap the vertical gap between cells
38
     * @param horizontalGap the horizontal gap between cells
39
     * @param vertical if true, then layout organizes the graph vertically; if false, then horizontally
40
     * @return the tree graph layout
41
     * @since 2.4
42
     */
43
    public static <N,E> GraphLayout<N,E> createTreeGraphLayout (int originX, int originY, int verticalGap, int horizontalGap, boolean vertical) {
44
        return new TreeGraphLayout<N,E> (originX, originY, verticalGap, horizontalGap, vertical);
45
    }
46
47
}
(-)graph/lib/src/org/netbeans/api/visual/graph/layout/GraphLayoutSupport.java (+40 lines)
Added Link Here
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
19
package org.netbeans.api.visual.graph.layout;
20
21
import org.netbeans.modules.visual.graph.layout.TreeGraphLayout;
22
23
/**
24
 * @author David Kaspar
25
 * @since 2.4
26
 */
27
public class GraphLayoutSupport {
28
29
    /**
30
     * Sets a root node to a tree graph layout.
31
     * @param graph the tree graph layout
32
     * @param rootNode the root node
33
     * @since 2.4
34
     */
35
    public static <N,E> void setTreeGraphLayoutRootNode (GraphLayout<N,E> graph, N rootNode) {
36
        if (graph instanceof TreeGraphLayout)
37
            ((TreeGraphLayout<N,E>) graph).setRootNode (rootNode);
38
    }
39
40
}
(-)graph/lib/src/org/netbeans/api/visual/widget/doc-files/documentation.html (-1 / +11 lines)
Lines 167-172 Link Here
167
<li><a href="#UniversalGraph">Universal Graph</a>
167
<li><a href="#UniversalGraph">Universal Graph</a>
168
<li><a href="#GraphLayout">Graph-oriented Layout</a>
168
<li><a href="#GraphLayout">Graph-oriented Layout</a>
169
<li><a href="#GraphLayoutListener">GraphLayoutListener</a>
169
<li><a href="#GraphLayoutListener">GraphLayoutListener</a>
170
<li><a href="#TreeGraphLayout">TreeGraphLayout</a>
170
<li><a href="#GridGraphLayout">GridGraphLayout</a>
171
<li><a href="#GridGraphLayout">GridGraphLayout</a>
171
</ul>
172
</ul>
172
<li><a href="#ComponentWidget">ComponentWidget</a>
173
<li><a href="#ComponentWidget">ComponentWidget</a>
Lines 1728-1737 Link Here
1728
<p>
1729
<p>
1729
The <code>GraphLayout</code> has possibility to attach a <code>GraphLayoutListener</code> which allows notification about: <code>graphLayoutStarted</code> (called when graph layout is started), <code>nodeLocationChanged</code> (called when a node location is changed, <code>graphLayoutFinished</code> (called when graph layout is finished).
1730
The <code>GraphLayout</code> has possibility to attach a <code>GraphLayoutListener</code> which allows notification about: <code>graphLayoutStarted</code> (called when graph layout is started), <code>nodeLocationChanged</code> (called when a node location is changed, <code>graphLayoutFinished</code> (called when graph layout is finished).
1730
1731
1732
<h3><a name="TreeGraphLayout">TreeGraphLayout</a></h3>
1733
1734
<p>
1735
The algotihm organizes nodes in to a tree. Nodes that are not in the tree (defined by a root node and connecting edges) are resolved at [0,0] location.
1736
1737
<p>
1738
The layout can be created using <code>GraphLayoutFactory.createTreeGraphLayout</code> factory method.
1739
For proper work, you have to specify a root node using <code>GraphLayoutSupport.createTreeGraphLayoutRootNode</code> method. See <code>test.graphlayout.TreeGraphLayoutTest</code> example for usages.
1740
1731
<h3><a name="GridGraphLayout">GridGraphLayout</a></h3>
1741
<h3><a name="GridGraphLayout">GridGraphLayout</a></h3>
1732
1742
1733
<p>
1743
<p>
1734
For now there is only one graph-oriented layout implemented by the <code>GridGraphLayout</code> class. The algorithm organizes nodes in to a grid.
1744
The algorithm organizes nodes in to a grid.
1735
1745
1736
<p>
1746
<p>
1737
You can call <code>setChecker</code> method to enable checker style. If a parameter is true, that the layout uses only half of nodes on a grid like single color on a chess board.
1747
You can call <code>setChecker</code> method to enable checker style. If a parameter is true, that the layout uses only half of nodes on a grid like single color on a chess board.
(-)graph/lib/src/org/netbeans/modules/visual/graph/layout/TreeGraphLayout.java (+164 lines)
Added Link Here
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
19
package org.netbeans.modules.visual.graph.layout;
20
21
import org.netbeans.api.visual.graph.layout.GraphLayout;
22
import org.netbeans.api.visual.graph.layout.UniversalGraph;
23
import org.netbeans.api.visual.widget.Widget;
24
25
import java.awt.*;
26
import java.util.*;
27
28
/**
29
 * @author David Kaspar
30
 */
31
public final class TreeGraphLayout<N,E> extends GraphLayout<N,E> {
32
33
    private int originX;
34
    private int originY;
35
    private int verticalGap;
36
    private int horizontalGap;
37
    private boolean vertical;
38
39
    private N rootNode;
40
41
    public TreeGraphLayout (int originX, int originY, int verticalGap, int horizontalGap, boolean vertical) {
42
        this.originX = originX;
43
        this.originY = originY;
44
        this.verticalGap = verticalGap;
45
        this.horizontalGap = horizontalGap;
46
        this.vertical = vertical;
47
    }
48
49
    public void setRootNode (N rootNode) {
50
        this.rootNode = rootNode;
51
    }
52
53
    protected void performGraphLayout (UniversalGraph<N, E> graph) {
54
        if (rootNode == null)
55
            return;
56
        Collection<N> allNodes = graph.getNodes ();
57
        ArrayList<N> nodesToResolve = new ArrayList<N> (allNodes);
58
59
        HashSet<N> loadedSet = new HashSet<N> ();
60
        Node root = new Node (graph, rootNode, loadedSet);
61
        nodesToResolve.removeAll (loadedSet);
62
        if (vertical) {
63
            root.allocateHorizontally (graph);
64
            root.resolveVertically (originX, originY);
65
        } else {
66
            root.allocateVertically (graph);
67
            root.resolveHorizontally (originX, originY);
68
        }
69
70
        final HashMap<N, Point> resultPosition = new HashMap<N, Point> ();
71
        root.upload (resultPosition);
72
73
        for (N node : nodesToResolve) {
74
            Point position = new Point ();
75
            // TODO - resolve others
76
            resultPosition.put (node, position);
77
        }
78
79
        for (Map.Entry<N, Point> entry : resultPosition.entrySet ())
80
            setResolvedNodeLocation (graph, entry.getKey (), entry.getValue ());
81
    }
82
83
    protected void performNodesLayout (UniversalGraph<N, E> universalGraph, Collection<N> nodes) {
84
        throw new UnsupportedOperationException (); // TODO
85
    }
86
87
    private class Node {
88
89
        private N node;
90
        private ArrayList<Node> children;
91
92
        private Rectangle relativeBounds;
93
        private int space;
94
        private int totalSpace;
95
        private Point point;
96
97
        private Node (UniversalGraph<N, E> graph, N node, HashSet<N> loadedSet) {
98
            this.node = node;
99
            loadedSet.add (node);
100
101
            children = new ArrayList<Node> ();
102
            for (E edge: graph.findNodeEdges (node, true, false)) {
103
                N child = graph.getEdgeTarget (edge);
104
                if (child != null  &&  ! loadedSet.contains (child))
105
                    children.add (new Node (graph, child, loadedSet));
106
            }
107
        }
108
109
        private int allocateHorizontally (UniversalGraph<N, E> graph) {
110
            Widget widget = graph.getScene ().findWidget (node);
111
            widget.getLayout ().layout (widget);
112
            relativeBounds = widget.getPreferredBounds ();
113
            space = 0;
114
            for (int i = 0; i < children.size (); i++) {
115
                if (i > 0)
116
                    space += horizontalGap;
117
                space += children.get (i).allocateHorizontally (graph);
118
            }
119
            totalSpace = Math.max (space, relativeBounds.width);
120
            return totalSpace;
121
        }
122
123
        private void resolveVertically (int x, int y) {
124
            point = new Point (x + totalSpace / 2, y - relativeBounds.y);
125
            x += (totalSpace - space) / 2;
126
            y += relativeBounds.height + verticalGap;
127
            for (Node child : children) {
128
                child.resolveVertically (x, y);
129
                x += child.totalSpace + horizontalGap;
130
            }
131
        }
132
133
        private int allocateVertically (UniversalGraph<N, E> graph) {
134
            Widget widget = graph.getScene ().findWidget (node);
135
            widget.getLayout ().layout (widget);
136
            relativeBounds = widget.getPreferredBounds ();
137
            space = 0;
138
            for (int i = 0; i < children.size (); i++) {
139
                if (i > 0)
140
                    space += verticalGap;
141
                space += children.get (i).allocateVertically (graph);
142
            }
143
            totalSpace = Math.max (space, relativeBounds.height);
144
            return totalSpace;
145
        }
146
147
        private void resolveHorizontally (int x, int y) {
148
            point = new Point (x - relativeBounds.x, y + totalSpace / 2);
149
            x += relativeBounds.width + horizontalGap;
150
            y += (totalSpace - space) / 2;
151
            for (Node child : children) {
152
                child.resolveHorizontally (x, y);
153
                y += child.totalSpace + verticalGap;
154
            }
155
        }
156
157
        private void upload (HashMap<N, Point> result) {
158
            result.put (node, point);
159
            for (Node child : children)
160
                child.upload (result);
161
        }
162
    }
163
164
}

Return to bug 105278