3
\$\begingroup\$

Scenario:

I am working on a Java/ZK Subscriber Management application. There are 2 tabs: Basic Info and STB-VC. The first one for creating a new subscriber and the second one for assigning STB/VC(Set-top box and viewing card).

I am trying to achieve/ensure Someone can only click/use the second tabpanel only after creating a new subscriber and not before.

enter image description here

I have the following code which does that. Is there a better way to achieve the task?

Subscriber.zul

    <window id="main" title="" border="normal"
        apply="com.sss.composer.SubscriberComposer" width="100%"
        height="100%">

        <div align="right">
            <image id="home" src="/img/home.png" width="30px" />
            <image id="logout" src="/img/logout.png" width="30px" />
        </div>

        <tabbox id="tb">
            <tabs id="tabs">
                <tab id="A" label="Basic Info" />
                <tab id="B" label="STB-VC" />
            </tabs>
            <tabpanels>
                <tabpanel id="basic">
                    <!--  Subscriber Creation Form -->
                    <include id="inc1" src="basic.zul"></include>
                </tabpanel>
                <tabpanel id="stbvc">
                    <include id="inc2"></include>
                </tabpanel>
                <tabpanel>
                    <label id="x3" value="x3"></label>
                </tabpanel>
                <tabpanel>
                    <label id="x4" value="x4"></label>

                <tabpanel>
                    <label id="x5" value="x5"></label>
                </tabpanel>
            </tabpanels>
        </tabbox>
    </window>

SubscriberComposer

public void onSelect$tb(ForwardEvent fe) {

        try {
            Object o = fe.getOrigin().getTarget();

            if (o instanceof Tab) {

                Tab t = (Tab) o;

                Tabpanel tp = t.getLinkedPanel();

                tp.invalidate();

                if ((tp.getId().equals("stbvc") && (Sessions.getCurrent()
                        .getAttribute("subscriber") == null))) {

                    Messagebox.show(
                            "Create Subscriber first,then assign STB/VC!",
                            "Warning", Messagebox.OK, Messagebox.INFORMATION);

                    tb.setSelectedPanel(basic);

                } else {

                    inc2.setSrc("stbvc.zul");

                    System.out.println("Subscriber ID set in session...");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Your code could be improved by using a guard clause which reduces horizontal spacing.


Names of variables shouldn't be shortened. Tabpanel tp could be easily refactored to a more meaningful Tabpanel tabPanel. Increasing readability helps Sam the Maintainer to the maintaining job.


You should catch specific exceptions, not just Exception. Also you should enclose only the code which can throw an exception.


Object target = fe.getOrigin().getTarget();

if (!(target instanceof Tab)) { return; }

Tab tab = (Tab) target ;

Tabpanel tabPanel = tab.getLinkedPanel();

tabPanel.invalidate();

if ((tabPanel.getId().equals("stbvc") && (Sessions.getCurrent()
        .getAttribute("subscriber") == null))) {

    Messagebox.show(
            "Create Subscriber first,then assign STB/VC!",
            "Warning", Messagebox.OK, Messagebox.INFORMATION);

    tabPanel.setSelectedPanel(basic);

} else {

    inc2.setSrc("stbvc.zul");

    System.out.println("Subscriber ID set in session...");
}
\$\endgroup\$
2
\$\begingroup\$

This is certainly a solution what you could use.

Another solution might be :

<tabpanel id="stbvc" disabled="true">

We start with that tab disabled.
As the subscriber is created you can set the disabled to false and set the source of the include, so the tab will unlock.

Another option, what I use in mine application is using the "visible" attribute.
As long that entity isn't saved (have an Id), I don't show the tab.
In mine application I work MVVM so it's a little easier there to notify the GUI when an object is changed.

Note:
For the 2 solutions : the tab is always rendered in the dom

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.