package com.onaro.sanscreen.client.view.composite; import java.util.HashMap; import java.util.Map; import java.util.ResourceBundle; import javax.swing.JCheckBoxMenuItem; import javax.swing.JMenu; import javax.swing.JMenuItem; import com.onaro.commons.util.IResourceCommonProperties; import com.onaro.sanscreen.client.Messages; import com.onaro.sanscreen.client.view.CompositeDirector; import com.onaro.sanscreen.client.view.ViewDirector; import com.onaro.sanscreen.client.view.CompositeDirector.ViewAdapter; import com.onaro.sanscreen.client.view.actions.CloseAllTabsAction; import com.onaro.util.jfc.ResourceCommonPropertiesFactory; /** * Manages the menu for toggling the visibility of the micro view based on the binding of micro views to main views. */ public class MicroViewsMenu { /** * The resource name for the menu itself. */ public static final String MICRO_VIEW_MENU_NAME = "composite.microViews.toggle"; //$NON-NLS-1$ private final ResourceBundle resources; private final CompositeDirector compositeDirector; /** * The menu displaying the toggle options. */ private JMenu menu; private Map viewsToCheckBoxMap = new HashMap(); private JMenuItem closeAllTabsItem; /** * Build the menu and items for all the given micro views. * * @param compositeDirector * provides interaction with the main and micro views * @param resources * used for building the menu and the items, menu items are built by concatenating the micro-view's name * with ".toggle" */ public MicroViewsMenu(CompositeDirector compositeDirector, ResourceBundle resources) { if (compositeDirector == null) throw new IllegalArgumentException(); if (resources == null) throw new IllegalArgumentException(); this.resources = resources; this.compositeDirector = compositeDirector; } private JMenuItem getCloseAllTabsItem() { if (closeAllTabsItem == null) { CloseAllTabsAction closeAllTabsAction = new CloseAllTabsAction(compositeDirector); closeAllTabsItem = new JMenuItem(closeAllTabsAction); } return closeAllTabsItem; } private JMenu getMenu() { if (menu == null) { IResourceCommonProperties props = ResourceCommonPropertiesFactory.create(resources, MICRO_VIEW_MENU_NAME); menu = new JMenu(props.getName()); menu.setName(MICRO_VIEW_MENU_NAME); menu.setToolTipText(props.getShortDescription()); menu.setIcon(props.getIcon()); if (props.getMnemonicKey() != null) { menu.setMnemonic((props.getMnemonicKey()).intValue()); } for (ViewAdapter microViewAdapter : compositeDirector.getMicroViewAdapters()) { JCheckBoxMenuItem item = getViewCheckBox(microViewAdapter); menu.add(item); } menu.addSeparator(); menu.add(getCloseAllTabsItem()); } return menu; } private JCheckBoxMenuItem getViewCheckBox(ViewAdapter viewAdapter) { if (viewAdapter == null) throw new IllegalArgumentException(); JCheckBoxMenuItem checkBox = viewsToCheckBoxMap.get(viewAdapter); if (checkBox == null) { checkBox = createViewVisibilityCheckBox(viewAdapter); viewsToCheckBoxMap.put(viewAdapter, checkBox); } return checkBox; } private JCheckBoxMenuItem createViewVisibilityCheckBox(ViewAdapter viewAdapter) { if (viewAdapter == null) throw new IllegalArgumentException(); JCheckBoxMenuItem checkBox; ViewDirector viewDirector = viewAdapter.getView(); checkBox = new JCheckBoxMenuItem(viewDirector.getDisplayName()); checkBox.setName(viewDirector.getName()); checkBox.setToolTipText(Messages.INSTANCE.getMicroViewVisibilityMenuItemToolTip(viewDirector.getDisplayName())); checkBox.setIcon(viewDirector.getSmallIcon()); compositeDirector.bindViewVisibility(checkBox, viewAdapter); return checkBox; } /** * Adds the menu and items to the given menu according to the current visibility and availability settings. * * @param targetMenu * the menu to which the sub-menu and items are added */ public void buildMenu(JMenu targetMenu) { if (targetMenu == null) throw new IllegalArgumentException(); JMenu menu = getMenu(); targetMenu.add(menu); } }