0

我正在尝试在小部件之间建立直端连接。但是当我这样做时,我的连接器的方向是错误的。它总是与所需的平行。此外,它独立于连接的小部件,即当我移动时我的小部件,我的连接器没有移动。我的代码片段如下:链接到问题的快照:http: //goo.gl/JUEmJ

公共类 DragNDropPage {

SyncCurrentUser cu = SyncCurrentUser.getUser();
private AbsolutePanel area = new AbsolutePanel();
HorizontalPanel toolsPanel = new HorizontalPanel();
AbsolutePanel canvas = new AbsolutePanel();
DragController toolboxDragController;
Label startLabel = new Label("START");
Label stopLabel = new Label("STOP");
Label activityLabel = new Label("ACTIVITY");
Label processLabel = new Label("PROCESS");
Button stopDrag = new Button("Done Dragging");
Button saveButton = new Button("Save");
PickupDragController dragController = new PickupDragController(area, true);
AbsolutePositionDropController dropController = new AbsolutePositionDropController(area);
private List<Widget> selected = new ArrayList<Widget>();
private List<Widget> onCanvas = new ArrayList<Widget>();
private List<Connection> connections = new ArrayList<Connection>();
private CActivity[] aItems;
private CProcess[] pItems;
MyHandler handler = new MyHandler();
int mouseX,mouseY;


String style;

public DragNDropPage() {
    toolboxDragController = new ToolboxDragController(dropController, dragController);
    RootPanel.get("rightbar").add(area);
    area.setSize("575px", "461px");


    area.add(toolsPanel);
    toolsPanel.setSize("575px", "37px");

    toolsPanel.add(startLabel);
    startLabel.setSize("76px", "37px");
    toolboxDragController.makeDraggable(startLabel);

    toolsPanel.add(stopLabel);
    stopLabel.setSize("66px", "37px");
    toolboxDragController.makeDraggable(stopLabel);

    toolsPanel.add(activityLabel);
    activityLabel.setSize("82px", "36px");
    toolboxDragController.makeDraggable(activityLabel);

    toolsPanel.add(processLabel);
    processLabel.setSize("85px", "36px");
    toolboxDragController.makeDraggable(processLabel);

    stopDrag.addClickHandler(handler);
    toolsPanel.add(stopDrag);
    stopDrag.setWidth("114px");

    saveButton.addClickHandler(handler);
    toolsPanel.add(saveButton);

    area.add(canvas, 0, 36);
    canvas.setSize("575px", "425px");

    Event.addNativePreviewHandler(new Event.NativePreviewHandler() {    
    @Override
    public void onPreviewNativeEvent(NativePreviewEvent event) {
        //46 is the key code for Delete Button
        if(event.getNativeEvent().getKeyCode() == 46 && !selected.isEmpty()) {
                for (Iterator<Widget> i = selected.listIterator(); i.hasNext();) {
                    Widget w = (Widget) i.next();
                    UIObjectConnector.unwrap(w);
                    i.remove();
                    w.removeFromParent();
                    onCanvas.remove(i);
                }
            }
        }
    });

aItems = cu.currentUser.getcActivity();
pItems = cu.currentUser.getcProcess();
}

private class ToolboxDragController extends PickupDragController {

    public ToolboxDragController(final DropController dropController, final DragController nodesDragController) {
        super(area ,false);
        setBehaviorDragProxy(true);
        registerDropController(dropController);

        addDragHandler(new DragHandlerAdapter(){

            public void onPreviewDragEnd(DragEndEvent event) throws VetoDragException {

                Widget node = (Widget) event.getSource();
                int left = event.getContext().desiredDraggableX;
                int top = event.getContext().desiredDraggableY;
                AbsolutePanel panel = (AbsolutePanel) dropController.getDropTarget();

                createConnector((Label) node, panel, left, top);
                throw new VetoDragException();
            }

        });

    }
}

protected UIObjectConnector createConnector(Label proxy, AbsolutePanel panel, int left, int top) {
    Widget w;
    String str = proxy.getText();
    if(str.equals("START") || str.equals("STOP")){
        w = new Label(proxy.getText()){
            public void onBrowserEvent(Event event) {
                if( DOM.eventGetType(event) == 4 
                    && DOM.eventGetCtrlKey(event)){
                    select(this);
                }
                super.onBrowserEvent(event);
            }
        };
        w.getElement().setClassName("dnd-start-stop");
    }
    else{
        w = new ListBox(){
            public void onBrowserEvent(Event event) {
                if( DOM.eventGetType(event) == 4 
                    && DOM.eventGetCtrlKey(event)){
                    select(this);
                }
                super.onBrowserEvent(event);
            }
        };
        if(str.equals("ACTIVITY")){
            getAItems((ListBox)w);
            //w.getElement().addClassName("dnd-activity");
        }
        else if(str.equals("PROCESS")){
            getPItems((ListBox)w);
            //w.getElement().addClassName("dnd-process");
        }
    }
    onCanvas.add(w);
    left -= panel.getAbsoluteLeft();
    top -= panel.getAbsoluteTop();
    //panel.add(w,10,10);
    panel.add(w, left, top);
    dragController.makeDraggable(w);
    return UIObjectConnector.wrap(w);

}

private void getAItems(ListBox w) {
    for(int i=0;i<aItems.length;i++)
        w.addItem(aItems[i].getActivityName());
}

private void getPItems(ListBox w) {
    /*for(int i=0;i<pItems.length;i++)
        w.addItem(pItems[i].getProcessName());*/
    w.addItem("Process1");
}

protected void select(Widget w){
    if(selected.isEmpty()) {
        selected.add(w);
        w.getElement().addClassName("color-green");
    } else if(selected.contains(w)){
        selected.remove(w);
        w.getElement().removeClassName("color-green");
    } else if(selected.size() == 1) {
        Widget w2 = (Widget) selected.get(0);
        connect(UIObjectConnector.getWrapper(w2), UIObjectConnector.getWrapper(w));
        selected.clear();
    }
}

protected void connect(Connector a, Connector b) {
    //System.out.println(a.getLeft());
    //System.out.println(b.getLeft());
    add(new StraightTwoEndedConnection(a,b));
}


private void add(StraightTwoEndedConnection c) {
    canvas.add(c);
    connections.add(c);
    c.update();
}

protected void remove(Connection c) {
    connections.remove(c);
}

class MyHandler implements ClickHandler{

    @Override
    public void onClick(ClickEvent event) {
        Button name = (Button) event.getSource();
        if(name.equals(stopDrag)){
            if(name.getText().equals("Done Dragging")){
                for(Iterator<Widget> i = onCanvas.listIterator();i.hasNext();){
                    Widget w = (Widget) i.next();
                    dragController.makeNotDraggable(w);
                    }
                name.setText("Continue");
                }
            else {
                for(Iterator<Widget> i = onCanvas.listIterator();i.hasNext();){
                    Widget w = (Widget) i.next();
                    dragController.makeDraggable(w);
                    }
                name.setText("Done Dragging");
                }
            }
        else{

            }
    }
}

}

4

1 回答 1

0

我知道这已经很老了,但我在 gwt-connector 库中遇到了类似的问题。

如果您使用标准模式,连接器将不会出现在正确的位置。改用怪癖模式

此外,您需要在拖动 dnd 组件(在拖动侦听器中)时手动执行 connector.update(),以便在拖动时连接与端点一起移动。

于 2011-12-26T01:31:59.400 回答