Menu

[r59]: / GraphScript / trunk / ls / graph / ui / MainWindow.java  Maximize  Restore  History

Download this file

372 lines (328 with data), 10.7 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package ls.graph.ui;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Collection;
import java.util.List;
import javax.swing.BorderFactory;
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.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.TabSet;
import javax.swing.text.TabStop;
import ls.graph.ControlListener;
import ls.graph.Controller;
import ls.graph.script.GraphScriptEngine;
import ls.graph.script.ScriptUIUtils;
import ls.graph.script.ScriptableGraph;
import ls.ui.msg.MessagePanel;
import ls.util.Utils;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.decorators.VertexStrokeFunction;
import edu.uci.ics.jung.visualization.GraphMouseListener;
import edu.uci.ics.jung.visualization.Layout;
import edu.uci.ics.jung.visualization.PickedState;
import edu.uci.ics.jung.visualization.PluggableRenderer;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.contrib.CircleLayout;
public class MainWindow extends JFrame implements ControlListener
{
private static final long serialVersionUID = 1L;
private Layout graphLayout = null;
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 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();
String initialDir = Controller.getProperty("SCRIPTS_DIR","C:\\");
fileChooser.setCurrentDirectory(new File(initialDir));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
msgHandler = new UIMessagesHandler(this);
}
public MainWindow(Controller c,String defaultScript)
{
this();
this.controller = c;
this.controller.addListener(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);
}
}
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();
}
private JComponent getGraphComponent()
{
graphLayout = getCurrentGraphLayout();
if (graphLayout == null)
graphLayout = new CircleLayout(getGraph());
Dimension d = new Dimension((int)(INITIAL_WINDOW_W * 0.7),(int)(INITIAL_WINDOW_H * 0.7));
graphLayout.initialize(d);
PluggableRenderer r = new PluggableRenderer();
ScriptUIUtils suu = controller.getGraphScriptEngine().getScriptUIUtils();
r.setVertexPaintFunction(suu.SVPF());
r.setVertexStringer(suu.SVS());
vertexStrokeFunction = new GSVertexStrokeFunction();
r.setVertexStrokeFunction(vertexStrokeFunction);
VisualizationViewer vv = new VisualizationViewer(graphLayout,r);
vv.addGraphMouseListener(new GSMouseListener());
suu.setVisualizationViewer(vv);
//Check to see if we should display the default node labels
String showNodeLabels = Controller.getProperty("SHOW_NODE_LABELS", "FALSE");
if ("TRUE".equalsIgnoreCase(showNodeLabels))
{
for (Vertex v : (Collection<Vertex>)(getGraph().getVertices()))
suu.setVertexLabel(v, v.toString());
}
JScrollPane zoomPane = new JScrollPane(vv);
return zoomPane;
//return vv;
}
GSVertexStrokeFunction vertexStrokeFunction = null;
private JPanel scriptPane;
private GraphSettingsPanel graphPanel;
private class GSVertexStrokeFunction implements VertexStrokeFunction
{
Vertex pickedVertex = null;
private final Stroke PICKED_STROKE = new BasicStroke(3.0f);
private final Stroke NON_PICKED_STROKE = new BasicStroke(1.0f);
//private ConstantVertexStrokeFunction cvsf = new ConstantVertexStrokeFunction(1.0f);
void setPickedVertex(Vertex v)
{
pickedVertex = v;
}
public Stroke getStroke(Vertex v)
{
if (v == pickedVertex)
return PICKED_STROKE;
else
return NON_PICKED_STROKE;
}
} //end of GSVertexStrokeFunction class
private class GSMouseListener implements GraphMouseListener
{
public void graphClicked(Vertex v, MouseEvent me)
{
setPickedVertex(v);
}
public void graphPressed(Vertex v, MouseEvent me)
{
setPickedVertex(v);
}
public void graphReleased(Vertex v, MouseEvent me)
{}
private void setPickedVertex(Vertex v)
{
ScriptUIUtils suu =
controller
.getGraphScriptEngine()
.getScriptUIUtils();
VisualizationViewer vv = suu.getVisualizationViewer();
PickedState ps = vv.getPickedState();
ps.clearPickedVertices();
ps.pick(v, true);
vertexStrokeFunction.setPickedVertex(v);
}
} //end of GSMouseListener
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());
spltGraphScript = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
this.getGraphComponent(),
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
.getGraphScriptEngine()
.getScriptUIUtils()
.addAlgorithmListener(mnu);
}
Container createStatusPanel()
{
StatusPanel panel = new StatusPanel();
controller
.getGraphScriptEngine()
.getScriptUIUtils()
.addAlgorithmListener(panel);
return panel;
}
private Container createRightPanel()
{
JTabbedPane tabbedPane = new JTabbedPane();
scriptSettingsPanel = new ScriptSettingsPanel();
tabbedPane.add("Script Settings", scriptSettingsPanel);
MessagePanel msgPanel = new MessagePanel();
tabbedPane.add("Messages",msgPanel);
graphPanel = new GraphSettingsPanel(this);
getScriptEngine().getScriptUIUtils().addGraphUIListener(graphPanel);
tabbedPane.add("Graph",graphPanel);
return tabbedPane;
}
private JPanel createScriptPane()
{
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));
Style style = txtScript.addStyle("Script", null);
float tabLength = 7.0f;
style.addAttribute(
StyleConstants.FontConstants.TabSet,
new TabSet(new TabStop[]
{new TabStop(tabLength),
new TabStop(tabLength*2),
new TabStop(tabLength*3),
new TabStop(tabLength*4),
new TabStop(tabLength*5),
new TabStop(tabLength*6)}));
style.addAttribute(StyleConstants.FontConstants.FontFamily, "Courier New");
txtScript.setLogicalStyle(style);
JScrollPane scrollPanel = new JScrollPane(txtScript);
scriptPane.add(scrollPanel,BorderLayout.CENTER);
Border scriptPaneBorder = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Algorithm Code");
scriptPane.setBorder(scriptPaneBorder);
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
.getGraphScriptEngine()
.getScriptUIUtils()
.addAlgorithmListener(panel);
return panel;
}
GraphScriptEngine getScriptEngine()
{
return this.controller.getGraphScriptEngine();
}
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());
}
} //end of MainWindow class
MongoDB Logo MongoDB