Author: mes
Date: 2012-02-16 16:48:21 -0800 (Thu, 16 Feb 2012)
New Revision: 28303

Modified:
   core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyTable.java
   
core3/api/trunk/model-api/src/test/java/org/cytoscape/model/AbstractCyTableTest.java
   
core3/api/trunk/swing-application-api/src/main/java/org/cytoscape/application/swing/StringEnableSupport.java
   
core3/impl/trunk/filter-impl/src/main/java/org/cytoscape/filter/internal/filters/view/FilterMainPanel.java
   
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
   
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/VirtualColumn.java
   
core3/impl/trunk/table-browser-impl/src/main/java/org/cytoscape/browser/internal/CyTableProjection.java
Log:
Added a method to CyTable which allows the number of rows with a particular 
value in a specified column to be counted.  This is primarily an optimization 
for checking whether selected nodes or edges exist at a given moment.

Modified: 
core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyTable.java
===================================================================
--- core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyTable.java    
2012-02-16 23:07:57 UTC (rev 28302)
+++ core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyTable.java    
2012-02-17 00:48:21 UTC (rev 28303)
@@ -217,13 +217,22 @@
         */
        String getLastInternalError();
 
-       /** Returns all the rows of a specified column that contain a certain 
value for that column.
-        *  @param columnName  the column for which we want the rows
-        *  @param value       the value for which we want the rows that 
contain it
-        *  @return the rows, if any that contain the value "value" for the 
column "columnName"
+       /**
+        * Returns all the rows of a specified column that contain a certain 
value for that column.
+        * @param columnName  the column for which we want the rows
+        * @param value       the value for which we want the rows that contain 
it
+        * @return the rows, if any that contain the value "value" for the 
column "columnName"
         */
        Collection<CyRow> getMatchingRows(String columnName, Object value);
 
+       /** 
+        * Returns the number of rows with the in the specified column with the 
specified value. 
+        * @param columnName  the column to check 
+        * @param value       the value we want to check for 
+        * @return the number of rows with the in the specified column with the 
specified value. 
+        */
+       int countMatchingRows(String columnName, Object value);
+
        /** Returns the number of rows in this table.
         *  @return The number if rows in the table.
         */

Modified: 
core3/api/trunk/model-api/src/test/java/org/cytoscape/model/AbstractCyTableTest.java
===================================================================
--- 
core3/api/trunk/model-api/src/test/java/org/cytoscape/model/AbstractCyTableTest.java
        2012-02-16 23:07:57 UTC (rev 28302)
+++ 
core3/api/trunk/model-api/src/test/java/org/cytoscape/model/AbstractCyTableTest.java
        2012-02-17 00:48:21 UTC (rev 28303)
@@ -1051,7 +1051,49 @@
                table.createListColumn("Test", String.class, false, null );
        }
        
+       @Test
+       public void testCountMatchingRows() {
+               table.createColumn("x", Integer.class, false);
+               CyRow row1 = table.getRow(1L);
+               row1.set("x", 44);
+               CyRow row2 =  table.getRow(2L);
+               row2.set("x", 44);
+               assertEquals(2, table.countMatchingRows("x", 44) );
+               assertEquals(0, table.countMatchingRows("x", 55) );
+       }
+
+       @Test
+       public void testCountMatchingRowsBooleanFalse() {
+               table.createColumn("selection", Boolean.class, false);
+               CyRow row1 = table.getRow(1L);
+               row1.set("selection", true);
+               CyRow row2 =  table.getRow(2L);
+               row2.set("selection", false);
+               assertEquals(1, table.countMatchingRows("selection", true) );
+       }
+
+       @Test
+       public void testCountMatchingRowsBooleanTrue() {
+               table.createColumn("selection", Boolean.class, false);
+               CyRow row1 = table.getRow(1L);
+               row1.set("selection", false);
+               CyRow row2 =  table.getRow(2L);
+               row2.set("selection", false);
+               assertEquals(0, table.countMatchingRows("selection", true) );
+       }
+
+       @Test
+       public void testVirtualColumnHasMatchingRow() {
+               table.createColumn("x", Integer.class, false);
+               CyRow row1 = table.getRow(1L);
+               row1.set("x", 33);
+               CyRow row2 =  table2.getRow(1L);
+               table2.createColumn("s", String.class, false);
+               table.addVirtualColumn("s1", "s", table2, 
table.getPrimaryKey().getName(), true);
+               assertFalse(row1.isSet("s1"));
+               row2.set("s", "abc");
+               assertEquals(1, table.countMatchingRows("s1", "abc") );
+       }
        
-       
 }
 

Modified: 
core3/api/trunk/swing-application-api/src/main/java/org/cytoscape/application/swing/StringEnableSupport.java
===================================================================
--- 
core3/api/trunk/swing-application-api/src/main/java/org/cytoscape/application/swing/StringEnableSupport.java
        2012-02-16 23:07:57 UTC (rev 28302)
+++ 
core3/api/trunk/swing-application-api/src/main/java/org/cytoscape/application/swing/StringEnableSupport.java
        2012-02-17 00:48:21 UTC (rev 28303)
@@ -225,24 +225,8 @@
                        return;
                }
 
-               // If any of nodes are selected, enable this.
-               for (CyNode node : curNetwork.getNodeList()) {
-                       if (curNetwork.getRow(node).get(CyNetwork.SELECTED, 
Boolean.class)) {
-                               setEnabled(true);
-
-                               return;
-                       }
-               }
-
-               for (CyEdge edge : curNetwork.getEdgeList()) {
-                       if (curNetwork.getRow(edge).get(CyNetwork.SELECTED, 
Boolean.class)) {
-                               setEnabled(true);
-
-                               return;
-                       }
-               }
-
-               setEnabled(false);
+               setEnabled( 
((curNetwork.getDefaultNodeTable().countMatchingRows(CyNetwork.SELECTED, true) 
> 0) ||
+                            
(curNetwork.getDefaultEdgeTable().countMatchingRows(CyNetwork.SELECTED, true) > 
0)) ); 
        }
 
        /**
@@ -258,15 +242,7 @@
                        return;
                }
 
-               for (CyNode node : n.getNodeList()) {
-                       if (n.getRow(node).get(CyNetwork.SELECTED, 
Boolean.class)) {
-                               setEnabled(true);
-
-                               return;
-                       }
-               }
-
-               setEnabled(false);
+               setEnabled( 
(n.getDefaultNodeTable().countMatchingRows(CyNetwork.SELECTED, true) > 0) );
        }
 
        /**
@@ -282,15 +258,7 @@
                        return;
                }
 
-               for (CyEdge edge : n.getEdgeList()) {
-                       if (n.getRow(edge).get(CyNetwork.SELECTED, 
Boolean.class)) {
-                               setEnabled(true);
-
-                               return;
-                       }
-               }
-
-               setEnabled(false);
+               setEnabled( 
(n.getDefaultEdgeTable().countMatchingRows(CyNetwork.SELECTED, true) > 0) );
        }
 
        /**

Modified: 
core3/impl/trunk/filter-impl/src/main/java/org/cytoscape/filter/internal/filters/view/FilterMainPanel.java
===================================================================
--- 
core3/impl/trunk/filter-impl/src/main/java/org/cytoscape/filter/internal/filters/view/FilterMainPanel.java
  2012-02-16 23:07:57 UTC (rev 28302)
+++ 
core3/impl/trunk/filter-impl/src/main/java/org/cytoscape/filter/internal/filters/view/FilterMainPanel.java
  2012-02-17 00:48:21 UTC (rev 28303)
@@ -87,7 +87,6 @@
 import org.cytoscape.model.CyNetwork;
 import org.cytoscape.model.CyNetworkManager;
 import org.cytoscape.model.CyTableEntry;
-import org.cytoscape.model.CyTableUtil;
 import org.cytoscape.model.events.NetworkAboutToBeDestroyedEvent;
 import org.cytoscape.model.events.NetworkAboutToBeDestroyedListener;
 import org.cytoscape.model.events.NetworkAddedEvent;
@@ -317,10 +316,10 @@
                                //String title = 
VisualPropertyUtil.get(lexicon, view, "NETWORK_TITLE", 
MinimalVisualLexicon.NETWORK, String.class);
                                
tblFeedBack.getModel().setValueAt(cyNetwork.getRow(cyNetwork).get("name", 
String.class), 0, 0);
 
-                               String nodeStr = "" + cyNetwork.getNodeCount() 
+ "(" + CyTableUtil.getNodesInState(cyNetwork,"selected",true).size() + ")";
+                               String nodeStr = "" + cyNetwork.getNodeCount() 
+ "(" + 
cyNetwork.getDefaultNodeTable().countMatchingRows(CyNetwork.SELECTED,true) + 
")";
                                tblFeedBack.getModel().setValueAt(nodeStr, 0, 
1);
 
-                               String edgeStr = "" + cyNetwork.getEdgeCount() 
+ "(" + CyTableUtil.getEdgesInState(cyNetwork,"selected",true).size() + ")";
+                               String edgeStr = "" + cyNetwork.getEdgeCount() 
+ "(" + 
cyNetwork.getDefaultEdgeTable().countMatchingRows(CyNetwork.SELECTED,true) + 
")";
                                tblFeedBack.getModel().setValueAt(edgeStr, 0, 
2);                               
                }});
        }

Modified: 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
===================================================================
--- 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
        2012-02-16 23:07:57 UTC (rev 28302)
+++ 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
        2012-02-17 00:48:21 UTC (rev 28303)
@@ -558,6 +558,20 @@
                return matchingRows;
        }
 
+       @Override
+       synchronized public int countMatchingRows(final String columnName, 
final Object value) {
+               final VirtualColumn virtColumn = 
virtualColumnMap.get(columnName);
+               if (virtColumn != null)
+                       return virtColumn.countMatchingRows(value);
+
+               final SetMultimap<Object,Object> valueToKeysMap = 
reverse.get(columnName);
+
+               if ( valueToKeysMap == null )
+                       return 0;
+               else
+                       return valueToKeysMap.get(value).size();
+       }
+
        private void setX(final Object key, final String columnName, final 
Object value) {
                if (columnName == null)
                        throw new NullPointerException("columnName must not be 
null!");

Modified: 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/VirtualColumn.java
===================================================================
--- 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/VirtualColumn.java
      2012-02-16 23:07:57 UTC (rev 28302)
+++ 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/VirtualColumn.java
      2012-02-17 00:48:21 UTC (rev 28303)
@@ -113,22 +113,22 @@
        }
 
        Collection<CyRow> getMatchingRows(final Object value) {
-               final Collection<CyRow> sourceRows =
-                       sourceTable.getMatchingRows(sourceColumn.getName(), 
value);
+               final Collection<CyRow> sourceRows = 
sourceTable.getMatchingRows(sourceColumn.getName(), value);
                final Set<CyRow> targetRows = new HashSet<CyRow>();
                for (final CyRow sourceRow : sourceRows) {
-                       final Object targetValue = 
sourceRow.get(sourceJoinColumn.getName(),
-                                                                
sourceJoinColumn.getType());
+                       final Object targetValue = 
sourceRow.get(sourceJoinColumn.getName(), sourceJoinColumn.getType());
                        if (targetValue != null) {
-                               final Collection<CyRow> rows =
-                                       
targetTable.getMatchingRows(targetJoinColumn.getName(),
-                                                                   
targetValue);
+                               final Collection<CyRow> rows = 
targetTable.getMatchingRows(targetJoinColumn.getName(), targetValue);
                                targetRows.addAll(rows);
                        }
                }
                return targetRows;
        }
 
+       int countMatchingRows(final Object value) {
+               return sourceTable.countMatchingRows(sourceColumn.getName(), 
value);
+       }
+
        List getColumnValues() {
                final List targetJoinColumnValues =
                        targetTable.getColumnValues(targetJoinColumn.getName(),

Modified: 
core3/impl/trunk/table-browser-impl/src/main/java/org/cytoscape/browser/internal/CyTableProjection.java
===================================================================
--- 
core3/impl/trunk/table-browser-impl/src/main/java/org/cytoscape/browser/internal/CyTableProjection.java
     2012-02-16 23:07:57 UTC (rev 28302)
+++ 
core3/impl/trunk/table-browser-impl/src/main/java/org/cytoscape/browser/internal/CyTableProjection.java
     2012-02-17 00:48:21 UTC (rev 28303)
@@ -300,6 +300,11 @@
                return matchingRows;
        }
 
+       @Override
+       public int countMatchingRows(final String columnName, final Object 
value) {
+               return underlyingTable.countMatchingRows(columnName, value);
+       }
+
        /** Returns the number of rows in this table.
         *  @return The number if rows in the table.
         */

-- 
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