All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pekinsoft.spi.MenuItemProvider Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2022 PekinSOFT Systems
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 * 
 * *****************************************************************************
 *  Project    :   Application
 *  Class      :   MenuItemProvider.java
 *  Author     :   Sean Carrick
 *  Created    :   Dec 12, 2022
 *  Modified   :   Dec 12, 2022
 *  
 *  Purpose: See class JavaDoc for explanation
 *  
 *  Revision History:
 *  
 *  WHEN          BY                   REASON
 *  ------------  -------------------  -----------------------------------------
 *  Dec 12, 2022  Sean Carrick         Initial creation.
 * *****************************************************************************
 */

package com.pekinsoft.spi;

import javax.swing.JMenuItem;

/**
 * The {@code MenuItemProvider} interface provides a method by which {@code Application} developers building on the
 * {@code org.jdesktop.application.Application Application} can dynamically create either
 * {@link com.pekinsoft.spi.MenuBarProvider a menu bar} or {@link 
 * com.pekinsoft.spi.MenuProvider individual menus} for the application.
 * 
 * The way this works is that the {@code Application} will use the {@link 
 * org.openide.util.Lookup Lookup} in an attempt to find an implementation
 * of the {@code MenuBarProvider} interface. If one is not found, then the
 * {@code Application} will search for implementations of the {@code MenuProvider}
 * interface, from which it will build a menu bar to add to the main window.
 * 
 * To provide the menu bar or the individual menus, the developer can create
 * instances of the menus by also using {@code Lookup} and seeking to find as
 * many instance of this interface as may be provided. Once the menus have been
 * created, the application developer could again use {@code Lookup} to find all
 * instances of the {@code MenuProvider} interface that may have been provided
 * in order to build the menu bar, which could then be exposed to the {@code 
 * Lookup} for the {@code Application} to install into the main window.
 * 
 * By creating an instance of the {@code MenuBarProvider} as explained in the
 * last paragraph, the application developer can build modular applications and
 * allow each module to provide the menu items that it requires. This allows for
 * as customizable and dynamic of an application as is possible.
 *
 * @author Sean Carrick <sean at pekinsoft dot com>
 * 
 * @version 1.3
 * @since 1.0
 */
public interface MenuItemProvider extends Comparable {
    
    /**
     * Retrieves the {@link javax.swing.JMenuItem JMenuItem} this provider is
     * providing to the encompassing {@code org.jdesktop.application.Application
     * Application} {@code org.jdesktop.application.Application Application}.
     * 
     * @return the fully setup and configured `JMenuItem`
     */
    public JMenuItem getMenuItem();
    
    /**
     * Retrieves the name of the {@link javax.swing.JMenu JMenu} into which this
     * {@code MenuItemProvider}'s {@link javax.swing.JMenuItem JMenuItem} should
     * be installed.
     * 
     * This method is implementation-specific as to what is returned from it.
     * One could have this method return the value of the {@code name} property
     * of the {@code JMenu} into which this `JMenuItem` should be installed. Or,
     * the value could be the value of the {@code JMenu}'s `text` property.
     * 
     * Another alternative would be to have an enumeration of the available
     * menus in an {@code Application}, and simply have this method return either
     * the {@code name} or `value` property of the enumeration constant.
     * 
     * @return a string that in some way specifies in which menu the provided
     * menu item should be installed
     */
    public String getMenuName();
    
    /**
     * Determines whether to have a {@link javax.swing.JPopupMenu.Separator
     * Separator} added after the menu item on the menu in which it is installed.
     * The default implementation returns {@code false}, since most menu items
     * will not want to add a separator after itself.
     * 
     * @return {@code true} to have a `Separator` added after the menu item
     */
    public default boolean separatorAfter() {
        return false;
    }
    
    /**
     * Determines whether to have a {@link javax.swing.JPopupMenu.Separator
     * Separator} added before the menu item on the menu in which it is installed.
     * The default implementation returns {@code false}, since most menu items
     * will not want to add a separator before it is added.
     * 
     * @return {@code true} to have a `Separator` added before the menu
     * item
     */
    public default boolean separatorBefore() {
        return false;
    }
    
    /**
     * Retrieves the menu item's desired position on the menu.
     * 
     * @return the desired menu position
     */
    public int getPosition();

    /**
     * Compares this {@code MenuItemProvider}'s desired position with another
     * {@code MenuItemProvider}'s desired position to allow for sorting lists
     * of {@code MenuItemProvider} instances. This will allow an application
     * developer to make sure that the menu items are always installed into the
     * application's menus in the same order each time the application is
     * launched.
     * 
     * @param o the other {@code MenuItemProvider} to compare positions
     * @return -1 if this provider is before the other; 0 if both want the same
     * position in the menu; 1 if this provider should come after the other one
     */
    @Override
    public default int compareTo(MenuItemProvider o) {
        return Integer.compare(getPosition(), o.getPosition());
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy