Changeset 340

Show
Ignore:
Timestamp:
05-04-12 12:36:50 (2 years ago)
Author:
tim.te.beek@…
Message:

Rework notification chain: PlottingParams? now only listens to changes in selected attributes
Introduce a Model to grab either nodes or edges as GraphObject?, in preparation of a toggle

Location:
trunk/expression-data-viewer/src
Files:
1 added
5 modified

Legend:

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

    r338 r340  
    11package nl.nbic.brs.expview.model; 
     2 
     3import java.awt.event.ItemListener; 
    24 
    35import javax.swing.ListModel; 
    46import javax.swing.ListSelectionModel; 
     7import javax.swing.event.ChangeListener; 
    58 
    69import lombok.Delegate; 
    710 
    8 public class AttributesModel implements ListModel, ListSelectionModel { 
    9         @Delegate(types = ListModel.class) 
     11public class AttributesModel implements ListModel, ListSelectionModel, ChangeListener, ItemListener { 
     12        @Delegate(types = { ListModel.class, ChangeListener.class, ItemListener.class }) 
    1013        private AvailableAttributesModel        availableAttributesModel; 
    1114 
     
    1417 
    1518        public AttributesModel(final PlottingParams plottingParams) { 
    16                 availableAttributesModel = new AvailableAttributesModel(); 
     19                availableAttributesModel = new AvailableAttributesModel(plottingParams); 
    1720                selectedAttributesModel = new SelectedAttributesModel(); 
    1821 
    19                 // Available attributes should be recalculated when any of the settings in PlottingParams changes 
    20                 plottingParams.addChangeListener(availableAttributesModel); 
    21  
    22                 // Selected attributes should be reset/updated when the available attributes change 
     22                // Selected attributes should change when the available attributes change 
    2323                availableAttributesModel.addListDataListener(selectedAttributesModel); 
    24  
    25                 // PlottingParams should rebuild JFreeChart dataset when selected attributes change 
    26                 selectedAttributesModel.addListSelectionListener(plottingParams); 
    2724        } 
    2825 
  • trunk/expression-data-viewer/src/main/java/nl/nbic/brs/expview/model/AvailableAttributesModel.java

    r338 r340  
    33import giny.model.GraphObject; 
    44 
     5import java.awt.event.ItemEvent; 
     6import java.awt.event.ItemListener; 
    57import java.util.ArrayList; 
    68import java.util.Collections; 
     
    1214import javax.swing.event.ChangeListener; 
    1315 
    14 import cytoscape.Cytoscape; 
    1516import cytoscape.data.CyAttributes; 
    1617 
    17 public class AvailableAttributesModel extends AbstractListModel implements ChangeListener { 
    18         private List<String>    availableAttributes     = Collections.emptyList(); 
     18public class AvailableAttributesModel extends AbstractListModel implements ItemListener, ChangeListener { 
     19        private final PlottingParams    plottingParams; 
     20 
     21        // Cache of available attributes 
     22        private List<String>                    availableAttributes     = Collections.emptyList(); 
     23 
     24        public AvailableAttributesModel(final PlottingParams plottingParams) { 
     25                this.plottingParams = plottingParams; 
     26        } 
    1927 
    2028        @Override 
     
    2937 
    3038        /** 
    31          * Listen for changes to PlottingParams to update the availableAttributes. 
     39         * Listen for changes in Set of GraphObjects and CyAttributes to look through. 
    3240         */ 
    3341        @Override 
    34         public void stateChanged(final ChangeEvent event) { 
    35                 cacheAvailableAttributes((PlottingParams) event.getSource()); 
     42        public void stateChanged(final ChangeEvent e) { 
     43                cacheAvailableAttributes(); 
     44        } 
     45 
     46        /** 
     47         * Listen for changes to whether only expression attributes should be made available. 
     48         */ 
     49        @Override 
     50        public void itemStateChanged(final ItemEvent e) { 
     51                cacheAvailableAttributes(); 
    3652        } 
    3753 
     
    3955         * Use the settings in plottingParams to determine new selectableAttributes. 
    4056         */ 
    41         private void cacheAvailableAttributes(final PlottingParams plottingParams) { 
    42                 // Retrieve selected nodes for selected network 
    43                 // TODO Add switch to retrieve attributes for selected edges instead 
    44                 @SuppressWarnings("unchecked") 
    45                 Set<GraphObject> selectedNodes = plottingParams.getNetworkModel().getSelectedItem().getSelectedNodes(); 
    46                 // XXX Changes to node selection are not picked up: we need to listen to Cytoscape.SELECT_NODES_ONLY 
     57        private void cacheAvailableAttributes() { 
     58                // Get selected graphObjects and the associated CyAttributes collection 
     59                GraphObjectsAndCyAttributesModel graphObjectAndCyAttributesModel = plottingParams.getGraphObjectAndCyAttributesModel(); 
     60                Set<GraphObject> selectedGraphObjects = graphObjectAndCyAttributesModel.getGraphObjects(); 
     61                CyAttributes cyAttributes = graphObjectAndCyAttributesModel.getAttributes(); 
    4762 
    4863                // Get only those expression attributes associated with selected nodes 
    49                 CyAttributes cyAttributes = Cytoscape.getNodeAttributes(); 
    5064                List<String> _availableAttributes = new ArrayList<String>(); 
    5165                for (String attributeName : cyAttributes.getAttributeNames()) { 
     
    6175                                continue; 
    6276 
    63                         for (GraphObject graphObject : selectedNodes) 
    64                                 // TODO Add flag to only select when all nodes have a certain attribute 
     77                        for (GraphObject graphObject : selectedGraphObjects) 
     78                                // TODO Add flag to only select when all graphObjects have a certain attribute 
    6579                                // Select if any of the selected nodes has this attribute 
    6680                                if (cyAttributes.hasAttribute(graphObject.getIdentifier(), attributeName)) { 
     
    7993 
    8094                // Notify UI by firing this event 
    81                 // XXX Because we're not sending any more fine-grained events, this leads to a full reset of selected attributes 
    8295                fireContentsChanged(this, 0, getSize() - 1); 
    8396        } 
  • trunk/expression-data-viewer/src/main/java/nl/nbic/brs/expview/model/PlottingParams.java

    r338 r340  
    33import giny.model.GraphObject; 
    44 
    5 import java.awt.event.ItemEvent; 
    6 import java.awt.event.ItemListener; 
    7 import java.util.ArrayList; 
    8 import java.util.List; 
    95import java.util.Set; 
    106import java.util.SortedSet; 
     
    139import javax.swing.ButtonModel; 
    1410import javax.swing.JToggleButton.ToggleButtonModel; 
    15 import javax.swing.event.ChangeEvent; 
    16 import javax.swing.event.ChangeListener; 
    1711import javax.swing.event.ListSelectionEvent; 
    1812import javax.swing.event.ListSelectionListener; 
    1913 
    20 import lombok.AccessLevel; 
    2114import lombok.Getter; 
    2215 
     
    2720 
    2821@Getter 
    29 public class PlottingParams implements ItemListener, ListSelectionListener { 
    30         NetworkComboBoxModel    networkModel                            = new NetworkComboBoxModel(); 
    31         ButtonModel                             onlyExpressionAttributes        = new ToggleButtonModel(); 
    32         DefaultCategoryDataset  dataset                                         = new DefaultCategoryDataset(); 
    33         final AttributesModel   attributesModel; 
     22public class PlottingParams implements ListSelectionListener { 
     23        NetworkComboBoxModel                                    networkModel                            = new NetworkComboBoxModel(); 
     24        ButtonModel                                                             onlyExpressionAttributes        = new ToggleButtonModel(); 
     25 
     26        ButtonModel                                                             attributesFromNodes                     = new ToggleButtonModel(); 
     27        ButtonModel                                                             attributesFromEdges                     = new ToggleButtonModel(); 
     28 
     29        final GraphObjectsAndCyAttributesModel  graphObjectAndCyAttributesModel; 
     30        final AttributesModel                                   attributesModel; 
     31 
     32        DefaultCategoryDataset                                  dataset                                         = new DefaultCategoryDataset(); 
    3433 
    3534        public PlottingParams() { 
     35                // Initialize the models that need this as argument 
     36                graphObjectAndCyAttributesModel = new GraphObjectsAndCyAttributesModel(this); 
    3637                attributesModel = new AttributesModel(this); 
    3738 
    38                 // Maintain network selection 
    39                 networkModel.addItemListener(this); 
     39                // Set the correct initial values for some models 
     40                onlyExpressionAttributes.setSelected(true); 
     41                attributesFromNodes.setSelected(true); 
     42                networkModel.setSelectedItem(Cytoscape.getCurrentNetwork()); 
    4043 
    41                 // Keep state of the only expression attributes toggle 
    42                 onlyExpressionAttributes.setSelected(true); 
    43                 onlyExpressionAttributes.addItemListener(this); 
    44         } 
     44                // Configure listeners 
     45                // Different nodes/edges should be cached when selected networks changes 
     46                networkModel.addItemListener(graphObjectAndCyAttributesModel); 
     47                // Different nodes/edges and CyAttributes should be cached when usage of either nodes or edges is toggled 
     48                attributesFromNodes.addItemListener(graphObjectAndCyAttributesModel); 
    4549 
    46         @Getter(value = AccessLevel.NONE) 
    47         private List<ChangeListener>    listeners       = new ArrayList<ChangeListener>(); 
     50                // Available attributes should be recalculated when exclusive selection of expression attributes is toggled 
     51                onlyExpressionAttributes.addItemListener(attributesModel); 
     52                // Available attributes should be recalculated when set of graphObjects and/or CyAttributes change 
     53                graphObjectAndCyAttributesModel.addChangeListener(attributesModel); 
    4854 
    49         public boolean addChangeListener(final ChangeListener listener) { 
    50                 return listeners.add(listener); 
    51         } 
    52  
    53         public boolean removeChangeListener(final ChangeListener listener) { 
    54                 return listeners.remove(listener); 
    55         } 
    56  
    57         public void fireChangeEvents() { 
    58                 for (ChangeListener listener : listeners.toArray(new ChangeListener[listeners.size()])) 
    59                         listener.stateChanged(new ChangeEvent(this)); 
    60         } 
    61  
    62         /** 
    63          * For network and checkbox selection events. 
    64          */ 
    65         @Override 
    66         public void itemStateChanged(final ItemEvent event) { 
    67                 allChangesHandler(event.getSource()); 
     55                // PlottingParams should rebuild JFreeChart dataset when selected attributes change 
     56                attributesModel.addListSelectionListener(this); 
    6857        } 
    6958 
     
    7766                if (event.getValueIsAdjusting()) 
    7867                        return; 
    79                 allChangesHandler(event.getSource()); 
    80         } 
    81  
    82         /** 
    83          * When any of the elements we are listening to through various Listening interfaces, this method should be called. 
    84          */ 
    85         private void allChangesHandler(final Object source) { 
    86                 System.out.println(source); 
    8768                // FIXME Calling this here means it all runs on the EventDispatchThread. Might not perform well with large data 
    88                 // Update values in dataset 
    8969                updateDataset(); 
    90  
    91                 // Notify listeners 
    92                 fireChangeEvents(); 
    9370        } 
    9471 
     
    10178                dataset.clear(); 
    10279 
     80                // Get selected graphObjects and the associated CyAttributes collection 
     81                Set<GraphObject> selectedGraphObjects = graphObjectAndCyAttributesModel.getGraphObjects(); 
     82                CyAttributes cyAttributes = graphObjectAndCyAttributesModel.getAttributes(); 
     83 
    10384                // Get selected nodes or edges, convert to identifiers and sort naturally 
    104                 @SuppressWarnings("unchecked") 
    105                 Set<GraphObject> selectedGraphObjects = networkModel.getSelectedItem().getSelectedNodes(); 
    10685                SortedSet<String> selectedGOIDs = new TreeSet<String>(); 
    10786                for (GraphObject graphObject : selectedGraphObjects) 
    10887                        selectedGOIDs.add(graphObject.getIdentifier()); 
    10988 
    110                 // Get selected attributes indices from model, convert to names and sort naturally 
     89                // Get selected attributes indices from model 
    11190                String[] selectedAttributes = attributesModel.getSelectedAttributes(); 
    112  
    113                 // The CyAttributes to look through 
    114                 CyAttributes cyAttributes = Cytoscape.getNodeAttributes(); 
    11591 
    11692                // Add data points for every selected node 
  • trunk/expression-data-viewer/src/main/java/nl/nbic/brs/expview/model/SelectedAttributesModel.java

    r339 r340  
    1616         * @return selected column indices 
    1717         */ 
    18         public int[] getSelectedAttributeIndices() { 
     18        protected int[] getSelectedAttributeIndices() { 
    1919                int iMin = getMinSelectionIndex(); 
    2020                int iMax = getMaxSelectionIndex(); 
  • trunk/expression-data-viewer/src/test/java/nl/nbic/brs/expview/view/LaunchPlotDialog.java

    r337 r340  
    4848                                // Set all the correct options on the PlottingParams contained in PlotDialog 
    4949                                PlottingParams plottingParams = dialog.getPlottingParams(); 
    50                                 plottingParams.getNetworkModel().setSelectedItem(network); 
     50                                // plottingParams.getNetworkModel().setSelectedItem(network); 
    5151                                AttributesModel attributeModel = plottingParams.getAttributesModel(); 
    5252                                // Select all the attributes for the plot