Skip to content

Commit 4954216

Browse files
committed
Fixed Profiler/Sampler Presets
- forced unique names - fixed threading - fixed repainting
1 parent 591352e commit 4954216

File tree

2 files changed

+100
-62
lines changed

2 files changed

+100
-62
lines changed

visualvm/profiling/src/org/graalvm/visualvm/profiling/presets/ProfilerPresets.java

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2020, 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
@@ -107,53 +107,49 @@ ProfilerPreset presetToCreate() {
107107
}
108108

109109
void optionsSubmitted(final ProfilerPreset selected) {
110-
SwingUtilities.invokeLater(new Runnable() {
111-
public void run() {
112-
Map<DefaultComboBoxModel, Boolean> models = new HashMap();
113-
Iterator<WeakReference<PresetSelector>> selectorsI =
114-
selectors.iterator();
115-
116-
while (selectorsI.hasNext()) {
117-
WeakReference<PresetSelector> selectorR = selectorsI.next();
118-
PresetSelector selector = selectorR.get();
119-
if (selector == null) {
120-
selectorsI.remove();
121-
} else if (SwingUtilities.getRoot(selector) == null) {
122-
selectorsI.remove();
123-
} else {
124-
DefaultComboBoxModel model = selector.getModel();
125-
boolean savingCustom = selector.checkSavingCustom();
126-
if (!Boolean.TRUE.equals(models.get(model)))
127-
models.put(model, savingCustom);
128-
}
129-
}
130-
131-
for (DefaultComboBoxModel model : models.keySet()) {
132-
ProfilerPreset selectedPreset = (ProfilerPreset)model.getSelectedItem();
133-
ProfilerPreset defaultPreset = (ProfilerPreset)model.getElementAt(0);
134-
ProfilerPreset customPreset = model.getSize() < 2 ? null :
135-
(ProfilerPreset)model.getElementAt(1);
136-
if (customPreset != null && !PresetSelector.isCustomPreset(customPreset))
137-
customPreset = null;
138-
139-
boolean savingCustom = models.get(model);
140-
110+
Map<DefaultComboBoxModel, Boolean> models = new HashMap();
111+
Iterator<WeakReference<PresetSelector>> selectorsI =
112+
selectors.iterator();
113+
114+
while (selectorsI.hasNext()) {
115+
WeakReference<PresetSelector> selectorR = selectorsI.next();
116+
PresetSelector selector = selectorR.get();
117+
if (selector == null) {
118+
selectorsI.remove();
119+
} else if (SwingUtilities.getRoot(selector) == null) {
120+
selectorsI.remove();
121+
} else {
122+
DefaultComboBoxModel model = selector.getModel();
123+
boolean savingCustom = selector.checkSavingCustom();
124+
if (!Boolean.TRUE.equals(models.get(model)))
125+
models.put(model, savingCustom);
126+
}
127+
}
128+
129+
for (DefaultComboBoxModel model : models.keySet()) {
130+
ProfilerPreset selectedPreset = (ProfilerPreset)model.getSelectedItem();
131+
ProfilerPreset defaultPreset = (ProfilerPreset)model.getElementAt(0);
132+
ProfilerPreset customPreset = model.getSize() < 2 ? null :
133+
(ProfilerPreset)model.getElementAt(1);
134+
if (customPreset != null && !PresetSelector.isCustomPreset(customPreset))
135+
customPreset = null;
136+
137+
boolean savingCustom = models.get(model);
138+
141139
// internalChange = true;
142140

143-
model.removeAllElements();
144-
model.addElement(defaultPreset);
145-
if (!savingCustom && customPreset != null) model.addElement(customPreset);
146-
for (ProfilerPreset preset : presets) model.addElement(preset);
147-
148-
ProfilerPreset toSelect = savingCustom && selected != null ? selected :
149-
selectedPreset;
150-
if (model.getIndexOf(toSelect) >= 0) model.setSelectedItem(toSelect);
151-
else model.setSelectedItem(model.getElementAt(0));
141+
model.removeAllElements();
142+
model.addElement(defaultPreset);
143+
if (!savingCustom && customPreset != null) model.addElement(customPreset);
144+
for (ProfilerPreset preset : presets) model.addElement(preset);
145+
146+
ProfilerPreset toSelect = savingCustom && selected != null ? selected :
147+
selectedPreset;
148+
if (model.getIndexOf(toSelect) >= 0) model.setSelectedItem(toSelect);
149+
else model.setSelectedItem(model.getElementAt(0));
152150

153151
// internalChange = false;
154-
}
155-
}
156-
});
152+
}
157153
}
158154

159155
public PresetSelector createSelector(DefaultComboBoxModel selectorsModel,
@@ -233,16 +229,12 @@ public void run() {
233229
}
234230

235231
void savePresets(final PresetsModel toSave) {
236-
SwingUtilities.invokeLater(new Runnable() {
237-
public void run() {
238-
presets.clear();
239-
Enumeration en = toSave.elements();
240-
while (en.hasMoreElements())
241-
presets.add((ProfilerPreset)en.nextElement());
242-
VisualVM.getInstance().runTask(new Runnable() {
243-
public void run() { doSavePresets(toSave); }
244-
});
245-
}
232+
presets.clear();
233+
Enumeration en = toSave.elements();
234+
while (en.hasMoreElements())
235+
presets.add((ProfilerPreset)en.nextElement());
236+
VisualVM.getInstance().runTask(new Runnable() {
237+
public void run() { doSavePresets(toSave); }
246238
});
247239
}
248240

visualvm/profiling/src/org/graalvm/visualvm/profiling/presets/ProfilingOptionsPanel.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2020, 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
@@ -39,7 +39,9 @@
3939
import java.awt.event.ActionEvent;
4040
import java.awt.event.FocusEvent;
4141
import java.util.Enumeration;
42+
import java.util.HashMap;
4243
import java.util.HashSet;
44+
import java.util.Map;
4345
import java.util.Set;
4446
import javax.swing.BorderFactory;
4547
import javax.swing.DefaultListModel;
@@ -213,7 +215,7 @@ private void updatePreset() {
213215

214216
controller.changed();
215217
}
216-
218+
217219
private void initComponents() {
218220
final boolean nimbusLaF =
219221
org.graalvm.visualvm.uisupport.UISupport.isNimbusLookAndFeel();
@@ -258,7 +260,7 @@ public void removeSelectionInterval(int i1, int i2) {}
258260
ProfilingOptionsPanel.class, "MSG_No_presets"), JLabel.CENTER); // NOI18N
259261
noPresetsLabel.setEnabled(false);
260262
noPresetsLabel.setSize(noPresetsLabel.getPreferredSize());
261-
JScrollPane listScroll = new JScrollPane(list) {
263+
final JScrollPane listScroll = new JScrollPane(list) {
262264
public Dimension getPreferredSize() {
263265
return oneDim;
264266
}
@@ -295,6 +297,7 @@ protected void fireActionPerformed(ActionEvent e) {
295297
removeButton = new JButton() {
296298
protected void fireActionPerformed(ActionEvent e) {
297299
deletePreset();
300+
listScroll.repaint();
298301
}
299302
};
300303
removeButton.setIcon(new ImageIcon(ImageUtilities.loadImage(
@@ -524,17 +527,29 @@ void load() {
524527
updateComponents();
525528

526529
if (toCreate != null) preselectNameField();
530+
531+
repaint();
527532
}
528533

529534
void store() {
530-
ProfilerPresets.getInstance().savePresets(listModel);
531-
ProfilerPreset selected = (ProfilerPreset)list.getSelectedValue();
532-
ProfilerPresets.getInstance().optionsSubmitted(selected);
535+
SwingUtilities.invokeLater(new Runnable() {
536+
public void run() {
537+
normalizeNames(listModel); // prevents duplicate preset names
538+
539+
ProfilerPresets.getInstance().savePresets(listModel);
540+
ProfilerPreset selected = (ProfilerPreset)list.getSelectedValue();
541+
ProfilerPresets.getInstance().optionsSubmitted(selected);
542+
}
543+
});
533544
}
534545

535546
void closed() {
536-
if (listModel != null) listModel.removeListDataListener(listModelListener);
537-
list.setModel(new DefaultListModel());
547+
SwingUtilities.invokeLater(new Runnable() {
548+
public void run() {
549+
if (listModel != null) listModel.removeListDataListener(listModelListener);
550+
list.setModel(new DefaultListModel());
551+
}
552+
});
538553
}
539554

540555
boolean valid() {
@@ -549,6 +564,37 @@ boolean valid() {
549564
// }
550565
// return true;
551566
// }
567+
568+
private static void normalizeNames(PresetsModel model) {
569+
Map<String, Integer> names = new HashMap();
570+
571+
for (int i = 0; i < model.getSize(); i++) {
572+
ProfilerPreset preset = (ProfilerPreset)model.getElementAt(i);
573+
names.put(preset.getName(), 0);
574+
}
575+
576+
for (int i = 0; i < model.getSize(); i++) {
577+
ProfilerPreset preset = (ProfilerPreset)model.getElementAt(i);
578+
String presetName = preset.getName();
579+
Integer nameCounts = names.get(presetName);
580+
if (nameCounts != null) {
581+
if (nameCounts == 0) {
582+
names.put(presetName, ++nameCounts);
583+
} else {
584+
String newName = normalizeName(presetName, nameCounts);
585+
while (names.containsKey(newName)) newName = normalizeName(presetName, ++nameCounts);
586+
preset.setName(newName);
587+
names.put(newName, 1);
588+
names.put(presetName, nameCounts);
589+
model.fireItemChanged(i);
590+
}
591+
}
592+
}
593+
}
594+
595+
private static String normalizeName(String name, int modifier) {
596+
return name + " (" + modifier + ")"; // NOI18N
597+
}
552598

553599

554600
private JPanel presetsPanel;

0 commit comments

Comments
 (0)