Author: ruschein
Date: 2009-12-29 14:15:43 -0800 (Tue, 29 Dec 2009)
New Revision: 18806

Modified:
   coreplugins/trunk/browser/src/browser/DataEditAction.java
   coreplugins/trunk/browser/src/browser/DataTableModel.java
Log:
Added list attribute editing.

Modified: coreplugins/trunk/browser/src/browser/DataEditAction.java
===================================================================
--- coreplugins/trunk/browser/src/browser/DataEditAction.java   2009-12-27 
08:34:06 UTC (rev 18805)
+++ coreplugins/trunk/browser/src/browser/DataEditAction.java   2009-12-29 
22:15:43 UTC (rev 18806)
@@ -35,11 +35,11 @@
 package browser;
 
 import static browser.DataObjectType.NETWORK;
-
 import cytoscape.Cytoscape;
-
 import cytoscape.data.CyAttributes;
-
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 import javax.swing.JOptionPane;
 import javax.swing.undo.AbstractUndoableEdit;
 
@@ -167,12 +167,27 @@
                } else if (targetType == CyAttributes.TYPE_STRING) {
                        attr.setAttribute(id, att, replaceNewlines( newValueStr 
));
                } else if (targetType == CyAttributes.TYPE_SIMPLE_LIST) {
-                       errMessage = "List editing is not supported in this 
version.";
-                       showErrorWindow(errMessage);
+                       final String escapedString = 
replaceNewlines(newValueStr);
+                       final List origList = attr.getListAttribute(id, att);
 
-                       return;
+                       List newList = null;
+                       if (origList.isEmpty() || origList.get(0).getClass() == 
String.class)
+                               newList = parseStringListValue(escapedString);
+                       else if (origList.get(0).getClass() == Double.class)
+                               newList = parseDoubleListValue(escapedString);
+                       else if (origList.get(0).getClass() == Integer.class)
+                               newList = parseIntegerListValue(escapedString);
+                       else if (origList.get(0).getClass() == Boolean.class)
+                               newList = parseBooleanListValue(escapedString);
+                       else
+                               throw new ClassCastException("can't determined 
List type!");
 
-                       // data.setAttributeList(id, att, (List) object);
+                       if (newList == null) {
+                               showErrorWindow("Invalid list!");
+                               return;
+                       }
+                       else
+                               attr.setListAttribute(id, att, newList);
                } else if (targetType == CyAttributes.TYPE_SIMPLE_MAP) {
                        errMessage = "Map editing is not supported in this 
version.";
                        showErrorWindow(errMessage);
@@ -183,6 +198,113 @@
                valid = true;
        }
 
+       /** Does some rudimentary list syntax checking and returns the number 
of items in "listCandidate."
+        * @param listCandidate a string that will be analysed as to 
list-syntax conformance.
+        * @returns -1 if "listCandidate" does not conform to a list syntax, 
otherwise the number of items in the simple list.
+        */
+       private int countListItems(final String listCandidate) {
+               if (listCandidate.length() < 2 || listCandidate.charAt(0) != 
'[' || listCandidate.charAt(listCandidate.length() - 1) != ']')
+                       return -1;
+
+               int commaCount = 0;
+               for (int charIndex = 1; charIndex < listCandidate.length() - 1; 
++charIndex) {
+                       if (listCandidate.charAt(charIndex) == ',')
+                               ++commaCount;
+               }
+
+               return commaCount;
+       }
+
+       /** Attemps to convert "listCandidate" to a List of String.
+        * @param listCandidate hopefully a list of strings.
+        * @returns the List if "listCandidate" has been successfully parsed, 
else null.
+        */
+       private List parseStringListValue(final String listCandidate) {
+               final int itemCount = countListItems(listCandidate);
+               if (itemCount == -1)
+                       return null;
+
+               final String bracketlessList = listCandidate.substring(1, 
listCandidate.length() - 1);
+               final String[] items = bracketlessList.split("\\s*,\\s*");
+
+               return Arrays.asList(items);
+       }
+
+       /** Attemps to convert "listCandidate" to a List of Double.
+        * @param listCandidate hopefully a list of doubles.
+        * @returns the List if "listCandidate" has been successfully parsed, 
else null.
+        */
+       private List parseDoubleListValue(final String listCandidate) {
+               final int itemCount = countListItems(listCandidate);
+               if (itemCount == -1)
+                       return null;
+
+               final String bracketlessList = listCandidate.substring(1, 
listCandidate.length() - 1);
+               final String[] items = bracketlessList.split("\\s*,\\s*");
+
+               final List<Double> doubleList = new 
ArrayList<Double>(itemCount);
+               try {
+                       for (final String item : items) {
+                               final Double newDouble = Double.valueOf(item);
+                               doubleList.add(newDouble);
+                       }
+               } catch (final NumberFormatException e) {
+                       return null; // At least one of the list items was not 
a double.
+               }
+
+               return doubleList;
+       }
+
+       /** Attemps to convert "listCandidate" to a List of Integer.
+        * @param listCandidate hopefully a list of ints.
+        * @returns the List if "listCandidate" has been successfully parsed, 
else null.
+        */
+       private List parseIntegerListValue(final String listCandidate) {
+               final int itemCount = countListItems(listCandidate);
+               if (itemCount == -1)
+                       return null;
+
+               final String bracketlessList = listCandidate.substring(1, 
listCandidate.length() - 1);
+               final String[] items = bracketlessList.split("\\s*,\\s*");
+
+               final List<Integer> intList = new ArrayList<Integer>(itemCount);
+               try {
+                       for (final String item : items) {
+                               final Integer newInteger = 
Integer.valueOf(item);
+                               intList.add(newInteger);
+                       }
+               } catch (final NumberFormatException e) {
+                       return null; // At least one of the list items was not 
a int.
+               }
+
+               return intList;
+       }
+
+       /** Attemps to convert "listCandidate" to a List of Boolean.
+        * @param listCandidate hopefully a list of booleans.
+        * @returns the List if "listCandidate" has been successfully parsed, 
else null.
+        */
+       private List parseBooleanListValue(final String listCandidate) {
+               final int itemCount = countListItems(listCandidate);
+               if (itemCount == -1)
+                       return null;
+
+               final String bracketlessList = listCandidate.substring(1, 
listCandidate.length() - 1);
+               final String[] items = bracketlessList.split("\\s*,\\s*");
+
+               final List<Boolean> booleanList = new 
ArrayList<Boolean>(itemCount);
+               try {
+                       for (final String item : items) {
+                               final Boolean newBoolean = 
Boolean.valueOf(item);
+                               booleanList.add(newBoolean);
+                       }
+               } catch (final NumberFormatException e) {
+                       return null; // At least one of the list items was not 
a boolean.
+               }
+
+               return booleanList;
+       }
+
        private String replaceNewlines(String s) {
                StringBuffer sb = new StringBuffer( s );
                int index = 0;

Modified: coreplugins/trunk/browser/src/browser/DataTableModel.java
===================================================================
--- coreplugins/trunk/browser/src/browser/DataTableModel.java   2009-12-27 
08:34:06 UTC (rev 18805)
+++ coreplugins/trunk/browser/src/browser/DataTableModel.java   2009-12-29 
22:15:43 UTC (rev 18806)
@@ -536,8 +536,6 @@
                if (objectType != null) {
                        if (colIndex == 0) {
                                return false;
-                       } else if (objectType == ArrayList.class) {
-                               return false;
                        } else if (objectType == HashMap.class) {
                                return false;
                        } else {

--

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