Author: Christian Lopes
Date: 2011-03-09 15:06:27 -0800 (Wed, 09 Mar 2011)
New Revision: 24351
Modified:
core3/default-mappingcalculators/trunk/src/main/java/org/cytoscape/view/vizmap/mappings/DiscreteMapping.java
Log:
Fixed discrete mapping for List-type attributes. Changed the internal Map from
TreeMap to HasMap, in order to avoid potential ClassCastExceptions.
Modified:
core3/default-mappingcalculators/trunk/src/main/java/org/cytoscape/view/vizmap/mappings/DiscreteMapping.java
===================================================================
---
core3/default-mappingcalculators/trunk/src/main/java/org/cytoscape/view/vizmap/mappings/DiscreteMapping.java
2011-03-09 22:09:34 UTC (rev 24350)
+++
core3/default-mappingcalculators/trunk/src/main/java/org/cytoscape/view/vizmap/mappings/DiscreteMapping.java
2011-03-09 23:06:27 UTC (rev 24351)
@@ -26,14 +26,12 @@
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
-*/
+ */
package org.cytoscape.view.vizmap.mappings;
-
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.SortedMap;
-import java.util.TreeMap;
import org.cytoscape.model.CyColumn;
import org.cytoscape.model.CyRow;
@@ -43,21 +41,21 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
/**
* Implements a lookup table mapping data to values of a particular class. The
* data value is extracted from a bundle of attributes by using a specified
data
* attribute name.
*/
public class DiscreteMapping<K, V> extends AbstractVisualMappingFunction<K, V>
{
-
- private static final Logger logger =
LoggerFactory.getLogger(DiscreteMapping.class);
-
- // Name of mapping. This will be used by toString() method.
+
+ private static final Logger logger = LoggerFactory
+ .getLogger(DiscreteMapping.class);
+
+ // Name of mapping. This will be used by toString() method.
protected static final String DISCRETE = "Discrete Mapping";
-
+
// contains the actual map elements (sorted)
- private final SortedMap<K, V> attribute2visualMap;
+ private final Map<K, V> attribute2visualMap;
/**
* Constructor.
@@ -68,7 +66,7 @@
public DiscreteMapping(final String attrName, final Class<K> attrType,
final VisualProperty<V> vp) {
super(attrName, attrType, vp);
- attribute2visualMap = new TreeMap<K, V>();
+ attribute2visualMap = new HashMap<K, V>();
}
@Override
@@ -101,42 +99,37 @@
* the type-parameter of the View
*/
private void applyDiscreteMapping(final View<? extends CyTableEntry>
view) {
-
final CyRow row = view.getModel().getCyRow();
-//
-// logger.debug("Target View = " + view.getModel().getSUID());
-// logger.debug("AttrName = " + attrName);
-// logger.debug("AttrType = " + attrType);
-// logger.debug("Row keys = " + row.getAllValues().keySet());
-// logger.debug("Row vals = " + row.getAllValues().values());
-
-
+ V value = null;
+
if (row.isSet(attrName)) {
// skip Views where source attribute is not defined;
// ViewColumn will automatically substitute the per-VS
or global
// default, as appropriate
final CyColumn column =
row.getTable().getColumn(attrName);
final Class<?> attrClass = column.getType();
- Object key = null;
-
+
if (attrClass.isAssignableFrom(List.class)) {
List<?> list = row.getList(attrName,
column.getListElementType());
- key = list != null ? list.toString() : "";
+
+ if (list != null) {
+ for (Object item : list) {
+ // TODO: should we convert
other types to String?
+ String key = item.toString();
+ value =
attribute2visualMap.get(key);
+ if (value != null) break;
+ }
+ }
} else {
- key = row.get(attrName, attrType);
+ Object key = row.get(attrName, attrType);
+
+ if (key != null)
+ value = attribute2visualMap.get(key);
}
-
- if (key != null &&
attribute2visualMap.containsKey(key)) {
- final V value = attribute2visualMap.get(key);
- // Assign value to view
- view.setVisualProperty(vp, value);
- } else { // remove value so that default value will be
used:
- // Set default value
- view.setVisualProperty(vp, null);
- }
- } else { // remove value so that default value will be used:
- view.setVisualProperty(vp, null);
}
+
+ // set a new value or null to use the default one:
+ view.setVisualProperty(vp, value);
}
/**
--
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.