1

I am developing an Wicket application. But my question is not really Wicket related. In that app I have a horizontal menu. This menu is created by few links. On clicking the link you will be navigated to some page. Now based on the page you are currently viewing the css class attribute of the link of the menu will be changed to "selected". This is the description of the problem.

Now I am solving this problem by using a integer value. The value is saved in the session and it is updated when any one link has been clicked. Based on that saved value, which link will be "selected", will be determined at runtime.

I am implementing this in following way:

//On link click I set a number in session

public void onClick() {
    session.setValue(1);// or 2 or 3
}

When the menu is created I switch between the value and modify the css class, as follows:

switch(session.getValue){
    case 1: add css to home;
    case 2: add css to profile;
    // and so on.
}

I was wondering that is this the only right way to do it? Or there some other better techniques or design patterns exist which can help me to achieve this in better way?

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331

4 Answers4

4

Store the menu items in an array (or an ArrayList):

items[0] = home
items[1] = profile

And use the index of the array as menu identifier. When you receive the selected menu itentifier, retrieve the corresponding item with

items[selectedItem]

You could also use a Map if the identifiers are not numbers, or don't go from 0 to N.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

For a start, use an enum or static constants instead of magic numbers (1, 2, 3).

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

The Visitor Pattern is commonly used to avoid this sort of switching. You might not want to implement the full pattern in your case, but it's worth knowning. JB Nizet's answer may be more practical in your situation.

See https://en.wikipedia.org/wiki/Visitor_pattern

These SO questions might give you some ideas, too

Java visitor pattern instead of instanceof switch

Java Enums - Switch statements vs Visitor Pattern on Enums - Performance benefits?

Community
  • 1
  • 1
dfb
  • 13,133
  • 2
  • 31
  • 52
0

I have implemented it using EnumMap and an Enum type as its key. I have defined an Enum:

public enum NavigationStatus {
    HOME,
    PROFILE;
}

In session I set the value of the current navigation as:

private NavigationStatus activeUserNavigationStatus;

public NavigationStatus getActiveUserNavigationStatus() {
    return activeUserNavigationStatus;
}

public void setActiveUserNavigationStatus(NavigationStatus activeUserNavigationStatus) {
    this.activeUserNavigationStatus = activeUserNavigationStatus;
}

Primarily I set it to: setActiveUserNavigationStatus(NavigationStatus.HOME);

Now where the menu is building I created an EnumMap:

EnumMap<NavigationStatus, Component[]> menuMap = new EnumMap<NavigationStatus, Component[]>(NavigationStatus.class);

And added elements to it, as:

menuMap.put(NavigationStatus.HOME, new Component[] { homeContainer, home });

And also on click methods of the links I set the status value:

public void onClick() {
    session.setActiveUserNavigationStatus(NavigationStatus.PROFILE);
}

Last of all I checked the current value from the session and set the css class accordingly:

Component[] menuComponents = menuMap.get(getSession().getActiveUserNavigationStatus());
menuComponents[0].add(new AttributeAppender("class", new Model<Serializable>(" active")));
menuComponents[1].add(new AttributeAppender("class", new Model<Serializable>(" active")));

This is without switch statement and combines the idea of JB Nizet's ArrayList index and Oli Charlesworth's Enum.

Thank you.

Community
  • 1
  • 1
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331