Search in sources :

Example 1 with YPoint

use of com.intellij.openapi.graph.geom.YPoint in project intellij-plugins by JetBrains.

the class AngularUiRouterDiagramProvider method createPresentationModel.

@Nullable
@Override
public DiagramPresentationModel createPresentationModel(Project project, Graph2D graph) {
    return new DiagramPresentationModelImpl(graph, project, this) {

        @Override
        public boolean allowChangeVisibleCategories() {
            return false;
        }

        private final Map<DiagramEdge, EdgeRealizer> myEdgeRealizers = new HashMap<>();

        @NotNull
        @Override
        public EdgeRealizer getEdgeRealizer(DiagramEdge edge) {
            if (!(edge instanceof AngularUiRouterEdge))
                return super.getEdgeRealizer(edge);
            if (myEdgeRealizers.containsKey(edge))
                return myEdgeRealizers.get(edge);
            UmlGraphBuilder builder = (UmlGraphBuilder) graph.getDataProvider(DiagramDataKeys.GRAPH_BUILDER).get(null);
            final Edge graphEdge = builder.getEdge(edge);
            final AngularEdgeLayouter.OneEdgeLayouter layouter = new AngularEdgeLayouter.OneEdgeLayouter(graphEdge, (AngularUiRouterEdge) edge, graph);
            layouter.calculateEdgeLayout();
            final QuadCurveEdgeRealizer realizer = layouter.getRealizer();
            for (int i = 0; i < realizer.labelCount(); i++) {
                realizer.removeLabel(realizer.getLabel(i));
            }
            /*final EdgeLabel[] labels = getEdgeLabels(edge, "");
        if (labels.length == 0) realizer.setLabelText("");
        else {
          for (EdgeLabel label : labels) {
            realizer.addLabel(label);
          }
        }*/
            myEdgeRealizers.put(edge, realizer);
            return realizer;
        }

        private Map<Integer, Integer> myEdgesPositions = new HashMap<>();

        private final Set<AngularUiRouterEdge> myVisibleEdges = new HashSet<>();

        @Override
        public EdgeLabel[] getEdgeLabels(DiagramEdge umlEdge, String label) {
            if (!(umlEdge instanceof AngularUiRouterEdge))
                return super.getEdgeLabels(umlEdge, label);
            AngularUiRouterEdge angularEdge = (AngularUiRouterEdge) umlEdge;
            if (!isShowEdgeLabels() || umlEdge == null || StringUtil.isEmptyOrSpaces(angularEdge.getLabel())) {
                return EMPTY_LABELS;
            }
            //if (!myVisibleEdges.contains(umlEdge)) return EMPTY_LABELS;
            UmlGraphBuilder builder = (UmlGraphBuilder) graph.getDataProvider(DiagramDataKeys.GRAPH_BUILDER).get(null);
            final Edge edge = builder.getEdge(umlEdge);
            final EdgeRealizer edgeRealizer = getEdgeRealizer(umlEdge);
            for (int i = 0; i < edgeRealizer.labelCount(); i++) {
                edgeRealizer.removeLabel(edgeRealizer.getLabel(i));
            }
            final Integer position = calculatePosition(edge, builder);
            final EdgeLabel edgeLabel = GraphManager.getGraphManager().createEdgeLabel();
            final SmartEdgeLabelModel model = GraphManager.getGraphManager().createSmartEdgeLabelModel();
            edgeLabel.setLabelModel(model, model.createDiscreteModelParameter(position));
            edgeLabel.setFontSize(9);
            edgeLabel.setDistance(5);
            edgeLabel.setTextColor(JBColor.foreground());
            myEdgesPositions.put(edge.index(), 1);
            return new EdgeLabel[] { edgeLabel };
        }

        private Integer calculatePosition(final Edge edge, UmlGraphBuilder builder) {
            final Integer existing = myEdgesPositions.get(edge.index());
            if (existing != null)
                return existing;
            final List<Edge> list = new ArrayList<>();
            for (Edge current : edge.getGraph().getEdgeArray()) {
                if (current.source().index() == edge.source().index() && current.target().index() == edge.target().index() || current.target().index() == edge.source().index() && current.source().index() == edge.target().index()) {
                    list.add(current);
                }
            }
            boolean sourceHeavier = edge.source().degree() > edge.target().degree();
            Collections.sort(list, (o1, o2) -> {
                final YPoint s1 = ((Graph2D) o1.getGraph()).getSourcePointAbs(o1);
                final YPoint s2 = ((Graph2D) o1.getGraph()).getSourcePointAbs(o2);
                if (Math.abs(s1.getX() - s2.getX()) > 5)
                    return Double.compare(s1.getX(), s2.getX());
                return Double.compare(s1.getY(), s2.getY());
            });
            int[] variants = sourceHeavier ? new int[] { SmartEdgeLabelModel.POSITION_TARGET_RIGHT, SmartEdgeLabelModel.POSITION_RIGHT, SmartEdgeLabelModel.POSITION_SOURCE_RIGHT } : new int[] { SmartEdgeLabelModel.POSITION_SOURCE_RIGHT, SmartEdgeLabelModel.POSITION_RIGHT, SmartEdgeLabelModel.POSITION_TARGET_RIGHT };
            int variantIdx = 0;
            for (Edge current : list) {
                myEdgesPositions.put(current.index(), variants[variantIdx++]);
                if (variantIdx >= variants.length)
                    variantIdx = 0;
            }
            return myEdgesPositions.get(edge.index());
        }

        private boolean inUpdate = false;

        @Override
        public void update() {
            if (inUpdate)
                return;
            try {
                inUpdate = true;
                myEdgeRealizers.clear();
                final List<DiagramNode> nodes = GraphUtil.getSelectedNodes(getGraphBuilder());
                super.update();
                myEdgesPositions.clear();
                final DiagramBuilder builder = getBuilder();
                builder.relayout();
                builder.getView().fitContent();
                builder.updateView();
                if (!nodes.isEmpty()) {
                    final Collection<DiagramNode> objects = builder.getNodeObjects();
                    for (DiagramNode object : objects) {
                        if (isInSelectedNodes(nodes, object)) {
                            builder.getGraph().setSelected(builder.getNode(object), true);
                        }
                    }
                }
                updateBySelection(nodes.isEmpty() ? null : nodes.get(0));
            } finally {
                inUpdate = false;
            }
        }

        @Override
        public void customizeSettings(Graph2DView view, EditMode editMode) {
            super.customizeSettings(view, editMode);
            view.getGraph2D().addGraph2DSelectionListener(new Graph2DSelectionListener() {

                @Override
                public void onGraph2DSelectionEvent(Graph2DSelectionEvent _e) {
                    myEdgesPositions.clear();
                    updateBySelection(null);
                    view.updateView();
                }
            });
            view.getJComponent().addComponentListener(new ComponentAdapter() {

                @Override
                public void componentShown(ComponentEvent e) {
                    ApplicationManager.getApplication().invokeLater(() -> {
                        UmlGraphBuilder builder = (UmlGraphBuilder) graph.getDataProvider(DiagramDataKeys.GRAPH_BUILDER).get(null);
                        builder.getPresentationModel().update();
                    });
                }
            });
            ApplicationManager.getApplication().invokeLater(() -> {
                UmlGraphBuilder builder = (UmlGraphBuilder) graph.getDataProvider(DiagramDataKeys.GRAPH_BUILDER).get(null);
                final AngularUiRouterDiagramModel model = (AngularUiRouterDiagramModel) builder.getDataModel();
                final AngularUiRouterNode rootNode = findDataObject(project, model).getRootNode();
                updateBySelection(rootNode);
            });
        }

        private void updateBySelection(DiagramNode node) {
            myVisibleEdges.clear();
            UmlGraphBuilder builder = (UmlGraphBuilder) graph.getDataProvider(DiagramDataKeys.GRAPH_BUILDER).get(null);
            final List<DiagramNode> nodes = new ArrayList<>(GraphUtil.getSelectedNodes(builder));
            if (node != null && !nodes.contains(node))
                nodes.add(node);
            DiagramNode selected = null;
            for (DiagramEdge edge : builder.getEdgeObjects()) {
                if (nodes.contains(edge.getSource())) {
                    selected = edge.getSource();
                } else if (nodes.contains(edge.getTarget())) {
                    selected = edge.getTarget();
                } else
                    continue;
                break;
            }
            if (selected == null) {
                for (DiagramEdge edge : builder.getEdgeObjects()) {
                    if (isInSelectedNodes(nodes, edge.getSource())) {
                        selected = edge.getSource();
                    } else if (isInSelectedNodes(nodes, edge.getTarget())) {
                        selected = edge.getTarget();
                    } else
                        continue;
                    break;
                }
            }
            for (DiagramEdge edge : builder.getEdgeObjects()) {
                if (!(edge instanceof AngularUiRouterEdge))
                    continue;
                if (isShowEdgeLabels() && selected != null && (selected.equals(edge.getSource()) || selected.equals(edge.getTarget()))) {
                    myVisibleEdges.add((AngularUiRouterEdge) edge);
                    graph.setLabelText(builder.getEdge(edge), ((AngularUiRouterEdge) edge).getLabel());
                } else {
                    graph.setLabelText(builder.getEdge(edge), "");
                }
            }
        }

        @Override
        public DefaultUmlRenderer getRenderer() {
            if (myRenderer == null) {
                myRenderer = new DefaultUmlRenderer(getBuilder(), createModificationTracker()) {

                    @Override
                    public void tuneNode(NodeRealizer realizer, JPanel wrapper) {
                        wrapper.setBorder(JBUI.Borders.empty());
                        if (wrapper.getParent() instanceof JComponent)
                            ((JComponent) wrapper.getParent()).setBorder(JBUI.Borders.empty());
                        super.tuneNode(realizer, wrapper);
                    }
                };
            }
            return myRenderer;
        }
    };
}
Also used : YPoint(com.intellij.openapi.graph.geom.YPoint) DefaultUmlRenderer(com.intellij.uml.core.renderers.DefaultUmlRenderer) ComponentAdapter(java.awt.event.ComponentAdapter) UmlGraphBuilder(com.intellij.uml.UmlGraphBuilder) YPoint(com.intellij.openapi.graph.geom.YPoint) DiagramPresentationModelImpl(com.intellij.uml.presentation.DiagramPresentationModelImpl) ComponentEvent(java.awt.event.ComponentEvent) Edge(com.intellij.openapi.graph.base.Edge) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Edge (com.intellij.openapi.graph.base.Edge)1 YPoint (com.intellij.openapi.graph.geom.YPoint)1 UmlGraphBuilder (com.intellij.uml.UmlGraphBuilder)1 DefaultUmlRenderer (com.intellij.uml.core.renderers.DefaultUmlRenderer)1 DiagramPresentationModelImpl (com.intellij.uml.presentation.DiagramPresentationModelImpl)1 ComponentAdapter (java.awt.event.ComponentAdapter)1 ComponentEvent (java.awt.event.ComponentEvent)1 Nullable (org.jetbrains.annotations.Nullable)1