<commit_before>/*
 * Copyright 2003-2007 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Revision:        $Rev$
 * Last Changed:    $Date$
 * Last Changed By: $Author$
 */

package org.efaps.webapp.components.listmenu;

import java.util.Iterator;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;

import org.efaps.webapp.EFapsSession;
import org.efaps.webapp.components.listmenu.ListMenuPanel.Rows;
import org.efaps.webapp.components.listmenu.ListMenuPanel.StyleClassName;
import org.efaps.webapp.models.MenuItemModel;

/**
 * This Class renders a Link wich removes a Sub-ListMenuPanel from the
 * Parent-ListMenuPanel.
 *
 * @author jmo
 * @version $Id$
 */
public class AjaxRemoveLink extends AbstractAjaxLink {

  private static final long serialVersionUID = 1L;

  /**
   * Construtor setting the ID and the Model of this Component
   *
   * @param _id
   * @param _model
   */
  public AjaxRemoveLink(final String _id, final MenuItemModel _model) {
    super(_id, _model);
  }

  @Override
  public void onClick(final AjaxRequestTarget _target) {
    // find the ListMenuPanel
    ListMenuPanel listmenupanel =
        (ListMenuPanel) this.findParent(ListMenuPanel.class);

    // change the Model of the ListMenuPanel and update it
    List<?> list =
        (List<?>) ((Component) listmenupanel.iterator().next())
            .getModelObject();
    ((Rows) listmenupanel.iterator().next()).setReuseItems(false);

    int del = list.indexOf(this.getModel());
    int index = 0;
    for (int i = 0; i < del; i++) {
      if (list.get(i) instanceof MenuItemModel) {
        index++;
      }

    }

    if (del > -1) {
      list.remove(del);
      if (list.size() > 0 && list.get(del) instanceof List) {
        list.remove(del);
      }
      if (listmenupanel.getHeaderComponents().size() > 1) {
        listmenupanel.getHeaderComponents().remove(index);
      }
    }
    _target.addComponent(listmenupanel);

    // get the previosly selected Component
    Component previous =
        ((EFapsSession) this.getSession()).getFromCache(listmenupanel
            .getMenuKey());

    if (isChildOf(previous, listmenupanel)) {
      ((EFapsSession) this.getSession()).removeFromCache(listmenupanel
          .getMenuKey());
      // get the root
      ListMenuPanel parentlistmenupanel =
          (ListMenuPanel) listmenupanel.findParent(ListMenuPanel.class);
      while (parentlistmenupanel.getHeaderComponents().isEmpty()) {
        parentlistmenupanel =
            (ListMenuPanel) parentlistmenupanel.findParent(ListMenuPanel.class);
      }<commit_msg>- remove of bug in ListMenu, for the Case that several header-only Submenus where removed<commit_after>/*
 * Copyright 2003-2007 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Revision:        $Rev$
 * Last Changed:    $Date$
 * Last Changed By: $Author$
 */

package org.efaps.webapp.components.listmenu;

import java.util.Iterator;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;

import org.efaps.webapp.EFapsSession;
import org.efaps.webapp.components.listmenu.ListMenuPanel.Rows;
import org.efaps.webapp.components.listmenu.ListMenuPanel.StyleClassName;
import org.efaps.webapp.models.MenuItemModel;

/**
 * This Class renders a Link wich removes a Sub-ListMenuPanel from the
 * Parent-ListMenuPanel.
 *
 * @author jmo
 * @version $Id$
 */
public class AjaxRemoveLink extends AbstractAjaxLink {

  private static final long serialVersionUID = 1L;

  /**
   * Construtor setting the ID and the Model of this Component
   *
   * @param _id
   * @param _model
   */
  public AjaxRemoveLink(final String _id, final MenuItemModel _model) {
    super(_id, _model);
  }

  @Override
  public void onClick(final AjaxRequestTarget _target) {
    // find the ListMenuPanel
    ListMenuPanel listmenupanel =
        (ListMenuPanel) this.findParent(ListMenuPanel.class);

    // change the Model of the ListMenuPanel and update it
    List<?> list =
        (List<?>) ((Component) listmenupanel.iterator().next())
            .getModelObject();
    ((Rows) listmenupanel.iterator().next()).setReuseItems(false);

    int del = list.indexOf(this.getModel());
    int index = 0;
    for (int i = 0; i < del; i++) {
      if (list.get(i) instanceof MenuItemModel) {
        index++;
      }

    }

    if (del > -1) {
      list.remove(del);
      if (list.size() > del && list.get(del) instanceof List) {
        list.remove(del);
      }
      if (listmenupanel.getHeaderComponents().size() > 1) {
        listmenupanel.getHeaderComponents().remove(index);
      }
    }
    _target.addComponent(listmenupanel);

    // get the previosly selected Component
    Component previous =
        ((EFapsSession) this.getSession()).getFromCache(listmenupanel
            .getMenuKey());

    if (isChildOf(previous, listmenupanel)) {
      ((EFapsSession) this.getSession()).removeFromCache(listmenupanel
          .getMenuKey());
      // get the root
      ListMenuPanel parentlistmenupanel =
          (ListMenuPanel) listmenupanel.findParent(ListMenuPanel.class);
      while (parentlistmenupanel.getHeaderComponents().isEmpty()) {
        parentlistmenupanel =
            (ListMenuPanel) parentlistmenupanel.findParent(ListMenuPanel.class);
      }
{'fraction_non_alphanumeric': 0.1061495673671199, 'fraction_numerical': 0.005407911001236094, 'mean_word_length': 3.28391793514229, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3439382', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1910, 'n_words': 524}