package com.intellij.openapi.graph.builder.actions.layout;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.graph.GraphManager;
import com.intellij.openapi.graph.anim.AnimationFactory;
import com.intellij.openapi.graph.anim.AnimationPlayer;
import com.intellij.openapi.graph.base.Graph;
import com.intellij.openapi.graph.builder.actions.AbstractGraphAction;
import com.intellij.openapi.graph.builder.actions.AbstractGraphToggleAction;
import com.intellij.openapi.graph.builder.util.GraphViewUtil;
import com.intellij.openapi.graph.layout.GraphLayout;
import com.intellij.openapi.graph.layout.Layouter;
import com.intellij.openapi.graph.settings.GraphSettingsProvider;
import com.intellij.openapi.graph.view.Graph2D;
import com.intellij.openapi.graph.view.Graph2DView;
import com.intellij.openapi.graph.view.LayoutMorpher;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
/**
* User: Sergey.Vasiliev
*/
public abstract class AbstractLayoutAction extends AbstractGraphToggleAction {
protected AbstractLayoutAction() {
super(null);
}
protected AbstractLayoutAction(Graph2D graph) {
super(graph, null);
}
protected boolean isSelected(Graph2D graph, final Project project, final AnActionEvent event) {
return GraphSettingsProvider.getInstance(project).getSettings(graph).getCurrentLayouter() == getLayouter(graph, project);
}
protected void setSelected(Graph2D graph, boolean state, final Project project, final AnActionEvent e) {
final Graph2DView view = AbstractGraphAction.getGraph2DView(graph);
final Layouter layouter = getLayouter(graph, project);
GraphSettingsProvider.getInstance(project).getSettings(graph).setCurrentLayouter(layouter);
doLayout(view, layouter, project);
}
protected String getText(final @NotNull Graph2D graph) {
return getLayoutName();
}
public static void doLayout(final Graph2DView view, Layouter layouter, final Project project) {
if (GraphSettingsProvider.getInstance(project).getSettings(view.getGraph2D()).isFitContentAfterLayout()) {
GraphManager.getGraphManager().createBufferedLayouter(layouter).doLayout(view.getGraph2D());
view.fitContent();
} else {
// with animation
GraphLayout layout = GraphManager.getGraphManager().createBufferedLayouter(layouter).calcLayout(view.getGraph2D());
LayoutMorpher morpher = GraphManager.getGraphManager().createLayoutMorpher(view, layout);
morpher.setPreferredDuration(800);
morpher.setKeepZoomFactor(true);
final AnimationPlayer player = GraphManager.getGraphManager().createAnimationPlayer();
player.addAnimationListener(view);
player.setFps(1200);
player.animate(AnimationFactory.Statics.createEasedAnimation(morpher));
GraphViewUtil.updateWorldRect(view);
}
view.getGraph2D().updateViews();
}
public void update(AnActionEvent e) {
final Presentation presentation = e.getPresentation();
final Graph2D graph = getGraph(e);
final Project project = getProject(e);
presentation.setEnabled(project!= null && graph != null && graph.getNodeArray().length > 0 && getLayouter(graph, project).canLayout(graph));
}
abstract protected Layouter getLayouter(Graph graph, final Project project);
abstract protected String getLayoutName();
}