/*
   * (non-Javadoc)
   * @see
   * cz.fi.muni.xkremser.editor.client.presenter.DigitalObjectMenuPresenter.
   * MyView#setDS(com.gwtplatform.dispatch.client.DispatchAsync)
   */
  @Override
  public void setDS(DispatchAsync dispatcher, EventBus bus) {
    this.sideNavGrid.setDataSource(new RecentlyTreeGwtRPCDS(dispatcher, lang, bus));
    ListGridField nameField = new ListGridField(Constants.ATTR_NAME, lang.name());
    nameField.setRequired(true);
    nameField.setWidth("40%");
    nameField.setCellFormatter(
        new CellFormatter() {

          @Override
          public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
            String owner = record.getAttribute(Constants.ATTR_LOCK_OWNER);
            if (owner != null) {
              return ("".equals(owner)
                      ? RecentlyTreeGwtRPCDS.FIRST_PART_OF_COLOR_LOCK_BY_USER
                      : RecentlyTreeGwtRPCDS.FIRST_PART_OF_COLOR_LOCK)
                  + record.getAttribute(Constants.ATTR_NAME)
                  + RecentlyTreeGwtRPCDS.SECOND_PART_OF_COLOR_LOCK;
            } else {
              return record.getAttribute(Constants.ATTR_NAME);
            }
          }
        });
    ListGridField uuidField = new ListGridField(Constants.ATTR_UUID, "PID");
    uuidField.setRequired(true);
    uuidField.setWidth("*");
    uuidField.setCellFormatter(
        new CellFormatter() {

          @Override
          public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
            String owner = record.getAttribute(Constants.ATTR_LOCK_OWNER);
            if (owner != null) {
              return ("".equals(owner)
                      ? RecentlyTreeGwtRPCDS.FIRST_PART_OF_COLOR_LOCK_BY_USER
                      : RecentlyTreeGwtRPCDS.FIRST_PART_OF_COLOR_LOCK)
                  + record.getAttribute(Constants.ATTR_UUID)
                  + RecentlyTreeGwtRPCDS.SECOND_PART_OF_COLOR_LOCK;
            } else {
              return record.getAttribute(Constants.ATTR_UUID);
            }
          }
        });
    sideNavGrid.setFields(nameField, uuidField);
  }
  /*
   * (non-Javadoc)
   * @see
   * cz.fi.muni.xkremser.editor.client.presenter.DigitalObjectMenuPresenter.
   * MyView#showInputQueue(com.gwtplatform.dispatch.client.DispatchAsync)
   */
  @Override
  public void showInputQueue(
      InputQueueTree tree, DispatchAsync dispatcher, final PlaceManager placeManager) {
    SectionStackSection section1 = new SectionStackSection();
    section1.setTitle(lang.inputQueue());
    if (tree == null) {
      inputTree = new InputQueueTree(dispatcher, lang, eventBus);
      inputTree
          .getCreateMenuItem()
          .addClickHandler(
              new com.smartgwt.client.widgets.menu.events.ClickHandler() {

                @Override
                public void onClick(final MenuItemClickEvent event) {
                  String msg = event.getMenu().getEmptyMessage();
                  String model = msg.substring(0, msg.indexOf("/"));
                  String path = msg.substring(msg.indexOf("/") + 1);
                  String id = path;
                  if (path.contains("/")) {
                    id = path.substring(0, path.indexOf("/"));
                  }

                  placeManager.revealRelativePlace(
                      new PlaceRequest(NameTokens.FIND_METADATA)
                          .with(Constants.ATTR_MODEL, model)
                          .with(Constants.URL_PARAM_SYSNO, id)
                          .with(Constants.URL_PARAM_PATH, path));
                }
              });
    } else {
      String isInputSection = sectionStack.getSection(0).getAttribute(SECTION_INPUT_ID);
      if (isInputSection != null && "yes".equals(isInputSection)) {
        sectionStack.removeSection(0);
      }
      inputTree = tree;
    }
    section1.setItems(inputTree);
    refreshButton = new ImgButton();
    refreshButton.setSrc("[SKIN]headerIcons/refresh.png");
    refreshButton.setSize(16);
    refreshButton.setShowRollOver(true);
    refreshButton.setCanHover(true);
    refreshButton.setShowDownIcon(false);
    refreshButton.setShowDown(false);
    refreshButton.setHoverStyle("interactImageHover");
    refreshButton.setHoverOpacity(75);
    refreshButton.addHoverHandler(
        new HoverHandler() {

          @Override
          public void onHover(HoverEvent event) {
            refreshButton.setPrompt(lang.inputQueueRescan());
          }
        });

    section1.setControls(refreshButton);
    section1.setResizeable(true);
    section1.setExpanded(true);
    sectionStack.addSection(section1, 0);
    section1.setAttribute(SECTION_INPUT_ID, "yes");
  }
  /** Instantiates a new digital object menu view. */
  @Inject
  public DigitalObjectMenuView(final LangConstants lang, final EventBus eventBus) {
    this.lang = lang;
    this.eventBus = eventBus;

    layout = new VLayout();

    layout.setHeight100();
    layout.setWidth100();
    layout.setOverflow(Overflow.AUTO);

    relatedGrid = new ListGrid();
    relatedGrid.setWidth100();
    relatedGrid.setHeight100();
    relatedGrid.setShowSortArrow(SortArrow.CORNER);
    relatedGrid.setShowAllRecords(true);
    relatedGrid.setAutoFetchData(false);
    relatedGrid.setCanHover(true);
    relatedGrid.setCanSort(false);
    ListGridField field1 = new ListGridField("relation", lang.relation());
    field1.setWidth("40%");
    ListGridField field2 = new ListGridField("uuid", "PID");
    field2.setWidth("*");
    relatedGrid.setFields(field1, field2);
    sectionRelated = new SectionStackSection();
    sectionRelated.setID(SECTION_RELATED_ID);
    sectionRelated.setTitle(lang.referencedBy());
    sectionRelated.setResizeable(true);
    sectionRelated.setItems(relatedGrid);
    sectionRelated.setExpanded(false);

    sideNavGrid =
        new ListGrid() {

          @Override
          protected Canvas getRollOverCanvas(Integer rowNum, Integer colNum) {
            final ListGridRecord rollOverRecord = this.getRecord(rowNum);
            if (rollOverCanvas == null) {
              rollOverCanvas = new HLayout();
              rollOverCanvas.setSnapTo("TR");
              rollOverCanvas.setWidth(50);
              rollOverCanvas.setHeight(22);
            }

            if (rollOverCanvas.getChildren().length > 0) {
              rollOverCanvas.removeChild(rollOverCanvas.getChildren()[0]);
            }
            final String lockOwner = rollOverRecord.getAttributeAsString(Constants.ATTR_LOCK_OWNER);
            if (lockOwner != null) {
              ImgButton lockImg = new ImgButton();
              lockImg.setShowDown(false);
              lockImg.setShowRollOver(false);
              lockImg.setLayoutAlign(Alignment.CENTER);

              if ("".equals(lockOwner)) {
                lockImg.setSrc("icons/16/lock_lock_all.png");
              } else if (lockOwner.length() > 0) {
                lockImg.setSrc("icons/16/lock_lock_all_red.png");
              }

              lockImg.setPrompt(lang.lockInfoButton());
              lockImg.setHeight(16);
              lockImg.setWidth(16);
              lockImg.addClickHandler(
                  new com.smartgwt.client.widgets.events.ClickHandler() {

                    @Override
                    public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
                      EditorSC.objectIsLock(
                          lang,
                          new LockInfo(
                              lockOwner,
                              rollOverRecord.getAttributeAsString(Constants.ATTR_LOCK_DESCRIPTION),
                              rollOverRecord.getAttributeAsStringArray(
                                  Constants.ATTR_TIME_TO_EXP_LOCK)));
                    }
                  });
              rollOverCanvas.addChild(lockImg);
            }
            return rollOverCanvas;
          }
        };
    sideNavGrid.setShowSelectionCanvas(false);

    sideNavGrid.setWidth100();
    sideNavGrid.setHeight100();
    sideNavGrid.setShowSortArrow(SortArrow.CORNER);
    sideNavGrid.setShowAllRecords(true);
    sideNavGrid.setAutoFetchData(true);
    sideNavGrid.setCanHover(true);
    sideNavGrid.setHoverOpacity(75);
    sideNavGrid.setHoverStyle("interactImageHover");
    sideNavGrid.setShowRollOverCanvas(true);

    final DynamicForm form = new DynamicForm();
    form.setHeight(1);
    form.setWidth(60);
    form.setNumCols(1);

    selectItem.setWidth(60);
    selectItem.setShowTitle(false);
    selectItem.setValueMap(lang.me(), lang.all());
    selectItem.setDefaultValue(lang.me());
    selectItem.setHoverOpacity(75);
    selectItem.setHoverStyle("interactImageHover");
    selectItem.addItemHoverHandler(
        new ItemHoverHandler() {

          @Override
          public void onItemHover(ItemHoverEvent event) {
            selectItem.setPrompt(
                DigitalObjectMenuView.this.lang.showModifiedHint() + selectItem.getValue());
          }
        });
    selectItem.addChangedHandler(
        new ChangedHandler() {

          @Override
          public void onChanged(ChangedEvent event) {
            getUiHandlers().refreshRecentlyModified();
          }
        });

    form.setFields(selectItem);
    form.setTitle("by:");

    sectionRecentlyModified = new SectionStackSection();
    sectionRecentlyModified.setTitle(lang.recentlyModified());
    sectionRecentlyModified.setResizeable(true);
    sectionRecentlyModified.setItems(sideNavGrid);
    sectionRecentlyModified.setControls(form);
    sectionRecentlyModified.setExpanded(true);

    sectionStack = new SectionStack();
    sectionStack.addSection(sectionRelated);
    sectionStack.addSection(sectionRecentlyModified);
    sectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
    sectionStack.setAnimateSections(true);
    sectionStack.setWidth100();
    sectionStack.setHeight100();
    sectionStack.setOverflow(Overflow.HIDDEN);
    layout.addMember(sectionStack);
  }