All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datacleaner.widgets.visualization.JobGraphContext Maven / Gradle / Ivy

/**
 * DataCleaner (community edition)
 * Copyright (C) 2014 Free Software Foundation, Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.datacleaner.widgets.visualization;

import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.Set;

import org.datacleaner.api.OutputDataStream;
import org.datacleaner.job.builder.AnalysisJobBuilder;
import org.datacleaner.job.builder.ComponentBuilder;

import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.LayoutDecorator;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.picking.PickedState;

public class JobGraphContext {

    private final VisualizationViewer _visualizationViewer;
    private final JobGraph _jobGraph;
    private final AnalysisJobBuilder _analysisJobBuilder;

    public JobGraphContext(final JobGraph jobGraph, final VisualizationViewer visualizationViewer,
            final AnalysisJobBuilder analysisJobBuilder) {
        _jobGraph = jobGraph;
        _visualizationViewer = visualizationViewer;
        _analysisJobBuilder = analysisJobBuilder;
    }

    public VisualizationViewer getVisualizationViewer() {
        return _visualizationViewer;
    }

    public JobGraph getJobGraph() {
        return _jobGraph;
    }

    /**
     * Gets the main/primary/parent {@link AnalysisJobBuilder} in use on the
     * {@link JobGraph} canvas. Note that other {@link AnalysisJobBuilder}s may
     * exist if the job has components which publish {@link OutputDataStream}s.
     * Therefore the {@link #getAnalysisJobBuilder(Object)} method is preferred
     * if any vertex-specific interaction is needed.
     *
     * @return
     */
    public AnalysisJobBuilder getMainAnalysisJobBuilder() {
        return _analysisJobBuilder;
    }

    /**
     * Gets the {@link AnalysisJobBuilder} object pertaining to a particular
     * vertex on the {@link JobGraph}.
     *
     * @param vertex
     * @return
     */
    public AnalysisJobBuilder getAnalysisJobBuilder(final Object vertex) {
        if (vertex instanceof ComponentBuilder) {
            return ((ComponentBuilder) vertex).getAnalysisJobBuilder();
        }
        return _analysisJobBuilder;
    }

    public Object getVertex(final MouseEvent me) {
        return getVertex(me.getPoint());
    }

    /**
     * Gets the vertex at a particular point, or null if it does not exist.
     *
     * @param point2D
     * @return
     */
    public Object getVertex(final Point2D point2D) {
        final GraphElementAccessor pickSupport = _visualizationViewer.getPickSupport();

        @SuppressWarnings("rawtypes") final Layout graphLayout = _visualizationViewer.getModel().getGraphLayout();

        @SuppressWarnings("unchecked") final Object vertex = pickSupport.getVertex(graphLayout, point2D.getX(), point2D.getY());

        return vertex;
    }

    public AbstractLayout getGraphLayout() {
        Layout layout = getVisualizationViewer().getGraphLayout();
        while (layout instanceof LayoutDecorator) {
            layout = ((LayoutDecorator) layout).getDelegate();
        }
        return (AbstractLayout) layout;
    }

    public Set getSelectedVertices() {
        final PickedState pickedVertexState = _visualizationViewer.getPickedVertexState();
        if (pickedVertexState == null) {
            return null;
        }
        return pickedVertexState.getPicked();
    }

}