package ls.graph.ui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.random.generators.GraphGenerator;
import edu.uci.ics.jung.random.generators.SimpleRandomGenerator;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.Layout;
import edu.uci.ics.jung.visualization.PluggableRenderer;
import edu.uci.ics.jung.visualization.Renderer;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.contrib.CircleLayout;
public class MainWindow extends JFrame
{
private static final long serialVersionUID = 1L;
private Graph graph = null;
private Layout graphLayout = null;
private final class ExecuteAction extends AbstractAction
{
private static final long serialVersionUID = 1L;
ExecuteAction()
{
putValue(NAME, "Execute");
}
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(null, "Execute event");
}
}
private final class ExitAction extends AbstractAction
{
private static final long serialVersionUID = 1L;
ExitAction()
{
putValue(NAME, "Exit");
}
public void actionPerformed(ActionEvent e)
{
MainWindow.this.setVisible(false);
MainWindow.this.dispose();
}
}
public MainWindow()
{
super();
loadGraph();
initUI();
setLocation(200,200);
setPreferredSize(new Dimension(400,600));
setSize(600, 400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void loadGraph()
{
GraphGenerator gGen = new SimpleRandomGenerator(5,4);
this.graph = (Graph)gGen.generateGraph();
}
private JComponent getGraphComponent()
{
graphLayout = new CircleLayout(this.graph);
graphLayout.initialize(new Dimension(300,300));
Renderer r = new PluggableRenderer();
VisualizationViewer vv = new VisualizationViewer(graphLayout,r);
GraphZoomScrollPane zoomPane = new GraphZoomScrollPane(vv);
return zoomPane;
}
private void initUI()
{
setTitle("Graph Script");
getContentPane().setLayout(new BorderLayout());
JSplitPane spltMain =
new JSplitPane(JSplitPane.VERTICAL_SPLIT,
this.getGraphComponent(),
createScriptPane());
spltMain.setDividerLocation(200);
spltMain.setResizeWeight(0.5);
getContentPane().add(spltMain,BorderLayout.CENTER);
getContentPane().add(createControlPane(),BorderLayout.SOUTH);
this.addComponentListener(new ComponentListener()
{
public void componentHidden(ComponentEvent evt){}
public void componentMoved(ComponentEvent evt) {}
public void componentResized(ComponentEvent evt)
{
//graphLayout.resize(evt.getComponent().getSize());
}
public void componentShown(ComponentEvent evt) {}
});
}
private JPanel createScriptPane()
{
JPanel scriptPane = new JPanel();
JTextArea txtScript = new JTextArea("text");
scriptPane.add(txtScript);
return scriptPane;
}
private JPanel createControlPane()
{
JPanel controlPane = new JPanel();
controlPane.setLayout(new FlowLayout());
JButton btnExecute = new JButton(new ExecuteAction());
JButton btnExit = new JButton(new ExitAction());
controlPane.add(btnExecute);
controlPane.add(btnExit);
return controlPane;
}
}