package ls.graph;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import ls.graph.script.GraphScriptEngine;
import ls.graph.script.ScriptableGraph;
import ls.graph.serialize.DefaultGraphReader;
import ls.graph.serialize.GraphReader;
import ls.graph.ui.MainWindow;
import ls.util.Utils;
import edu.uci.ics.jung.graph.DirectedEdge;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.graph.Edge;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.decorators.EdgeWeightLabeller;
import edu.uci.ics.jung.graph.decorators.EdgeWeightLabellerStringer;
import edu.uci.ics.jung.graph.impl.DirectedSparseEdge;
import edu.uci.ics.jung.graph.impl.DirectedSparseGraph;
import edu.uci.ics.jung.graph.impl.DirectedSparseVertex;
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);
}
public void fireScriptLoaded(File f, String code)
{
if (f != null && code != null)
for (ControlListener l : listeners)
{
l.scriptLoaded(f, code);
}
}
public void fireGraphLoaded(File f)
{
if (f != null)
for (ControlListener l : listeners)
l.graphLoaded(f);
}
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 void setProperty(String key,String value)
{
initProps.setProperty(key, value);
}
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;
}
public static ImageIcon getIcon(String path)
{
URL imgURL = Controller.class.getResource(path);
if (imgURL != null)
{
return new ImageIcon(imgURL);
}
else
{
Utils.err("Couldn't find file: " + path);
return null;
}
}
/**
* @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,boolean directed)
throws Exception
{
GraphGenerator gGen = new SimpleRandomGenerator(nodeCount,edgeCount);
Graph g = (Graph)gGen.generateGraph();
ScriptableGraph sg = null;
if (directed)
{
DirectedGraph dg = convertToDirected(g);
sg = new ScriptableGraph(dg);
}
else
sg = new ScriptableGraph(g);
setNewGraph(sg);
}
private DirectedGraph convertToDirected(Graph g)
{
if (g == null) return null;
DirectedGraph dg = new DirectedSparseGraph();
Map<Vertex,DirectedSparseVertex> vMap = new HashMap<Vertex,DirectedSparseVertex>();
for (Vertex v : ((Set<Vertex>)g.getVertices()))
{
DirectedSparseVertex dv = new DirectedSparseVertex();
dv.importUserData(v);
vMap.put(v, dv);
dg.addVertex(dv);
}
for (Edge e : ((Set<Edge>)g.getEdges()))
{
Vertex v = (Vertex)(e.getEndpoints().getFirst());
DirectedSparseVertex dv1 = vMap.get(v);
v = (Vertex)(e.getEndpoints().getSecond());
DirectedSparseVertex dv2 = vMap.get(v);
if (dv1 == null || dv2 == null)
{
Utils.err("While converting to a directed graph - one of the vertices is missing");
continue;
}
else
{
DirectedEdge de = new DirectedSparseEdge(dv1,dv2);
dg.addEdge(de);
}
}
return dg;
}
public void setNewGraph(ScriptableGraph g)
{
if (g != null)
{
graph = g;
String weighted = getProperty("WEIGHTED","FALSE");
boolean isWeighted = "TRUE".equalsIgnoreCase(weighted);
if (isWeighted)
{
String sDefWeight = getProperty("DEF_EDGE_WEIGHT","1");
int defWeight = Integer.parseInt(sDefWeight);
EdgeWeightLabeller ewl = graph.getEdgeWeightLabeller();
for (Edge e : ((Set<Edge>)graph.getEdges()))
{
getGraph().setEdgeWeight(e, defWeight);
}
}
fireGraphChanged();
}
}
public void setGraphFromText(String gt)
{
BufferedReader br = new BufferedReader(new StringReader(gt));
GraphReader gr = new DefaultGraphReader();
try
{
ScriptableGraph sg = gr.read(br);
setNewGraph(sg);
}
catch (IOException exn)
{
Utils.exn(exn);
}
}
public static void msg(String m)
{
Utils.msg("SYSTEM: " + m);
}
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,false);
}
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;
}
}