Java JMenu addSeparatorIfNeeded(JMenu menu)

Here you can find the source of addSeparatorIfNeeded(JMenu menu)

Description

Convenience method that calls the method addSeparatorIfNeeded(JPopupMenu) with the JPopupMenu of the given menu as parameter.

License

Apache License

Parameter

Parameter Description
menu the menu that will be processed

Return

true if the separator was added, false otherwise.

Declaration

public static boolean addSeparatorIfNeeded(JMenu menu) 

Method Source Code


//package com.java2s;
/*// w w w  .  jav 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 {
    /**
     * Appends a separator to the end of the menu if it exists at least one non separator menu component immediately before and
     * if there isn't, already, a separator at the end of the menu.
     * 
     * @param popupMenu the pop up menu that will be processed
     * @return {@code true} if the separator was added, {@code false} otherwise.
     * @see javax.swing.JPopupMenu.Separator
     */
    public static boolean addSeparatorIfNeeded(JPopupMenu popupMenu) {
        final int menuComponentCount = popupMenu.getComponentCount();
        if (menuComponentCount == 0) {
            return false;
        }
        final Component lastMenuComponent = popupMenu.getComponent(menuComponentCount - 1);
        if (isPopupMenuSeparator(lastMenuComponent)) {
            return false;
        }
        popupMenu.addSeparator();
        return true;
    }

    /**
     * Convenience method that calls the method {@code addSeparatorIfNeeded(JPopupMenu)} with the {@code JPopupMenu} of the
     * given {@code menu} as parameter.
     * 
     * @param menu the menu that will be processed
     * @return {@code true} if the separator was added, {@code false} otherwise.
     * @see #addSeparatorIfNeeded(JPopupMenu)
     * @see JMenu#getPopupMenu()
     * @see javax.swing.JPopupMenu.Separator
     */
    public static boolean addSeparatorIfNeeded(JMenu menu) {
        return addSeparatorIfNeeded(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. addMenuItem(JMenu menu, String label, Action action, int mnemonic, String tooltip, boolean isEnabled)
  2. addMenuItem(Window window, JMenu menu, String title, String icon, int mnemonic, KeyStroke key, ActionListener listener)
  3. addMenuItems(JMenu menu, JComponent... items)
  4. addRadioButtonMenuItem(ActionListener al, JMenu m, String label, String command, boolean b, ButtonGroup g)
  5. addSeparator(JMenu menu)
  6. applyDefaultProperties(final JMenu comp)
  7. assignMnemonics(JMenu menu)
  8. changeMenuItemsState(final JMenu menu, final boolean enableState)
  9. constructRecentlyFilesMenuItems(JMenu recentlyOpenFilesMenuItem, LinkedHashMap filesOpenedLHM)