Menu

Help required for TreeMap class

Help
maliha z
2015-09-13
2015-09-14
  • maliha z

    maliha z - 2015-09-13

    Hi,
    Using the TreeView.java class of prefuse demo project, Im trying to modify one of the node to work as a clickable node, and when user clicks on it, it should lauch the browser, well i have seen many people posting for this but in reply they are told to user Browserlauncher class etc, but my query is how would i modify my .xml file to tell the code which of the node is clickable and which one is not....plz help me with this, i would be really grateful.

    Here is my Java Code

    package com.mycompany.mavenproject1;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Point2D;

    import javax.swing.AbstractAction;
    import javax.swing.BorderFactory;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;

    import prefuse.Constants;
    import prefuse.Display;
    import prefuse.Visualization;
    import prefuse.action.Action;
    import prefuse.action.ActionList;
    import prefuse.action.ItemAction;
    import prefuse.action.RepaintAction;
    import prefuse.action.animate.ColorAnimator;
    import prefuse.action.animate.LocationAnimator;
    import prefuse.action.animate.QualityControlAnimator;
    import prefuse.action.animate.VisibilityAnimator;
    import prefuse.action.assignment.ColorAction;
    import prefuse.action.assignment.FontAction;
    import prefuse.action.assignment.SizeAction;
    import prefuse.action.filter.FisheyeTreeFilter;
    import prefuse.action.layout.CollapsedSubtreeLayout;

    import prefuse.action.layout.graph.NodeLinkTreeLayout;

    import prefuse.activity.SlowInSlowOutPacer;
    import prefuse.controls.ControlAdapter;

    import prefuse.controls.FocusControl;
    import prefuse.controls.PanControl;
    import prefuse.controls.WheelZoomControl;
    import prefuse.controls.ZoomControl;
    import prefuse.controls.ZoomToFitControl;
    import prefuse.data.Schema;

    import prefuse.data.Tree;
    import prefuse.data.Tuple;
    import prefuse.data.event.TupleSetListener;
    import prefuse.data.io.TreeMLReader;
    import prefuse.data.search.PrefixSearchTupleSet;
    import prefuse.data.tuple.TupleSet;
    import static prefuse.demos.FisheyeMenu.LABEL;
    import prefuse.render.DefaultRendererFactory;
    import prefuse.render.EdgeRenderer;
    import prefuse.render.AbstractShapeRenderer;
    import prefuse.render.LabelRenderer;
    import prefuse.util.ColorLib;
    import prefuse.util.FontLib;
    import prefuse.util.force.ForceItem;
    import prefuse.util.ui.BrowserLauncher;
    import prefuse.util.ui.JFastLabel;
    import prefuse.util.ui.JSearchPanel;
    import prefuse.visual.VisualItem;
    import prefuse.visual.expression.InGroupPredicate;
    import prefuse.visual.sort.ItemSorter;
    import prefuse.visual.sort.TreeDepthItemSorter;
    /**
    *
    * @version 1.0

    */
    public class NewDspaceTopicMap extends Display {

    public static final String TREE_CHI = "data/my-onto.xml";
    
    private static final String tree = "tree";
    private static final String treeNodes = "tree.nodes";
    private static final String treeEdges = "tree.edges";
    
    private LabelRenderer m_nodeRenderer;
    private EdgeRenderer m_edgeRenderer;
    
    private String m_label = "label";
    private int m_orientation = Constants.ORIENT_LEFT_RIGHT;
    
    public NewDspaceTopicMap(Tree t, String label) {
        super(new Visualization());
        m_label = label;
        m_vis.add(tree, t);
        m_nodeRenderer = new LabelRenderer(m_label, "image");
        m_nodeRenderer.setRenderType(AbstractShapeRenderer.RENDER_TYPE_FILL);
        m_nodeRenderer.setHorizontalAlignment(Constants.LEFT);
        m_nodeRenderer.setRoundedCorner(8,8);
        m_nodeRenderer.setMaxImageDimensions(100,100);
        m_edgeRenderer = new EdgeRenderer(Constants.EDGE_TYPE_CURVE);
    
        DefaultRendererFactory rf = new DefaultRendererFactory(m_nodeRenderer);
        rf.add(new InGroupPredicate(treeEdges), m_edgeRenderer);
        m_vis.setRendererFactory(rf);
    
        // colors
        ItemAction nodeColor = new NodeColorAction(treeNodes);
        ItemAction textColor = new ColorAction(treeNodes,
        VisualItem.TEXTCOLOR, ColorLib.rgb(0,0,0));
        m_vis.putAction("textColor", textColor);
    
        ItemAction edgeColor = new ColorAction(treeEdges,
                VisualItem.STROKECOLOR, ColorLib.rgb(255,255,200));
    
        // quick repaint
        ActionList repaint = new ActionList();
        repaint.add(nodeColor);
        repaint.add(new RepaintAction());
        m_vis.putAction("repaint", repaint);
    
        // full paint
        ActionList fullPaint = new ActionList();
        fullPaint.add(nodeColor);
        m_vis.putAction("fullPaint", fullPaint);
    
        // animate paint change
        ActionList animatePaint = new ActionList(400);
        animatePaint.add(new ColorAnimator(treeNodes));
        animatePaint.add(new RepaintAction());
        m_vis.putAction("animatePaint", animatePaint);
    
        // create the tree layout action
        NodeLinkTreeLayout treeLayout = new NodeLinkTreeLayout(tree,
                m_orientation, 50, 0, 8);
        treeLayout.setLayoutAnchor(new Point2D.Double(25,400));
        m_vis.putAction("treeLayout", treeLayout);
    
        CollapsedSubtreeLayout subLayout = 
            new CollapsedSubtreeLayout(tree, m_orientation);
        m_vis.putAction("subLayout", subLayout);
    
        AutoPanAction autoPan = new AutoPanAction();
    
        // create the filtering and layout
        ActionList filter = new ActionList();
        filter.add(new FisheyeTreeFilter(tree, 2));
        filter.add(new FontAction(treeNodes, FontLib.getFont("Tahoma", 14)));
        filter.add(treeLayout);
        filter.add(subLayout);
        filter.add(textColor);
        filter.add(nodeColor);
        filter.add(edgeColor);
        m_vis.putAction("filter", filter);
    
        // animated transition
        ActionList animate = new ActionList(1000);
        animate.setPacingFunction(new SlowInSlowOutPacer());
        animate.add(autoPan);
        animate.add(new QualityControlAnimator());
        animate.add(new VisibilityAnimator(tree));
        animate.add(new LocationAnimator(treeNodes));
        animate.add(new ColorAnimator(treeNodes));
        animate.add(new RepaintAction());
        m_vis.putAction("animate", animate);
        m_vis.alwaysRunAfter("filter", "animate");
    
        // create animator for orientation changes
        ActionList orient = new ActionList(2000);
        orient.setPacingFunction(new SlowInSlowOutPacer());
        orient.add(autoPan);
        orient.add(new QualityControlAnimator());
        orient.add(new LocationAnimator(treeNodes));
        orient.add(new RepaintAction());
        m_vis.putAction("orient", orient);
    
        // ------------------------------------------------
    
        // initialize the display
        setSize(700,600);
        setItemSorter(new TreeDepthItemSorter());
        addControlListener(new ZoomToFitControl());
        addControlListener(new ZoomControl());
        addControlListener(new WheelZoomControl());
        addControlListener(new PanControl());
        addControlListener(new FocusControl(1, "filter"));
    
        registerKeyboardAction(
            new OrientAction(Constants.ORIENT_LEFT_RIGHT),
            "left-to-right", KeyStroke.getKeyStroke("ctrl 1"), WHEN_FOCUSED);
        registerKeyboardAction(
            new OrientAction(Constants.ORIENT_TOP_BOTTOM),
            "top-to-bottom", KeyStroke.getKeyStroke("ctrl 2"), WHEN_FOCUSED);
        registerKeyboardAction(
            new OrientAction(Constants.ORIENT_RIGHT_LEFT),
            "right-to-left", KeyStroke.getKeyStroke("ctrl 3"), WHEN_FOCUSED);
        registerKeyboardAction(
            new OrientAction(Constants.ORIENT_BOTTOM_TOP),
            "bottom-to-top", KeyStroke.getKeyStroke("ctrl 4"), WHEN_FOCUSED);
    
        // ------------------------------------------------
    
        // filter graph and perform layout
        setOrientation(m_orientation);
        m_vis.run("filter");
    
        TupleSet search = new PrefixSearchTupleSet(); 
        m_vis.addFocusGroup(Visualization.SEARCH_ITEMS, search);
        search.addTupleSetListener(new TupleSetListener() {
            public void tupleSetChanged(TupleSet t, Tuple[] add, Tuple[] rem) {
                m_vis.cancel("animatePaint");
                m_vis.run("fullPaint");
                m_vis.run("animatePaint");
            }
        });
    }
    
    // ------------------------------------------------------------------------
    
    public void setOrientation(int orientation) {
        NodeLinkTreeLayout rtl 
            = (NodeLinkTreeLayout)m_vis.getAction("treeLayout");
        CollapsedSubtreeLayout stl
            = (CollapsedSubtreeLayout)m_vis.getAction("subLayout");
        switch ( orientation ) {
        case Constants.ORIENT_LEFT_RIGHT:
            m_nodeRenderer.setHorizontalAlignment(Constants.LEFT);
            m_edgeRenderer.setHorizontalAlignment1(Constants.RIGHT);
            m_edgeRenderer.setHorizontalAlignment2(Constants.LEFT);
            m_edgeRenderer.setVerticalAlignment1(Constants.CENTER);
            m_edgeRenderer.setVerticalAlignment2(Constants.CENTER);
            break;
        case Constants.ORIENT_RIGHT_LEFT:
            m_nodeRenderer.setHorizontalAlignment(Constants.RIGHT);
            m_edgeRenderer.setHorizontalAlignment1(Constants.LEFT);
            m_edgeRenderer.setHorizontalAlignment2(Constants.RIGHT);
            m_edgeRenderer.setVerticalAlignment1(Constants.CENTER);
            m_edgeRenderer.setVerticalAlignment2(Constants.CENTER);
            break;
        case Constants.ORIENT_TOP_BOTTOM:
            m_nodeRenderer.setHorizontalAlignment(Constants.CENTER);
            m_edgeRenderer.setHorizontalAlignment1(Constants.CENTER);
            m_edgeRenderer.setHorizontalAlignment2(Constants.CENTER);
            m_edgeRenderer.setVerticalAlignment1(Constants.BOTTOM);
            m_edgeRenderer.setVerticalAlignment2(Constants.TOP);
            break;
        case Constants.ORIENT_BOTTOM_TOP:
            m_nodeRenderer.setHorizontalAlignment(Constants.CENTER);
            m_edgeRenderer.setHorizontalAlignment1(Constants.CENTER);
            m_edgeRenderer.setHorizontalAlignment2(Constants.CENTER);
            m_edgeRenderer.setVerticalAlignment1(Constants.TOP);
            m_edgeRenderer.setVerticalAlignment2(Constants.BOTTOM);
            break;
        default:
            throw new IllegalArgumentException(
                "Unrecognized orientation value: "+orientation);
        }
        m_orientation = orientation;
        rtl.setOrientation(orientation);
        stl.setOrientation(orientation);
    }
    
    public int getOrientation() {
        return m_orientation;
    }
    
    // ------------------------------------------------------------------------
    
    public static void main(String argv[]) {
    
        /***********/
        String infile = TREE_CHI;
        String label = "name";
        if ( argv.length > 1 ) {
            infile = argv[0];
            label = argv[1];
        }
        JComponent treeview = demo(infile, label);
    
        JFrame frame = new JFrame("..:: My Topic Map View ::..");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(treeview);
        frame.pack();
        frame.setVisible(true);
    }
    
    public static JComponent demo() {
        return demo(TREE_CHI, "name");
    }
    
    public static JComponent demo(String datafile, final String label) {
        Color BACKGROUND = Color.LIGHT_GRAY;
        Color FOREGROUND = Color.blue;
    
        Tree t = null;
        try {
            t = (Tree)new TreeMLReader().readGraph(datafile);
            System.out.println("code comes here");
        } catch ( Exception e ) {
            e.printStackTrace();
            System.exit(1);
        }
    
        // create a new treemap
        final NewDspaceTopicMap tview = new NewDspaceTopicMap(t, label);
        tview.setBackground(BACKGROUND);
        tview.setForeground(FOREGROUND);
    
        // create a search panel for the tree map
        JSearchPanel search = new JSearchPanel(tview.getVisualization(),
            treeNodes, Visualization.SEARCH_ITEMS, label, true, true);
        search.setShowResultCount(true);
        search.setBorder(BorderFactory.createEmptyBorder(5,5,4,0));
        search.setFont(FontLib.getFont("Tahoma", Font.PLAIN, 11));
        search.setBackground(BACKGROUND);
        search.setForeground(FOREGROUND);
    
        final JFastLabel title = new JFastLabel("                 ");
        title.setPreferredSize(new Dimension(350, 20));
        title.setVerticalAlignment(SwingConstants.BOTTOM);
        title.setBorder(BorderFactory.createEmptyBorder(3,0,0,0));
        title.setFont(FontLib.getFont("Tahoma", Font.PLAIN, 14));
        title.setBackground(BACKGROUND);
        title.setForeground(FOREGROUND);
    
        tview.addControlListener(new ControlAdapter() {
            public void itemEntered(VisualItem item, MouseEvent e) {
                if ( item.canGetString(label) )
                   // System.out.println("item entered" + item.canGetString(label));
                    title.setText(item.getString(label));
            }
            public void itemExited(VisualItem item, MouseEvent e) {
                title.setText(null);
            }
            public static final String URL = "http://myurl.tk/jspuiseecs/handle/123456789/";
    
            public void itemClicked(VisualItem item, MouseEvent e) {
            if (!SwingUtilities.isLeftMouseButton(e)) return;
            if ( e.getClickCount() == 1 ) {
                String id = item.getString("type");
                System.out.println(id);
                BrowserLauncher.showDocument(URL+id);
            }
        }
        });
    
        Box box = new Box(BoxLayout.X_AXIS);
        box.add(Box.createHorizontalStrut(10));
        box.add(title);
        box.add(Box.createHorizontalGlue());
        box.add(search);
        box.add(Box.createHorizontalStrut(3));
        box.setBackground(Color.blue);
    
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(BACKGROUND);
        panel.setForeground(FOREGROUND);
        panel.add(tview, BorderLayout.CENTER);
        panel.add(box, BorderLayout.SOUTH);
        return panel;
    //return new NewDspaceTopicMap(t,label);
    }
    
    // ------------------------------------------------------------------------
    
    public class OrientAction extends AbstractAction {
        private int orientation;
    
        public OrientAction(int orientation) {
            this.orientation = orientation;
        }
        public void actionPerformed(ActionEvent evt) {
            setOrientation(orientation);
            getVisualization().cancel("orient");
            getVisualization().run("treeLayout");
            getVisualization().run("orient");
        }
    }
    
    public class AutoPanAction extends Action {
        private Point2D m_start = new Point2D.Double();
        private Point2D m_end   = new Point2D.Double();
        private Point2D m_cur   = new Point2D.Double();
        private int     m_bias  = 150;
    
        public void run(double frac) {
            TupleSet ts = m_vis.getFocusGroup(Visualization.FOCUS_ITEMS);
            if ( ts.getTupleCount() == 0 )
                return;
    
            if ( frac == 0.0 ) {
                int xbias=0, ybias=0;
                switch ( m_orientation ) {
                case Constants.ORIENT_LEFT_RIGHT:
                    xbias = m_bias;
                    break;
                case Constants.ORIENT_RIGHT_LEFT:
                    xbias = -m_bias;
                    break;
                case Constants.ORIENT_TOP_BOTTOM:
                    ybias = m_bias;
                    break;
                case Constants.ORIENT_BOTTOM_TOP:
                    ybias = -m_bias;
                    break;
                }
    
                VisualItem vi = (VisualItem)ts.tuples().next();
                m_cur.setLocation(getWidth()/2, getHeight()/2);
                getAbsoluteCoordinate(m_cur, m_start);
                m_end.setLocation(vi.getX()+xbias, vi.getY()+ybias);
            } else {
                m_cur.setLocation(m_start.getX() + frac*(m_end.getX()-m_start.getX()),
                                  m_start.getY() + frac*(m_end.getY()-m_start.getY()));
                panToAbs(m_cur);
            }
        }
    }
    
    public static class NodeColorAction extends ColorAction {
    
        public NodeColorAction(String group) {
            super(group, VisualItem.FILLCOLOR);
        }
    
        public int getColor(VisualItem item) {
            if ( m_vis.isInGroup(item, Visualization.SEARCH_ITEMS) )
                return ColorLib.rgb(255,190,190);
            else if ( m_vis.isInGroup(item, Visualization.FOCUS_ITEMS) )
                return ColorLib.rgb(198,229,229);
            else if ( item.getDOI() > -1 )
                return ColorLib.rgb(164,193,193);
            else
                return ColorLib.rgba(255,255,255,0);
        }
    
    } // end of inner class TreeMapColorAction
    
        public static class DataMountainSorter extends ItemSorter {
        public int score(VisualItem item) {
            return (int)(10000*item.getY());
        }
    } // end of inner class DataMountainComparator
    
    public class DataMountainSizeAction extends SizeAction {
        public double getSize(VisualItem item) {
            double y = item.getEndY();
            return 0.2 + y/1400;
        }
    } // end of inner class DataMountainSizeAction
    
    private static final String ANCHORITEM = "_anchorItem";
    private static final Schema ANCHORITEM_SCHEMA = new Schema();
    static {
        ANCHORITEM_SCHEMA.addColumn(ANCHORITEM, ForceItem.class);
    }
    

    }// end of class TreeView

    Regards,
    malih

     

    Last edit: maliha z 2015-09-13
  • maliha z

    maliha z - 2015-09-13

    And people here is my xml file, i am unable to define my url value to the attribute, plz tell me where im going wrong, currently im able to navigate to the desired page, but every node gets clicked, i.e even those whom i dnt want to be clicked.
    here is my xml:

    <declarations>
    <attributeDecl name="name" type="String"/>
    <attributeDecl name="type" type="String"/>

    </declarations>
    <branch>
    <attribute name="name" value="DDBC"/>
    <branch>
    <attribute name="name" value="Documents"/>

    <branch>
    <attribute name="name" value="Reports"/>
    <branch>
    <attribute name="name" value="Doc name"/>
    <branch>
    <attribute name="name" value="Efficient Hybrid Parallelism"/>
    <leaf>
    <attribute name="name" value="Author- Javed, Muhammad Ansar"/>
    </leaf>
    <leaf>
    <attribute name="name" value="Advisor"/>
    </leaf>
    <branch>
    <attribute name="name" value="Co-Advisor"/>
    <leaf>
    <attribute name="name" value="Jam, Moh"/>
    </leaf>
    </branch>
    <leaf>
    <attribute name="name" value="Issue Date-4/24/2015"/>
    </leaf>
    <branch>
    <attribute name="name" value="URI"/>
    <leaf>
    <attribute name="name" value="1204"/>
    </leaf>
    </branch>

     
  • maliha z

    maliha z - 2015-09-13

    dear all, the problem is resolved when i define itemclicked==2, instead of 1 :) Now the other problem is how to i use image in one of the nodes and make it clickable? any suggestions??

     
  • Björn Kruse

    Björn Kruse - 2015-09-13

    Hi,
    I haven't been reading this forum for years, but take a look at DataMountain Demo http://prefuse.org/gallery/datamountain/

    It uses images and is clickable, if I remember right.

    Maybe, in the itemClicked method, just get the field value from your node/item and check if its type/value is what you want to be clickable. If not, just exit without doing anything.
    Looks like you want nodes with name==URI to be clickable?

    Good luck,
    Björn

     
  • maliha z

    maliha z - 2015-09-14

    Hey, Bjorn, thanks for your response. Yes DataMountain is what im looking at but it defines its images in a folder and also uses amazon.txt file to get the ids of the images, pass it to URL and navigate to the respective page on amazon site. But in my case im using xml file, and also NodeLinkTree layout, in which i have multiple nodes, out of which i want one of my nodes(URI) to be clickable, that i have done already. Now im trying to load an image in another node, on click of that image, i want it to launch browser and open the URL i specify in the code may be..
    any suggestions for that please?

     

Log in to post a comment.