use of org.eclipse.titanium.graph.components.NodeDescriptor in project titan.EclipsePlug-ins by eclipse.
the class GraphHandler method changeLayout.
/**
* This function changes the layout for the graph set in the
* {@link GraphHandler} class
*
* @param newLayout
* : The chosen layout's code
* @param newWindowSize
* : The size of the parent window where to draw (or any
* resolution bigger than this)
* @throws BadLayoutException On wrong layout code or bad graph
*/
public void changeLayout(final LayoutEntry newLayout, final Dimension newWindowSize) throws BadLayoutException {
if (g == null) {
throw new BadLayoutException("You must draw a graph before!", ErrorType.NO_OBJECT);
}
Dimension extSize = null;
if (g.getVertexCount() >= 20) {
extSize = new Dimension(newWindowSize.height * (g.getVertexCount() / 20), newWindowSize.width * (g.getVertexCount() / 20));
} else {
extSize = newWindowSize;
}
layout = new LayoutBuilder(g, newLayout, extSize).clusters(clusters).build();
actVisualisator = new CustomVisualizationViewer(layout, popupMenu);
actVisualisator.setPreferredSize(new Dimension(newWindowSize.width, newWindowSize.height));
actVisualisator.getRenderContext().setVertexLabelTransformer(NODE_LABELER);
final GraphRenderer<NodeDescriptor, EdgeDescriptor> rnd = new GraphRenderer<NodeDescriptor, EdgeDescriptor>(NODE_LABELER, actVisualisator.getPickedVertexState(), actVisualisator.getPickedEdgeState());
setNodeRenderer(rnd, actVisualisator);
renderer = rnd;
actVisualisator.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
actVisualisator.setBackground(Color.white);
actVisualisator.setDoubleBuffered(false);
initSatView();
}
use of org.eclipse.titanium.graph.components.NodeDescriptor in project titan.EclipsePlug-ins by eclipse.
the class GraphHandler method saveToImage.
/**
* Exports the graph set for this class to a PNG file
*
* @param path
* : The PNG file's path
* @param mode
* : The way of export, see {@link GraphHandler}
* <code>public static</code> fields for possible values (EXPORT_
* named fields)
* @param size
* : This parameter sets the size of the exported image in pixels
* @throws Exception
* on file handling error
*/
public void saveToImage(final String path, final ImageExportType mode) throws BadLayoutException {
if (layout == null || actVisualisator == null) {
throw new BadLayoutException("Either the layout or the visuaizer is not set (is null)", ErrorType.NO_OBJECT);
}
VisualizationViewer<NodeDescriptor, EdgeDescriptor> tempVisualisator = null;
Dimension size = null;
switch(mode) {
case EXPORT_SEEN_GRAPH:
{
tempVisualisator = actVisualisator;
size = actVisualisator.getPreferredSize();
}
break;
case EXPORT_WHOLE_GRAPH:
{
layout = actVisualisator.getGraphLayout();
if (size == null) {
size = new Dimension(layout.getSize().width, layout.getSize().height);
}
final Function<NodeDescriptor, Point2D> trf = new Function<NodeDescriptor, Point2D>() {
@Override
public Point2D apply(final NodeDescriptor v) {
return layout.apply(v);
}
};
tempVisualisator = new VisualizationViewer<NodeDescriptor, EdgeDescriptor>(new LayoutBuilder(g, Layouts.LAYOUT_STATIC, size).transformer(trf).build());
tempVisualisator.setPreferredSize(size);
tempVisualisator.setSize(size);
tempVisualisator.getRenderContext().setVertexLabelTransformer(NODE_LABELER);
final GraphRenderer<NodeDescriptor, EdgeDescriptor> rnd = new GraphRenderer<NodeDescriptor, EdgeDescriptor>(NODE_LABELER, tempVisualisator.getPickedVertexState(), tempVisualisator.getPickedEdgeState());
setNodeRenderer(rnd, tempVisualisator);
tempVisualisator.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
tempVisualisator.setBackground(Color.white);
tempVisualisator.setDoubleBuffered(false);
}
break;
case EXPORT_SATELLITE:
{
tempVisualisator = satView;
size = tempVisualisator.getSize();
}
break;
default:
ErrorReporter.logError("Unexpected image export type " + mode);
return;
}
BufferedImage image;
final GUIErrorHandler errorHandler = new GUIErrorHandler();
try {
image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
} catch (OutOfMemoryError e) {
final long needed = (long) size.width * (long) size.height * 4;
String temp;
if (needed < 1024) {
temp = needed + " bytes";
} else if (needed < 1024 * 1024) {
temp = needed / 1024 + " Kbytes";
} else {
temp = needed / 1024 / 1024 + " Mbytes";
}
final String errorText = "Could not save an image of " + size.width + "*" + size.height + " size as there was not enough free memory (" + temp + ")";
errorHandler.reportErrorMessage(errorText);
ErrorReporter.logExceptionStackTrace(errorText, e);
return;
}
final Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
tempVisualisator.paint(g2);
g2.dispose();
try {
ImageIO.write(image, "png", new File(path));
} catch (IOException e) {
final String message = "Error while writing to file" + path;
ErrorReporter.logExceptionStackTrace(message, e);
errorHandler.reportException(message, e);
}
}
use of org.eclipse.titanium.graph.components.NodeDescriptor in project titan.EclipsePlug-ins by eclipse.
the class LayoutBuilder method build.
/**
* This method implements the building of the chosen layout with chosen parameters
* @return The built layout
* @throws BadLayoutException If the chosen layout doesn't exist (invalid code was provided)
*/
public Layout<NodeDescriptor, EdgeDescriptor> build() throws BadLayoutException {
Layout<NodeDescriptor, EdgeDescriptor> layout;
final String layoutCode = layoutEntry.getCode();
if (layoutCode.equals(Layouts.LAYOUT_ISOM.getCode())) {
layout = new TitaniumISOMLayout<NodeDescriptor, EdgeDescriptor>(g);
((TitaniumISOMLayout<NodeDescriptor, EdgeDescriptor>) layout).setMaxIterations(Activator.getDefault().getPreferenceStore().getInt(PreferenceConstants.NO_ITERATIONS));
} else if (layoutCode.equals(Layouts.LAYOUT_KK.getCode())) {
layout = new KKLayout<NodeDescriptor, EdgeDescriptor>(g);
((KKLayout<NodeDescriptor, EdgeDescriptor>) layout).setMaxIterations(Activator.getDefault().getPreferenceStore().getInt(PreferenceConstants.NO_ITERATIONS));
} else if (layoutCode.equals(Layouts.LAYOUT_FR.getCode())) {
layout = new FRLayout<NodeDescriptor, EdgeDescriptor>(g);
((FRLayout<NodeDescriptor, EdgeDescriptor>) layout).setAttractionMultiplier(0.6);
((FRLayout<NodeDescriptor, EdgeDescriptor>) layout).setRepulsionMultiplier(0.8);
((FRLayout<NodeDescriptor, EdgeDescriptor>) layout).setMaxIterations(Activator.getDefault().getPreferenceStore().getInt(PreferenceConstants.NO_ITERATIONS));
} else if (layoutCode.equals(Layouts.LAYOUT_SPRING.getCode())) {
layout = new SpringLayout<NodeDescriptor, EdgeDescriptor>(g);
} else if (layoutCode.equals(Layouts.LAYOUT_CIRCLE.getCode())) {
layout = new CircleLayout<NodeDescriptor, EdgeDescriptor>(g);
} else if (layoutCode.equals(Layouts.LAYOUT_RTDAG.getCode())) {
layout = new ReverseDAGLayout<NodeDescriptor, EdgeDescriptor>(g, size);
} else if (layoutCode.equals(Layouts.LAYOUT_TDAG.getCode())) {
layout = new TitaniumDAGLayout<NodeDescriptor, EdgeDescriptor>(g, size);
} else if (layoutCode.equals(Layouts.METRIC_LAYOUT_CODE)) {
if (!(layoutEntry instanceof MetricsLayoutEntry)) {
throw new IllegalStateException("A metric must be chosen before using metric layout!");
}
layout = new MetricLayout<EdgeDescriptor>(g, size, ((MetricsLayoutEntry) layoutEntry).getMetric());
} else if (layoutCode.equals(Layouts.S_LAYOUT_CLUSTER)) {
if (clusters == null) {
throw new IllegalStateException("A clustering must be set before using cluster layout!");
}
final ClusterTransformer trf = new ClusterTransformer(new FRLayout<NodeDescriptor, EdgeDescriptor>(g), clusters, size);
layout = new StaticLayout<NodeDescriptor, EdgeDescriptor>(g, trf);
} else if ("STATIC".equals(layoutCode)) {
if (pointTransformer == null) {
throw new IllegalStateException("A point transformer must be set before using static layout!");
}
layout = new StaticLayout<NodeDescriptor, EdgeDescriptor>(g, pointTransformer);
} else {
throw new BadLayoutException("There is no such layout! (Layout=" + layoutCode + ")", ErrorType.NOT_EXISITING_LAYOUT);
}
layout.setSize(size);
return layout;
}
use of org.eclipse.titanium.graph.components.NodeDescriptor in project titan.EclipsePlug-ins by eclipse.
the class AutomaticCluster method calculatePriorities.
/**
* Calculates the priorities of all the nodes.
*/
private Queue<NodeDescriptor> calculatePriorities() {
final Map<NodeDescriptor, Integer> priority = new HashMap<NodeDescriptor, Integer>();
final PriorityQueue<NodeDescriptor> queue = new PriorityQueue<NodeDescriptor>(nodenum, new Comparator<NodeDescriptor>() {
@Override
public int compare(final NodeDescriptor v, final NodeDescriptor w) {
final int priorv = priority.get(v);
final int priorw = priority.get(w);
if (priorv < priorw) {
return -1;
} else if (priorv == priorw) {
return 0;
} else {
return 1;
}
}
});
for (final NodeDescriptor v : moduleGraph.getVertices()) {
final int prio = calculatePriority(v);
priority.put(v, prio);
queue.offer(v);
}
return queue;
}
use of org.eclipse.titanium.graph.components.NodeDescriptor in project titan.EclipsePlug-ins by eclipse.
the class AutomaticCluster method checkNode.
/**
* Finds a better cluster for the given node.
*
* @param v
* The node to be moved for better clustering
* @return True if the clustering did not change
*/
private boolean checkNode(final NodeDescriptor v) {
final int originalIndex = mapClusterIndex.get(v);
int bestIndex = mapClusterIndex.get(v);
double bestmq = MQ();
// if the cluster is too small, then move the node
if (size.get(originalIndex) == 1) {
bestmq = -1;
}
double currentmq;
// check current neighbouring clusters
for (final NodeDescriptor w : moduleGraph.getNeighbors(v)) {
final int newIndex = mapClusterIndex.get(w);
moveNode(v, newIndex);
currentmq = MQ();
if (currentmq > bestmq) {
bestmq = currentmq;
bestIndex = newIndex;
}
moveNode(v, originalIndex);
}
// check, if in a new cluster
if (clusternum < maxclusters) {
mapIndexCluster.put(index, new HashSet<NodeDescriptor>());
indices.add(index);
size.put(index, 0);
// add new row and column to matrix
final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (final int j : indices) {
map.put(j, 0);
}
mapBetweenArcs.put(index, map);
for (final int j : indices) {
mapBetweenArcs.get(j).put(index, 0);
}
moveNode(v, index);
currentmq = MQ();
if (currentmq > bestmq) {
bestmq = currentmq;
bestIndex = index;
}
moveNode(v, originalIndex);
// if not in a new empty cluster we can use the same index again
if (bestIndex != index) {
indices.remove(index);
} else {
index++;
}
}
// the new cluster of the node
moveNode(v, bestIndex);
mq = MQ();
return originalIndex == bestIndex;
}
Aggregations