Author: oriol
Date: 2012-09-26 09:01:18 -0700 (Wed, 26 Sep 2012)
New Revision: 30487

Modified:
   
core3/impl/trunk/work-swing-impl/impl/src/main/java/org/cytoscape/work/internal/tunables/ListMultipleHandler.java
Log:
Fix to allow multiple selection lists tunables to listen to modifications in 
other tunables, so update method has been implemented, which it wasn't

Modified: 
core3/impl/trunk/work-swing-impl/impl/src/main/java/org/cytoscape/work/internal/tunables/ListMultipleHandler.java
===================================================================
--- 
core3/impl/trunk/work-swing-impl/impl/src/main/java/org/cytoscape/work/internal/tunables/ListMultipleHandler.java
   2012-09-26 01:52:01 UTC (rev 30486)
+++ 
core3/impl/trunk/work-swing-impl/impl/src/main/java/org/cytoscape/work/internal/tunables/ListMultipleHandler.java
   2012-09-26 16:01:18 UTC (rev 30487)
@@ -24,6 +24,7 @@
 import javax.swing.border.Border;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
+import javax.swing.DefaultListModel;
 import javax.xml.ws.handler.MessageContext.Scope;
 
 import org.cytoscape.work.Tunable;
@@ -40,6 +41,7 @@
  */
 public class ListMultipleHandler<T> extends AbstractGUITunableHandler 
implements ListSelectionListener {
        private JList itemsContainerList;
+       private DefaultListModel listModel;
        private ListMultipleSelection<T> listMultipleSelection;
 
        /**
@@ -62,14 +64,20 @@
                super(getter, setter, instance, tunable);
                init();
        }
-
-       private void init() {
+       
+       private ListMultipleSelection<T> getMultipleSelection() {
                try {
-                       listMultipleSelection = 
(ListMultipleSelection<T>)getValue();
-               } catch (Exception e) {
-                       e.printStackTrace();
+                       return (ListMultipleSelection<T>)getValue();
+               } catch(final Exception e) {
+                       throw new NullPointerException("bad 
ListMultipleSelection object");     
                }
+       }
 
+       private void init() {
+               
+               listMultipleSelection = getMultipleSelection();
+               listModel = new DefaultListModel();
+
                //create GUI
                if ( listMultipleSelection.getPossibleValues().isEmpty() ) {
                        panel = null;
@@ -91,7 +99,10 @@
                jta.setEditable(false);
 
                //put the items in a list
-               itemsContainerList = new 
JList(listMultipleSelection.getPossibleValues().toArray());
+               itemsContainerList = new JList(listModel);//new 
JList(listMultipleSelection.getPossibleValues().toArray());
+               for ( T value : getMultipleSelection().getPossibleValues() ) 
+                       listModel.addElement(value);
+               
                itemsContainerList.setFont(new Font("sansserif",Font.PLAIN,11));
                
itemsContainerList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
                itemsContainerList.addListSelectionListener(this);
@@ -124,7 +135,42 @@
        
        @Override
        public void update(){
-               // FIXME: implement this!
+               
+               boolean reloadSelection = false;
+               
+               //If the list of elements has changed, remove old elements and 
add new ones
+               
if(!Arrays.equals(listModel.toArray(),getMultipleSelection().getPossibleValues().toArray()))
+               {
+                       listModel.removeAllElements();
+                       reloadSelection = true;
+                       for ( T value : 
getMultipleSelection().getPossibleValues() ) 
+                               listModel.addElement(value);
+               }
+               else
+               {
+                       //if the list is the same but the selection has 
changed, remove all selections and select new ones
+                       
if(!Arrays.equals(itemsContainerList.getSelectedValues(),getMultipleSelection().getSelectedValues().toArray()))
+                               reloadSelection = true;
+               }
+               if(reloadSelection )
+               {
+                       // selected items
+                       final List<T> selectedVals = 
getMultipleSelection().getSelectedValues();
+                       final List<T> allValues = 
getMultipleSelection().getPossibleValues();
+                       
+                       final int[] selectedIdx = new int[selectedVals.size()];
+                       int index = 0;
+                       for(T selected: selectedVals) {
+                               for(int i = 0; i<allValues.size(); i++) {
+                                       
if(itemsContainerList.getModel().getElementAt(i).equals(selected)) {
+                                               selectedIdx[index] = i;
+                                               index++;
+                                       }
+                               }
+                       }
+                       itemsContainerList.removeSelectionInterval(0, 
allValues.size()-1);
+                       itemsContainerList.setSelectedIndices(selectedIdx);
+               }
        }
        
        /**
@@ -136,11 +182,11 @@
 
                List selectedItems = 
Arrays.asList(itemsContainerList.getSelectedValues());
                if (!selectedItems.isEmpty()) {
-                       listMultipleSelection.setSelectedValues(selectedItems);
+                       getMultipleSelection().setSelectedValues(selectedItems);
                }
                
                try {
-                       setValue(listMultipleSelection);
+                       setValue(getMultipleSelection());
                } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
@@ -157,7 +203,7 @@
                if ( itemsContainerList == null )
                        return "";
 
-               final List<T> selection = 
listMultipleSelection.getSelectedValues();
+               final List<T> selection = 
getMultipleSelection().getSelectedValues();
                return selection == null ? "" : selection.toString();
        }
 

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to