package com.onaro.sanscreen.client; import java.io.IOException; import java.net.URL; import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.onaro.sanscreen.client.view.ActionFactory; import com.onaro.sanscreen.client.view.init.MenusInitInfo; import com.onaro.sanscreen.client.view.init.MenusInitInfo.MenuInitInfo; import com.onaro.sanscreen.server.interfaces.ServerInterfacesUtils; import com.onaro.sanscreen.server.interfaces.data.ServerInterfaceException; import com.onaro.sanscreen.server.interfaces.data.misc.HelpMenuLink; import com.onaro.sanscreen.server.interfaces.remote.MiscRemote; import com.onaro.util.launch.LaunchUrlAction; /** * Class used to assist in construction of client Frame's help menu. * Constructs menu by loading menu properties. Requests the menu * properties from the server, in case the customer has placed a * custom version of the properties file there. If the server does * not have the menu properties, the properties are read from the * helpGuides.properties file installed with the client. Prior to * v6.0.1, the menu properties always came from the client. * * @author barrykl * */ /*package*/ class HelpMenuBuilder { /** * The logger for this class. */ static final Logger logger = LogManager.getLogger (HelpMenuBuilder.class); /** * The keyword in the helpGuides.properties file specifying a menu * separator. */ private static final String HELP_MENU_SEPARATOR_PROP = "SEPARATOR"; //$NON-NLS-1$ public static void addHelpMenuItems (MenusInitInfo menusInitInfo, ActionFactory frameActions) { MenuInitInfo helpMenu = menusInitInfo.getMenuInitInfo (MenusInitInfo.MENU_HELP); MenusInitInfo.MenuItemInitInfo item; // Add top level online help for SANscreen item = new MenusInitInfo.MenuItemInitInfo (FrameDirector.SANSCREEN_HELP_MENU_ITEM_NAME, null, null); helpMenu.addMenuItem (item); helpMenu.addSeparator(); // Ask the server for any customer-defined help menu URLs. MiscRemote misc = ServerInterfacesUtils.getMiscRemote(); List helpMenuLinks = null; try { helpMenuLinks = misc.getUserDefinedHelpMenuLinks(); } catch (ServerInterfaceException e) { logger.warn ("Unable to get customer-defined help menu links from server: " + e); //$NON-NLS-1$ } // Add any customer-defined help menu items. if (helpMenuLinks != null && ! helpMenuLinks.isEmpty()) { addCustomHelpItems (helpMenu, helpMenuLinks); helpMenu.addSeparator(); } // Add "About" item = new MenusInitInfo.MenuItemInitInfo ("main.help.about", null, null); //$NON-NLS-1$ helpMenu.addMenuItem (item); } private static void addCustomHelpItems (MenuInitInfo parentMenu, List hyperlinks) { for (HelpMenuLink hyperlink: hyperlinks) { // Don't bother adding unlabeled action. String label = hyperlink.getName(); if (label.isEmpty()) { logger.info ("Customer did not specify label for help menu URL '" + hyperlink.getUrl() + "'"); //$NON-NLS-1$ //$NON-NLS-2$ continue; } // Check for customer-supplied separator or missing URL. String urlString = hyperlink.getUrl().trim(); if (urlString.isEmpty()) { if (HELP_MENU_SEPARATOR_PROP.equalsIgnoreCase (label)) { parentMenu.addSeparator(); } else { logger.info ("Customer did not specify URL for help menu item '" + label + "'"); //$NON-NLS-1$ //$NON-NLS-2$ } continue; } // Customer supplied URL. try { // Although the URL is passed to LaunchUrlAction as a string, the conversion to URL here ensures it is launchable. Convert // any spaces to %20. We don't want to use URLEncoder.encode since it will try to encode :'s, /'s, etc in the URL. URL url = new URL (urlString.replaceAll(" ", "%20")); //$NON-NLS-1$ //$NON-NLS-2$ // Log use of "Separator" as the name of a menu command, with a valid URL. May be OK, but will often be a mistake. if (HELP_MENU_SEPARATOR_PROP.equalsIgnoreCase (label)) { logger.info ("Customer specified help menu item named '" + label + "' with URL " + urlString); //$NON-NLS-1$ //$NON-NLS-2$ } // Add menu item parentMenu.addMenuItem (new MenusInitInfo.MenuItemInitAction (new LaunchUrlAction (label, url.toString()))); } catch (IOException e) { logger.info ("Customer specified help menu URL '" + hyperlink.getUrl() + "': " + e); //$NON-NLS-1$ //$NON-NLS-2$ } } } }