Custom connector extend TreeConnector not works

Hi,

I trying to create server-side component, which extends Tree, with custom connector, which extend TreeConnector.
I`m create jar-file with my component, connector, custom cilent-side component extends VTree and gwt.xml file, and i put to my application lib, compile with vaadin gradle plugin.

As result i can see my custom component in application, but it connects with TreeConnector.

What i am doing wrong?

26040.jpg
26041.jpg

Hi,

Without seeing your code, the only thing that I can think of is that you are missing the @Connect(“MainTree.class”) annotation on your MainTreeConnector class. If this is not the case, could you share the code for your connector and server side classes?

Here is my code

package com.smartway.tree.client;

import com.google.gwt.aria.client.Roles;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Widget;
import com.smartway.tree.MainTree;
import com.vaadin.client.*;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.client.ui.VTree;
import com.vaadin.client.ui.tree.TreeConnector;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.MultiSelectMode;
import com.vaadin.shared.ui.tree.TreeConstants;

import java.util.Iterator;
import java.util.Set;

@Connect(MainTree.class)
public class MainTreeConnector extends TreeConnector {

    @Override
    protected Widget createWidget() {
        return GWT.create(VMainTreeWidget.class);
    }
    @Override
    protected void init() {
        this.getWidget().connector = this;
    }

    public VMainTreeWidget getWidget() {
        return (VMainTreeWidget)super.getWidget();
    }

    @Override
    public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
        if (!AbstractComponentConnector.isRealUpdate(uidl)) {
            return;
        }

        getWidget().rendering = true;

        getWidget().client = client;

        if (uidl.hasAttribute("partialUpdate")) {
            handleUpdate(uidl);

            // IE8 needs a hack to measure the tree again after update
            WidgetUtil.forceIE8Redraw(getWidget().getElement());

            getWidget().rendering = false;
            return;
        }

        getWidget().paintableId = uidl.getId();

        getWidget().immediate = getState().immediate;

        getWidget().disabled = !isEnabled();
        getWidget().readonly = isReadOnly();

        getWidget().dragMode = uidl.hasAttribute("dragMode") ? uidl
                .getIntAttribute("dragMode") : 0;

        getWidget().isNullSelectionAllowed = uidl
                .getBooleanAttribute("nullselect");
        getWidget().isHtmlContentAllowed = uidl
                .getBooleanAttribute(TreeConstants.ATTRIBUTE_HTML_ALLOWED);

        if (uidl.hasAttribute("alb")) {
            getWidget().bodyActionKeys = uidl.getStringArrayAttribute("alb");
        }

        getWidget().body.clear();
        // clear out any references to nodes that no longer are attached
        getWidget().clearNodeToKeyMap();
        tooltipMap.clear();

        VTree.TreeNode childTree = null;
        UIDL childUidl = null;
        for (final Iterator<?> i = uidl.getChildIterator(); i.hasNext();) {
            childUidl = (UIDL) i.next();
            if ("actions".equals(childUidl.getTag())) {
                updateActionMap(childUidl);
                continue;
            } else if ("-ac".equals(childUidl.getTag())) {
                getWidget().updateDropHandler(childUidl);
                continue;
            }
            childTree = getWidget().new TreeNode();
            getConnection().getVTooltip().connectHandlersToWidget(childTree);
            updateNodeFromUIDL(childTree, childUidl, 1);
            getWidget().body.add(childTree);
            childTree.addStyleDependentName("root");
            childTree.childNodeContainer.addStyleDependentName("root");
        }
        if (childTree != null && childUidl != null) {
            boolean leaf = !childUidl.getTag().equals("node");
            childTree.addStyleDependentName(leaf ? "leaf-last" : "last");
            childTree.childNodeContainer.addStyleDependentName("last");
        }
        final String selectMode = uidl.getStringAttribute("selectmode");
        getWidget().selectable = !"none".equals(selectMode);
        getWidget().isMultiselect = "multi".equals(selectMode);

        if (getWidget().isMultiselect) {
            Roles.getTreeRole().setAriaMultiselectableProperty(
                    getWidget().getElement(), true);

            if (BrowserInfo.get().isTouchDevice()) {
                // Always use the simple mode for touch devices that do not have
                // shift/ctrl keys (#8595)
                getWidget().multiSelectMode = MultiSelectMode.SIMPLE;
            } else {
                getWidget().multiSelectMode = MultiSelectMode.valueOf(uidl
                        .getStringAttribute("multiselectmode"));
            }
        } else {
            Roles.getTreeRole().setAriaMultiselectableProperty(
                    getWidget().getElement(), false);
        }

        getWidget().selectedIds = uidl.getStringArrayVariableAsSet("selected");

        // Update lastSelection and focusedNode to point to *actual* nodes again
        // after the old ones have been cleared from the body. This fixes focus
        // and keyboard navigation issues as described in #7057 and other
        // tickets.
        if (getWidget().lastSelection != null) {
            getWidget().lastSelection = getWidget().getNodeByKey(
                    getWidget().lastSelection.key);
        }

        if (getWidget().focusedNode != null) {

            Set<String> selectedIds = getWidget().selectedIds;

            // If the focused node is not between the selected nodes, we need to
            // refresh the focused node to prevent an undesired scroll. #12618.
            if (!selectedIds.isEmpty()
                    && !selectedIds.contains(getWidget().focusedNode.key)) {
                String keySelectedId = selectedIds.iterator().next();

                VTree.TreeNode nodeToSelect = getWidget().getNodeByKey(keySelectedId);

               // getWidget().setFocusedNode(nodeToSelect);
          //  } else {
                getWidget().setFocusedNode(
                        getWidget().getNodeByKey(getWidget().focusedNode.key));
            }
        }

        if (getWidget().lastSelection == null
                && getWidget().focusedNode == null
                && !getWidget().selectedIds.isEmpty()) {
            getWidget().setFocusedNode(
                    getWidget().getNodeByKey(
                            getWidget().selectedIds.iterator().next()));
            getWidget().focusedNode.setFocused(false);
        }

        // IE8 needs a hack to measure the tree again after update
        WidgetUtil.forceIE8Redraw(getWidget().getElement());

        getWidget().rendering = false;

    }
    private void updateActionMap(UIDL uidl) {
        final Iterator<?> it = uidl.getChildIterator();
        while (it.hasNext()) {
            final UIDL action = (UIDL) it.next();
            final String key = action.getStringAttribute("key");
            final String caption = action
                    .getStringAttribute(TreeConstants.ATTRIBUTE_ACTION_CAPTION);
            String iconUrl = null;
            if (action.hasAttribute(TreeConstants.ATTRIBUTE_ACTION_ICON)) {
                iconUrl = getConnection()
                        .translateVaadinUri(
                                action.getStringAttribute(TreeConstants.ATTRIBUTE_ACTION_ICON));
            }
            getWidget().registerAction(key, caption, iconUrl);
        }

    }
    private void handleUpdate(UIDL uidl) {
        final VTree.TreeNode rootNode = getWidget().getNodeByKey(
                uidl.getStringAttribute("rootKey"));
        if (rootNode != null) {
            if (!rootNode.getState()) {
                // expanding node happened server side
                rootNode.setState(true, false);
            }
            String levelPropertyString = Roles.getTreeitemRole()
                    .getAriaLevelProperty(rootNode.getElement());
            int levelProperty;
            try {
                levelProperty = Integer.valueOf(levelPropertyString);
            } catch (NumberFormatException e) {
                levelProperty = 1;
                VConsole.error(e);
            }

            renderChildNodes(rootNode, (Iterator) uidl.getChildIterator(),
                    levelProperty + 1);
        }
    }

    void renderChildNodes(VTree.TreeNode containerNode, Iterator<UIDL> i, int level) {
        containerNode.childNodeContainer.clear();
        containerNode.childNodeContainer.setVisible(true);
        while (i.hasNext()) {
            final UIDL childUidl = i.next();
            // actions are in bit weird place, don't mix them with children,
            // but current node's actions
            if ("actions".equals(childUidl.getTag())) {
                updateActionMap(childUidl);
                continue;
            }
            final VTree.TreeNode childTree = getWidget().new TreeNode();
            getConnection().getVTooltip().connectHandlersToWidget(childTree);
            updateNodeFromUIDL(childTree, childUidl, level);
            containerNode.childNodeContainer.add(childTree);
            if (!i.hasNext()) {
                childTree
                        .addStyleDependentName(childTree.isLeaf() ? "leaf-last"
                                : "last");
                childTree.childNodeContainer.addStyleDependentName("last");
            }
        }
        containerNode.childrenLoaded = true;
    }

}
package com.smartway.tree;

import com.vaadin.event.Transferable;
import com.vaadin.ui.Tree;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public class MainTree extends Tree {
    private NodeStyleSupplier nodeStyleSupplier;

    public NodeStyleSupplier getNodeStyleSupplier() {
        return nodeStyleSupplier;
    }

    public void setNodeStyleSupplier(NodeStyleSupplier nodeStyleSupplier) {
        this.nodeStyleSupplier = nodeStyleSupplier;
    }

    @Override
    public Transferable getTransferable(Map<String, Object> payload) {
        if(nodeStyleSupplier ==null)
        return super.getTransferable(payload);
           TreeTransferable transferable = new TreeTransferable(this, payload);
        Object object = payload.get("itemId");
        if (object != null) {
            Object value = itemIdMapper.get((String) object);

            transferable.setData("itemId", value);
            final String nodeStyle = nodeStyleSupplier.apply(value);
                if (StringUtils.isNotEmpty(nodeStyle)) {
                    transferable.setData(nodeStyle, true);

            }
        }

        return transferable;
    }
}
package com.smartway.tree.client;

import com.vaadin.client.ui.VTree;


public class VMainTreeWidget extends VTree {
    public VMainTreeWidget(){
    }
}

So I tried out your code, it seems to work for me. See the results in the attached image.

Have you made sure that you recompiled the widgetset ?

26042.png

Yep, my widgetset compiles with vaadin gradle plugin,i attach my library, add link on my lib to gradle build and compile project widgetset - i have automaticaly include to my project widgetset inheritence to custom component widgetset. May be i should do something else?

Sometimes i get the same problem. After updating widgetset and recompiling, browser don’t download new version of widgetset, instead it use the old (cached).

Try private (incognito) mode window.

Tried in different browsers - result still same.

Thanks all for help, problem was decided. When i build component with maven plugin it works correctly.