組織図ツリーが必要です。どのレベルでもノードを折りたたんで展開したいと考えています。私はJGraphXを初めて使っていますが、私が読んだところでは、折り畳みを実装する方法のように、頂点をグループ化するように聞こえます。問題は、親頂点の中にすべての子頂点を置くグループを作成するときです。折りたたみ可能なノードを持つ階層ツリーをJGraphXで作成する方法
package com.mxgraph.examples.swing;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxGraph;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
public class HelloWorld extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -2707712944901661771L;
public HelloWorld()
{
super("Hello, puppies!");
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
//Notice that the parent is the default parent...
//The hierarchical structure looks great but I cannot collapse/expand the tree.
Object vDogsRoot = graph.insertVertex(parent, null, "DOG", 0, 0, 80, 30);
Object v2 = graph.insertVertex(parent, null, "Shar Pei", 0, 0, 80, 30);
Object v3 = graph.insertVertex(parent, null, "Pug", 0, 0, 80, 30);
Object v4 = graph.insertVertex(parent, null, "Cocker Spaniel", 0, 0, 80, 30);
Object v5 = graph.insertVertex(parent, null, "Pit Bull", 0, 0, 80, 30);
Object v6 = graph.insertVertex(parent, null, "Chihuahua", 0, 0, 80, 30);
graph.insertEdge(parent, null, "", vDogsRoot, v2);
graph.insertEdge(parent, null, "", vDogsRoot, v3);
graph.insertEdge(parent, null, "", vDogsRoot, v4);
graph.insertEdge(parent, null, "", vDogsRoot, v5);
graph.insertEdge(parent, null, "", vDogsRoot, v6);
mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);
layout.setUseBoundingBox(false);
layout.execute(parent);
}
finally
{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}
public static void main(String[] args)
{
HelloWorld frame = new HelloWorld();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}
}
が生成されます:
素晴らしいスタートが、無崩壊ボタンここ
は偉大なレイアウトを与えるが、折りたたみをサポートしていないいくつかのサンプルコードです。次のコードは、私が抱えている問題を示しています。フォールディングをサポートするため、頂点の親をデフォルトの親からツリーのルートであるvDogVertexに変更してグループを作成しようとします。これは折りたたみ可能になりますが、すべての子頂点はvDogVertexの内側にあり、これはツリーのレイアウトを破壊します。
package com.mxgraph.examples.swing;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxGraph;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
public class HelloWorld extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -2707712944901661771L;
public HelloWorld()
{
super("Hello, puppies!");
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
//Notice this time the parent is the vDogsRoot vertex.
//This creates a cell group if I understand correctly.
Object vDogsRoot = graph.insertVertex(parent, null, "DOG", 0, 0, 80, 30, "");
Object v2 = graph.insertVertex(vDogsRoot, null, "Shar Pei", 0, 0, 80, 30, "");
Object v3 = graph.insertVertex(vDogsRoot, null, "Pug", 0, 0, 80, 30, "");
Object v4 = graph.insertVertex(vDogsRoot, null, "Cocker Spaniel", 0, 0, 80, 30, "");
Object v5 = graph.insertVertex(vDogsRoot, null, "Pit Bull", 0, 0, 80, 30, "");
Object v6 = graph.insertVertex(vDogsRoot, null, "Chihuahua", 0, 0, 80, 30, "");
graph.insertEdge(parent, null, "", vDogsRoot, v2);
graph.insertEdge(parent, null, "", vDogsRoot, v3);
graph.insertEdge(parent, null, "", vDogsRoot, v4);
graph.insertEdge(parent, null, "", vDogsRoot, v5);
graph.insertEdge(parent, null, "", vDogsRoot, v6);
mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);
layout.setUseBoundingBox(false);
layout.execute(vDogsRoot); //apply the layout to the root group node.
layout.execute(parent);
}
finally
{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}
public static void main(String[] args)
{
HelloWorld frame = new HelloWorld();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}
}
が生成されます 私はセルグループの親細胞の内部であることから頂点を防ぐにはどうすればよい(崩壊ボタンを気づか)
を?私は、ツリー階層を維持して折りたたむ必要があります。セルグループを使用して正しいパスにいますか?私が間違っていることは何ですか?
私はセルグループ(vDogsRoot)の親に、境界線の外にセルを描画できるようにする必要があると思いますが、まだこれを行う方法はありません。あるいは、私はまったく間違ったアプローチを取っています。これを考えるのは簡単なことではありませんが、私はまだ多くのことを試してきました。
更新1:
グループは私がここで必要としているものではありません。私は、指示木をたどり、選択されたノードの下にあるノードを表示するように切り替えるだけです。 mxGraph examplesフォルダにtree.htmlという名前のJavaスクリプトのサンプルが見つかりました。その例をJavaScriptからJavaに変換するだけです。