Search in sources :

Example 1 with NodeDescriptor

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();
}
Also used : CustomVisualizationViewer(org.eclipse.titanium.graph.gui.common.CustomVisualizationViewer) NodeDescriptor(org.eclipse.titanium.graph.components.NodeDescriptor) Dimension(java.awt.Dimension) EdgeDescriptor(org.eclipse.titanium.graph.components.EdgeDescriptor)

Example 2 with NodeDescriptor

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);
    }
}
Also used : GUIErrorHandler(org.eclipse.titanium.error.GUIErrorHandler) NodeDescriptor(org.eclipse.titanium.graph.components.NodeDescriptor) Dimension(java.awt.Dimension) IOException(java.io.IOException) EdgeDescriptor(org.eclipse.titanium.graph.components.EdgeDescriptor) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) Function(com.google.common.base.Function) Point2D(java.awt.geom.Point2D) VisualizationViewer(edu.uci.ics.jung.visualization.VisualizationViewer) CustomVisualizationViewer(org.eclipse.titanium.graph.gui.common.CustomVisualizationViewer) File(java.io.File)

Example 3 with NodeDescriptor

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;
}
Also used : KKLayout(edu.uci.ics.jung.algorithms.layout.KKLayout) ClusterTransformer(org.eclipse.titanium.graph.clustering.gui.ClusterTransformer) MetricsLayoutEntry(org.eclipse.titanium.graph.gui.utils.MetricsLayoutEntry) FRLayout(edu.uci.ics.jung.algorithms.layout.FRLayout) NodeDescriptor(org.eclipse.titanium.graph.components.NodeDescriptor) StaticLayout(edu.uci.ics.jung.algorithms.layout.StaticLayout) EdgeDescriptor(org.eclipse.titanium.graph.components.EdgeDescriptor) ReverseDAGLayout(org.eclipse.titanium.graph.gui.layouts.ReverseDAGLayout) MetricLayout(org.eclipse.titanium.graph.gui.layouts.MetricLayout) SpringLayout(edu.uci.ics.jung.algorithms.layout.SpringLayout) TitaniumISOMLayout(org.eclipse.titanium.graph.gui.layouts.TitaniumISOMLayout)

Example 4 with NodeDescriptor

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;
}
Also used : HashMap(java.util.HashMap) NodeDescriptor(org.eclipse.titanium.graph.components.NodeDescriptor) PriorityQueue(java.util.PriorityQueue)

Example 5 with NodeDescriptor

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;
}
Also used : HashMap(java.util.HashMap) NodeDescriptor(org.eclipse.titanium.graph.components.NodeDescriptor)

Aggregations

NodeDescriptor (org.eclipse.titanium.graph.components.NodeDescriptor)30 EdgeDescriptor (org.eclipse.titanium.graph.components.EdgeDescriptor)10 HashSet (java.util.HashSet)7 HashMap (java.util.HashMap)5 Set (java.util.Set)5 Dimension (java.awt.Dimension)4 CustomVisualizationViewer (org.eclipse.titanium.graph.gui.common.CustomVisualizationViewer)3 DirectedSparseGraph (edu.uci.ics.jung.graph.DirectedSparseGraph)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 Point2D (java.awt.geom.Point2D)2 Matcher (java.util.regex.Matcher)2 JMenu (javax.swing.JMenu)2 JMenuItem (javax.swing.JMenuItem)2 IResource (org.eclipse.core.resources.IResource)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 ClusterNode (org.eclipse.titanium.graph.clustering.visualization.ClusterNode)2 LayoutEntry (org.eclipse.titanium.graph.gui.utils.LayoutEntry)2 MetricsLayoutEntry (org.eclipse.titanium.graph.gui.utils.MetricsLayoutEntry)2 Function (com.google.common.base.Function)1