final mxGraph upGraph = new Palette();
final mxGraphComponent up = new ComponentPalette(upGraph);

final mxGraph downGraph = new EditionGraph();
final mxGraphComponent down = new ComponentEditionGraph(downGraph);

I have that code in a sigle class, inserted into many Swing components. I have 4 custom Cells in 'upGraph' (Palette class), and i can drag and drop them to 'down' (ComponentEditionGraph class), but i don't want it to be in reverse mode: from 'down' (ComponentEditionGraph class) to 'up' (ComponentPalette class).

Allowed: from 'up' (ComponentPalette class) to 'down' (ComponentEditionGraph class).

Not allowed: from 'down' (ComponentEditionGraph class) to 'up' (ComponentPalette class).

I've set dragEnabled=false; inside ComponentEditionGraph class, bot the cell disappears when i am dropping it into ComponentPalette class.

Is there any way not to allow drop into ComponentPalette class and place the Cell in its initial position before drag and drop?

Thanks a lot.

asked 30 Oct '12, 08:48

walolinux's gravatar image

walolinux
54
accept rate: 0%


I was bored, so here you go:

package com.mxgraph.examples.swing;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.datatransfer.Transferable;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.TransferHandler;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.handler.mxGraphTransferHandler;
import com.mxgraph.view.mxGraph;

public class DualHelloWorld extends JFrame
{

    private final static String DROPALLOWED_KEY = "DropAllowed";

    private static final long serialVersionUID = -2707712944901661771L;

    public DualHelloWorld()
    {
        super("Hello, World!");

        // left graph
        mxGraph graph1 = new mxGraph();
        Object parent1 = graph1.getDefaultParent();

        graph1.getModel().beginUpdate();
        try
        {
            Object v1 = graph1.insertVertex(parent1, null, "Hello", 20, 20, 80,
                    30);
            Object v2 = graph1.insertVertex(parent1, null, "World!", 240, 150,
                    80, 30);
            graph1.insertEdge(parent1, null, "Edge", v1, v2);
        }
        finally
        {
            graph1.getModel().endUpdate();
        }

            // check the drag source
        mxGraphComponent graphComponent1 = new mxGraphComponent(graph1) {

            protected TransferHandler createTransferHandler()
            {
                return new mxGraphTransferHandler() {

                    public boolean importData(JComponent c, Transferable t)
                    {
                        if( c instanceof mxGraphComponent) {

                            Object obj = ((mxGraphComponent) c).getClientProperty(DROPALLOWED_KEY);
                            boolean allowDrop = obj != null && ((Boolean) obj) == true;

                            if( !allowDrop) {
                                System.out.println( "Drop not allowed here!");
                                return false;
                            }

                        }

                        return super.importData(c, t);

                    }

                };
            }

        };
            // drop from bottom to top panel NOT allowed
        graphComponent1.putClientProperty(DROPALLOWED_KEY, Boolean.FALSE);

        graphComponent1.setBorder( BorderFactory.createLineBorder(Color.red));

        mxGraph graph2 = new mxGraph();
        Object parent2 = graph2.getDefaultParent();

        graph2.getModel().beginUpdate();
        try
        {
            Object v1 = graph2.insertVertex(parent2, null, "Hello", 20, 20, 80,
                    30);
            Object v2 = graph2.insertVertex(parent2, null, "World!", 240, 150,
                    80, 30);
            graph2.insertEdge(parent2, null, "Edge", v1, v2);
        }
        finally
        {
            graph2.getModel().endUpdate();
        }

        mxGraphComponent graphComponent2 = new mxGraphComponent(graph2);

            // drop from top to bottom panel allowed
        graphComponent2.putClientProperty(DROPALLOWED_KEY, Boolean.TRUE);

        getContentPane().setLayout( new GridLayout( 2,1));
        getContentPane().add(graphComponent1);
        getContentPane().add(graphComponent2);

    }

    public static void main(String[] args)
    {
        DualHelloWorld frame = new DualHelloWorld();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

}
link

answered 31 Oct '12, 14:11

Lofi's gravatar image

Lofi
1.0k1221
accept rate: 37%

edited 31 Oct '12, 14:16

It would be helpful if you could provide a proper example, not just a description. However, for the solution you might want to check out mxGraphHandler and/or mxGraphTransferHandler and do proper checks there in the drag/drop code.

link

answered 30 Oct '12, 16:13

Lofi's gravatar image

Lofi
1.0k1221
accept rate: 37%

edited 30 Oct '12, 16:14

Pallete and EditionGraph extends mxgraph. ComponentPalette and ComponentEditionGraph extends mxGraphComponent.

imagen

I want to drag and drop from top to bottom, but no from bottom to top. I've been searching for mxGraphHandler, but without success. Any ideas?.

Thanks.

link

answered 31 Oct '12, 06:19

walolinux's gravatar image

walolinux
54
accept rate: 0%

Thanks but with proper example I meant a fully working example code that we can fix, not a screenshot. Helping you with this is work, it should be as minimum as possible :-)

(31 Oct '12, 13:32) Lofi
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×66
×45
×13

Asked: 30 Oct '12, 08:48

Seen: 2,351 times

Last updated: 31 Oct '12, 14:16