Skip to content

Commit f3ba767

Browse files
8343535: IGV: Colorize nodes on demand
Co-authored-by: Roberto Castañeda Lozano <rcastanedalo@openjdk.org> Reviewed-by: chagedorn, rcastanedalo
1 parent 5016132 commit f3ba767

File tree

7 files changed

+216
-22
lines changed

7 files changed

+216
-22
lines changed

src/utils/IdealGraphVisualizer/View/src/main/java/com/sun/hotspot/igv/view/DiagramScene.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -222,6 +222,17 @@ public void filteredChanged(SelectionCoordinator coordinator) {
222222
}
223223
};
224224

225+
public void colorSelectedFigures(Color color) {
226+
for (Figure figure : model.getSelectedFigures()) {
227+
figure.setColor(color);
228+
FigureWidget figureWidget = getWidget(figure);
229+
if (figureWidget != null) {
230+
figureWidget.refreshColor();
231+
}
232+
}
233+
validateAll();
234+
}
235+
225236
private Point getScrollPosition() {
226237
return scrollPane.getViewport().getViewPosition();
227238
}

src/utils/IdealGraphVisualizer/View/src/main/java/com/sun/hotspot/igv/view/DiagramViewer.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,10 +26,7 @@
2626

2727
import com.sun.hotspot.igv.data.ChangedEvent;
2828
import com.sun.hotspot.igv.data.InputNode;
29-
import java.awt.Component;
30-
import java.awt.Graphics2D;
31-
import java.awt.Point;
32-
import java.awt.Rectangle;
29+
import java.awt.*;
3330
import java.util.Collection;
3431
import javax.swing.JComponent;
3532
import org.openide.awt.UndoRedo;
@@ -89,4 +86,6 @@ enum InteractionMode {
8986
Rectangle getBounds();
9087

9188
JComponent getView();
89+
90+
void colorSelectedFigures(Color color);
9291
}

src/utils/IdealGraphVisualizer/View/src/main/java/com/sun/hotspot/igv/view/EditorTopComponent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public EditorTopComponent(DiagramViewModel diagramViewModel) {
9999
};
100100

101101
Action[] actionsWithSelection = new Action[]{
102+
ColorAction.get(ColorAction.class),
102103
ExtractAction.get(ExtractAction.class),
103104
HideAction.get(HideAction.class),
104105
null,
@@ -349,6 +350,10 @@ public void addSelectedNodes(Collection<InputNode> nodes, boolean showIfHidden)
349350
scene.addSelectedNodes(nodes, showIfHidden);
350351
}
351352

353+
public void colorSelectedFigures(Color color) {
354+
scene.colorSelectedFigures(color);
355+
}
356+
352357
public void centerSelectedNodes() {
353358
scene.centerSelectedFigures();
354359
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
package com.sun.hotspot.igv.view.actions;
25+
26+
import com.sun.hotspot.igv.view.DiagramViewModel;
27+
import com.sun.hotspot.igv.view.EditorTopComponent;
28+
import com.sun.hotspot.igv.view.widgets.FigureWidget;
29+
import java.awt.*;
30+
import java.util.ArrayList;
31+
import java.util.Arrays;
32+
import javax.swing.*;
33+
import org.openide.awt.ActionID;
34+
import org.openide.awt.ActionReference;
35+
import org.openide.awt.ActionReferences;
36+
import org.openide.awt.ActionRegistration;
37+
import org.openide.util.NbBundle;
38+
import org.openide.util.NbBundle.Messages;
39+
40+
41+
@ActionID(category = "View", id = "com.sun.hotspot.igv.view.actions.ColorAction")
42+
@ActionRegistration(displayName = "#CTL_ColorAction")
43+
@ActionReferences({
44+
@ActionReference(path = "Menu/View", position = 360),
45+
@ActionReference(path = "Shortcuts", name = "D-C")
46+
})
47+
@Messages({
48+
"CTL_ColorAction=Color",
49+
"HINT_ColorAction=Color current set of selected nodes"
50+
})
51+
public final class ColorAction extends ModelAwareAction {
52+
53+
@Override
54+
protected String iconResource() {
55+
return "com/sun/hotspot/igv/view/images/color.gif"; // NOI18N
56+
}
57+
58+
@Override
59+
protected String getDescription() {
60+
return NbBundle.getMessage(ColorAction.class, "HINT_ColorAction");
61+
}
62+
63+
@Override
64+
public String getName() {
65+
return NbBundle.getMessage(ColorAction.class, "CTL_ColorAction");
66+
}
67+
68+
private static final ArrayList<Color> colors = new ArrayList<>(Arrays.asList(
69+
Color.RED,
70+
Color.ORANGE,
71+
Color.YELLOW,
72+
Color.GREEN,
73+
Color.CYAN,
74+
Color.BLUE,
75+
Color.MAGENTA,
76+
Color.PINK,
77+
Color.DARK_GRAY,
78+
Color.GRAY,
79+
Color.LIGHT_GRAY,
80+
Color.WHITE
81+
));
82+
83+
private static final JLabel selectedColorLabel = new JLabel("Preview");
84+
private static final JColorChooser colorChooser = new JColorChooser(Color.WHITE);
85+
86+
public ColorAction() {
87+
initializeComponents();
88+
}
89+
90+
private void initializeComponents() {
91+
selectedColorLabel.setPreferredSize(new Dimension(3 * 32, 32));
92+
selectedColorLabel.setOpaque(true);
93+
selectedColorLabel.setBackground(Color.WHITE);
94+
selectedColorLabel.setForeground(Color.BLACK); // Set text color
95+
selectedColorLabel.setHorizontalAlignment(SwingConstants.CENTER); // Center the text
96+
97+
98+
// Add a ChangeListener to react to color selection changes
99+
colorChooser.getSelectionModel().addChangeListener(e -> {
100+
Color selectedColor = colorChooser.getColor();
101+
if (selectedColor != null) {
102+
selectedColorLabel.setBackground(selectedColor);
103+
selectedColorLabel.setForeground(FigureWidget.getTextColor(selectedColor));
104+
}
105+
});
106+
107+
// Create a panel to display recent colors
108+
JPanel colorsPanel = new JPanel();
109+
colorsPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
110+
for (Color color : colors) {
111+
JButton colorButton = new JButton();
112+
colorButton.setBackground(color);
113+
colorButton.setOpaque(true);
114+
colorButton.setBorderPainted(false);
115+
colorButton.setRolloverEnabled(false);
116+
colorButton.setRequestFocusEnabled(false);
117+
118+
colorButton.setPreferredSize(new Dimension(16, 16));
119+
colorButton.addActionListener(e -> {
120+
selectedColorLabel.setBackground(color);
121+
selectedColorLabel.setForeground(FigureWidget.getTextColor(color));
122+
});
123+
colorsPanel.add(colorButton);
124+
}
125+
colorsPanel.add(selectedColorLabel, 0);
126+
colorsPanel.revalidate();
127+
colorsPanel.repaint();
128+
129+
// Add recent colors panel below the color chooser
130+
colorChooser.setPreviewPanel(colorsPanel);
131+
}
132+
133+
// Variables to store the dialog position
134+
private Point dialogLoc = null;
135+
136+
public void performAction(DiagramViewModel model) {
137+
EditorTopComponent editor = EditorTopComponent.getActive();
138+
if (editor != null) {
139+
// Create the dialog with an OK button to select the color
140+
final JDialog[] dialogHolder = new JDialog[1];
141+
dialogHolder[0] = JColorChooser.createDialog(
142+
null,
143+
"Choose a Color",
144+
true,
145+
colorChooser,
146+
e -> {
147+
// Save the current location
148+
dialogLoc = dialogHolder[0].getLocation();
149+
// OK button action
150+
Color selectedColor = selectedColorLabel.getBackground();
151+
if (selectedColor != null) {
152+
editor.colorSelectedFigures(selectedColor);
153+
}
154+
},
155+
null // Cancel button action
156+
);
157+
158+
// Set the dialog's position if previously saved
159+
if (dialogLoc != null) {
160+
dialogHolder[0].setLocation(dialogLoc);
161+
}
162+
dialogHolder[0].setVisible(true);
163+
}
164+
}
165+
166+
@Override
167+
public boolean isEnabled(DiagramViewModel model) {
168+
return model != null && !model.getSelectedNodes().isEmpty();
169+
}
170+
}

src/utils/IdealGraphVisualizer/View/src/main/java/com/sun/hotspot/igv/view/actions/ExtractAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -42,7 +42,7 @@
4242
@ActionReference(path = "Shortcuts", name = "D-X")
4343
})
4444
@Messages({
45-
"CTL_ExtractAction=Extract action",
45+
"CTL_ExtractAction=Extract",
4646
"HINT_ExtractAction=Extract current set of selected nodes"
4747
})
4848
public final class ExtractAction extends ModelAwareAction {

src/utils/IdealGraphVisualizer/View/src/main/java/com/sun/hotspot/igv/view/widgets/FigureWidget.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -90,7 +90,20 @@ private void formatExtraLabel(boolean selected) {
9090
if (getFigure().getProperties().get("extra_label") != null) {
9191
LabelWidget extraLabelWidget = labelWidgets.get(labelWidgets.size() - 1);
9292
extraLabelWidget.setFont(Diagram.FONT.deriveFont(Font.ITALIC));
93-
extraLabelWidget.setForeground(selected ? getTextColor() : Color.DARK_GRAY);
93+
extraLabelWidget.setForeground(getTextColorHelper(figure.getColor(), !selected));
94+
}
95+
}
96+
97+
public static Color getTextColor(Color color) {
98+
return getTextColorHelper(color, false);
99+
}
100+
101+
private static Color getTextColorHelper(Color bg, boolean useGrey) {
102+
double brightness = bg.getRed() * 0.21 + bg.getGreen() * 0.72 + bg.getBlue() * 0.07;
103+
if (brightness < 150) {
104+
return useGrey ? Color.LIGHT_GRAY : Color.WHITE;
105+
} else {
106+
return useGrey ? Color.DARK_GRAY : Color.BLACK;
94107
}
95108
}
96109

@@ -113,7 +126,6 @@ public FigureWidget(final Figure f, DiagramScene scene) {
113126
LayoutFactory.SerialAlignment.LEFT_TOP :
114127
LayoutFactory.SerialAlignment.CENTER;
115128
middleWidget.setLayout(LayoutFactory.createVerticalFlowLayout(textAlign, 0));
116-
middleWidget.setBackground(f.getColor());
117129
middleWidget.setOpaque(true);
118130
middleWidget.getActions().addAction(new DoubleClickAction(this));
119131
middleWidget.setCheckClipping(false);
@@ -143,13 +155,13 @@ public FigureWidget(final Figure f, DiagramScene scene) {
143155
textWidget.addChild(lw);
144156
lw.setLabel(displayString);
145157
lw.setFont(Diagram.FONT);
146-
lw.setForeground(getTextColor());
147158
lw.setAlignment(LabelWidget.Alignment.CENTER);
148159
lw.setVerticalAlignment(LabelWidget.VerticalAlignment.CENTER);
149160
lw.setBorder(BorderFactory.createEmptyBorder());
150161
lw.setCheckClipping(false);
151162
}
152163
formatExtraLabel(false);
164+
refreshColor();
153165

154166
if (getFigure().getWarning() != null) {
155167
ImageWidget warningWidget = new ImageWidget(scene, warningSign);
@@ -184,6 +196,13 @@ protected Sheet createSheet() {
184196
this.setToolTipText(PropertiesConverter.convertToHTML(f.getProperties()));
185197
}
186198

199+
public void refreshColor() {
200+
middleWidget.setBackground(figure.getColor());
201+
for (LabelWidget lw : labelWidgets) {
202+
lw.setForeground(getTextColor(figure.getColor()));
203+
}
204+
}
205+
187206
@Override
188207
protected void notifyStateChanged(ObjectState previousState, ObjectState state) {
189208
super.notifyStateChanged(previousState, state);
@@ -222,16 +241,6 @@ public Figure getFigure() {
222241
return figure;
223242
}
224243

225-
private Color getTextColor() {
226-
Color bg = figure.getColor();
227-
double brightness = bg.getRed() * 0.21 + bg.getGreen() * 0.72 + bg.getBlue() * 0.07;
228-
if (brightness < 150) {
229-
return Color.WHITE;
230-
} else {
231-
return Color.BLACK;
232-
}
233-
}
234-
235244
@Override
236245
protected void paintChildren() {
237246
Composite oldComposite = null;
207 Bytes
Loading

0 commit comments

Comments
 (0)