Java JPopupMenu removeConsecutiveSeparators(JPopupMenu popupMenu)

Here you can find the source of removeConsecutiveSeparators(JPopupMenu popupMenu)

Description

Removes all consecutive separators from the given menu.

License

Apache License

Parameter

Parameter Description
popupMenu the pop up menu whose consecutive separators will be removed

Declaration

public static void removeConsecutiveSeparators(JPopupMenu popupMenu) 

Method Source Code


//package com.java2s;
/*//from   ww  w .j  a 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 {
    /**
     * Removes all consecutive separators from the given menu.
     * <p>
     * For example, calling the method on the given menu:
     * <pre>
     *    Menu Entry
     *    Separator
     *    Menu Entry
     *    Separator
     *    Separator
     *    Menu Entry
     * </pre>
     * would result in:
     * <pre>
     *    Menu Entry
     *    Separator
     *    Menu Entry
     *    Separator
     *    Menu Entry
     * </pre>
     * </p>
     * 
     * @param popupMenu the pop up menu whose consecutive separators will be removed
     * @see javax.swing.JPopupMenu.Separator
     */
    public static void removeConsecutiveSeparators(JPopupMenu popupMenu) {
        for (int i = 1; i < popupMenu.getComponentCount(); i++) {
            if (isPopupMenuSeparator(popupMenu.getComponent(i))) {
                if (isPopupMenuSeparator(popupMenu.getComponent(i - 1))) {
                    popupMenu.remove(i);
                    i--;
                }
            }
        }
    }

    /**
     * Convenience method that calls the method {@code removeConsecutiveSeparators(JPopupMenu)} with the {@code JPopupMenu} of
     * the given {@code menu} as parameter.
     * 
     * @param menu the menu whose consecutive separators will be removed
     * @see #removeConsecutiveSeparators(JPopupMenu)
     * @see JMenu#getPopupMenu()
     * @see javax.swing.JPopupMenu.Separator
     */
    public static void removeConsecutiveSeparators(JMenu menu) {
        removeConsecutiveSeparators(menu.getPopupMenu());
    }

    /**
     * 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. maybeShowPopup(MouseEvent e, JPopupMenu menu, JComponent parent)
  2. optimizeSeparators(JPopupMenu menu)
  3. positionPopup(Component component, JPopupMenu jpm, int xCoord, int yCoord)
  4. positionPopupMenu(final JPopupMenu popupMenu, final MouseEvent event, final Rectangle rectangle, final int dividerlocation)
  5. refreshPopup(JPopupMenu popup)
  6. removeExtraSeparators(JPopupMenu popup)
  7. removeTopAndBottomSeparators(JPopupMenu popupMenu)
  8. setLocationOnScreen(JPopupMenu visibleMenu, int x, int y)
  9. setPopupMenu(final JComponent component, final JPopupMenu popup)