Changeset 339

Show
Ignore:
Timestamp:
04-04-12 21:40:27 (2 years ago)
Author:
tim.te.beek@…
Message:

Retain selected attributes when available attributes change

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/expression-data-viewer/src/main/java/nl/nbic/brs/expview/model/SelectedAttributesModel.java

    r338 r339  
    11package nl.nbic.brs.expview.model; 
    22 
     3import java.util.ArrayList; 
     4import java.util.Collections; 
     5import java.util.List; 
     6 
    37import javax.swing.DefaultListSelectionModel; 
     8import javax.swing.ListModel; 
    49import javax.swing.event.ListDataEvent; 
    510import javax.swing.event.ListDataListener; 
     
    2833        } 
    2934 
     35        private List<String>    allPreviousAttributes   = Collections.emptyList(); 
     36 
    3037        /** 
    31          * Listen for changes to available attributes. Clear selection when changes are found. 
     38         * Listen for changes to available attributes. Retain selection as much as possible when changes are found. 
    3239         */ 
    3340        @Override 
    3441        public void contentsChanged(final ListDataEvent event) { 
     42                int[] selectedAttributeIndices = getSelectedAttributeIndices(); 
     43                ListModel availableAttributesModel = (ListModel) event.getSource(); 
     44 
     45                // Extract all available attributes 
     46                List<String> allAttributes = new ArrayList<String>(); 
     47                for (int i = 0; i < availableAttributesModel.getSize(); i++) 
     48                        allAttributes.add(i, (String) availableAttributesModel.getElementAt(i)); 
     49 
     50                // Batch all updates together 
     51                setValueIsAdjusting(true); 
    3552                clearSelection(); 
     53 
     54                // Restore selection by looking up old selected attributes in new list of attributes 
     55                for (int previouslySelectedIndex : selectedAttributeIndices) { 
     56                        String attribute = allPreviousAttributes.get(previouslySelectedIndex); 
     57                        int newIndex = allAttributes.indexOf(attribute); 
     58                        addSelectionInterval(newIndex, newIndex); 
     59                } 
     60 
     61                // Done updating: Store all attributes for future use 
     62                setValueIsAdjusting(false); 
     63                allPreviousAttributes = allAttributes; 
    3664        } 
    3765