/** Helper method to enable/disable the import button depending on mode and permission. */
 private void checkValidationButton() {
   boolean enable = false;
   if (mode == MODE_IMPORT) {
     if (parentFolder != null) {
       Permissions permission =
           alfSession.getServiceRegistry().getDocumentFolderService().getPermissions(parentFolder);
       enable = permission.canAddChildren();
     }
     validationButton.setEnabled(enable);
   } else if (mode == MODE_PICK && selectedItems != null) {
     validationButton.setText(
         String.format(
             MessageFormat.format(
                 getString(R.string.picker_attach_document), selectedMapItems.size()),
             selectedMapItems.size()));
     validationButton.setEnabled(!selectedMapItems.isEmpty());
     validationButton.setOnClickListener(
         new OnClickListener() {
           @Override
           public void onClick(View v) {
             fragmentPick.onSelectDocument(new ArrayList<Document>(selectedMapItems.values()));
           }
         });
   }
 }
  // //////////////////////////////////////////////////////////////////////
  // MENU
  // //////////////////////////////////////////////////////////////////////
  public void getMenu(Menu menu) {
    if (parentFolder == null) {
      return;
    }

    if (getActivity() instanceof MainActivity) {
      getMenu(alfSession, menu, parentFolder);

      if (hasDocument()) {
        displayMenuItem =
            menu.add(
                Menu.NONE,
                MenuActionItem.MENU_DISPLAY_GALLERY,
                Menu.FIRST + MenuActionItem.MENU_DISPLAY_GALLERY,
                R.string.display_gallery);
        displayMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
      }
    } else if (getActivity() instanceof PublicDispatcherActivity) {
      Permissions permission =
          alfSession.getServiceRegistry().getDocumentFolderService().getPermissions(parentFolder);

      if (permission.canAddChildren()) {
        MenuItem mi =
            menu.add(
                Menu.NONE,
                MenuActionItem.MENU_CREATE_FOLDER,
                Menu.FIRST + MenuActionItem.MENU_CREATE_FOLDER,
                R.string.folder_create);
        mi.setIcon(R.drawable.ic_add_folder);
        mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
      }
    }

    // Uncomment for enabling view switcher
    /*
     * displayMenuItem = menu.add(Menu.NONE,
     * MenuActionItem.MENU_DISPLAY_ITEMS, Menu.FIRST +
     * MenuActionItem.MENU_DISPLAY_ITEMS, R.string.display_views); switch
     * (displayMode) { case DISPLAY_LIST:
     * displayMenuItem.setIcon(R.drawable.ic_action_list); break; case
     * DISPLAY_LIST_LARGE:
     * displayMenuItem.setIcon(R.drawable.ic_action_tiles_small); break;
     * case DISPLAY_GRID:
     * displayMenuItem.setIcon(R.drawable.ic_action_list_2); break; default:
     * break; }
     * displayMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
     */
  }
  public void getMenu(Menu menu, Node node) {
    MenuItem mi;

    Permissions permission =
        SessionUtils.getSession(context)
            .getServiceRegistry()
            .getDocumentFolderService()
            .getPermissions(node);

    mi =
        menu.add(
            Menu.NONE,
            MenuActionItem.MENU_DETAILS,
            Menu.FIRST + MenuActionItem.MENU_DETAILS,
            R.string.action_view_properties);
    mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);

    if (permission.canEdit()) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_EDIT,
              Menu.FIRST + MenuActionItem.MENU_EDIT,
              R.string.action_edit_properties);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
    }

    if (permission.canDelete()) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_DELETE_FOLDER,
              Menu.FIRST + MenuActionItem.MENU_DELETE_FOLDER,
              R.string.delete);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
    }
  }
  public static void getMenu(
      AlfrescoSession session, Menu menu, Folder parentFolder, boolean extended) {
    MenuItem mi;

    if (parentFolder == null) {
      return;
    }

    Permissions permission =
        session.getServiceRegistry().getDocumentFolderService().getPermissions(parentFolder);

    if (!extended && parentFolder != null && permission.canAddChildren()) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_CREATE_FOLDER,
              Menu.FIRST + MenuActionItem.MENU_CREATE_FOLDER,
              R.string.folder_create);
      mi.setIcon(R.drawable.ic_add_folder);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }

    if (!extended && parentFolder != null && permission.canAddChildren()) {
      SubMenu devCaptureMenu =
          menu.addSubMenu(
              Menu.NONE,
              MenuActionItem.MENU_DEVICE_CAPTURE,
              Menu.FIRST + MenuActionItem.MENU_DEVICE_CAPTURE,
              R.string.upload);
      devCaptureMenu.setIcon(android.R.drawable.ic_menu_add);
      devCaptureMenu.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

      devCaptureMenu.add(
          Menu.NONE,
          MenuActionItem.MENU_DEVICE_CAPTURE_CAMERA_PHOTO,
          Menu.FIRST + MenuActionItem.MENU_DEVICE_CAPTURE_CAMERA_PHOTO,
          R.string.take_photo);

      if (AndroidVersion.isICSOrAbove()) {
        devCaptureMenu.add(
            Menu.NONE,
            MenuActionItem.MENU_DEVICE_CAPTURE_CAMERA_VIDEO,
            Menu.FIRST + MenuActionItem.MENU_DEVICE_CAPTURE_CAMERA_VIDEO,
            R.string.make_video);
      }

      devCaptureMenu.add(
          Menu.NONE,
          MenuActionItem.MENU_DEVICE_CAPTURE_MIC_AUDIO,
          Menu.FIRST + MenuActionItem.MENU_DEVICE_CAPTURE_MIC_AUDIO,
          R.string.record_audio);
      devCaptureMenu.add(
          Menu.NONE,
          MenuActionItem.MENU_UPLOAD,
          Menu.FIRST + MenuActionItem.MENU_UPLOAD,
          R.string.content_upload);
    }

    if (extended && parentFolder != null && permission.canEdit()) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_EDIT,
              Menu.FIRST + MenuActionItem.MENU_EDIT,
              R.string.edit);
      mi.setIcon(R.drawable.ic_edit);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }

    if (extended && parentFolder != null && permission.canDelete()) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_DELETE_FOLDER,
              Menu.FIRST + MenuActionItem.MENU_DELETE_FOLDER,
              R.string.delete);
      mi.setIcon(R.drawable.ic_delete);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }

    if (!extended && parentFolder != null) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_REFRESH,
              Menu.FIRST + MenuActionItem.MENU_REFRESH,
              R.string.refresh);
      mi.setIcon(R.drawable.ic_refresh);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }
  }
  public static void getMenu(
      AlfrescoSession session, Menu menu, Folder parentFolder, boolean actionMode) {
    MenuItem mi;

    if (parentFolder == null) {
      return;
    }
    Permissions permission = null;
    try {
      permission =
          session.getServiceRegistry().getDocumentFolderService().getPermissions(parentFolder);
    } catch (Exception e) {
      return;
    }

    if (!actionMode) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_SEARCH_FOLDER,
              Menu.FIRST + MenuActionItem.MENU_SEARCH_FOLDER,
              R.string.search);
      mi.setIcon(R.drawable.ic_search);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }

    if (!actionMode && permission.canAddChildren()) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_CREATE_FOLDER,
              Menu.FIRST + MenuActionItem.MENU_CREATE_FOLDER,
              R.string.folder_create);
      mi.setIcon(R.drawable.ic_add_folder);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

      SubMenu createMenu =
          menu.addSubMenu(
              Menu.NONE,
              MenuActionItem.MENU_DEVICE_CAPTURE,
              Menu.FIRST + MenuActionItem.MENU_DEVICE_CAPTURE,
              R.string.add_menu);
      createMenu.setIcon(android.R.drawable.ic_menu_add);
      createMenu.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

      createMenu.add(
          Menu.NONE,
          MenuActionItem.MENU_UPLOAD,
          Menu.FIRST + MenuActionItem.MENU_UPLOAD,
          R.string.upload_title);

      createMenu.add(
          Menu.NONE,
          MenuActionItem.MENU_CREATE_DOCUMENT,
          Menu.FIRST + MenuActionItem.MENU_CREATE_DOCUMENT,
          R.string.create_document);

      createMenu.add(
          Menu.NONE,
          MenuActionItem.MENU_DEVICE_CAPTURE_CAMERA_PHOTO,
          Menu.FIRST + MenuActionItem.MENU_DEVICE_CAPTURE_CAMERA_PHOTO,
          R.string.take_photo);

      if (AndroidVersion.isICSOrAbove()) {
        createMenu.add(
            Menu.NONE,
            MenuActionItem.MENU_DEVICE_CAPTURE_CAMERA_VIDEO,
            Menu.FIRST + MenuActionItem.MENU_DEVICE_CAPTURE_CAMERA_VIDEO,
            R.string.make_video);
      }

      createMenu.add(
          Menu.NONE,
          MenuActionItem.MENU_DEVICE_CAPTURE_MIC_AUDIO,
          Menu.FIRST + MenuActionItem.MENU_DEVICE_CAPTURE_MIC_AUDIO,
          R.string.record_audio);
    }

    if (actionMode) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_DETAILS,
              Menu.FIRST + MenuActionItem.MENU_DETAILS,
              R.string.action_view_properties);
      mi.setIcon(R.drawable.ic_details);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }

    if (actionMode && permission.canEdit()) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_EDIT,
              Menu.FIRST + MenuActionItem.MENU_EDIT,
              R.string.edit);
      mi.setIcon(R.drawable.ic_edit);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }

    if (actionMode && permission.canDelete()) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_DELETE,
              Menu.FIRST + MenuActionItem.MENU_DELETE,
              R.string.delete);
      mi.setIcon(R.drawable.ic_delete);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }

    if (!actionMode) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_REFRESH,
              Menu.FIRST + MenuActionItem.MENU_REFRESH,
              R.string.refresh);
      mi.setIcon(R.drawable.ic_refresh);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }
  }