001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package jsx.ui.box;
021 
022 import js.Js;
023 import jsx.ui.Box;
024 import jsx.ui.Component;
025 import jsx.ui.ctrl.Checkable;
026 
027 /**
028  * <p>A base class for tab-panel widgets.</p>
029  * <p>A {@link Tab} is a {@link Box} of a group of {@link Checkable}s.</p>
030  * 
031  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
032  */
033 public class Tab extends Box<Checkable>
034 {
035     /**
036      * <p>Constructs a default tab-panel.</p>
037      * <p>This constructor invokes {@link #Tab(Component)} with a new component created by calling
038      * {@link Component#div()} as argument.</p>
039      * @since 1.0
040      */
041     public Tab() {
042         super(Component.div());
043     }
044 
045     /**
046      * <p>Typically constructs a frame widget.</p>
047      * <p>This constructor simply invokes its super constructor with the given component as argument.</p>
048      * @param e The underlying component for the tab-panel.
049      * @since 1.0
050      */
051     protected Tab(Component e) {
052         super(e);
053     }
054 
055     /**
056      * <p>Adds a checkable to this tab-panel.</p>
057      * <p>The {@link Checkable} is automatically grouped with other children after added.</p>
058      * <p>This method will force a layout for the current box if the children list has been changed.</p>
059      * @param c A {@link Checkable} to be added to the tab-panel.
060      * @return <tt>true</tt> if the specified @link Checkable} becomes a new child of the 
061      * tab-panel; <tt>false</tt>, otherwise.
062      * @since 1.0
063      */
064     @Override
065     public boolean add(Checkable c) {
066         boolean ret = super.add(c);
067         if (ret) {
068             Checkable c0 = children().get(0);
069             if (Js.be(c0)) {
070                 c.group(c0);
071             }
072         }
073         return ret;
074     }
075 
076     /**
077      * <p>Inserts a given checkable to this tab-panel immediately before an existing child.</p>
078      * <p>The {@link Checkable} is automatically grouped with other children after added.</p>
079      * <p>This method will force a layout for the current box if the children list has been changed.</p>
080      * @param c A {@link Checkable} to be inserted to the tab-panel.
081      * @param r An existing {@link Checkable} for reference.
082      * @return <tt>true</tt> if it is successfully inserted; <tt>false</tt>, otherwise.
083      * @since 1.0
084      */
085     @Override
086     public boolean insert(Checkable c, Checkable r) {
087         boolean ret = super.insert(c, r);
088         if (ret) {
089             c.group(r);
090         }
091         return ret;
092     }
093 
094     /**
095      * <p>Removes a checkable from the current tab-panel.</p>
096      * <p>The {@link Checkable} is automatically ungrouped from other children after removed.</p>
097      * <p>This method will force a layout for the current box if the children list has been changed.</p>
098      * @param c A {@link Checkable} to be removed from the tab-panel.
099      * @return <tt>true</tt> if the specified widget is successfully removed; <tt>false</tt>, 
100      * otherwise.
101      * @since 1.0
102      */
103     @Override
104     public boolean remove(Checkable c) {
105         boolean removed = super.remove(c);
106         if (removed) {
107             c.ungroup();
108         }
109         return removed;
110     }
111 }