package com.onaro.sanscreen.client.view.composite; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.swing.AbstractButton; import javax.swing.JCheckBoxMenuItem; import javax.swing.SwingConstants; import com.onaro.client.swing.PopupMenuButton; 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; /** * Class that implements a popup button to show all of our microviews in a menu and allows the user to show/hide them globally. * Changes to the user chosen visibility of the microview buttons is saved in preferences so as to be persisted across sessions. * * @author rnoel * */ public class ShowHideMicroViewsPopupButton extends PopupMenuButton { private static final long serialVersionUID = 1L; private final CompositeDirector compositeDirector; // Map of view adapters to checkbox items in the popup menu private Map viewsToCheckBoxMap = new HashMap(); public ShowHideMicroViewsPopupButton(CompositeDirector compositeDirector) { // Init PopupMenuButton with no main button, just the arrow pointing up super(false, SwingConstants.NORTH); if (compositeDirector == null) throw new IllegalArgumentException(); this.compositeDirector = compositeDirector; /* Add tooltip for the button. The button is technically a panel containing buttons so we need to set the tooltip on the arrow button or we won't see it. */ getArrowButton().setToolTipText(Messages.INSTANCE.getShowHideMicroViewsPopupButtonTooltip()); // Get all microview adapaters from the composite director for building the popup menu List microViewAdapters = new ArrayList(compositeDirector.getMicroViewAdapters()); // Sort the adapters by view display name so the popup menu we generate is sorted alphabetically Collections.sort(microViewAdapters, new Comparator() { @Override public int compare(ViewAdapter va1, ViewAdapter va2) { return va1.getView().getDisplayName().compareToIgnoreCase(va2.getView().getDisplayName()); } }); // Generate the list of menu items for our popup List items = new ArrayList(microViewAdapters.size()); for (ViewAdapter microViewAdapter : microViewAdapters) { JCheckBoxMenuItem item = getUserWantsViewVisibleCheckBox(microViewAdapter); items.add(item); } // Set the items in the popup setButtons(items); } /** * Returns the menu item of the popup associated with the view adapter. * * @param viewAdapter * @return the menu item of the popup associated with the view adapter. */ public JCheckBoxMenuItem getUserWantsViewVisibleCheckBox(ViewAdapter viewAdapter) { if (viewAdapter == null) throw new IllegalArgumentException(); JCheckBoxMenuItem checkBox = viewsToCheckBoxMap.get(viewAdapter); if (checkBox == null) { checkBox = createUserWantsViewVisibleCheckBox(viewAdapter); viewsToCheckBoxMap.put(viewAdapter, checkBox); } return checkBox; } /* Creates and returns a menu item for the given viewAdapter for our popup */ private JCheckBoxMenuItem createUserWantsViewVisibleCheckBox(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.getMicroViewUserWantsVisibilityMenuItemToolTip(viewDirector.getDisplayName())); checkBox.setIcon(viewDirector.getSmallIcon()); // Bind the checkbox to show/hide behavior in the compositeDirector compositeDirector.bindUserWantsViewVisible(checkBox, viewAdapter); return checkBox; } }