package ls.graph;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import javax.swing.JFrame;
import ls.graph.script.GraphScriptEngine;
import ls.graph.script.ScriptableGraph;
import ls.graph.ui.MainWindow;
import ls.util.Utils;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.random.generators.GraphGenerator;
import edu.uci.ics.jung.random.generators.SimpleRandomGenerator;
public class Controller
{
// private SwingEngine swingEngine;
// private JSplitPane spltMain;
// private ScriptPanel scriptPanel;
private static Properties initProps = new Properties();
private ScriptableGraph graph = null;
private GraphScriptEngine graphScriptEngine = null;
private List<ControlListener> listeners = new LinkedList<ControlListener>();
static
{
BufferedReader br = null;
try
{
File f = new File("graphscript.ini");
br = new BufferedReader(new FileReader(f));
initProps.load(br);
}
catch (IOException exn)
{
Utils.err("while loading ini file: " + exn.getMessage());
}
finally
{
try { if (br != null) br.close(); }
catch (IOException exn) { Utils.exn(exn); }
}
}
private void start()
{
// graph = new ScriptableGraph(initGraph());
initGraph();
graphScriptEngine = new GraphScriptEngine(this);
String defScript = getDefaultScriptText();
// MainWindow mainWindow = new MainWindow(eng,graph,eng.getScriptUIUtils(),defScript);
MainWindow mainWindow = new MainWindow(this,defScript);
mainWindow.setExtendedState(JFrame.MAXIMIZED_BOTH);
mainWindow.setVisible(true);
}
public void addListener(ControlListener l)
{
this.listeners.add(l);
}
private void fireGraphChanged()
{
ScriptableGraph sg = getGraph();
for (ControlListener l : listeners)
l.graphChanged(sg);
}
private String getDefaultScriptText()
{
String defFilename = getProperty("DEFAULT_SCRIPT",null);
if (defFilename != null)
{
File f = new File(defFilename);
if (!f.isAbsolute())
{
String dir = getProperty("SCRIPTS_DIR",null);
if (dir == null) return null;
f = new File(dir + File.separator + defFilename);
}
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try
{
br = new BufferedReader(new FileReader(f));
String line = br.readLine();
while (line != null)
{
sb.append(line).append('\n');
line = br.readLine();
}
}
catch (IOException exn)
{
Utils.exn(exn);
return null;
}
finally
{
try {if (br != null) br.close(); }
catch (IOException exn) { Utils.exn(exn); }
}
return sb.toString();
}
return null;
}
public static String getProperty(String key)
{
String val = initProps.getProperty(key);
return val;
}
public static List<String> getPropertyValues(String key)
{
String sValues = getProperty(key,"");
if ("".equals(sValues) || sValues == null)
return Arrays.asList(new String[] {}); //return empty list
return Arrays.asList(sValues.split(","));
}
public static String getProperty(String key, String defVal)
{
String ret = getProperty(key);
return ret == null ? defVal : ret;
}
/**
* @param args
*/
public static void main(String[] args)
{
Controller m = new Controller();
m.start();
}
public ScriptableGraph getGraph()
{
return graph;
}
public void createNewRandomGraph(int nodeCount, int edgeCount)
throws Exception
{
GraphGenerator gGen = new SimpleRandomGenerator(nodeCount,edgeCount);
Graph g = (Graph)gGen.generateGraph();
graph = new ScriptableGraph(g);
fireGraphChanged();
}
private Graph initGraph()
{
int nc,ec;
try
{ nc = Integer.parseInt(getProperty("DEF_GRAPH_NODE_COUNT","5")); }
catch (NumberFormatException exn) {Utils.exn(exn); nc = 5;}
try
{ ec = Integer.parseInt(getProperty("DEF_GRAPH_EDGE_COUNT","8")); }
catch (NumberFormatException exn) {Utils.exn(exn); ec = 8;}
try
{
createNewRandomGraph(nc,ec);
}
catch (Exception exn)
{
Utils.exn(exn);
}
return graph;
}
// private void setGraph(Graph g)
// {
// if (!(g instanceof ScriptableGraph))
// this.graph = new ScriptableGraph(g);
// else
// this.graph = (ScriptableGraph)g;
// fireGraphChanged();
// }
public GraphScriptEngine getGraphScriptEngine()
{
return graphScriptEngine;
}
public void setGraphScriptEngine(GraphScriptEngine graphScriptEngine)
{
this.graphScriptEngine = graphScriptEngine;
}
}