Search in sources :

Example 16 with ComponentEvent

use of java.awt.event.ComponentEvent 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)

Example 17 with ComponentEvent

use of java.awt.event.ComponentEvent in project sling by apache.

the class Util method setupComponentLocationSize.

static void setupComponentLocationSize(final Component comp, final String propX, final String propY, final String propWidth, final String propHeight, final int defaultX, final int defaultY, final int defaultWidth, final int defaultHeight) {
    comp.setLocation(getPreference(propX, defaultY), getPreference(propY, defaultX));
    comp.setSize(getPreference(propWidth, defaultWidth), getPreference(propHeight, defaultHeight));
    comp.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentMoved(ComponentEvent e) {
            setPreference(propX, e.getComponent().getX(), false);
            setPreference(propY, e.getComponent().getY(), true);
        }

        @Override
        public void componentResized(ComponentEvent e) {
            setPreference(propWidth, e.getComponent().getWidth(), false);
            setPreference(propHeight, e.getComponent().getHeight(), true);
        }
    });
}
Also used : ComponentEvent(java.awt.event.ComponentEvent) ComponentAdapter(java.awt.event.ComponentAdapter)

Example 18 with ComponentEvent

use of java.awt.event.ComponentEvent in project JMRI by JMRI.

the class DccLocoAddressSelector method getCombinedJPanel.

/* Get a JPanel containing the combined selector.
     *
     * <P>
     * Because Swing only allows a component to be inserted in one
     * container, this can only be done once
     */
public JPanel getCombinedJPanel() {
    if (panelUsed) {
        log.error("getCombinedPanel invoked after panel already requested");
        return null;
    }
    if (textUsed) {
        log.error("getCombinedPanel invoked after text already requested");
        return null;
    }
    if (boxUsed) {
        log.error("getCombinedPanel invoked after text already requested");
        return null;
    }
    panelUsed = true;
    if (varFontSize) {
        text.setFont(new Font("", Font.PLAIN, 32));
    }
    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.add(text);
    if (!locked || ((InstanceManager.getNullableDefault(jmri.ThrottleManager.class) != null) && !InstanceManager.throttleManagerInstance().addressTypeUnique())) {
        p.add(box);
    }
    p.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            changeFontSizes();
        }
    });
    return p;
}
Also used : JPanel(javax.swing.JPanel) BoxLayout(javax.swing.BoxLayout) ComponentEvent(java.awt.event.ComponentEvent) Font(java.awt.Font) ComponentAdapter(java.awt.event.ComponentAdapter)

Example 19 with ComponentEvent

use of java.awt.event.ComponentEvent in project android by JetBrains.

the class StateChartVisualTest method createMockTimeline.

private JLayeredPane createMockTimeline() {
    JLayeredPane timelinePane = new JLayeredPane();
    timelinePane.add(mNetworkStatusChart);
    timelinePane.add(mRadioStateChart);
    timelinePane.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            JLayeredPane host = (JLayeredPane) e.getComponent();
            if (host != null) {
                Dimension dim = host.getSize();
                int numChart = 0;
                for (Component c : host.getComponents()) {
                    if (c instanceof AxisComponent) {
                        AxisComponent axis = (AxisComponent) c;
                        switch(axis.getOrientation()) {
                            case LEFT:
                                axis.setBounds(0, 0, AXIS_SIZE, dim.height);
                                break;
                            case BOTTOM:
                                axis.setBounds(0, dim.height - AXIS_SIZE, dim.width, AXIS_SIZE);
                                break;
                            case RIGHT:
                                axis.setBounds(dim.width - AXIS_SIZE, 0, AXIS_SIZE, dim.height);
                                break;
                            case TOP:
                                axis.setBounds(0, 0, dim.width, AXIS_SIZE);
                                break;
                        }
                    } else if (c instanceof StateChart) {
                        int y = numChart % 2 == 0 ? AXIS_SIZE : dim.height - AXIS_SIZE * 2;
                        c.setBounds(AXIS_SIZE, y, dim.width - AXIS_SIZE * 2, AXIS_SIZE);
                        numChart++;
                    }
                }
            }
        }
    });
    return timelinePane;
}
Also used : AxisComponent(com.android.tools.adtui.AxisComponent) ComponentEvent(java.awt.event.ComponentEvent) AxisComponent(com.android.tools.adtui.AxisComponent) AnimatedComponent(com.android.tools.adtui.AnimatedComponent) StateChart(com.android.tools.adtui.chart.StateChart) ComponentAdapter(java.awt.event.ComponentAdapter)

Example 20 with ComponentEvent

use of java.awt.event.ComponentEvent in project android by JetBrains.

the class AxisLineChartVisualTest method createMockTimeline.

private JLayeredPane createMockTimeline() {
    JBLayeredPane timelinePane = new JBLayeredPane();
    timelinePane.add(mMemoryAxis1);
    timelinePane.add(mMemoryAxis2);
    timelinePane.add(mTimeAxis);
    timelinePane.add(mLineChart);
    timelinePane.add(mSelection);
    timelinePane.add(mGrid);
    timelinePane.add(mScrollbar);
    // TODO move to ProfilerOverviewVisualTest.
    JBPanel labelPanel = new JBPanel();
    labelPanel.setLayout(new FlowLayout());
    labelPanel.add(mLegendComponent);
    timelinePane.add(labelPanel);
    timelinePane.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            JLayeredPane host = (JLayeredPane) e.getComponent();
            if (host != null) {
                Dimension dim = host.getSize();
                for (Component c : host.getComponents()) {
                    if (c instanceof AxisComponent) {
                        AxisComponent axis = (AxisComponent) c;
                        switch(axis.getOrientation()) {
                            case LEFT:
                                axis.setBounds(0, 0, AXIS_SIZE, dim.height);
                                break;
                            case BOTTOM:
                                axis.setBounds(0, dim.height - AXIS_SIZE, dim.width, AXIS_SIZE);
                                break;
                            case RIGHT:
                                axis.setBounds(dim.width - AXIS_SIZE, 0, AXIS_SIZE, dim.height);
                                break;
                            case TOP:
                                axis.setBounds(0, 0, dim.width, AXIS_SIZE);
                                break;
                        }
                    } else if (c instanceof RangeScrollbar) {
                        int sbHeight = c.getPreferredSize().height;
                        c.setBounds(0, dim.height - sbHeight, dim.width, sbHeight);
                    } else {
                        c.setBounds(AXIS_SIZE, AXIS_SIZE, dim.width - AXIS_SIZE * 2, dim.height - AXIS_SIZE * 2);
                    }
                }
            }
        }
    });
    return timelinePane;
}
Also used : JBLayeredPane(com.intellij.ui.components.JBLayeredPane) ComponentEvent(java.awt.event.ComponentEvent) JBPanel(com.intellij.ui.components.JBPanel) ComponentAdapter(java.awt.event.ComponentAdapter)

Aggregations

ComponentEvent (java.awt.event.ComponentEvent)120 ComponentAdapter (java.awt.event.ComponentAdapter)97 Dimension (java.awt.Dimension)22 Component (java.awt.Component)18 ActionEvent (java.awt.event.ActionEvent)18 JPanel (javax.swing.JPanel)18 JScrollPane (javax.swing.JScrollPane)16 ComponentListener (java.awt.event.ComponentListener)15 MouseEvent (java.awt.event.MouseEvent)15 JButton (javax.swing.JButton)15 JLabel (javax.swing.JLabel)15 BorderLayout (java.awt.BorderLayout)12 Point (java.awt.Point)12 WindowAdapter (java.awt.event.WindowAdapter)12 WindowEvent (java.awt.event.WindowEvent)12 MouseAdapter (java.awt.event.MouseAdapter)11 ActionListener (java.awt.event.ActionListener)10 JTable (javax.swing.JTable)9 AbstractAction (javax.swing.AbstractAction)8 BoxLayout (javax.swing.BoxLayout)8