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.ArrayLike;
023 import jsx.ui.Box;
024 import jsx.ui.Component;
025 import jsx.ui.Widget;
026 
027 /**
028  * <p>A base class for frame widgets.</p>
029  * <p>A frame widget is a {@link Box} that normally has two basic {@link Box}es as its 
030  * children. Its first child is for the title of the frame; its second child is for 
031  * the content of the frame.</p>
032  * <p>The {@link Box}'s methods for {@link Frame} have been overridden and redirected 
033  * to its content box.</p>
034  * 
035  * @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>
036  */
037 public class Frame extends Box<Widget>
038 {
039     /**
040      * <p>Constructs a default frame widget.</p>
041      * <p>This constructor invokes {@link #Frame(Component)} with a new component created by calling
042      * {@link Component#div()} as argument.</p>
043      * @since 1.0
044      */
045     public Frame() {
046         this(Component.div());
047     }
048 
049     /**
050      * <p>Typically constructs a frame widget.</p>
051      * <p>This constructor invokes its super constructor and creates its title box with a CSS sub 
052      * selector "tbox" expended and content box with a CSS sub selector "tbox" expended.</p>
053      * @param e The underlying component for the frame.
054      * @since 1.0
055      */
056     protected Frame(Component e) {
057         super(e);
058         Box<Widget> t = new Box<Widget>(Component.span());
059         Component.addClasses(t.unwrap(), subs("tbox"));
060         Box<Widget> c = new Box<Widget>(Component.span());
061         Component.addClasses(c.unwrap(), subs("cbox"));
062         super.add(t);
063         super.add(c);
064     }
065 
066 
067     /**
068      * <p>Gets the title box of the frame.</p>
069      * @return The title box.
070      * @since 1.0
071      */
072     @SuppressWarnings("unchecked")
073     public final Box<Widget> getTitle() {
074         return (Box<Widget>)super.children().get(0);
075     }
076 
077     /**
078      * <p>Gets the content box of the frame.</p>
079      * @return The content box.
080      * @since 1.0
081      */
082     @SuppressWarnings("unchecked")
083     public final Box<Widget> getContent() {
084         return (Box<Widget>)super.children().get(1);
085     }
086 
087     /**
088      * <p>Gets an array of child widgets of frame's content.</p>
089      * <p>This overriding method redirects its invocation to that of the same method of the content box.</p>
090      * <p>{@link Box#children()} has to be used to get the real children of this box, but normally
091      * this situation is not expected.</p>
092      * @return An array of child widgets of frame's content.
093      * @since 1.0
094      */
095     @Override
096     public ArrayLike<Widget> children() {
097         return getContent().children();
098     }
099 
100     /**
101      * <p>Adds a widget to the content.</p>
102      * <p>This method will force the content to layout if its children list has been changed.</p>
103      * <p>This overriding method redirects its invocation to that of the same method of the content box.</p>
104      * <p>{@link Box#add(Widget)} has to be used to add a widget to this box, but normally
105      * this situation is not expected.</p>
106      * @param w A widget to be added to the content.
107      * @return <tt>true</tt> if the specified widget becomes a new child of the 
108      * content; <tt>false</tt>, otherwise.
109      * @since 1.0
110      */
111     @Override
112     public boolean add(Widget w) {
113         return getContent().add(w);
114     }
115 
116     /**
117      * <p>Inserts a given widget to the content immediately before an existing child widget.</p>
118      * <p>This method will force the content to layout if its children list has been changed.</p>
119      * <p>This overriding method redirects its invocation to that of the same method of the content box.</p>
120      * <p>{@link Box#insert(Widget, Widget)} has to be used to insert a widget to this box, but normally
121      * this situation is not expected.</p>
122      * @param w A widget to insert.
123      * @param r An existing child widget for reference.
124      * @return <tt>true</tt> if it is successfully inserted; <tt>false</tt>, otherwise.
125      * @since 1.0
126      */
127     @Override
128     public boolean insert(Widget w, Widget r) {
129         return getContent().insert(w, r);
130     }
131 
132     /**
133      * <p>Removes a widget from the content.</p>
134      * <p>This method will force the content to layout if its children list has been changed.</p>
135      * <p>This overriding method redirects its invocation to that of the same method of the content box.</p>
136      * <p>{@link Box#remove(Widget)} has to be used to remote a widget from this box, but normally
137      * this situation is not expected.</p>
138      * @param w A widget to be removed from the children list of this box.
139      * @return <tt>true</tt> if the specified widget is successfully removed; <tt>false</tt>, 
140      * otherwise.
141      * @since 1.0
142      */
143     @Override
144     public boolean remove(Widget w) {
145         return getContent().remove(w);
146     }
147 }