javagwtgwtp

PresenterWidget removeFromSlot fails


When trying to remove presenterWidget from slot, comparison in class PresenterWidget method rawRemoveFromSlot() fails and getView().removeFromSlot() is not being called.

Exmaple:

//this is called from my presenter
//widget here is an extension of PresenterWidget
WidgetSlot removeSlot = new WidgetSlot("widget" + widget.getId());
removeFromSlot(removeSlot, widget);

WidgetSlot class:

public class WidgetSlot<T extends PresenterWidget<?>> extends Slot<T> {

    private Object identifier;

    public WidgetSlot(Object identifier) {
        super();
        this.identifier = identifier;
    }

    public Object getIdentifier() {
        return identifier;
    }
}

When debugging client side code with browser:

public abstract class PresenterWidget <> {
   ...
    //this method is called
    private void rawRemoveFromSlot(IsSlot<?> slot, PresenterWidget<?> child) {
        if (child != null && child.slot == slot) { <!-- this fails because child.slot is not equal to slot even though they are...
            if (!child.isPopup()) {
                this.getView().removeFromSlot(slot.getRawSlot(), child);
            }

            child.orphan();
        }
    }
   ...
}

slot in method (copied from browser debugger):

slot_0_g$: bZy_g$
    identifier_2_g$: "widget1" 

child.slot in method (copied from browser debugger):

slot_3_g$: bZy_g$
    identifier_2_g$: "widget1"

Unless I am misunderstanding debugger information, both slots seem to be equal? The only way the slot comparison in the method would fail is if slot objects had different references.

Any ideas?


GWT version: 2.8.2

GWTP version: 1.6


Solution

  • Problem is that to determine if slot and child.slot is equal, GWTP implementation uses direct comparison, meaning object references must be equal. In the GWT 2.7 and GWTP 1.4 you could send String as slot, like removeFromSlot("SOME_SLOT", widget) and this would work, since mthod was removeFromSlot(Object object, PresenterWidget w).

    With new version, this method was deprecated and one would have to pass Slot object to method instead...and since I don't have these Slot objects saved and instead have them created on runtime by widget id, removeFromSlot() slot comparison fails.

    Solution would be to save Slot references when widget is created with method setInslot() or maybe there already is way to retrieve widget slot...

    In general using == to compare objects is not advisable. It would be so easy to fix my problem if equals() was used instead.