package ls.graph.ui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.io.File;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.TabSet;
import javax.swing.text.TabStop;
import ls.graph.Main;
import ls.graph.script.ScriptUIUtils;
import ls.script.ConfigurableScriptEngine;
import ls.ui.msg.MessagePanel;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.Layout;
import edu.uci.ics.jung.visualization.PluggableRenderer;
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;
JTextPane txtScript;
JFileChooser fileChooser;
private MainWindow() throws HeadlessException
{
super();
fileChooser = new JFileChooser();
String initialDir = Main.getProperty("SCRIPTS_DIR","C:\\");
fileChooser.setCurrentDirectory(new File(initialDir));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
msgHandler = new UIMessagesHandler(this);
//MessageManager.registerErrorHandler(msgHandler);
//MessageManager.registerMessageHandler(msgHandler);
}
public void popup(String msg)
{
if (msgHandler != null) msgHandler.handleMsg(msg);
}
private final int INITIAL_WINDOW_W = 800;
private final int INITIAL_WINDOW_H = 600;
/**
* Get the list of files to be included
* @return
*/
List<File> getScriptIncludes()
{
List<File> l = scriptSettingsPanel.getScriptIncludes();
return l;
}
ConfigurableScriptEngine scriptEngine = null;
private ScriptSettingsPanel scriptSettingsPanel;
private ScriptUIUtils scriptUIUtils = null;
private UIMessagesHandler msgHandler;
public MainWindow(ConfigurableScriptEngine engine,Graph g,ScriptUIUtils _suu,String defaultScript)
{
this();
this.scriptUIUtils = _suu;
this.graph = g;
// loadGraph();
this.scriptEngine = engine;
initUI();
setLocation(100,100);
setPreferredSize(new Dimension(400,600));
setSize(INITIAL_WINDOW_W, INITIAL_WINDOW_H);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
if (defaultScript != null)
setScriptText(defaultScript);
}
void setScriptText(String scriptText)
{
if (txtScript != null)
{
txtScript.setText(scriptText);
}
}
private JComponent getGraphComponent()
{
graphLayout = new CircleLayout(this.graph);
// graphLayout = new SpringLayout(this.graph);
Dimension d = new Dimension((int)(INITIAL_WINDOW_W * 0.7),(int)(INITIAL_WINDOW_H * 0.7));
graphLayout.initialize(d);
PluggableRenderer r = new PluggableRenderer();
r.setVertexPaintFunction(this.scriptUIUtils.SVPF());
r.setVertexStringer(this.scriptUIUtils.SVS());
VisualizationViewer vv = new VisualizationViewer(graphLayout,r);
this.scriptUIUtils.setVisualizationViewer(vv);
// GraphZoomScrollPane zoomPane = new GraphZoomScrollPane(vv);
JScrollPane zoomPane = new JScrollPane(vv);
return zoomPane;
//return vv;
}
private void initUI()
{
setTitle("Graph Script");
getContentPane().setLayout(new BorderLayout());
JSplitPane spltGraphScript =
new JSplitPane(JSplitPane.VERTICAL_SPLIT,
this.getGraphComponent(),
createScriptPane());
spltGraphScript.setDividerLocation((int)(INITIAL_WINDOW_H * 0.7));
spltGraphScript.setResizeWeight(0.5);
getContentPane().add(createControlPane(),BorderLayout.SOUTH);
JSplitPane spltMain =
new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
spltGraphScript,
createRightPanel());
spltMain.setDividerLocation((int)(INITIAL_WINDOW_W * 0.7));
spltMain.setResizeWeight(0.5);
getContentPane().add(spltMain,BorderLayout.CENTER);
}
Container createStatusPanel()
{
StatusPanel panel = new StatusPanel();
this.scriptUIUtils.addAlgorithmListener(panel);
return panel;
}
private Container createRightPanel()
{
JTabbedPane tabbedPane = new JTabbedPane();
scriptSettingsPanel = new ScriptSettingsPanel();
tabbedPane.add("Script Settings", scriptSettingsPanel);
MessagePanel msgPanel = new MessagePanel();
tabbedPane.add("Messages",msgPanel);
return tabbedPane;
}
private JPanel createScriptPane()
{
JPanel scriptPane = new JPanel();
scriptPane.setLayout(new BorderLayout());
txtScript = new JTextPane();
txtScript.setEditable(true);
txtScript.setEnabled(true);
txtScript.setAlignmentY(0.0f);
//txtScript.setFont(new Font("Arial",Font.PLAIN,11));
Style style = txtScript.addStyle("Script", null);
float tabLength = 7.0f;
style.addAttribute(
StyleConstants.FontConstants.TabSet,
new TabSet(new TabStop[]
{new TabStop(tabLength),
new TabStop(tabLength*2),
new TabStop(tabLength*3),
new TabStop(tabLength*4),
new TabStop(tabLength*5),
new TabStop(tabLength*6)}));
style.addAttribute(StyleConstants.FontConstants.FontFamily, "Courier New");
txtScript.setLogicalStyle(style);
JScrollPane scrollPanel = new JScrollPane(txtScript);
scriptPane.add(scrollPanel,BorderLayout.CENTER);
return scriptPane;
}
private JPanel createControlPane()
{
ControlPanel panel = new ControlPanel(this);
this.scriptUIUtils.addAlgorithmListener(panel);
return panel;
}
}