changeset 1803:4281c1d18ea0

Update profiling session list in-place PR 2636 Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-September/016220.html
author Omair Majid <omajid@redhat.com>
date Fri, 18 Sep 2015 10:18:08 -0400
parents 5b66dcca659d
children f57cc3785d25
files vm-profiler/client-swing/src/main/java/com/redhat/thermostat/vm/profiler/client/swing/internal/SwingVmProfileView.java
diffstat 1 files changed, 24 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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<Profile> listModel = (DefaultListModel<Profile>) profileList.getModel();
+                updateSwingModel(data, listModel);
+            }
 
-                DefaultListModel<Profile> 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<Profile> data, DefaultListModel<Profile> model) {
+                for (Profile profile : data) {
+                    if (!model.contains(profile)) {
+                        model.addElement(profile);
+                    }
                 }
 
-                profileList.setModel(newListModel);
-                if (selectedValue != null) {
-                    profileList.setSelectedValue(selectedValue, /* shouldScroll = */ true);
+                List<Profile> toRemove = new ArrayList<>();
+                Enumeration<Profile> e = model.elements();
+                while (e.hasMoreElements()) {
+                    Profile profile = e.nextElement();
+                    if (!data.contains(profile)) {
+                        toRemove.add(profile);
+                    }
+                }
+
+                for (Profile profile : toRemove) {
+                    model.removeElement(profile);
                 }
             }
         });