package trust.jfcm.gui;
import java.awt.Component;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.UIManager;
import trust.jfcm.CognitiveMap;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
public class JGraph2DViewer extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -2707712944901661771L;
private Map<String, CognitiveMap> maps = null;
private mxGraph graph = null;
public JGraph2DViewer(Map<String, CognitiveMap> m)
{
super("Hello, World!");
maps = m;
getContentPane().add(getGraphComponent());
}
public JGraph2DViewer()
{
super("Hello, World!");
getContentPane().add(getGraphComponent());
}
public Component getGraphComponent(){
graph = new mxGraph();
graph.getModel().beginUpdate();
try
{
/*
String[] mapList = (String[]) maps.keySet().toArray(new String[0]);
for (String mapName : mapList){
String[] mapConcepts = maps.get(mapName).getConcepts().keySet().toArray(new String[0]);
for(int i=0; i<mapConcepts.length; i++){
graph.insertVertex(parent, null, mapConcepts[i], 20, 20+i*50, 80, 30);
}
}*/
/*
Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80,
30);
Object v2 = graph.insertVertex(parent, null, "World!", 240, 150,
80, 30);
graph.insertEdge(parent, null, "Edge", v1, v2);*/
}
finally
{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
return graphComponent;
}
public void update(){
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
String[] mapList = (String[]) maps.keySet().toArray(new String[0]);
for (String mapName : mapList){
String[] mapConcepts = maps.get(mapName).getConcepts().keySet().toArray(new String[0]);
for(int i=0; i<mapConcepts.length; i++){
graph.insertVertex(parent, null, mapConcepts[i], 20, 20+i*50, 80, 30);
}
}
}
finally
{
graph.getModel().endUpdate();
}
}
public void setMap(Map<String, CognitiveMap> m){
maps = m;
getContentPane().add(getGraphComponent());
update();
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e1)
{
e1.printStackTrace();
}
JGraph2DViewer frame = new JGraph2DViewer();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}
}