Menu

[r66]: / ls / graph / script / ScriptUIUtils.java  Maximize  Restore  History

Download this file

155 lines (130 with data), 3.9 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
package ls.graph.script;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.swing.JOptionPane;
import ls.graph.ui.MainWindow;
import ls.graph.ui.ScriptableVertexPaintFunction;
import ls.script.Scriptable;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.visualization.PickedState;
import edu.uci.ics.jung.visualization.VisualizationViewer;
public class ScriptUIUtils implements Scriptable
{
public String getBindingName()
{
return "GraphUI"; //assumption - only one instance per script engine
}
private MainWindow parentWindow = null;
private List<AlgorithmListener> algorithmListeners = new LinkedList<AlgorithmListener>();
private List<GraphUIListener> graphUIListeners = new LinkedList<GraphUIListener>();
public void addGraphUIListener(GraphUIListener l)
{
if (l != null) graphUIListeners.add(l);
}
public void popup(String msg)
{
if (msg == null) msg = "null";
JOptionPane.showMessageDialog(
this.getParentWindow(), msg,"Info",JOptionPane.INFORMATION_MESSAGE);
}
// Graph graph = null;
ScriptableVertexPaintFunction svpf = null;
ScriptableVertexStringer svs = null;
VisualizationViewer visualizationViewer = null;
ScriptUIUtils(Graph g, ScriptableVertexPaintFunction _svpf, ScriptableVertexStringer _svs)
{
super();
this.svpf = _svpf;
this.svs = _svs;
}
public ScriptableVertexPaintFunction SVPF() { return this.svpf; }
public ScriptableVertexStringer SVS() { return this.svs; }
public void setVertexColor(Vertex v,VertexColors vc)
{
this.SVPF().setColor(v, vc);
refreshGraph();
}
public void refreshGraph()
{
VisualizationViewer vv = this.getVisualizationViewer();
if (vv != null)
{
vv.repaint();
}
}
/**
* Retrieve the selected vertex. If a few are selected, the first one
* (as given by the set's operator) is returned - no order guaranteed.
* @return The selected vertex
*/
public Vertex getSelectedVertex()
{
VisualizationViewer vv = this.getVisualizationViewer();
PickedState ps = vv.getPickedState();
for (Vertex v : ((Set<Vertex>)ps.getPickedVertices()))
{
return v; //return the first vertex
}
return null;
}
public void setVertexLabel(Vertex v, String label)
{
this.SVS().setVertexLabel(v, label);
VisualizationViewer vv = this.getVisualizationViewer();
if (vv != null)
{
// vv.fireStateChanged();
vv.repaint();
}
}
public VisualizationViewer getVisualizationViewer()
{
return visualizationViewer;
}
public void setVisualizationViewer(VisualizationViewer visualizationViewer)
{
this.visualizationViewer = visualizationViewer;
this.visualizationViewer.getPickedState().addItemListener(new PickStateListener());
}
public void addAlgorithmListener(AlgorithmListener l)
{
if (l != null) this.algorithmListeners.add(l);
}
public Algorithm createAlgorithm()
{
Algorithm a = new Algorithm();
for (AlgorithmListener l : this.algorithmListeners) a.addListener(l);
return a;
}
public MainWindow getParentWindow()
{
return parentWindow;
}
public void setParentWindow(MainWindow parentWindow)
{
this.parentWindow = parentWindow;
}
public List<AlgorithmListener> getAlgorithmListeners()
{
return algorithmListeners;
}
private class PickStateListener implements ItemListener
{
public void itemStateChanged(ItemEvent evt)
{
Object o = evt.getItem();
Graph g = getVisualizationViewer().getGraphLayout().getGraph();
if (o instanceof Vertex)
fireVertexSelected(g,(Vertex)o);
}
}
private void fireVertexSelected(Graph g,Vertex v)
{
for (GraphUIListener l : this.graphUIListeners)
l.vertexSelected(g, v);
}
}
MongoDB Logo MongoDB