gpt4 book ai didi

java - FormComponents 有共同的 isVisible 方法

转载 作者:行者123 更新时间:2023-11-30 04:40:06 27 4
gpt4 key购买 nike

有什么办法可以让几个不同的 wicket 组件具有相同的 isVisible() 实现

例如,我有标签、文本字段、DropdownChoices 等,它们具有相同的 isVisible 方法,但我不会为所有这些实现自定义类,因为很难维护对代码的更改。

顺便说一句,由于页面的设计,我无法将它们放入 webmarkupcontainer 中。

我希望他们都能继承这样的东西。

public class DepositoryFormComponent extends Component
{
public DepositoryFormComponent(String id) {
super(id);
}

public DepositoryFormComponent(String id, IModel model) {
super(id, model);
}

public boolean isVisible() {
return isFormDepositoryType();
}

protected boolean isFormDepositoryType() {
return getCurrentSelections().getSelectedOwnedAccount().getAssetType() == AssetType.DEPOSITORY;
}

protected CurrentSelections getCurrentSelections() {
return (CurrentSelections) getSession().getAttribute(CurrentSelections.ATTRIBUTE_NAME);
}

public void onRender(){};

}

最佳答案

您有多种选择:

  1. 如果您可以控制标记,并且可以将要控制其可见性的所有组件分组到单个标记中,则可以使用 <wicket:enclosure> 标签使组件控制整个标记的可见性。请注意,这不会影响页面设计,并且会达到与添加 WebMarkupContainer 类似的效果。

  2. 您可以向这些组件添加 IBehavior这将计算可见性并调用 setVisible()关于Component 。您还可以调用 Component#setVisibilityAllowed() 如果您不想以后再调用setVisible()更改 Component的可见度。也许不完全是最重要的isVisible ,但我认为如果您不创建自定义组件,则不太可能实现覆盖。

    public class VisiblityControlBehavior extends AbstractBehavior { 

    private boolean isComponentVisible() {
    return isFormDepositoryType();
    }

    protected boolean isFormDepositoryType() {
    return getCurrentSelections().getSelectedOwnedAccount().getAssetType() == AssetType.DEPOSITORY;
    }

    protected CurrentSelections getCurrentSelections() {
    return (CurrentSelections) getSession().getAttribute(CurrentSelections.ATTRIBUTE_NAME);
    }

    @Override
    public void bind(Component component) {
    boolean visible = isComponentVisible();
    component.setVisible(visible);
    component.setVisibilityAllowed(visible);
    }
    }

关于java - FormComponents 有共同的 isVisible 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12490676/

27 4 0