001    package org.hackystat.projectbrowser.page.contextsensitive;
002    
003    import java.util.Arrays;
004    import java.util.List;
005    
006    import org.apache.wicket.ajax.AjaxRequestTarget;
007    import org.apache.wicket.markup.html.basic.Label;
008    import org.apache.wicket.markup.html.form.DropDownChoice;
009    import org.apache.wicket.markup.html.list.ListItem;
010    import org.apache.wicket.markup.html.list.ListView;
011    import org.apache.wicket.markup.html.panel.Panel;
012    import org.apache.wicket.model.PropertyModel;
013    
014    /**
015     * Provides a panel containing a set of label/drop-down menu pairs, each of whose visibility 
016     * can be controlled via Ajax callbacks. 
017     * @author Philip Johnson
018     */
019    public class ContextSensitivePanel extends Panel {
020    
021      /** Support serialization. */
022      private static final long serialVersionUID = 1L;
023      /** The list of ContextSensitiveMenus, each containing a label and drop-down menu. */
024      private List<ContextSensitiveMenu> menus;
025      
026      /**
027       * Create the ContextSensitivePanel with the supplied ContextSensitiveMenus.
028       * @param id The wicket:id for this panel.
029       * @param menus The list of ContextSensitiveMenus.
030       */
031      public ContextSensitivePanel(String id, List<ContextSensitiveMenu> menus) {
032        super(id);
033        this.setOutputMarkupPlaceholderTag(true);
034        this.menus = menus;
035        add(new ListView("ContextSensitiveMenus", new PropertyModel(this, "menus")) {
036    
037          /** Support serialization. */
038         private static final long serialVersionUID = 1L;
039    
040         /**
041          * Used to generate the list of label/menu pairs.
042          */
043          @Override
044          protected void populateItem(ListItem item) {
045            ContextSensitiveMenu menu = (ContextSensitiveMenu) item.getModelObject();
046            Label label = new Label("ContextSensitiveMenuLabel", menu.getName());
047            label.setVisible(menu.isVisible());
048            label.setOutputMarkupPlaceholderTag(true);
049            item.add(label);
050            DropDownChoice ddMenu = 
051              new DropDownChoice ("ContextSensitiveMenu", 
052                  new PropertyModel(menu, "selectedValue"),
053                  new PropertyModel(menu, "values"));
054            ddMenu.setRequired(false);
055            ddMenu.setVisible(menu.isVisible());
056            ddMenu.setOutputMarkupPlaceholderTag(true);
057            item.add(ddMenu);
058          }
059        });
060      }
061      
062      /**
063       * Gets the list of ContextSensitiveMenus associated with this panel.
064       * @return The list of ContextSensitiveMenus.
065       */
066      public List<ContextSensitiveMenu> getMenus() {
067        return this.menus;
068      }
069      
070      /**
071       * Returns the ContextSensitiveMenu with the passed name, or null if not found.
072       * @param name The menu name to search for.
073       * @return The menu, or null if not found.
074       */
075      public ContextSensitiveMenu getMenu(String name) {
076        for (ContextSensitiveMenu menu : this.menus) {
077          if (menu.getName().equals(name)) {
078            return menu;
079          }
080        }
081        return null;
082      }
083      
084      /**
085       * Makes the specified ContextSensitiveMenu names visible. All others in the panel will be 
086       * invisible.
087       * @param target The Component displaying this Panel.
088       * @param names The list of ContextSensitiveMenu names that should be made visible.
089       */
090      public void setVisible(AjaxRequestTarget target, String... names) {
091        List<String> namesList = Arrays.asList(names);
092        for (ContextSensitiveMenu menu : menus) {
093          menu.setVisible(namesList.contains(menu.getName()));
094          }
095        target.addComponent(this);
096      }
097    }