private Layout buildPrimaryMenu(String currentModule) {

    HLayout moduleLayout = new HLayout();
    moduleLayout.setWidth100();
    moduleLayout.setHeight(38);
    moduleLayout.setBackgroundImage(GWT.getModuleBaseURL() + "admin/images/nav_bg.png");
    moduleLayout.setBackgroundRepeat(BkgndRepeat.REPEAT_X);

    moduleLayout.addMember(new LayoutSpacer());

    HLayout primaryMenuOptionsHolder = new HLayout();
    primaryMenuOptionsHolder.setLayoutAlign(VerticalAlignment.BOTTOM);
    // primaryMenuOptionsHolder.setLayoutAlign(Alignment.LEFT);
    primaryMenuOptionsHolder.setMembersMargin(5);
    primaryMenuOptionsHolder.setWidth100();
    primaryMenuOptionsHolder.setHeight(30);
    primaryMenuOptionsHolder.setAlign(Alignment.LEFT);

    LayoutSpacer sp = new LayoutSpacer();
    sp.setWidth(20);
    primaryMenuOptionsHolder.addMember(sp);
    addAuthorizedModulesToMenu(primaryMenuOptionsHolder, currentModule);
    moduleLayout.addMember(primaryMenuOptionsHolder);
    return moduleLayout;
  }
  private ArrayList<LocatableHLayout> createTagLayouts() {
    ArrayList<LocatableHLayout> tagLayouts = new ArrayList<LocatableHLayout>(tags.size());

    for (final Tag tag : tags) {
      LocatableHLayout tagLayout = new LocatableHLayout(extendLocatorId(tag.getName()));
      tagLayout.setHeight(18);
      tagLayout.setHeight(16);

      HTMLFlow tagString =
          new HTMLFlow(
              "<nobr><a href=\""
                  + LinkManager.getTagLink(tag.toString())
                  + "\">"
                  + tag.toString()
                  + "</a></nobr>");
      tagString.setAutoWidth();
      tagLayout.addMember(tagString);

      if (!readOnly) {
        final LayoutSpacer spacer = new LayoutSpacer();
        spacer.setHeight(16);
        spacer.setWidth(16);

        final Img remove =
            new LocatableImg(
                tagLayout.extendLocatorId("Remove"), "[skin]/images/actions/remove.png", 16, 16);
        remove.setTooltip(MSG.view_tags_tooltip_1());
        remove.addClickHandler(
            new ClickHandler() {
              public void onClick(ClickEvent clickEvent) {
                tags.remove(tag);
                save();
              }
            });

        tagLayout.addMember(remove);
        tagLayout.addMember(spacer);
        remove.hide();

        tagLayout.addMouseOverHandler(
            new MouseOverHandler() {
              public void onMouseOver(MouseOverEvent mouseOverEvent) {
                remove.show();
                spacer.hide();
              }
            });
        tagLayout.addMouseOutHandler(
            new MouseOutHandler() {
              public void onMouseOut(MouseOutEvent mouseOutEvent) {
                spacer.show();
                remove.hide();
              }
            });
      }

      tagLayouts.add(tagLayout);
    }

    return tagLayouts;
  }
  /**
   * Builds the toolbar
   *
   * @param mapWidget The mapWidget containing the layerTree
   * @return {@link com.smartgwt.client.widgets.toolbar.ToolStrip} which was built
   */
  private ToolStrip buildToolstrip(MapWidget mapWidget) {
    toolStrip = new ToolStrip();
    toolStrip.setWidth100();
    toolStrip.setPadding(3);

    ClientLayerTreeInfo layerTreeInfo = mapModel.getMapInfo().getLayerTree();
    if (layerTreeInfo != null) {
      for (ClientToolInfo tool : layerTreeInfo.getTools()) {
        String id = tool.getToolId();
        Canvas button = null;
        ToolbarBaseAction action = LayerTreeRegistry.getToolbarAction(id, mapWidget);
        if (action instanceof ToolbarWidget) {
          toolStrip.addMember(((ToolbarWidget) action).getWidget());
        } else if (action instanceof ToolbarCanvas) {
          button = ((ToolbarCanvas) action).getCanvas();
        } else if (action instanceof LayerTreeAction) {
          button = new LayerTreeButton(this, (LayerTreeAction) action);
        } else if (action instanceof LayerTreeModalAction) {
          button = new LayerTreeModalButton(this, (LayerTreeModalAction) action);
        } else {
          String msg = "LayerTree tool with id " + id + " unknown.";
          Log.logError(msg); // console log
          SC.warn(msg); // in your face
        }
        if (button != null) {
          toolStrip.addMember(button);
          LayoutSpacer spacer = new LayoutSpacer();
          spacer.setWidth(WidgetLayout.layerTreePadding);
          toolStrip.addMember(spacer);
        }
      }
    }
    final Canvas[] toolStripMembers = toolStrip.getMembers();
    // delaying this fixes an image 'undefined' error
    Timer t =
        new Timer() {
          public void run() {
            updateButtonIconsAndStates(toolStripMembers);
          }
        };
    t.schedule(10);
    return toolStrip;
  }
  private Layout buildHeader() {
    HLayout header = new HLayout();
    header.setWidth100();
    header.setLayoutMargin(10);
    header.setBackgroundImage(GWT.getModuleBaseURL() + "admin/images/header_bg.png");

    LayoutSpacer sp = new LayoutSpacer();
    sp.setWidth(20);
    header.addMember(sp);
    header.addMember(buildLogo());
    header.addMember(new LayoutSpacer());

    VLayout userAndLocale = new VLayout();
    userAndLocale.setHeight100();
    userAndLocale.addMember(buildUserInfo());
    // userAndLocale.addMember(buildLocaleSelection());

    header.addMember(userAndLocale);
    return header;
  }
  private Layout buildSecondaryMenu(String selectedPage, String moduleKey) {
    secondaryMenu.removeMembers(secondaryMenu.getMembers());

    LayoutSpacer sp2 = new LayoutSpacer();
    sp2.setWidth(10);
    secondaryMenu.addMember(sp2);
    secondaryMenu.setHeight(40);
    // secondaryMenu.setBackgroundColor("#78a22F");
    secondaryMenu.setBackgroundImage(GWT.getModuleBaseURL() + "admin/images/nav_sec_bg.png");
    secondaryMenu.addMember(sp2);

    LinkedHashMap<String, String[]> pages = modules.get(moduleKey).getPages();

    Collection<String> allowedPages = pages.keySet();

    for (Iterator<String> iterator = allowedPages.iterator(); iterator.hasNext(); ) {
      String testPage = (String) iterator.next();
      if (!SecurityManager.getInstance().isUserAuthorizedToViewSection(pages.get(testPage)[0])) {
        iterator.remove();
        if (selectedPage != null && selectedPage.equals(testPage)) {
          selectedPage = null;
        }
      }
    }

    if (selectedPage == null && allowedPages.size() > 0) {
      selectedPage = (String) allowedPages.iterator().next();
    }

    for (String page : allowedPages) {
      boolean selected = (page.equals(selectedPage));
      secondaryMenu.addMember(buildSecondaryMenuOption(page, selected));
      if (selectedPage == null) {
        selectedPage = page;
      }
      selected = false;
    }

    return secondaryMenu;
  }
  private Canvas buildUserInfo() {
    HLayout userFields = new HLayout();
    userFields.setAlign(Alignment.RIGHT);
    userFields.addMember(buildUserImage());
    LayoutSpacer sp1 = new LayoutSpacer();
    sp1.setWidth(8);
    userFields.addMember(sp1);

    // Label userLabel = new Label(SecurityManager.USER.getUserName());
    //  userLabel.setBaseStyle("userText");
    //  userLabel.setWidth(1);
    //   userLabel.setOverflow(Overflow.VISIBLE);
    //   userFields.addMember(userLabel);

    //  userFields.addMember(buildLogoutImage());

    Menu menu = new Menu();
    menu.setShowShadow(true);
    menu.setShadowDepth(10);
    menu.setShowIcons(false);

    MenuItem logout = new MenuItem("Logout");
    MenuItem edit = new MenuItem("Edit ...");
    MenuItem changePassword = new MenuItem("Change Password ...");

    menu.setItems(edit, changePassword, logout);

    changePassword.addClickHandler(
        new com.smartgwt.client.widgets.menu.events.ClickHandler() {
          @Override
          public void onClick(MenuItemClickEvent event) {
            redirect(GWT.getHostPageBaseURL() + "blcadmin/changePassword");
          }
        });
    //        changePassword.addClickHandler(new
    // com.smartgwt.client.widgets.menu.events.ClickHandler() {
    //                    @Override
    //                    public void onClick(MenuItemClickEvent event) {
    //                        final DynamicEntityDataSource userDS = new
    // DynamicEntityDataSource(CeilingEntities.ADMIN_USER);
    //                        userDS.buildFields(null, false, new AsyncCallbackAdapter() {
    //                            public void onSetupSuccess(DataSource ds) {
    //                                //strip out password validation
    //                                userDS.getField("password").setValidators();
    //
    //                                AdminUser currentUser = SecurityManager.USER;
    //                                Record userRecord = new Record();
    //                                userRecord.setAttribute("id", currentUser.getId());
    //                                userRecord.setAttribute("login", currentUser.getUserName());
    //                                userRecord.setAttribute("_type", new
    // String[]{EntityImplementations.ADMIN_USER});
    //
    //                                EntityEditDialog ed = new EntityEditDialog();
    //
    //                                ed.editRecord("Change Password", userDS, userRecord, new
    // ItemEditedHandler() {
    //                                    public void onItemEdited(ItemEdited event) {
    //                                        String currentPage =
    // BLCLaunch.getSelectedPage(History.getToken());
    //                                        if ("User Management".equals(currentPage)) {
    //                                            buildHistoryNewItem(currentPage,
    // BLCLaunch.getSelectedModule(History.getToken()), event.getRecord().getAttribute("id"));
    //                                        }
    //                                    }
    //                                }, new String[]{"password"}, new String[]{}, false);
    //                            }
    //                        });
    //
    //                    }
    //                });

    edit.addClickHandler(
        new com.smartgwt.client.widgets.menu.events.ClickHandler() {
          @Override
          public void onClick(MenuItemClickEvent event) {
            final DynamicEntityDataSource userDS =
                new DynamicEntityDataSource(CeilingEntities.ADMIN_USER);
            userDS.buildFields(
                null,
                false,
                new AsyncCallbackAdapter() {
                  public void onSetupSuccess(DataSource ds) {
                    AdminUser currentUser = SecurityManager.USER;
                    Record userRecord = new Record();
                    userRecord.setAttribute("id", currentUser.getId());
                    userRecord.setAttribute("name", currentUser.getName());
                    userRecord.setAttribute("email", currentUser.getEmail());
                    userRecord.setAttribute("phoneNumber", currentUser.getPhoneNumber());
                    userRecord.setAttribute("login", currentUser.getUserName());
                    userRecord.setAttribute(
                        "_type", new String[] {EntityImplementations.ADMIN_USER});

                    EntityEditDialog ed = new EntityEditDialog();

                    ed.editRecord(
                        "Edit User Information",
                        userDS,
                        userRecord,
                        new ItemEditedHandler() {
                          public void onItemEdited(ItemEdited event) {
                            SecurityManager.USER.setPhoneNumber(
                                event.getRecord().getAttribute("phoneNumber"));
                            SecurityManager.USER.setName(event.getRecord().getAttribute("name"));
                            SecurityManager.USER.setEmail(event.getRecord().getAttribute("email"));
                            String currentPage = BLCLaunch.getSelectedPage(History.getToken());
                            // If we are on the user module, reload the page with the specifically
                            // edited item.
                            if ("User Management".equals(currentPage)) {
                              buildHistoryNewItem(
                                  currentPage,
                                  BLCLaunch.getSelectedModule(History.getToken()),
                                  event.getRecord().getAttribute("id"));
                            }
                          }
                        },
                        null,
                        new String[] {"login", "activeStatusFlag", "password"},
                        false);
                  }
                });
          }
        });

    logout.addClickHandler(
        new com.smartgwt.client.widgets.menu.events.ClickHandler() {
          @Override
          public void onClick(MenuItemClickEvent event) {
            UrlBuilder builder = Window.Location.createUrlBuilder();
            builder.setPath(BLCMain.webAppContext + "/adminLogout.htm");
            builder.setHash(null);
            builder.setParameter("time", String.valueOf(System.currentTimeMillis()));
            Window.open(builder.buildString(), "_self", null);
          }
        });

    IMenuButton menuButton = new IMenuButton(SecurityManager.USER.getUserName(), menu);
    menuButton.setPadding(5);
    menuButton.setChildrenSnapResizeToGrid(true);
    menuButton.setOverflow(Overflow.VISIBLE);
    userFields.addMember(menuButton);

    LayoutSpacer sp2 = new LayoutSpacer();
    sp2.setWidth(200);
    userFields.addMember(sp2);

    return userFields;
  }