Menu

[r49]: / GraphScript / trunk / ls / graph / Controller.java  Maximize  Restore  History

Download this file

203 lines (177 with data), 4.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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;
}
}
MongoDB Logo MongoDB