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.html;
021 
022 import js.ArrayLike;
023 import js.Id;
024 import js.Js;
025 import js.Initializer;
026 import js.ObjectLike;
027 import js.Vars;
028 import jsx.Configurable;
029 import jsx.core.ArrayLikes;
030 import jsx.core.ObjectLikes;
031 
032 /**
033  * <p>A class for choice groups with its static methods providing group management.</p>
034  * <p>{@link Group} objects help {@link Choice} widgets maintain synchronized states
035  * with their underlying HTML input elements.</p>
036  * 
037  * @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>
038  */
039 public class Group extends Configurable
040 {
041     /**
042      * <p>A global identifier for a configurable property of a {@link Group} object.</p>
043      * <p>The identified configurable property of a {@link Group} object is a reference 
044      * to an array of {@link Choice} objects that holds members of the group.</p>
045      * @since 1.0
046      */
047     public static Id<ArrayLike<Choice>> CHOICES = new Id<ArrayLike<Choice>>();
048 
049     private static Id<String> NAME = new Id<String>();
050     private static int NEXT = 0;
051 
052     private Group(String name) {
053         super(new Initializer().var());
054         ini(this).var(NAME, name);
055         clear();
056     }
057 
058     /**
059      * <p>Constructs a choice group.</p>
060      * @since 1.0
061      */
062     public Group() {
063         this(Js.add(Group.class.getName(), NEXT++));
064     }
065 
066     /**
067      * <p>Clears the choice group.</p>
068      * @since 1.0
069      */
070     public void clear() {
071         ini(this).var(CHOICES, new Vars<Choice>().var());
072         ObjectLikes.delete(GROUPS, getName());
073     }
074 
075     private final static ObjectLike GROUPS = new Initializer().var();
076 
077     /**
078      * <p>Statically gets a choice group by its name.</p>
079      * @param name The name of a choice group.
080      * @return The choice group with the name.
081      * @since 1.0
082      */
083     public static final Group get(String name) {
084         Group g = (Group)GROUPS.var(name);
085         if (Js.not(g)) {
086             g = new Group(name);
087             GROUPS.var(name, g);
088         }
089         return g;
090     }
091 
092     /**
093      * <p>Gets the name of the current choice group.</p>
094      * @return The name of the current choice group.
095      * @since 1.0
096      */
097     public String getName() {
098         return ini(this).var(NAME);
099     }
100 
101     /**
102      * <p>Synchronizes all members of the group.</p>
103      * <p>This method basically invokes {@link Choice#synchronize()} method on each
104      * choice member of the current group.</p>
105      * @since 1.0
106      */
107     public void synchronize() {
108         ArrayLike<Choice> choices = ini(this).var(CHOICES);
109         for (int i = 0, len = ArrayLikes.length(choices); i < len; i++) {
110             choices.get(i).synchronize();
111         }
112     }
113 }