Search in sources :

Example 1 with TreeMapNode

use of net.sf.jtreemap.swing.TreeMapNode in project beakerx by twosigma.

the class TreeMapSerializer method serialize.

@Override
public void serialize(final TreeMap treeMap, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
    TreeMapNode root = treeMap.getRoot();
    process(root, new Visitor<TreeMapNode>() {

        @Override
        public void visit(TreeMapNode node) {
            Object userObject = node.getUserObject();
            Map<String, Object> values;
            if (userObject instanceof Map) {
                values = (Map<String, Object>) userObject;
                if (node.isLeaf()) {
                    Color color = treeMap.getColorProvider().getColor(node);
                    values.put("color", toHex(color));
                    IToolTipBuilder toolTipBuilder = treeMap.getToolTipBuilder();
                    if (toolTipBuilder != null) {
                        values.put(TOOLTIP, toolTipBuilder.getToolTip(node));
                    } else {
                        values.put(TOOLTIP, values.get("label"));
                    }
                }
                node.setUserObject(values);
            } else {
                values = new HashMap<>();
                values.put("label", userObject);
                IToolTipBuilder toolTipBuilder = treeMap.getToolTipBuilder();
                if (toolTipBuilder != null) {
                    values.put(TOOLTIP, toolTipBuilder.getToolTip(node));
                } else {
                    values.put(TOOLTIP, userObject);
                }
            }
            if (node.isLeaf()) {
                Color color = treeMap.getColorProvider().getColor(node);
                values.put("color", toHex(color));
            }
            node.setUserObject(values);
        }
    });
    jgen.writeStartObject();
    serialize(treeMap, jgen);
    if (root != null)
        jgen.writeObjectField(GRAPHICS_LIST, root);
    if (treeMap.getMode() != null)
        jgen.writeObjectField(MODE, treeMap.getMode().getJsName());
    if (treeMap.getSticky() != null)
        jgen.writeObjectField(STICKY, treeMap.getSticky());
    if (treeMap.getRatio() != null)
        jgen.writeObjectField(RATIO, treeMap.getRatio());
    if (treeMap.getRound() != null)
        jgen.writeObjectField(ROUND, treeMap.getRound());
    jgen.writeObjectField(VALUE_ACCESSOR, treeMap.getValueAccessor());
    jgen.writeEndObject();
}
Also used : IToolTipBuilder(com.twosigma.beakerx.chart.treemap.util.IToolTipBuilder) HashMap(java.util.HashMap) Color(com.twosigma.beakerx.chart.Color) TreeMapNode(net.sf.jtreemap.swing.TreeMapNode) TreeMap(com.twosigma.beakerx.chart.treemap.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with TreeMapNode

use of net.sf.jtreemap.swing.TreeMapNode in project beakerx by twosigma.

the class TreeMapSerializer method process.

private void process(TreeMapNode node, Visitor<TreeMapNode> visitor) {
    visitor.visit(node);
    Iterable<TreeMapNode> children = node.getChildren();
    if (children != null) {
        for (TreeMapNode child : children) {
            process(child, visitor);
        }
    }
}
Also used : TreeMapNode(net.sf.jtreemap.swing.TreeMapNode)

Example 3 with TreeMapNode

use of net.sf.jtreemap.swing.TreeMapNode in project beakerx by twosigma.

the class TreeMapNodeSerializerTest method serializeTreeMapNode_resultJsonHasType.

@Test
public void serializeTreeMapNode_resultJsonHasType() throws IOException {
    // when
    TreeMapNode treeMapNode = new TreeMapNode("label") {
    };
    treeMapNodeSerializer.serialize(treeMapNode, jgen, new DefaultSerializerProvider.Impl());
    jgen.flush();
    // then
    JsonNode actualObj = mapper.readTree(sw.toString());
    Assertions.assertThat(actualObj.has("type")).isTrue();
    Assertions.assertThat(actualObj.get("type").asText()).isEqualTo("TreeMapNode");
}
Also used : DefaultSerializerProvider(com.fasterxml.jackson.databind.ser.DefaultSerializerProvider) TreeMapNode(net.sf.jtreemap.swing.TreeMapNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 4 with TreeMapNode

use of net.sf.jtreemap.swing.TreeMapNode in project beakerx by twosigma.

the class TreeMapNodeSerializerTest method serializeDoubleValueOfTreeMapNode_resultJsonHasDoubleValue.

@Test
public void serializeDoubleValueOfTreeMapNode_resultJsonHasDoubleValue() throws IOException {
    // when
    TreeMapNode treeMapNode = new TreeMapNode("010", 1, new DefaultValue(1.5));
    treeMapNode.setUserObject(values);
    treeMapNodeSerializer.serialize(treeMapNode, jgen, new DefaultSerializerProvider.Impl());
    jgen.flush();
    // then
    JsonNode actualObj = mapper.readTree(sw.toString());
    Assertions.assertThat(actualObj.has("doubleValue")).isTrue();
    Assertions.assertThat(actualObj.get("doubleValue").asDouble()).isEqualTo(1.5);
}
Also used : DefaultValue(net.sf.jtreemap.swing.DefaultValue) DefaultSerializerProvider(com.fasterxml.jackson.databind.ser.DefaultSerializerProvider) TreeMapNode(net.sf.jtreemap.swing.TreeMapNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 5 with TreeMapNode

use of net.sf.jtreemap.swing.TreeMapNode in project beakerx by twosigma.

the class TreeMapTest method setToolTipBuilder_hasToolTipBuilder.

@Test
public void setToolTipBuilder_hasToolTipBuilder() {
    // given
    treeMap = createWidget();
    // when
    treeMap.setToolTipBuilder(new IToolTipBuilder() {

        @Override
        public String getToolTip(TreeMapNode node) {
            return "tooltip";
        }
    });
    // then
    assertThat(treeMap.getToolTipBuilder()).isNotNull();
    LinkedHashMap model = getModelUpdate();
    assertThat(model).isNotNull();
}
Also used : IToolTipBuilder(com.twosigma.beakerx.chart.treemap.util.IToolTipBuilder) TreeMapNode(net.sf.jtreemap.swing.TreeMapNode) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test) ChartTest(com.twosigma.beakerx.chart.ChartTest)

Aggregations

TreeMapNode (net.sf.jtreemap.swing.TreeMapNode)18 Test (org.junit.Test)11 DefaultValue (net.sf.jtreemap.swing.DefaultValue)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 DefaultSerializerProvider (com.fasterxml.jackson.databind.ser.DefaultSerializerProvider)8 KernelTest (com.twosigma.beakerx.KernelTest)3 TreeMap (com.twosigma.beakerx.chart.treemap.TreeMap)3 Before (org.junit.Before)3 ChartTest (com.twosigma.beakerx.chart.ChartTest)2 IToolTipBuilder (com.twosigma.beakerx.chart.treemap.util.IToolTipBuilder)2 Color (com.twosigma.beakerx.chart.Color)1 StringWriter (java.io.StringWriter)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1