001    package net.databinder.components;
002    
003    import java.io.Serializable;
004    
005    import java.util.LinkedList;
006    import java.util.List;
007    
008    import org.apache.wicket.Component;
009    import org.apache.wicket.markup.html.link.Link;
010    
011    /**
012     * Generator for links to components (usually panels) that are only visible one at a time.
013     * Similar to TabbedPanel, but the links and target component can be placed anywhere in 
014     * the associated markup template. Ex:
015     * <pre>
016     * SourceList sl = new SourceList();
017     * add(sl.new SourceLink("link-a", new MyPanel("panel-a")));
018     * add(sl.new SourceLink("link-b", new MyOtherPanel("panel-b")));
019     * </pre>
020     * @author Nathan Hamblen
021     */
022    public class SourceList implements Serializable {
023            private List<SourceLink> links = new LinkedList<SourceLink>();
024            SourceLink current;
025            
026            /** 
027             * Link that that causes the paired component to be visible when clicked, and all other
028             * components in the SourceList to be invisible. Paired components are set to invisible
029             * on initialization; to select a linked component programmatically, call onClick().  
030             */
031            public class SourceLink extends Link {
032                    Component target;
033                    public SourceLink(String id, Component target) {
034                            super(id);
035                            this.target = target;
036                            links.add(this);
037                            target.setVisible(false);
038                    }
039                    /** return false when paired component is selected */
040                    @Override
041                    public boolean isEnabled() {
042                            return current != this;
043                    }
044                    @Override
045                    public void onClick() {
046                            if (current != null)
047                                    current.getTarget().setVisible(false);
048                            current = this;
049                            target.setVisible(true);
050                    }
051                    Component getTarget() { return target; } 
052            }
053    }