DjNetViewer.java
import fr.esrf.tangoatk.widget.util.interlock.NetEditor;
import fr.esrf.tangoatk.widget.util.interlock.NetObject;
import fr.esrf.tangoatk.widget.util.interlock.NetEditorListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Vector;
import java.io.IOException;
public class DjNetViewer extends NetEditor implements NetEditorListener {
class DijkstraInfo {
double dist; NetObject pred; }
NetObject[] bubbles; int nbBubble; NetObject theSource=null; NetObject theDest=null;
DjNetViewer(JFrame parent) {
super(parent);
setEditable(false);
setMoveableBubble(true);
addEditorListener(this);
}
public void loadFile(String fileName) throws IOException {
super.loadFile(fileName);
bubbles = new NetObject[getNetObjectNumber()];
nbBubble = 0;
for (int i = 0; i < getNetObjectNumber(); i++) {
if (getNetObjectAt(i).getType() == NetObject.OBJECT_BUBBLE) {
bubbles[nbBubble] = getNetObjectAt(i);
bubbles[nbBubble].setUserValue(new DijkstraInfo());
nbBubble++;
}
}
theSource = bubbles[0];
theDest = bubbles[nbBubble - 1];
computeDijkstra();
}
public void paintLinks(Graphics2D g2) {
super.paintLinks(g2);
if (theDest != null) {
NetObject dst = theDest;
g2.setColor(Color.ORANGE);
while (getDI(dst).pred != null) {
getDI(dst).pred.paintLink(g2, dst, true);
dst = getDI(dst).pred;
}
}
}
private double distance(NetObject n1,NetObject n2) {
double x = ( n2.getXOrigin() - n1.getXOrigin() );
double y = ( n2.getYOrigin() - n1.getYOrigin() );
return Math.sqrt(x*x+y*y);
}
private DijkstraInfo getDI(NetObject o) {
return (DijkstraInfo)o.getUserValue();
}
private Vector initDijkstra() {
Vector S = new Vector();
for(int i=0;i<nbBubble;i++) {
if( theSource.isParentOf(bubbles[i]) ) {
getDI(bubbles[i]).dist = distance(theSource,bubbles[i]);
getDI(bubbles[i]).pred = theSource;
} else {
getDI(bubbles[i]).dist = Double.POSITIVE_INFINITY;
getDI(bubbles[i]).pred = null;
}
S.add(bubbles[i]);
}
getDI(theSource).dist = 0.0;
S.remove(theSource);
return S;
}
private void computeDijkstra() {
int i;
NetObject s;
double minDist;
Vector S = initDijkstra();
do {
for( minDist = Double.POSITIVE_INFINITY , s = null , i=0 ; i < S.size() ; i++ ) {
NetObject toDo = (NetObject)S.get(i);
if(getDI(toDo).dist < minDist) {
s = toDo;
minDist = getDI(toDo).dist;
}
}
if(s!=null) {
S.remove(s);
for(i=0;i < s.getChildrenNumber();i++) {
NetObject t = s.getChildAt(i);
double d = distance(s,t);
if( getDI(s).dist + d < getDI(t).dist ) {
getDI(t).dist = getDI(s).dist + d;
getDI(t).pred = s;
}
}
}
} while(s!=null);
updateBubble();
}
private void updateBubble() {
for(int i=0;i < nbBubble;i++) {
if( bubbles[i].equals(theSource) )
bubbles[i].setColor(Color.BLUE);
else if ( bubbles[i].equals(theDest) )
bubbles[i].setColor(Color.CYAN);
else
bubbles[i].setColor(Color.GRAY);
double d = getDI(bubbles[i]).dist;
if( d==Double.POSITIVE_INFINITY ) {
bubbles[i].setColor(Color.RED);
bubbles[i].setCenterLabel("No");
} else {
bubbles[i].setCenterLabel(Integer.toString((int)d));
}
}
repaint();
}
public void valueChanged(NetEditor src) {
if( nbBubble>0 ) {
computeDijkstra();
}
}
public void objectClicked(NetEditor src,NetObject obj,MouseEvent e) {
if( obj.getType()!=NetObject.OBJECT_BUBBLE )
return;
if( e.getButton()==MouseEvent.BUTTON1 ) {
theSource = obj;
computeDijkstra();
}
if( e.getButton()==MouseEvent.BUTTON3 ) {
theDest = obj;
computeDijkstra();
}
}
public void linkClicked(NetEditor src,NetObject obj,int childIdx,MouseEvent e) {}
public void sizeChanged(NetEditor src,Dimension d) {}
public void cancelCreate(NetEditor src) {}
}