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.HeadlessException;
import java.beans.PropertyVetoException;
import java.io.File;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import ls.graph.ControlListener;
import ls.graph.Controller;
import ls.graph.script.GraphScriptEngine;
import ls.graph.script.ScriptableGraph;
import ls.graph.script.ScriptableVertexStringer;
import ls.ui.msg.MessagePanel;
import ls.util.Utils;
import edu.uci.ics.jung.graph.Graph;
public class MainWindow extends JFrame implements ControlListener
{
private static final long serialVersionUID = 1L;
// JTextPane txtScript;
JFileChooser fileChooser;
private Controller controller = null;
private final int INITIAL_WINDOW_W = 800;
private final int INITIAL_WINDOW_H = 600;
private ScriptSettingsPanel scriptSettingsPanel;
private UIMessagesHandler msgHandler;
private JSplitPane spltGraphScript;
private GraphEditPanel graphEditPane;
private JDesktopPane desktopPane = null;
private static final String SCRIPT_PANE_TITLE_PREFIX = "Algorithm Code";
private MainWindow() throws HeadlessException
{
super();
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception exn)
{
Utils.exn(exn);
}
fileChooser = new JFileChooser();
final String defInitialDir = "C:" + File.separator;
String initialDir = Controller.getProperty("SCRIPTS_DIR",defInitialDir);
if (!initialDir.equals(defInitialDir))
initialDir = System.getProperty("user.dir") + initialDir;
fileChooser.setCurrentDirectory(new File(initialDir));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
msgHandler = new UIMessagesHandler(this);
desktopPane = new JDesktopPane();
desktopPane.setSize(INITIAL_WINDOW_W/10, INITIAL_WINDOW_H/10);
desktopPane.setAutoscrolls(true);
}
public MainWindow(Controller c,String defaultScript)
{
this();
this.controller = c;
this.controller.addControlListener(this);
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);
}
public void popup(String msg)
{
if (msgHandler != null) msgHandler.handleMsg(msg);
}
/**
* Get the list of files to be included
* @return
*/
List<File> getScriptIncludes()
{
List<File> l = scriptSettingsPanel.getScriptIncludes();
return l;
}
void setScriptText(String scriptText)
{
// if (txtScript != null)
// {
// txtScript.setText(scriptText);
// }
scriptPane.setScriptText(scriptText);
}
String getScriptText()
{
return scriptPane != null ? scriptPane.getScriptText() : null;
}
// ScriptableGraph getGraph()
// {
// return this.controller.getGraph();
// }
// private Layout getCurrentGraphLayout()
// {
// ScriptUIUtils suu = controller.getGraphScriptEngine().getScriptUIUtils();
// if (suu == null) return null;
// VisualizationViewer vv = suu.getVisualizationViewer();
// if (vv == null) return null;
// return vv.getGraphLayout();
// }
GSVertexStrokeFunction vertexStrokeFunction = null;
private ScriptPanel scriptPane;
private GraphSettingsPanel graphPanel;
// void setGraphLayout(Layout gLayout)
// {
// if (gLayout != null)
// this.controller
// .getGraphScriptEngine()
// .getScriptUIUtils()
// .getVisualizationViewer()
// .setGraphLayout(gLayout,true);
// }
private void initUI()
{
setTitle("Graph Script");
createMenuBar();
getContentPane().setLayout(new BorderLayout());
// GraphInternalFrame internalGraphFrame = new GraphInternalFrame(this);
// desktopPane.add(internalGraphFrame);
spltGraphScript = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
// this.getGraphComponent(),
// new JScrollPane(desktopPane),
desktopPane,
getLowerPane());
// 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);
}
private Component getLowerPane()
{
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
tabbedPane.addTab("Script",createScriptPane());
tabbedPane.addTab("Graph",getGraphEditPane());
return tabbedPane;
}
private Component getGraphEditPane()
{
graphEditPane = new GraphEditPanel(this);
return graphEditPane;
}
private void createMenuBar()
{
Menu mnu = new Menu(this);
this.setJMenuBar(mnu);
this.controller.addAlgorithmListener(mnu);
}
Container createStatusPanel()
{
StatusPanel panel = new StatusPanel();
controller.addAlgorithmListener(panel);
return panel;
}
private Container createRightPanel()
{
JTabbedPane tabbedPane = new JTabbedPane();
scriptSettingsPanel = new ScriptSettingsPanel();
tabbedPane.add("Script Settings", scriptSettingsPanel);
MessagePanel msgPanel = new MessagePanel();
Utils.addErrorHandler(msgPanel);
Utils.addMessageHandler(msgPanel);
tabbedPane.add("Messages",msgPanel);
graphPanel = new GraphSettingsPanel(this);
this.controller.addGlobalGraphUIListener(graphPanel);
tabbedPane.add("Graph",graphPanel);
AlgorithmSettingsPanel algorithmPanel = new AlgorithmSettingsPanel(this);
tabbedPane.add("Algorithm",algorithmPanel);
return tabbedPane;
}
private JPanel createScriptPane()
{
scriptPane = new ScriptPanel(this);
return scriptPane;
}
public void scriptLoaded(File scriptFile, String scriptText)
{
String newTitle = SCRIPT_PANE_TITLE_PREFIX + " (" + scriptFile.getName() + ")";
scriptPane.setBorder(BorderFactory.createTitledBorder(newTitle));
// scriptPaneBorder.setTitle(newTitle);
}
private JPanel createControlPane()
{
ControlPanel panel = new ControlPanel(this);
controller.addAlgorithmListener(panel);
return panel;
}
GraphScriptEngine getScriptEngine()
{
return this.controller.getCurrentGraphScriptEngine();
}
public void graphChanged(ScriptableGraph sg)
{
//refresh the graph display
// spltGraphScript.setLeftComponent(getGraphComponent());
//refresh the graph text display
graphEditPane.setGraph(sg);
}
Controller getController()
{
return controller;
}
public void graphLoaded(File graphFile)
{
if (graphFile != null)
graphEditPane.setGraphFilename(graphFile.getName());
}
/**
* Add a new graph window
*/
public GraphInternalFrame newGraphWindow(ScriptableGraph g)
{
//TODO: For now, just create new default functions - later - get from configuration
ScriptableVertexPaintFunction svpf = new ScriptableVertexPaintFunction(Color.RED);
ScriptableVertexStringer svs = new ScriptableVertexStringer();
GraphInternalFrame newFrame = new GraphInternalFrame(g,svpf,svs);
desktopPane.add(newFrame);
return newFrame;
}
public GraphInternalFrame selectedGraphFrame()
{
return ((GraphInternalFrame)this.desktopPane.getSelectedFrame());
}
public Graph getSelectedGraph() { return this.controller.selectedGraph(); }
public void graphUnloaded(ScriptableGraph sg)
{
JInternalFrame[] frames = this.desktopPane.getAllFrames();
if (frames.length > 0)
{
try
{
frames[0].setSelected(true);
}
catch (PropertyVetoException exn)
{
Utils.exn(exn);
}
}
}
} //end of MainWindow class