View Javadoc

1   /*
2    * $Id: SelectablePanelGroup.java,v 1.4 2006/09/06 17:48:19 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  
23  package org.xmlhammer.gui.util;
24  
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  
28  /***
29   * Put comment...
30   * 
31   * @version $Revision: 1.4 $, $Date: 2006/09/06 17:48:19 $
32   * @author Edwin Dankert <edankert@gmail.com>
33   */
34  
35  public class SelectablePanelGroup {
36  
37      // the list of buttons panels in this group
38      protected ArrayList<SelectablePanel> panels = null;
39  
40      /***
41       * The current selection.
42       */
43      SelectablePanel selection = null;
44  
45      /***
46  	 * default constructor
47  	 */
48  	public SelectablePanelGroup() {
49  		super();
50          
51          panels = new ArrayList<SelectablePanel>();
52  	}
53  	
54  
55      /***
56       * Adds the panel to the group.
57       * 
58       * @param p the panel to be added
59       */ 
60      public void add( SelectablePanel p) {
61          if( p == null) {
62              return;
63          }
64  
65          panels.add( p);
66  
67          if ( p.isSelected()) {
68              if (selection == null) {
69                  selection = p;
70              } else {
71                  p.setSelected(false);
72              }
73          }
74  
75          p.setGroup(this);
76      }
77   
78      /***
79       * Removes the panel from the group.
80       * 
81       * @param p the panel to be removed
82       */ 
83      public void remove( SelectablePanel p) {
84          if( p == null) {
85              return;
86          }
87          
88          panels.remove( p);
89          
90          if( p == selection) {
91              selection = null;
92          }
93  
94          p.setGroup(null);
95      }
96  
97      /***
98       * Returns all the panels that are participating in
99       * this group.
100      * 
101      * @return an <code>Enumeration</code> of the panels in this group
102      */
103     public Iterator<SelectablePanel> getElements() {
104         return panels.iterator();
105     }
106 
107     /***
108      * Returns the model of the selected button.
109      * 
110      * @return the selected button model
111      */
112     public SelectablePanel getSelection() {
113         return selection;
114     }
115 
116     /***
117      * Sets the selected value for the <code>SelectablePanel</code>.
118      * Only one panel in the group may be selected at a time.
119      * 
120      * @param p the <code>SelectablePanel</code>
121      * @param b <code>true</code> if this panel is to be
122      *   selected, otherwise <code>false</code>
123      */
124     public void setSelected( SelectablePanel p, boolean b) {
125         if ( b && p != null && p != selection) {
126             SelectablePanel oldSelection = selection;
127             selection = p;
128 
129             if ( oldSelection != null) {
130                 oldSelection.setSelected(false);
131             }
132             
133             p.setSelected(true);
134         } 
135     }
136 
137     /***
138      * Returns whether a <code>SelectablePanel</code> is selected.
139      * 
140      * @return <code>true</code> if the panel is selected,
141      *   otherwise returns <code>false</code>
142      */
143     public boolean isSelected( SelectablePanel p) {
144         return (p == selection);
145     }
146 
147     /***
148      * Returns the number of panels in the group.
149      * 
150      * @return the number of panels.
151      */
152     public int getPanelCount() {
153 		if (panels == null) {
154 		    return 0;
155 		}
156 		
157         return panels.size();
158     }
159 }