So.. here's what im trying to do.
I have a class named MenuWidget which has "options" (array of Label's) and a container (VLayout).
I want to be able to change the current active option (menu option) based on where I am on the page. I already have this working, but my current approach requires instantiating a new MenuWidget each time I change page, because, after many attempts, I havent been able to re-use the same object of MenuWidget.
**This is my LayoutHelper (I use it to interact with the layout)**
***Then, this is MenuWidget***
The first time I add the widget to the RootPanel, it renders fine, the second time, it shows nothing =/.
Any help would be greatly appreciated, and.. if someone knows a better way to do something I'm doing, suggestions are also welcomed (I havent been able to find good documentation besides the javadoc api =/)
I have a class named MenuWidget which has "options" (array of Label's) and a container (VLayout).
I want to be able to change the current active option (menu option) based on where I am on the page. I already have this working, but my current approach requires instantiating a new MenuWidget each time I change page, because, after many attempts, I havent been able to re-use the same object of MenuWidget.
**This is my LayoutHelper (I use it to interact with the layout)**
Code:
public class LayoutHelper {
private static MenuWidget menu;
public static void render(String currentOption) {
/**
* @todo: mejorar esto, se tiene que instanciar un menu cada vez que se cambia de página, debe haber alguna manera mejor
*/
if(menu == null) {
menu = new MenuWidget();
menu.setCurrentOption(currentOption);
menu.build();
} else {
menu.setCurrentOption(currentOption); //this part
menu.rebuild(); //doesnt work
}
RootPanel.get("menu").clear();
RootPanel.get("menu").add(menu.getCanvas());
}
}
Code:
public class MenuWidget implements Widget {
private Label[] options;
private VLayout container;
private String currentOption;
public MenuWidget() {
this.options = new Label[10];
this.container = new VLayout();
}
@Override
public Canvas getCanvas() {
return container;
}
public void setupContainer() {
container.setWidth("250");
container.setBackgroundColor("#3f3f3f");
for (int i = 0; i < this.options.length; ++i) {
container.addMember(this.options[i]);
}
}
public void setupOptions() {
for (int i = 0; i < this.options.length; ++i) {
options[i] = new Label();
options[i].setStyleName("menu_nav");
options[i].setHeight("25");
}
options[0].setContents("Inicio");
options[0].addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
EventBus.getEventBus().fireEvent(new HomeShowViewEvent());
}
});
options[1].setContents("Usuarios");
options[2].setContents("Clientes");
options[3].setContents("Proveedores");
options[4].setContents("Orden pedido");
options[4].addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
EventBus.getEventBus().fireEvent(new OpedidoShowViewEvent());
}
});
options[5].setContents("Orden compra");
options[6].setContents("Orden reparto");
options[7].setContents("Orden recepción");
options[8].setContents("Orden pago");
updateCurrentOption();
}
public void updateCurrentOption() {
for (int i = 0; i < this.options.length; ++i) {
if (options[i].getContents().equals(this.currentOption)) {
options[i].setStyleName("menu_nav_active");
break;
}
}
}
@Override
public void build() {
setupOptions();
setupContainer();
}
public void rebuild() {
updateCurrentOption();
container.clear();
setupContainer();
//container.redraw();
}
public void setCurrentOption(String option) {
currentOption = option;
}
}
Any help would be greatly appreciated, and.. if someone knows a better way to do something I'm doing, suggestions are also welcomed (I havent been able to find good documentation besides the javadoc api =/)
Comment