Java JMenu insertSeparatorIfNeeded(JMenu menu, int position)

Here you can find the source of insertSeparatorIfNeeded(JMenu menu, int position)

Description

Convenience method that calls the method insertSeparatorIfNeeded(JPopupMenu,int) with the JPopupMenu of the given menu as the first parameter and with the given position as second parameter.

License

Apache License

Parameter

Parameter Description
menu the menu that will be processed
position the position where a separator might be inserted

Return

true if the separator was inserted, false otherwise.

Declaration

public static boolean insertSeparatorIfNeeded(JMenu menu, int position) 

Method Source Code


//package com.java2s;
/*//from  w w  w . ja  v a 2  s . c  o  m
 * Zed Attack Proxy (ZAP) and its related class files.
 * 
 * ZAP is an HTTP/HTTPS proxy for assessing web application security.
 * 
 * Copyright 2014 The ZAP Development 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.
 */

import java.awt.Component;

import javax.swing.JMenu;
import javax.swing.JPopupMenu;

public class Main {
    /**
     * Inserts a separator at the given {@code position} if it exists a non separator menu component at the given
     * {@code position} and if there isn't, already, a separator immediately before the insert {@code position} (to prevent
     * consecutive separators).
     * 
     * @param popupMenu the pop up menu that will be processed
     * @param position the position where a separator might be inserted
     * @return {@code true} if the separator was inserted, {@code false} otherwise.
     * @see javax.swing.JPopupMenu.Separator
     */
    public static boolean insertSeparatorIfNeeded(JPopupMenu popupMenu, int position) {
        final int menuComponentCount = popupMenu.getComponentCount();
        if (menuComponentCount == 0 || position <= 0 || position > menuComponentCount) {
            return false;
        }
        final Component currentComponent = popupMenu.getComponent(position);
        if (isPopupMenuSeparator(currentComponent)) {
            return false;
        }
        final Component previousComponent = popupMenu.getComponent(position - 1);
        if (isPopupMenuSeparator(previousComponent)) {
            return false;
        }
        popupMenu.insert(new JPopupMenu.Separator(), position);
        return true;
    }

    /**
     * Convenience method that calls the method {@code insertSeparatorIfNeeded(JPopupMenu,int)} with the {@code JPopupMenu} of
     * the given {@code menu} as the first parameter and with the given {@code position} as second parameter.
     * 
     * @param menu the menu that will be processed
     * @param position the position where a separator might be inserted
     * @return {@code true} if the separator was inserted, {@code false} otherwise.
     * @see #insertSeparatorIfNeeded(JPopupMenu,int)
     * @see JMenu#getPopupMenu()
     * @see javax.swing.JPopupMenu.Separator
     */
    public static boolean insertSeparatorIfNeeded(JMenu menu, int position) {
        return insertSeparatorIfNeeded(menu.getPopupMenu(), position);
    }

    /**
     * Tells whether or not the given {@code component} is a {@code JPopupMenu.Separator}.
     * 
     * @param component the component that will be checked.
     * @return {@code true} if the given component is a {@code JPopupMenu.Separator}, {@code false} otherwise.
     * @see javax.swing.JPopupMenu.Separator
     * @see #isExtensionPopupMenu(Component)
     * @see #isExtensionPopupMenuItem(Component)
     */
    public static boolean isPopupMenuSeparator(Component component) {
        return (component instanceof JPopupMenu.Separator);
    }
}

Related

  1. getMenu(JMenu poMenu, String psComando, boolean pbRecurs)
  2. getMenuItem(JMenu menu, String text)
  3. getMenuItemIndex(JMenu menu, String menuItemText)
  4. getMenuItems(JMenu menu)
  5. getTextByJMenu(List lstMenus)
  6. isAtLeastOneChildComponentVisible(JMenu menu)
  7. limitMenuSize(JMenu menu, String name, int size)
  8. makeMenu(JMenu menu, List menuItems)
  9. menuItem(JMenu parent, String label, Object... attrs)