Picollo with interactive swing GUI nodes

65 views
Skip to first unread message

Nedelin Rusev

unread,
Mar 26, 2013, 4:09:25 PM3/26/13
to piccolo...@googlegroups.com

Hi all,

I have a Java application which uses Swing for the GUI items.

The application has a number of JPanles, which contains some JButtons, JCheckBox etc.
They are placed in a specific order, using some Layout manager and depend on one another.
Simple example:

Each JButton calls a Dialogue window and the check box will make the grayed nodes active.
Unfortunately this layout is static and the nodes can not be moved. I need to make the movable and add some connections between them.

I used Picollo2D for this and succeeded creating the connections. Now the application looks like this:


The problem now is that the Jitems in the nodes become inactive. The JButtons can not be pressed and no dialogues are opened.
The check box can not be clicked and no activation is performed in the grayed nodes.
I tried to add mouse listeners to each Jitem, but it only transferred the focus to the item.

My questions are:
- why I can't transfer the mouse clicks to the Jitems
- how should I fix this


Thank you.

 I will place here my code. Unfortunately the code for the JPanels is quite big so I can not place it here.

package WoWPanelUI.PicolloGraph;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import edu.umd.cs.piccolo.PLayer;
import edu.umd.cs.piccolo.PNode;
import edu.umd.cs.piccolo.event.PDragEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;
import edu.umd.cs.piccolo.event.PInputEventFilter;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolox.pswing.PSwing;
import edu.umd.cs.piccolox.pswing.PSwingCanvas;

public class WoWTGraph extends PSwingCanvas
{
    private static final long serialVersionUID = 1L;

    private PLayer nodeLayer = getLayer();
    private PLayer edgeLayer = new PLayer();

    public WoWTGraph(Dimension dim)
    {
        super();
        setPreferredSize(dim);
        getRoot().addChild(edgeLayer);
        getCamera().addLayer(0, edgeLayer);

        nodeLayer.addInputEventListener(new PDragEventHandler()
        {
            {
                PInputEventFilter filter = new PInputEventFilter();
                filter.setOrMask(InputEvent.BUTTON1_MASK | InputEvent.BUTTON3_MASK);
                setEventFilter(filter);
            }

            public void mouseEntered(PInputEvent e)
            {
                super.mouseEntered(e);
                if (e.getButton() == MouseEvent.NOBUTTON) {
                    e.getPickedNode().setPaint(Color.RED);
                }
            }

            public void mouseExited(PInputEvent e)
            {
                super.mouseExited(e);
                if (e.getButton() == MouseEvent.NOBUTTON) {
                    e.getPickedNode().setPaint(Color.WHITE);
                }
            }

            protected void startDrag(PInputEvent e)
            {
                super.startDrag(e);
                e.setHandled(true);
                e.getPickedNode().moveToFront();
            }

            @SuppressWarnings("unchecked")
            protected void drag(PInputEvent e)
            {
                try
                {
                    super.drag(e);
                    ArrayList<PPath> edges = (ArrayList<PPath>) e.getPickedNode().getAttribute("egdes");
                    for (int i = 0; i < edges.size(); i++)
                    {
                        update_connection(edges.get(i));
                    }
                }
                catch (Exception error)
                {
                    System.err.println("Failed to drag panel!");
                    System.err.println("Caught Exception: " + error.getMessage());
                }
            }
        });
    }

    private void attach_mouse_listener(JComponent panel)
    {
        for(int i=0; i<panel.getComponentCount(); i++)
        {
            panel.getComponent(i).addMouseListener(new MyJPanelMouseListener());
        }
    }
   
    private PSwing position_node(JComponent panel)
    {
        PSwing node = null;
        attach_mouse_listener(panel);
        Point position = panel.getLocation();
        node = new PSwing(panel);
        node.addAttribute("egdes", new ArrayList<PPath>());
        node.translate(position.getX(), position.getY());
        nodeLayer.addChild(node);
        return node;
    }

    @SuppressWarnings("unchecked")
    private void update_connection(PPath edge)
    {
        try
        {
            PNode node_to = (PNode) ((ArrayList<PSwing>)edge.getAttribute("nodes")).get(0);
            PNode node_from = (PNode) ((ArrayList<PSwing>)edge.getAttribute("nodes")).get(1);

            Point2D start = node_from.getFullBoundsReference().getCenter2D();
            Point2D end = node_to.getFullBoundsReference().getCenter2D();

            edge.reset();
            edge.moveTo((float)start.getX(), (float)start.getY());
            edge.lineTo((float)end.getX(), (float)end.getY());
        }
        catch (Exception e)
        {
            System.err.println("Failed to update connection!");
            System.err.println("Caught Exception: " + e.getMessage());
        }
    }


    public PSwing add_node(JComponent panel)
    {
        try
        {
            PSwing node = null;
            boolean match = false;
            // Check if the component is already added.
            // If yes return this reference
            for(int i=0; i<nodeLayer.getChildrenCount(); i++)
            {
                node = (PSwing)nodeLayer.getChild(i);
                JComponent used_panel = node.getComponent();
                if(used_panel.equals(panel))
                {
                    match = true;
                    break;
                }
            }
            if(match == false)
            {
                node = position_node(panel);
            }
            return node;
        }
        catch (Exception e)
        {
            System.err.println("Failed to add panel!");
            System.err.println("Caught Exception: " + e.getMessage());
            return null;
        }
    }


    @SuppressWarnings("unchecked")
    public void create_connections(JComponent to, JComponent from)
    {
        try
        {
            PSwing node_to = add_node(to);
            PSwing node_from = add_node(from);

            PPath edge = new PPath();
            edge.addAttribute("nodes", new ArrayList<PSwing>());

            ((ArrayList<PSwing>)edge.getAttribute("nodes")).add(node_to);
            ((ArrayList<PSwing>)edge.getAttribute("nodes")).add(node_from);

            ((ArrayList<PPath>)node_to.getAttribute("egdes")).add(edge);
            ((ArrayList<PPath>)node_from.getAttribute("egdes")).add(edge);

            edgeLayer.addChild(edge);
            update_connection(edge);
        }
        catch (Exception e)
        {
            System.err.println("Failed to create connection!");
            System.err.println("Caught Exception: " + e.getMessage());
        }
    }
}


class MyJPanelMouseListener implements MouseListener
{
    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.printf("MyJPanel: Button %s mouseClicked!\n", e.getComponent().getClass().getName());
        JComponent comp = (JComponent) e.getComponent();
        comp.requestFocus();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.printf("MyJPanel: Button %s mouseEntered!\n",  e.getComponent().getClass().getName());
        JComponent comp = (JComponent) e.getComponent();
        comp.requestFocus();
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.printf("MyJPanel: Button %s mouseExited!\n",  e.getComponent().getClass().getName());
    }

    @Override
    public void mousePressed(MouseEvent e) {
        e.consume();
        // TODO Auto-generated method stub
        System.out.printf("MyJPanel: Button %s mousePressed!\n",  e.getComponent().getClass().getName());
        JComponent comp = (JComponent) e.getComponent();
        comp.requestFocus();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.printf("MyJPanel: Button %s mouseReleased!\n",  e.getComponent().getClass().getName());
    }
}



Nedelin Rusev

unread,
Mar 28, 2013, 2:32:00 AM3/28/13
to piccolo...@googlegroups.com
Hi,

I can't believe that nobody has done this. I even found it in some examples, with the small difference that there is no graph on the examples(PSwingExample*.java). But these examples did not solve my problem.

Anybody?

Greetings, Ned.

Nedelin Rusev

unread,
Mar 28, 2013, 3:13:09 AM3/28/13
to piccolo...@googlegroups.com
OK,

False allert. I found the problem. I was consuming the events when the dragging of nodes started:

            protected void startDrag(PInputEvent e)
            {
                super.startDrag(e);
                e.setHandled(true); ------> remove this line
                e.getPickedNode().moveToFront();
  }
Also the event filter is not needed. I removed these lines:


            {
                PInputEventFilter filter = new PInputEventFilter();
                filter.setOrMask(InputEvent.
BUTTON1_MASK | InputEvent.BUTTON3_MASK);
                setEventFilter(filter);
            }

Greetings, Nedelin.
Reply all
Reply to author
Forward
0 new messages