8294564: IGV: IllegalArgumentException for "Difference to current graph"

Reviewed-by: rcastanedalo, chagedorn
This commit is contained in:
Tobias Holenstein 2022-10-04 07:29:29 +00:00
parent ae79af2ad6
commit f957ce9959
9 changed files with 154 additions and 80 deletions

View File

@ -40,7 +40,6 @@ import java.io.ObjectOutput;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import org.openide.ErrorManager;
@ -228,48 +227,51 @@ public final class OutlineTopComponent extends TopComponent implements ExplorerM
@Override
public void changed(InputGraphProvider lastProvider) {
// Wait for LookupHistory to be updated with the last active graph
// before selecting it.
SwingUtilities.invokeLater(() -> {
for (GraphNode graphNode : selectedGraphs) {
graphNode.setSelected(false);
}
for (FolderNode folderNode : selectedFolders) {
folderNode.setSelected(false);
}
selectedGraphs = new GraphNode[0];
selectedFolders.clear();
if (lastProvider != null) {
// Try to fetch and select the latest active graph.
InputGraph graph = lastProvider.getGraph();
if (graph != null) {
if (graph.isDiffGraph()) {
EditorTopComponent editor = EditorTopComponent.getActive();
if (editor != null) {
InputGraph firstGraph = editor.getModel().getFirstGraph();
InputGraph secondGraph = editor.getModel().getSecondGraph();
selectedGraphs = new GraphNode[]{FolderNode.getGraphNode(firstGraph), FolderNode.getGraphNode(secondGraph)};
for (GraphNode graphNode : selectedGraphs) {
graphNode.setSelected(false);
}
for (FolderNode folderNode : selectedFolders) {
folderNode.setSelected(false);
}
selectedGraphs = new GraphNode[0];
selectedFolders.clear();
if (lastProvider != null) {
// Try to fetch and select the latest active graph.
InputGraph graph = lastProvider.getGraph();
if (graph != null) {
if (graph.isDiffGraph()) {
EditorTopComponent editor = EditorTopComponent.getActive();
if (editor != null) {
InputGraph firstGraph = editor.getModel().getFirstGraph();
GraphNode firstNode = FolderNode.getGraphNode(firstGraph);
InputGraph secondGraph = editor.getModel().getSecondGraph();
GraphNode secondNode = FolderNode.getGraphNode(secondGraph);
if (firstNode != null && secondNode != null) {
selectedGraphs = new GraphNode[]{firstNode, secondNode};
}
} else {
selectedGraphs = new GraphNode[]{FolderNode.getGraphNode(graph)};
}
} else {
GraphNode graphNode = FolderNode.getGraphNode(graph);
if (graphNode != null) {
selectedGraphs = new GraphNode[]{graphNode};
}
}
}
try {
for (GraphNode graphNode : selectedGraphs) {
Node parentNode = graphNode.getParentNode();
if (parentNode instanceof FolderNode) {
FolderNode folderNode = (FolderNode) graphNode.getParentNode();
folderNode.setSelected(true);
selectedFolders.add(folderNode);
}
graphNode.setSelected(true);
}
try {
for (GraphNode graphNode : selectedGraphs) {
Node parentNode = graphNode.getParentNode();
if (parentNode instanceof FolderNode) {
FolderNode folderNode = (FolderNode) graphNode.getParentNode();
folderNode.setSelected(true);
selectedFolders.add(folderNode);
}
manager.setSelectedNodes(selectedGraphs);
} catch (Exception e) {
Exceptions.printStackTrace(e);
graphNode.setSelected(true);
}
});
manager.setSelectedNodes(selectedGraphs);
} catch (Exception e) {
Exceptions.printStackTrace(e);
}
}
@Override

View File

@ -28,6 +28,8 @@ import com.sun.hotspot.igv.data.services.GraphViewer;
import com.sun.hotspot.igv.data.services.InputGraphProvider;
import com.sun.hotspot.igv.difference.Difference;
import com.sun.hotspot.igv.util.LookupHistory;
import com.sun.hotspot.igv.view.EditorTopComponent;
import com.sun.hotspot.igv.view.GraphViewerImplementation;
import org.openide.nodes.Node;
import org.openide.util.Lookup;
@ -37,7 +39,7 @@ import org.openide.util.Lookup;
*/
public class DiffGraphCookie implements Node.Cookie {
private InputGraph graph;
private final InputGraph graph;
public DiffGraphCookie(InputGraph graph) {
this.graph = graph;
@ -52,15 +54,15 @@ public class DiffGraphCookie implements Node.Cookie {
}
public boolean isPossible() {
return getCurrentGraph() != null;
InputGraph currentGraph = getCurrentGraph();
return currentGraph != null && !currentGraph.isDiffGraph() && currentGraph != graph;
}
public void openDiff() {
InputGraph other = getCurrentGraph();
final GraphViewer viewer = Lookup.getDefault().lookup(GraphViewer.class);
if (viewer != null) {
InputGraph diffGraph = Difference.createDiffGraph(other, graph);
viewer.view(diffGraph, true);
if (viewer != null && other != null) {
viewer.viewDifference(other, graph);
}
}
}

View File

@ -39,10 +39,16 @@ public class InputGraph extends Properties.Entity implements FolderElement {
private List<InputBlockEdge> blockEdges;
private Map<Integer, InputBlock> nodeToBlock;
private boolean isDiffGraph;
private InputGraph firstGraph;
private InputGraph secondGraph;
public InputGraph(String name, boolean isDiffGraph) {
this(name);
this.isDiffGraph = isDiffGraph;
public InputGraph(InputGraph firstGraph, InputGraph secondGraph) {
this(firstGraph.getName() + " Δ " + secondGraph.getName());
assert !firstGraph.isDiffGraph() && !secondGraph.isDiffGraph();
this.firstGraph = firstGraph;
this.secondGraph = secondGraph;
isDiffGraph = true;
}
public InputGraph(String name) {
@ -52,6 +58,8 @@ public class InputGraph extends Properties.Entity implements FolderElement {
blocks = new LinkedHashMap<>();
blockEdges = new ArrayList<>();
nodeToBlock = new LinkedHashMap<>();
firstGraph = null;
secondGraph = null;
isDiffGraph = false;
}
@ -59,6 +67,14 @@ public class InputGraph extends Properties.Entity implements FolderElement {
return this.isDiffGraph;
}
public InputGraph getFirstGraph() {
return firstGraph;
}
public InputGraph getSecondGraph() {
return secondGraph;
}
@Override
public void setParent(Folder parent) {
this.parent = parent;

View File

@ -31,5 +31,7 @@ import com.sun.hotspot.igv.data.InputGraph;
*/
public interface GraphViewer {
public void view(InputGraph graph, boolean clone);
void view(InputGraph graph, boolean clone);
void viewDifference(InputGraph firstGraph, InputGraph secondGraph);
}

View File

@ -105,7 +105,7 @@ public class Difference {
}
}
g.getProperties().setProperty("name", "Difference");
InputGraph graph = new InputGraph(a.getName() + ", " + b.getName(), true);
InputGraph graph = new InputGraph(a, b);
g.addElement(graph);
Map<InputBlock, InputBlock> blocksMap = new HashMap<>();

View File

@ -405,30 +405,59 @@ public class DiagramViewModel extends RangeSliderModel implements ChangedListene
}
public InputGraph getFirstGraph() {
InputGraph firstGraph;
if (getFirstPosition() < graphs.size()) {
return graphs.get(getFirstPosition());
firstGraph = graphs.get(getFirstPosition());
} else {
firstGraph = graphs.get(graphs.size() - 1);
}
return graphs.get(graphs.size() - 1);
if (firstGraph.isDiffGraph()) {
firstGraph = firstGraph.getFirstGraph();
}
return firstGraph;
}
public InputGraph getSecondGraph() {
InputGraph secondGraph;
if (getSecondPosition() < graphs.size()) {
return graphs.get(getSecondPosition());
secondGraph = graphs.get(getSecondPosition());
} else {
secondGraph = getFirstGraph();
}
return getFirstGraph();
if (secondGraph.isDiffGraph()) {
secondGraph = secondGraph.getSecondGraph();
}
return secondGraph;
}
public void selectGraph(InputGraph g) {
int index = graphs.indexOf(g);
public void selectGraph(InputGraph graph) {
int index = graphs.indexOf(graph);
if (index == -1 && hideDuplicates) {
// A graph was selected that's currently hidden, so unhide and select it.
setHideDuplicates(false);
index = graphs.indexOf(g);
index = graphs.indexOf(graph);
}
assert index != -1;
setPositions(index, index);
}
public void selectDiffGraph(InputGraph graph) {
int index = graphs.indexOf(graph);
if (index == -1 && hideDuplicates) {
// A graph was selected that's currently hidden, so unhide and select it.
setHideDuplicates(false);
index = graphs.indexOf(graph);
}
assert index != -1;
int firstIndex = getFirstPosition();
int secondIndex = getSecondPosition();
if (firstIndex <= index) {
setPositions(firstIndex, index);
} else {
setPositions(index, secondIndex);
}
}
private static ColorFilter.ColorRule stateColorRule(String state, Color color) {
return new ColorFilter.ColorRule(new MatcherSelector(new Properties.RegexpPropertyMatcher("state", state)), color);
}

View File

@ -49,7 +49,7 @@ public class EditorInputGraphProvider implements InputGraphProvider {
@Override
public InputGraph getGraph() {
if (editor != null) {
if (editor != null && EditorTopComponent.isOpen(editor)) {
return editor.getModel().getGraphToView();
} else {
return null;
@ -58,14 +58,14 @@ public class EditorInputGraphProvider implements InputGraphProvider {
@Override
public void setSelectedNodes(Set<InputNode> nodes) {
if (editor != null) {
if (editor != null && EditorTopComponent.isOpen(editor)) {
editor.setSelectedNodes(nodes);
}
}
@Override
public Iterable<InputGraph> searchBackward() {
if (editor != null) {
if (editor != null && EditorTopComponent.isOpen(editor)) {
return editor.getModel().getGraphsBackward();
} else {
return null;
@ -74,7 +74,7 @@ public class EditorInputGraphProvider implements InputGraphProvider {
@Override
public Iterable<InputGraph> searchForward() {
if (editor != null) {
if (editor != null && EditorTopComponent.isOpen(editor)) {
return editor.getModel().getGraphsForward();
} else {
return null;

View File

@ -54,7 +54,9 @@ import org.openide.util.actions.Presenter;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
import org.openide.util.lookup.ProxyLookup;
import org.openide.windows.Mode;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
/**
@ -283,6 +285,10 @@ public final class EditorTopComponent extends TopComponent {
scene.setZoomPercentage(percentage);
}
public static boolean isOpen(EditorTopComponent editor) {
return WindowManager.getDefault().isOpenedEditorTopComponent(editor);
}
public static EditorTopComponent getActive() {
TopComponent topComponent = getRegistry().getActivated();
if (topComponent instanceof EditorTopComponent) {
@ -291,6 +297,24 @@ public final class EditorTopComponent extends TopComponent {
return null;
}
public static EditorTopComponent findEditorForGraph(InputGraph graph) {
WindowManager manager = WindowManager.getDefault();
for (Mode m : manager.getModes()) {
List<TopComponent> l = new ArrayList<>();
l.add(m.getSelectedTopComponent());
l.addAll(Arrays.asList(manager.getOpenedTopComponents(m)));
for (TopComponent t : l) {
if (t instanceof EditorTopComponent) {
EditorTopComponent etc = (EditorTopComponent) t;
if (etc.getModel().getGroup().getGraphs().contains(graph)) {
return etc;
}
}
}
}
return null;
}
@Override
public int getPersistenceType() {
return TopComponent.PERSISTENCE_NEVER;

View File

@ -25,14 +25,9 @@ package com.sun.hotspot.igv.view;
import com.sun.hotspot.igv.data.InputGraph;
import com.sun.hotspot.igv.data.services.GraphViewer;
import com.sun.hotspot.igv.difference.Difference;
import com.sun.hotspot.igv.graph.Diagram;
import com.sun.hotspot.igv.settings.Settings;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.openide.windows.Mode;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.openide.util.lookup.ServiceProvider;
/**
@ -43,24 +38,28 @@ import org.openide.util.lookup.ServiceProvider;
public class GraphViewerImplementation implements GraphViewer {
@Override
public void view(InputGraph graph, boolean clone) {
public void viewDifference(InputGraph firstGraph, InputGraph secondGraph) {
if (firstGraph.getGroup() != secondGraph.getGroup()) {
InputGraph diffGraph = Difference.createDiffGraph(firstGraph, secondGraph);
view(diffGraph, true);
} else {
view(firstGraph, true);
EditorTopComponent etc = EditorTopComponent.findEditorForGraph(firstGraph);
if (etc != null) {
etc.getModel().selectDiffGraph(secondGraph);
etc.requestActive();
}
}
}
@Override
public void view(InputGraph graph, boolean clone) {
if (!clone) {
WindowManager manager = WindowManager.getDefault();
for (Mode m : manager.getModes()) {
List<TopComponent> l = new ArrayList<>();
l.add(m.getSelectedTopComponent());
l.addAll(Arrays.asList(manager.getOpenedTopComponents(m)));
for (TopComponent t : l) {
if (t instanceof EditorTopComponent) {
EditorTopComponent etc = (EditorTopComponent) t;
if (etc.getModel().getGroup().getGraphs().contains(graph)) {
etc.getModel().selectGraph(graph);
t.requestActive();
return;
}
}
}
EditorTopComponent etc = EditorTopComponent.findEditorForGraph(graph);
if (etc != null) {
etc.getModel().selectGraph(graph);
etc.requestActive();
return;
}
}