KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > layoutPanels > tabSetPanel > DynamicTabSetBean


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33 package com.icesoft.icefaces.samples.showcase.layoutPanels.tabSetPanel;
34
35 import com.icesoft.faces.component.paneltabset.PanelTabSet;
36 import java.util.ArrayList JavaDoc;
37 import java.util.List JavaDoc;
38 import javax.faces.event.ActionEvent;
39 import javax.faces.model.SelectItem;
40
41 /**
42  * The DynamicTabSetBean is the backing bean for the TabbedPane showcase
43  * demonstration. It is used to dynamically add and remove tabs used in
44  * conjunction with the ice:panelTabSet component.
45  *
46  * @since 0.3.0
47  */

48 public class DynamicTabSetBean{
49     
50     private int tabIndex;
51     private String JavaDoc newTabLabel;
52     private String JavaDoc newTabContent;
53     private List JavaDoc tabs = new ArrayList JavaDoc();
54     private String JavaDoc removedTab;
55     private List JavaDoc tabItems = new ArrayList JavaDoc();
56     private PanelTabSet dynamicTabSet;
57         
58     public DynamicTabSetBean() {
59         
60         //pre-defined two tabs into the panelTabSet
61
Tab newTab1 = new Tab();
62         newTab1.setLabel("Label1");
63         newTab1.setContent("Content1");
64         newTab1.setIndex(tabIndex++);
65
66         Tab newTab2 = new Tab();
67         newTab2.setLabel("Label2");
68         newTab2.setContent("Content2");
69         newTab2.setIndex(tabIndex++);
70
71         tabItems.add(
72                 new SelectItem(Integer.toString(newTab1.index), newTab1.label));
73         tabItems.add(
74                 new SelectItem(Integer.toString(newTab2.index), newTab2.label));
75
76         tabs.add(newTab1);
77         tabs.add(newTab2);
78     }
79
80     /**
81      * remove a tab from panelTabSet.
82      *
83      * @param event remove button click.
84      */

85     public void removeTab(ActionEvent event) {
86         int selectedIndex = dynamicTabSet.getSelectedIndex();
87         //remove from tabs
88
for (int i = 0; i < tabs.size(); i++) {
89             if (((Tab) tabs.get(i)).getIndex() ==
90                 Integer.parseInt(removedTab)) {
91                 tabs.remove(i);
92                 if (selectedIndex > i) {
93                     dynamicTabSet.setSelectedIndex((selectedIndex > 0)?
94                         (selectedIndex -1) : selectedIndex);
95                 } else if (tabs.size() ==1) {
96                     dynamicTabSet.setSelectedIndex(0);
97                 }
98                 break;
99             }
100         }
101
102         //remove select option from selectRadiobox
103
for (int i = 0; i < tabItems.size(); i++) {
104             if ((((SelectItem) tabItems.get(i)).getValue())
105                     .equals(removedTab)) {
106                 tabItems.remove(i);
107                 break;
108             }
109         }
110     }
111     
112     /**
113      * add a new tab to the panelTabSet.
114      *
115      * @param event add button click.
116      */

117     public void addTab(ActionEvent event) {
118
119         //assign default label if it's blank
120
if (newTabLabel.equals("")) {
121             newTabLabel = "Tab " + (tabIndex + 1);
122         }
123
124         //set the new tab from the input
125
Tab newTab = new Tab();
126         newTab.setContent(newTabContent);
127         newTab.setLabel(newTabLabel);
128         newTab.setIndex(tabIndex++);
129
130         //add to both tabs and select options of selectRadiobox
131
tabs.add(newTab);
132         tabItems.add(
133                 new SelectItem(Integer.toString(newTab.index), newTabLabel));
134
135         //clean up input field
136
newTabLabel = "";
137         newTabContent = "";
138     }
139     
140     /**
141      * Gets the label of a new tab.
142      *
143      * @return newTabLabel current tab label.
144      */

145     public String JavaDoc getNewTabLabel() {
146         return newTabLabel;
147     }
148     
149     /**
150      * Sets the label of a new tab.
151      *
152      * @param newTabLabel label of the new tab.
153      */

154     public void setNewTabLabel(String JavaDoc newTabLabel) {
155         this.newTabLabel = newTabLabel;
156     }
157     
158     /**
159      * Gets the content of a new tab.
160      *
161      * @return newTabContent of a new tab.
162      */

163     public String JavaDoc getNewTabContent() {
164         return newTabContent;
165     }
166     
167     /**
168      * Sets the content of a new tab.
169      *
170      * @param newTabContent of a new tab.
171      */

172     public void setNewTabContent(String JavaDoc newTabContent) {
173         this.newTabContent = newTabContent;
174     }
175     
176     /**
177      * Gets the removed tab.
178      *
179      * @return removedTab label of the removed tab.
180      */

181     public String JavaDoc getRemovedTab() {
182         return removedTab;
183     }
184     
185     /**
186      * Sets the removed tab.
187      *
188      * @param removedTab label of tab to be removed.
189      */

190     public void setRemovedTab(String JavaDoc removedTab) {
191         this.removedTab = removedTab;
192     }
193
194     /**
195      * Gets the list of tab items.
196      *
197      * @return tabItems list of tabs.
198      */

199     public List JavaDoc getTabItems() {
200         return tabItems;
201     }
202     
203     /**
204      * Sets the list of tab items.
205      *
206      * @param tabItems list of tabs.
207      */

208     public void setTabItems(List JavaDoc tabItems) {
209         this.tabItems = tabItems;
210     }
211     
212     /**
213      * Gets the list of tabs.
214      *
215      * @return tabs list of tabs.
216      */

217     public List JavaDoc getTabs() {
218         return tabs;
219     }
220
221     /**
222      * Sets the list of tabs.
223      *
224      * @param tabs list of tabs.
225      */

226     public void setTabs(List JavaDoc tabs) {
227         this.tabs = tabs;
228     }
229     
230     /**
231      * Gets the dynamicTabSet PanelTabSet object.
232      *
233      * @return dynamicTabSet PanelTabSet object.
234      */

235     public PanelTabSet getDynamicTabSet() {
236         return dynamicTabSet;
237     }
238     
239     /**
240      * Sets the dynamicTabSet PanelTabSet object.
241      *
242      * @param dynamicTabSet PanelTabSet object.
243      */

244     public void setDynamicTabSet(PanelTabSet dynamicTabSet) {
245         this.dynamicTabSet = dynamicTabSet;
246     }
247     
248     /**
249      * Inner class that represents a tab object with a label, content, and an
250      * index.
251      */

252     public class Tab {
253         String JavaDoc label;
254         String JavaDoc content;
255         int index;
256         
257         /**
258          * Gets the content of the tab.
259          *
260          * @return content of the tab.
261          */

262         public String JavaDoc getContent() {
263             return content;
264         }
265         
266         /**
267          * Sets the content of the tab.
268          *
269          * @param content of the tab.
270          */

271         public void setContent(String JavaDoc content) {
272             this.content = content;
273         }
274
275         /**
276          * Gets the label of the tab.
277          *
278          * @return label of the tab.
279          */

280         public String JavaDoc getLabel() {
281             return label;
282         }
283
284         /**
285          * Sets the label of the tab.
286          *
287          * @param label of the tab.
288          */

289         public void setLabel(String JavaDoc label) {
290             this.label = label;
291         }
292
293         /**
294          * Gets the index of the tab.
295          *
296          * @return index of the tab.
297          */

298         public int getIndex() {
299             return index;
300         }
301         
302         /**
303          * Sets the index of the tab.
304          *
305          * @param index of the tab.
306          */

307         public void setIndex(int index) {
308             this.index = index;
309         }
310
311     }
312     
313 }
314
Popular Tags