Skip to content

Commit

Permalink
8345039: IGV: save user-defined node colors to XML
Browse files Browse the repository at this point in the history
Co-authored-by: Roberto Castañeda Lozano <[email protected]>
Co-authored-by: Christian Hagedorn <[email protected]>
Reviewed-by: chagedorn, epeter, rcastanedalo
  • Loading branch information
3 people committed Nov 29, 2024
1 parent 28b0f3e commit a80ccf2
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.sun.hotspot.igv.data;

import java.awt.Color;
import java.util.Objects;

/**
Expand Down Expand Up @@ -67,4 +68,27 @@ public boolean equals(Object obj) {
public String toString() {
return "Node " + id + " " + getProperties().toString();
}

public void setCustomColor(Color color) {
if (color != null) {
String hexColor = String.format("#%08X", color.getRGB());
getProperties().setProperty("color", hexColor);
} else {
getProperties().setProperty("color", null);
}
}

public Color getCustomColor() {
String hexColor = getProperties().get("color");
if (hexColor != null) {
try {
String hex = hexColor.startsWith("#") ? hexColor.substring(1) : hexColor;
int argb = (int) Long.parseLong(hex, 16);
return new Color(argb, true);
} catch (Exception ignored) {
return null;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class Diagram {

private final Map<InputNode, Figure> figures;
private final Map<InputBlock, Block> blocks;
private final InputGraph inputGraph;
private final String nodeText;
private final String shortNodeText;
private final String tinyNodeText;
Expand Down Expand Up @@ -66,6 +67,7 @@ public Diagram(InputGraph graph, String nodeText, String shortNodeText,
this.figures = new LinkedHashMap<>();
this.blocks = new LinkedHashMap<>(8);
this.blockConnections = new HashSet<>();
this.inputGraph = graph;
this.cfg = false;
int curId = 0;

Expand Down Expand Up @@ -128,6 +130,10 @@ public Diagram(InputGraph graph, String nodeText, String shortNodeText,
}
}

public InputGraph getInputGraph() {
return inputGraph;
}

public Block getBlock(InputBlock b) {
assert blocks.containsKey(b);
return blocks.get(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.sun.hotspot.igv.graph;

import com.sun.hotspot.igv.data.InputGraph;
import com.sun.hotspot.igv.data.InputNode;
import com.sun.hotspot.igv.data.Properties;
import com.sun.hotspot.igv.layout.Cluster;
Expand Down Expand Up @@ -153,7 +154,12 @@ public void setColor(Color color) {
}

public Color getColor() {
return color;
Color customColor = inputNode.getCustomColor();
if (customColor != null) {
return customColor;
} else {
return color;
}
}

public void setWarning(String warning) {
Expand Down Expand Up @@ -415,4 +421,16 @@ public boolean isRoot() {
public int compareTo(Vertex f) {
return toString().compareTo(f.toString());
}

public void setCustomColor(Color color) {
// Apply custom color not just to this input node but to all
// corresponding input nodes in the group.
InputGraph graph = diagram.getInputGraph();
for (InputGraph g : graph.getGroup().getGraphs()) {
InputNode n = g.getNode(inputNode.getId());
if (n != null) {
n.setCustomColor(color);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public void filteredChanged(SelectionCoordinator coordinator) {

public void colorSelectedFigures(Color color) {
for (Figure figure : model.getSelectedFigures()) {
figure.setColor(color);
figure.setCustomColor(color);
FigureWidget figureWidget = getWidget(figure);
if (figureWidget != null) {
figureWidget.refreshColor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,16 @@ public String getName() {

private static final JLabel selectedColorLabel = new JLabel("Preview");
private static final JColorChooser colorChooser = new JColorChooser(Color.WHITE);
private static final Color NO_COLOR = new Color(0, 0, 0, 0);

public ColorAction() {
initializeComponents();
}

private void initializeComponents() {
selectedColorLabel.setPreferredSize(new Dimension(3 * 32, 32));
selectedColorLabel.setOpaque(true);
selectedColorLabel.setBackground(Color.WHITE);
selectedColorLabel.setOpaque(false); // Allow transparency
selectedColorLabel.setBackground(NO_COLOR); // Set transparent background
selectedColorLabel.setForeground(Color.BLACK); // Set text color
selectedColorLabel.setHorizontalAlignment(SwingConstants.CENTER); // Center the text

Expand All @@ -100,6 +101,7 @@ private void initializeComponents() {
Color selectedColor = colorChooser.getColor();
if (selectedColor != null) {
selectedColorLabel.setBackground(selectedColor);
selectedColorLabel.setOpaque(selectedColor.getAlpha() != 0);
selectedColorLabel.setForeground(FigureWidget.getTextColor(selectedColor));
}
});
Expand All @@ -118,10 +120,27 @@ private void initializeComponents() {
colorButton.setPreferredSize(new Dimension(16, 16));
colorButton.addActionListener(e -> {
selectedColorLabel.setBackground(color);
selectedColorLabel.setOpaque(color.getAlpha() != 0);
selectedColorLabel.setForeground(FigureWidget.getTextColor(color));
});
colorsPanel.add(colorButton);
}

// Add "No Color" button
JButton noColorButton = new JButton("No Color");
noColorButton.setOpaque(true);
noColorButton.setContentAreaFilled(true);
noColorButton.setBorderPainted(true);
noColorButton.setPreferredSize(new Dimension(90, 24));
noColorButton.setFocusPainted(false);
noColorButton.addActionListener(e -> {
selectedColorLabel.setBackground(NO_COLOR);
selectedColorLabel.setOpaque(false);
selectedColorLabel.setForeground(Color.BLACK);
});
colorsPanel.add(noColorButton);

// Add the preview label
colorsPanel.add(selectedColorLabel, 0);
colorsPanel.revalidate();
colorsPanel.repaint();
Expand All @@ -148,9 +167,10 @@ public void performAction(DiagramViewModel model) {
dialogLoc = dialogHolder[0].getLocation();
// OK button action
Color selectedColor = selectedColorLabel.getBackground();
if (selectedColor != null) {
editor.colorSelectedFigures(selectedColor);
if (selectedColor.equals(NO_COLOR)) {
selectedColor = null;
}
editor.colorSelectedFigures(selectedColor);
},
null // Cancel button action
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public Figure getFigure() {

@Override
protected void paintChildren() {
refreshColor();
Composite oldComposite = null;
if (boundary) {
oldComposite = getScene().getGraphics().getComposite();
Expand Down

0 comments on commit a80ccf2

Please sign in to comment.