listeners = new ToolEventListener[1];
listeners[0] = new NodeClickEventListener() {
public void clickNodes(Node[] nodes) {
try {
Node n = nodes[0];
Color[] colors;
float[] positions;
if (heatMapPanel.isUsePalette()) {
colors = heatMapPanel.getSelectedPalette().getColors();
positions = heatMapPanel.getSelectedPalette().getPositions();
dontPaintUnreachable = true;
} else {
gradientColors = colors = heatMapPanel.getGradientColors();
gradientPositions = positions = heatMapPanel.getGradientPositions();
dontPaintUnreachable = heatMapPanel.isDontPaintUnreachable();
}
GraphController gc = Lookup.getDefault().lookup(GraphController.class);
AbstractShortestPathAlgorithm algorithm;
if (gc.getModel().getGraphVisible() instanceof DirectedGraph) {
DirectedGraph graph = (DirectedGraph) gc.getModel().getGraphVisible();
algorithm = new BellmanFordShortestPathAlgorithm(graph, n);
algorithm.compute();
} else {
Graph graph = gc.getModel().getGraphVisible();
algorithm = new DijkstraShortestPathAlgorithm(graph, n);
algorithm.compute();
}
//Color
LinearGradient linearGradient = new LinearGradient(colors, positions);
//Algorithm
double maxDistance = algorithm.getMaxDistance();
if (!dontPaintUnreachable) {
maxDistance++; //+1 to have the maxdistance nodes a ratio<1
}
if (maxDistance > 0) {
for (Entry<Node, Double> entry : algorithm.getDistances().entrySet()) {
NodeData node = entry.getKey().getNodeData();
if (!Double.isInfinite(entry.getValue())) {
float ratio = (float) (entry.getValue() / maxDistance);
Color c = linearGradient.getValue(ratio);
node.setColor(c.getRed() / 255f, c.getGreen() / 255f, c.getBlue() / 255f);
} else if (!dontPaintUnreachable) {
Color c = colors[colors.length - 1];
node.setColor(c.getRed() / 255f, c.getGreen() / 255f, c.getBlue() / 255f);
}
}
}
Color c = colors[0];
n.getNodeData().setColor(c.getRed() / 255f, c.getGreen() / 255f, c.getBlue() / 255f);
heatMapPanel.setStatus(NbBundle.getMessage(HeatMap.class, "HeatMap.status.maxdistance") + new DecimalFormat("#.##").format(algorithm.getMaxDistance()));
} catch (Exception e) {
e.printStackTrace();
}
}