# HG changeset patch # User Omair Majid # Date 1442585888 14400 # Node ID 4281c1d18ea0c8f31bead2e071edfa6bd0339883 # Parent 5b66dcca659df0111c0eccacf86d9d6ffff7d001 Update profiling session list in-place PR 2636 Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-September/016220.html diff -r 5b66dcca659d -r 4281c1d18ea0 vm-profiler/client-swing/src/main/java/com/redhat/thermostat/vm/profiler/client/swing/internal/SwingVmProfileView.java --- a/vm-profiler/client-swing/src/main/java/com/redhat/thermostat/vm/profiler/client/swing/internal/SwingVmProfileView.java Sun Sep 20 16:02:08 2015 -0400 +++ b/vm-profiler/client-swing/src/main/java/com/redhat/thermostat/vm/profiler/client/swing/internal/SwingVmProfileView.java Fri Sep 18 10:18:08 2015 -0400 @@ -45,6 +45,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Date; +import java.util.Enumeration; import java.util.List; import java.util.Vector; import java.util.concurrent.Callable; @@ -318,16 +319,32 @@ SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - Profile selectedValue = profileList.getSelectedValue(); + DefaultListModel listModel = (DefaultListModel) profileList.getModel(); + updateSwingModel(data, listModel); + } - DefaultListModel newListModel = new DefaultListModel<>(); - for (Profile item : data) { - newListModel.addElement(item); + /** + * Update the swing model based on the provided data, adding new + * items and removing no-longer-present items from the swing model + */ + private void updateSwingModel(final List data, DefaultListModel model) { + for (Profile profile : data) { + if (!model.contains(profile)) { + model.addElement(profile); + } } - profileList.setModel(newListModel); - if (selectedValue != null) { - profileList.setSelectedValue(selectedValue, /* shouldScroll = */ true); + List toRemove = new ArrayList<>(); + Enumeration e = model.elements(); + while (e.hasMoreElements()) { + Profile profile = e.nextElement(); + if (!data.contains(profile)) { + toRemove.add(profile); + } + } + + for (Profile profile : toRemove) { + model.removeElement(profile); } } });