/*
* Copyright 2000-2006 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.intellij.openapi.graph.builder.components;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.graph.GraphManager;
import com.intellij.openapi.graph.base.Graph;
import com.intellij.openapi.graph.builder.EdgeCreationPolicy;
import com.intellij.openapi.graph.builder.GraphBuilder;
import com.intellij.openapi.graph.builder.GraphPresentationModel;
import com.intellij.openapi.graph.builder.DeleteProvider;
import com.intellij.openapi.graph.builder.actions.ShowEdgeLabels;
import com.intellij.openapi.graph.builder.util.GraphViewUtil;
import com.intellij.openapi.graph.layout.Layouter;
import com.intellij.openapi.graph.settings.GraphSettingsProvider;
import com.intellij.openapi.graph.view.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.util.Iterator;
import java.util.Set;
public class BasicGraphPresentationModel<N, E> extends GraphPresentationModel<N, E> {
private Graph myGraph;
private GraphBuilder<N, E> myGraphBuilder;
private boolean myShowEdgeLabels;
public BasicGraphPresentationModel(final Graph graph) {
myGraph = graph;
}
public Graph getGraph() {
return myGraph;
}
public void setGraph(final Graph graph) {
myGraph = graph;
}
@NotNull
public NodeRealizer getNodeRealizer(final N n) {
return GraphManager.getGraphManager().createGraph2DNodeRealizer();
}
@NotNull
public EdgeRealizer getEdgeRealizer(final E e) {
final EdgeRealizer edgeRealizer = GraphManager.getGraphManager().createPolyLineEdgeRealizer();
edgeRealizer.setLineType(LineType.LINE_1);
edgeRealizer.setLineColor(Color.GRAY);
edgeRealizer.setArrow(Arrow.STANDARD);
return edgeRealizer;
}
public String getNodeTooltip(final N n) {
return null;
}
public String getEdgeTooltip(final E e) {
return null;
}
public boolean editNode(final N n) {
return false;
}
public boolean editEdge(final E e) {
return false;
}
public NodeCellEditor getCustomNodeCellEditor(final N n) {
return null;
}
public DefaultActionGroup getNodeActionGroup(final N n) {
return getCommonActionGroup();
}
public DefaultActionGroup getEdgeActionGroup(final E e) {
return getCommonActionGroup();
}
public DefaultActionGroup getPaperActionGroup() {
return getCommonActionGroup();
}
protected DefaultActionGroup getCommonActionGroup() {
DefaultActionGroup group = new DefaultActionGroup();
group.add(GraphViewUtil.getCommonPopupActions());
return group;
}
@NotNull
public Layouter getDefaultLayouter() {
final GraphBuilder<N, E> builder = getGraphBuilder();
if (builder != null) {
return GraphSettingsProvider.getInstance(builder.getProject()).getSettings(getGraph()).getCurrentLayouter();
}
else {
return GraphManager.getGraphManager().createHierarchicGroupLayouter();
}
}
public void dispose() {
GenericNodeRealizer.Factory factory = GenericNodeRealizer.Statics.getFactory();
final Set configurations = factory.getAvailableConfigurations();
for (Object object : configurations.toArray(new Object[configurations.size()])) {
if (object instanceof String) {
factory.removeConfiguration((String)object);
}
}
}
public EdgeLabel[] getEdgeLabels(final E e, final String edgeName) {
if (!isShowEdgeLabels()) return EMPTY_LABELS;
final EdgeLabel label = createLabel(edgeName);
// add an auto rotating label
if (isAutoRotateLabels()) {
final AutoRotationSliderEdgeLabelModel labelModel = GraphManager.getGraphManager().createAutoRotationSliderEdgeLabelModel();
labelModel.setDistance(-15);
label.setLabelModel(labelModel);
label.setModelParameter(labelModel.getDefaultParameter());
}
return new EdgeLabel[]{label};
}
public boolean isShowEdgeLabels() {
return myShowEdgeLabels;
}
public void setShowEdgeLabels(final boolean showEdgeLabels) {
myShowEdgeLabels = showEdgeLabels;
}
protected EdgeLabel createLabel(final String edgeName) {
final EdgeLabel label = GraphManager.getGraphManager().createEdgeLabel(edgeName, EdgeLabel.TWO_POS);
label.setPosition(EdgeLabel.HEAD);
label.setFontSize(10);
label.setFontStyle(Font.ITALIC);
return label;
}
public void customizeSettings(final Graph2DView view, final EditMode editMode) {
}
public EdgeCreationPolicy<N> getEdgeCreationPolicy() {
return new EdgeCreationPolicy<N>() {
public boolean acceptSource(final N source) {
return true;
}
public boolean acceptTarget(final N target) {
return true;
}
};
}
public DeleteProvider getDeleteProvider() {
return null;
}
public boolean isAutoRotateLabels() {
return false;
}
@Nullable
public GraphBuilder<N, E> getGraphBuilder() {
return myGraphBuilder;
}
public void setGraphBuilder(final GraphBuilder<N, E> graphBuilder) {
myGraphBuilder = graphBuilder;
}
}