protected void restoreHeader() {
   layout.getNorth().setVisible(true);
   max.setImage(ThemeManager.getThemeResource(IMAGES_UPARROW_PNG));
   showHeader.setVisible(false);
   pnlHead.detach();
   headerContainer.appendChild(pnlHead);
   Clients.resize(pnlHead);
   updateHeaderCollapsedPreference(false);
 }
 @Override
 public void onMenuSelected(int menuId) {
   super.onMenuSelected(menuId);
   if (showHeader.isVisible()) {
     // ensure header popup is close
     String script =
         "var w=zk.Widget.$('#" + layout.getUuid() + "'); " + "zWatch.fire('onFloatUp', w);";
     Clients.response(new AuScript(script));
   }
 }
 protected void collapseHeader() {
   layout.getNorth().setVisible(false);
   max.setImage(ThemeManager.getThemeResource(IMAGES_DOWNARROW_PNG));
   showHeader.setVisible(true);
   pnlHead.detach();
   if (headerPopup == null) {
     headerPopup = new Window();
     headerPopup.setSclass("desktop-header-popup");
     headerPopup.setVflex("true");
     headerPopup.setVisible(false);
     headerPopup.addEventListener(
         Events.ON_OPEN,
         new EventListener<OpenEvent>() {
           @Override
           public void onEvent(OpenEvent event) throws Exception {
             if (!event.isOpen()) {
               if (showHeader.isPressed()) showHeader.setPressed(false);
             }
           }
         });
   }
   headerPopup.appendChild(pnlHead);
   updateHeaderCollapsedPreference(true);
 }
  public void onEvent(Event event) {
    Component comp = event.getTarget();
    String eventName = event.getName();

    if (eventName.equals(Events.ON_CLICK)) {
      if (comp == max) {
        if (layout.getNorth().isVisible()) {
          collapseHeader();
        } else {
          restoreHeader();
        }
      } else if (comp == showHeader) {
        showHeader.setPressed(true);
        if (pnlHead.getParent() != headerPopup) headerPopup.appendChild(pnlHead);
        LayoutUtils.openPopupWindow(showHeader, headerPopup, "after_start");
      } else if (comp == contextHelp) {
        layout.getEast().setVisible(true);
        layout.getEast().setOpen(true);
        contextHelp.setVisible(false);
        updateHelpCollapsedPreference(false);
      } else if (comp instanceof ToolBarButton) {
        ToolBarButton btn = (ToolBarButton) comp;

        if (btn.getAttribute("AD_Menu_ID") != null) {
          int menuId = (Integer) btn.getAttribute("AD_Menu_ID");
          if (menuId > 0) onMenuSelected(menuId);
        }
      }
    } else if (eventName.equals(ON_ACTIVITIES_CHANGED_EVENT)) {
      @SuppressWarnings("unchecked")
      Map<String, Object> map = (Map<String, Object>) event.getData();
      Integer notice = (Integer) map.get("notice");
      Integer request = (Integer) map.get("request");
      Integer workflow = (Integer) map.get("workflow");
      Integer unprocessed = (Integer) map.get("unprocessed");
      boolean change = false;
      if (notice != null && notice.intValue() != noOfNotice) {
        noOfNotice = notice.intValue();
        change = true;
      }
      if (request != null && request.intValue() != noOfRequest) {
        noOfRequest = request.intValue();
        change = true;
      }
      if (workflow != null && workflow.intValue() != noOfWorkflow) {
        noOfWorkflow = workflow.intValue();
        change = true;
      }
      if (unprocessed != null && unprocessed.intValue() != noOfUnprocessed) {
        noOfUnprocessed = unprocessed.intValue();
        change = true;
      }
      if (change) updateUI();
    } else if (event instanceof ZoomEvent) {
      Clients.clearBusy();
      ZoomEvent ze = (ZoomEvent) event;
      if (ze.getData() != null && ze.getData() instanceof MQuery) {
        AEnv.zoom((MQuery) ze.getData());
      }
    } else if (event instanceof DrillEvent) {
      Clients.clearBusy();
      DrillEvent de = (DrillEvent) event;
      if (de.getData() != null && de.getData() instanceof MQuery) {
        MQuery query = (MQuery) de.getData();
        executeDrill(query);
      }
    }
  }
  @SuppressWarnings("serial")
  protected Component doCreatePart(Component parent) {
    PageDefinition pagedef =
        Executions.getCurrent()
            .getPageDefinition(ThemeManager.getThemeResource("zul/desktop/desktop.zul"));
    Component page = Executions.createComponents(pagedef, parent, null);
    layout = (Borderlayout) page.getFellow("layout");
    headerContainer = page.getFellow("northBody");
    pnlHead = (HeaderPanel) headerContainer.getFellow("header");

    layout.addEventListener("onZoom", this);
    layout.addEventListener(DrillEvent.ON_DRILL_DOWN, this);

    West w = layout.getWest();
    w.addEventListener(
        Events.ON_OPEN,
        new EventListener<Event>() {
          @Override
          public void onEvent(Event event) throws Exception {
            OpenEvent oe = (OpenEvent) event;
            updateMenuCollapsedPreference(!oe.isOpen());
          }
        });
    w.addEventListener(
        Events.ON_SWIPE,
        new EventListener<SwipeEvent>() {

          @Override
          public void onEvent(SwipeEvent event) throws Exception {
            if ("left".equals(event.getSwipeDirection())) {
              West w = (West) event.getTarget();
              if (w.isOpen()) {
                w.setOpen(false);
                updateMenuCollapsedPreference(true);
              }
            }
          }
        });

    w.addEventListener(
        Events.ON_SIZE,
        new EventListener<Event>() {

          @Override
          public void onEvent(Event event) throws Exception {
            West west = (West) event.getTarget();
            updateSideControllerWidthPreference(west.getWidth());
          }
        });

    UserPreference pref = SessionManager.getSessionApplication().getUserPreference();
    boolean menuCollapsed = pref.isPropertyBool(UserPreference.P_MENU_COLLAPSED);
    w.setOpen(!menuCollapsed);

    East e = layout.getEast();
    e.addEventListener(
        Events.ON_OPEN,
        new EventListener<Event>() {
          @Override
          public void onEvent(Event event) throws Exception {
            OpenEvent oe = (OpenEvent) event;
            updateHelpCollapsedPreference(!oe.isOpen());
            HtmlBasedComponent comp = windowContainer.getComponent();
            if (comp != null) {
              contextHelp.setVisible(!oe.isOpen());
              if (!oe.isOpen()) layout.getEast().setVisible(false);
            }
          }
        });

    e.addEventListener(
        Events.ON_SWIPE,
        new EventListener<SwipeEvent>() {

          @Override
          public void onEvent(SwipeEvent event) throws Exception {
            if ("right".equals(event.getSwipeDirection())) {
              East e = (East) event.getTarget();
              if (e.isOpen()) {
                e.setOpen(false);
                updateHelpCollapsedPreference(true);
              }
            }
          }
        });

    e.addEventListener(
        Events.ON_SIZE,
        new EventListener<Event>() {

          @Override
          public void onEvent(Event event) throws Exception {
            East east = (East) event.getTarget();
            updateHelpWidthPreference(east.getWidth());
          }
        });

    String westWidth = getWestWidthPreference();
    String eastWidth = getEastWidthPreference();

    // Set preference width
    if (westWidth != null || eastWidth != null) {

      // If both panels have prefered size check that the sum is not bigger than the browser
      if (westWidth != null && eastWidth != null) {
        ClientInfo browserInfo = getClientInfo();
        int browserWidth = browserInfo.desktopWidth;
        int wWidth = Integer.valueOf(westWidth.replace("px", ""));
        int eWidth = Integer.valueOf(eastWidth.replace("px", ""));

        if (eWidth + wWidth <= browserWidth) {
          w.setWidth(westWidth);
          e.setWidth(eastWidth);
        }

      } else if (westWidth != null) w.setWidth(westWidth);
      else if (eastWidth != null) e.setWidth(eastWidth);
    }

    boolean helpCollapsed = pref.isPropertyBool(UserPreference.P_HELP_COLLAPSED);
    e.setVisible(!helpCollapsed);

    helpController.render(e, this);

    Center windowArea = layout.getCenter();

    windowContainer.createPart(windowArea);

    homeTab = new Tabpanel();
    windowContainer.addWindow(
        homeTab, Util.cleanAmp(Msg.getMsg(Env.getCtx(), "Home")), false, null);
    homeTab.getLinkedTab().setSclass("desktop-hometab");
    homeTab.setSclass("desktop-home-tabpanel");
    BusyDialog busyDialog = new BusyDialog();
    busyDialog.setShadow(false);
    homeTab.appendChild(busyDialog);

    // register as 0
    registerWindow(homeTab);

    BroadcastMessageWindow messageWindow = new BroadcastMessageWindow(pnlHead);
    BroadcastMsgUtil.showPendingMessage(Env.getAD_User_ID(Env.getCtx()), messageWindow);

    if (!layout.getDesktop().isServerPushEnabled()) {
      layout.getDesktop().enableServerPush(true);
    }

    Runnable runnable =
        new Runnable() {
          public void run() {
            try {
              Thread.sleep(100);
            } catch (InterruptedException e) {
            }

            IServerPushCallback callback =
                new IServerPushCallback() {
                  public void updateUI() {
                    Properties ctx =
                        (Properties)
                            layout
                                .getDesktop()
                                .getSession()
                                .getAttribute(SessionContextListener.SESSION_CTX);
                    try {
                      ServerContext.setCurrentInstance(ctx);
                      renderHomeTab();
                    } finally {
                      ServerContext.dispose();
                    }
                  }
                };
            ServerPushTemplate template = new ServerPushTemplate(layout.getDesktop());
            template.executeAsync(callback);
          }
        };

    Adempiere.getThreadPoolExecutor().submit(new DesktopRunnable(runnable, layout.getDesktop()));

    ToolBar toolbar = new ToolBar();
    windowContainer.getComponent().appendChild(toolbar);

    showHeader =
        new ToolBarButton() {
          @Override
          public void onPageDetached(Page page) {
            super.onPageDetached(page);
            if (DefaultDesktop.this.headerPopup != null) {
              DefaultDesktop.this.headerPopup.setPage(null);
            }
          }
        };
    toolbar.appendChild(showHeader);
    showHeader.setImage(ThemeManager.getThemeResource(IMAGES_THREELINE_MENU_PNG));
    showHeader.addEventListener(Events.ON_CLICK, this);
    showHeader.setSclass("window-container-toolbar-btn");
    showHeader.setStyle("cursor: pointer; border: 1px solid transparent; padding: 2px;");
    showHeader.setVisible(false);

    max = new ToolBarButton();
    toolbar.appendChild(max);
    max.setImage(ThemeManager.getThemeResource(IMAGES_UPARROW_PNG));
    max.addEventListener(Events.ON_CLICK, this);
    max.setSclass("window-container-toolbar-btn");
    max.setStyle("cursor: pointer; border: 1px solid transparent; padding: 2px;");

    contextHelp = new ToolBarButton();
    toolbar.appendChild(contextHelp);
    contextHelp.setImage(ThemeManager.getThemeResource(IMAGES_CONTEXT_HELP_PNG));
    contextHelp.addEventListener(Events.ON_CLICK, this);
    contextHelp.setSclass("window-container-toolbar-btn context-help-btn");
    contextHelp.setStyle("cursor: pointer; border: 1px solid transparent; padding: 2px;");
    contextHelp.setTooltiptext(Util.cleanAmp(Msg.getElement(Env.getCtx(), "AD_CtxHelp_ID")));
    contextHelp.setVisible(!e.isVisible());

    boolean headerCollapsed = pref.isPropertyBool(UserPreference.P_HEADER_COLLAPSED);
    if (headerCollapsed) {
      collapseHeader();
    }

    return layout;
  }