Changeset 404


Ignore:
Timestamp:
Apr 21, 2012, 9:14:10 PM (5 years ago)
Author:
tim.te.beek@…
Message:

Start listening to the correct network
(De)register for listening to the correct network, even if switching multiple times
Respond to network creation and deletion events

Location:
trunk/expression-data-viewer/src/main/java/nl/nbic/brs/expview/model
Files:
2 edited

Legend:

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

    r403 r404  
    3232        public GraphObjectsAndCyAttributesModel(final PlottingParams plottingParams) {
    3333                this.plottingParams = plottingParams;
     34                listenToNetwork(plottingParams.getNetworkModel().getSelectedItem());
    3435                updateGraphObjectsAndCyAttributes();
    3536        }
     
    5253        }
    5354
    54         private CyNetwork       previousNetwork = Cytoscape.getNullNetwork();
     55        private CyNetwork       listenNetwork;
     56
     57        private void listenToNetwork(final CyNetwork newNetwork) {
     58                if (listenNetwork != null)
     59                        // Deregister ourselves with the previous network
     60                        listenNetwork.removeSelectEventListener(this);
     61                // Register ourselves with the new network
     62                newNetwork.addSelectEventListener(this);
     63                // Ensure we can do the same next time around
     64                listenNetwork = newNetwork;
     65        }
    5566
    5667        /**
     
    6273        public void itemStateChanged(final ItemEvent event) {
    6374                // When a new network is selected, listen for node/edge selection changes on that network
    64                 if (event.getItem() instanceof CyNetwork) {
    65                         // Deregister ourselves with the previous network
    66                         previousNetwork.removeSelectEventListener(this);
    67                         // Register ourselves with the new network
    68                         CyNetwork newnetwork = (CyNetwork) event.getItem();
    69                         newnetwork.addSelectEventListener(this);
    70                 }
     75                if (event.getItem() instanceof CyNetwork)
     76                        listenToNetwork((CyNetwork) event.getItem());
    7177
    7278                // Update the cached graphObject and CyAttributes
  • trunk/expression-data-viewer/src/main/java/nl/nbic/brs/expview/model/NetworkComboBoxModel.java

    r336 r404  
    44import java.awt.event.ItemEvent;
    55import java.awt.event.ItemListener;
     6import java.beans.PropertyChangeEvent;
     7import java.beans.PropertyChangeListener;
    68import java.util.ArrayList;
    79import java.util.Collections;
     
    1416import cytoscape.Cytoscape;
    1517
    16 public class NetworkComboBoxModel extends DefaultComboBoxModel implements ItemSelectable {
     18public class NetworkComboBoxModel extends DefaultComboBoxModel implements ItemSelectable, PropertyChangeListener {
    1719        public NetworkComboBoxModel() {
    18                 // Sort networks by name and add them to this model
    19                 List<CyNetwork> networks = new ArrayList<CyNetwork>(Cytoscape.getNetworkSet());
    20                 Collections.sort(networks, new Comparator<CyNetwork>() {
    21                         @Override
    22                         public int compare(final CyNetwork o1, final CyNetwork o2) {
    23                                 return o1.getTitle().compareTo(o2.getTitle());
    24                         }
    25                 });
    26                 for (CyNetwork network : networks.toArray(new CyNetwork[networks.size()]))
    27                         addElement(network);
     20                rebuildNetworkList(null);
    2821
    29                 // Select the network the user has selected
    30                 setSelectedItem(Cytoscape.getCurrentNetwork());
     22                // Listen for added and removed networks, and update selectables and selection accordingly
     23                Cytoscape.getPropertyChangeSupport().addPropertyChangeListener(Cytoscape.NETWORK_CREATED, this);
     24                Cytoscape.getPropertyChangeSupport().addPropertyChangeListener(Cytoscape.NETWORK_DESTROYED, this);
    3125        }
    3226
     
    6559                listeners.remove(listener);
    6660        }
     61
     62        @Override
     63        public void propertyChange(final PropertyChangeEvent evt) {
     64                String propertyName = evt.getPropertyName();
     65                if (propertyName.equalsIgnoreCase(Cytoscape.NETWORK_CREATED))
     66                        rebuildNetworkList(null);
     67                else
     68                        if (propertyName.equalsIgnoreCase(Cytoscape.NETWORK_DESTROYED)) {
     69                                // NETWORK_DESTROYED is called before network is actually removed from networkset:
     70                                // Ensure the removed network is (de)selected, if applicable, and not added to new set of networks
     71                                String removedNetworkID = (String) evt.getNewValue();
     72                                CyNetwork removedNetwork = Cytoscape.getNetwork(removedNetworkID);
     73                                rebuildNetworkList(removedNetwork);
     74                        }
     75        }
     76
     77        private void rebuildNetworkList(final CyNetwork removedNetwork) {
     78                // Save previously selected network
     79                CyNetwork selectedNetwork = getSelectedItem();
     80
     81                // Clear current list of networks
     82                removeAllElements();
     83
     84                // Get the available networks
     85                List<CyNetwork> networks = new ArrayList<CyNetwork>(Cytoscape.getNetworkSet());
     86
     87                // NETWORK_DESTROYED is called before network is removed from networkset: do not let it show up as an option
     88                if (removedNetwork != null)
     89                        networks.remove(removedNetwork);
     90
     91                // Sort networks by name and add them to this model
     92                Collections.sort(networks, new Comparator<CyNetwork>() {
     93                        @Override
     94                        public int compare(final CyNetwork o1, final CyNetwork o2) {
     95                                return o1.getTitle().compareTo(o2.getTitle());
     96                        }
     97                });
     98                for (CyNetwork network : networks)
     99                        addElement(network);
     100
     101                // Determine the network to (re)select
     102                if (selectedNetwork == null || !networks.contains(selectedNetwork))
     103                        // Revert to current network if previous was null, or previous was removed
     104                        selectedNetwork = Cytoscape.getCurrentNetwork();
     105                setSelectedItem(selectedNetwork);
     106        }
    67107}
Note: See TracChangeset for help on using the changeset viewer.