HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Managing ZK Tabpanels

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
managingtabpanelsstackoverflow

Problem

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.

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

Subscriber.zul


        
            
            
        

        
            
                
                
            
            
                
                    
                    
                
                
                    
                
                
                    
                
                
                    

                
                    
                
            
        
    


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();
        }
    }

Solution

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...");
}

Code Snippets

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...");
}

Context

StackExchange Code Review Q#61563, answer score: 3

Revisions (0)

No revisions yet.