package ls.graph.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
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.filechooser.FileNameExtensionFilter;
import ls.graph.Main;
import ls.graph.script.ScriptUIUtils;
import ls.graph.script.ScriptableVertexStringer;
import ls.script.ConfigurableScriptEngine;
import ls.script.ExecuteScriptJob;
import ls.util.Utils;
import ls.util.msg.MessageManager;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.decorators.StringLabeller;
import edu.uci.ics.jung.graph.decorators.ToStringLabeller;
import edu.uci.ics.jung.graph.decorators.VertexPaintFunction;
import edu.uci.ics.jung.visualization.DefaultGraphLabelRenderer;
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.SpringLayout;
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 JTextPane txtScript;
private JFileChooser fileChooser;
public MainWindow() throws HeadlessException
{
super();
fileChooser = new JFileChooser();
String initialDir = Main.getProperty("SCRIPTS_DIR","C:\\");
fileChooser.setCurrentDirectory(new File(initialDir));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
UIMessagesHandler msgHandler = new UIMessagesHandler(this);
MessageManager.registerErrorHandler(msgHandler);
MessageManager.registerMessageHandler(msgHandler);
}
private final int INITIAL_WINDOW_W = 800;
private final int INITIAL_WINDOW_H = 600;
/**
* Get the list of files to be included
* @return
*/
private List<File> getScriptIncludes()
{
List<File> l = scriptSettingsPanel.getScriptIncludes();
return l;
}
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");
String scriptText = txtScript.getText();
ExecuteScriptJob job = new ExecuteScriptJob(scriptText,scriptEngine, getScriptIncludes());
Thread t = new Thread(job);
t.start();
}
} //end of ExecuteAction class
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();
}
}
private final class LoadScriptAction extends AbstractAction
{
private static final long serialVersionUID = 1L;
private Component parent = null;
LoadScriptAction(Component p)
{
super();
this.parent = p;
putValue(NAME,"Load Script");
}
public void actionPerformed(ActionEvent arg0)
{
fileChooser.setFileFilter(new FileNameExtensionFilter("Javascript files","js"));
if (fileChooser.showOpenDialog(this.parent) == JFileChooser.APPROVE_OPTION)
{
File f = fileChooser.getSelectedFile();
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(f));
StringBuffer sb = new StringBuffer();
String line = br.readLine();
while (line != null)
{
sb.append(line).append('\n');
line = br.readLine();
}
txtScript.setText(sb.toString());
}
catch (IOException exn)
{
Utils.err(exn.getMessage());
}
finally
{
try { if (br != null) br.close(); }
catch (IOException exn) { Utils.exn(exn); }
}
}
}
} //end of LoadScriptAction class
private final class SaveScriptAction extends AbstractAction
{
private static final long serialVersionUID = 1L;
Component parent = null;
SaveScriptAction(Component p)
{
super();
this.parent = p;
putValue(NAME, "Save Script");
}
public void actionPerformed(ActionEvent arg0)
{
fileChooser.setFileFilter(new FileNameExtensionFilter("Javascript files","js"));
int res = fileChooser.showSaveDialog(this.parent);
if (res == JFileChooser.APPROVE_OPTION)
{
File f = fileChooser.getSelectedFile();
SaveScriptJob saveJob = new SaveScriptJob(f,txtScript.getText());
Thread t = new Thread(saveJob);
t.start();
}
}
} //end of SaveScriptAction class
private ConfigurableScriptEngine scriptEngine = null;
private ScriptSettingsPanel scriptSettingsPanel;
private ScriptUIUtils scriptUIUtils = null;
public MainWindow(ConfigurableScriptEngine engine,Graph g,ScriptUIUtils _suu)
{
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);
}
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);
// 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(spltGraphScript,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) {}
});
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);
}
private Container createRightPanel()
{
JTabbedPane tabbedPane = new JTabbedPane();
scriptSettingsPanel = new ScriptSettingsPanel();
tabbedPane.add("Script Settings", scriptSettingsPanel);
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));
JScrollPane scrollPanel = new JScrollPane(txtScript);
scriptPane.add(scrollPanel,BorderLayout.CENTER);
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());
JButton btnSave = new JButton(new SaveScriptAction(this));
JButton btnLoad = new JButton(new LoadScriptAction(this));
controlPane.add(btnLoad);
controlPane.add(btnSave);
controlPane.add(btnExecute);
controlPane.add(btnExit);
return controlPane;
}
}