KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > components > expandableTable > SalesGroupRecordBean


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
34 package com.icesoft.icefaces.samples.showcase.components.expandableTable;
35
36
37 import com.icesoft.icefaces.samples.showcase.util.StyleBean;
38
39 import javax.faces.event.ActionEvent;
40 import java.util.ArrayList JavaDoc;
41
42 /**
43  * <p>The <code>SalesGroupRecordBean</code> class is responsible for storing
44  * view specific information for the salesGroupRecord Bean. Such things as
45  * expand/contract images and css attributes are stored in this Bean. Model can
46  * be found in the sub class <code>SalesGroupRecord</code>. </p>
47  * <p/>
48  * <p>This class is also responsible for handling all events that control how
49  * the view (jspx) page behaves.</p>
50  */

51 public class SalesGroupRecordBean extends SalesGroupRecord {
52     protected static final String JavaDoc DEFAULT_IMAGE_DIR =
53             "./xmlhttp/css/xp/css-images/";
54     protected static final String JavaDoc SPACER_IMAGE =
55             "tree_line_blank.gif";
56
57     // style for column that holds expand/contract image toggle, in the sales
58
// record row.
59
protected String JavaDoc indentStyleClass = "";
60
61     // style for all other columns in the sales record row.
62
protected String JavaDoc rowStyleClass = "";
63     
64     protected StyleBean styleBean;
65     
66     // Images used to represent expand/contract, spacer by default
67
protected String JavaDoc expandImage; // + or >
68
protected String JavaDoc contractImage; // - or v
69

70     // callback to list which contains all data in the dataTable. This callback
71
// is needed so that a node can be set in the expanded state at construction time.
72
protected ArrayList JavaDoc parentInventoryList;
73
74     // indicates if node is in expanded state.
75
protected boolean isExpanded;
76
77     /**
78      * <p>Creates a new <code>SalesGroupRecordBean</code>. This constructor
79      * should be use when creating SalesGroupRecordBeans which will contain
80      * children</p>
81      *
82      * @param isExpanded true, indicates that the specified node will be
83      * expanded by default; otherwise, false.
84      */

85     public SalesGroupRecordBean(String JavaDoc indentStyleClass,
86                                 String JavaDoc rowStyleClass,
87                                 StyleBean styleBean,
88                                 String JavaDoc expandImage,
89                                 String JavaDoc contractImage,
90                                 ArrayList JavaDoc parentInventoryList,
91                                 boolean isExpanded) {
92
93         this.indentStyleClass = indentStyleClass;
94         this.rowStyleClass = rowStyleClass;
95         this.styleBean = styleBean;
96         this.expandImage = expandImage;
97         this.contractImage = contractImage;
98         this.parentInventoryList = parentInventoryList;
99         this.parentInventoryList.add(this);
100         this.isExpanded = isExpanded;
101         // update the default state of the node.
102
if (this.isExpanded) {
103             expandNodeAction();
104         }
105     }
106
107     /**
108      * <p>Creates a new <code>SalesGroupRecordBean</code>. This constructor
109      * should be used when creating a SalesGroupRecordBean which will be a child
110      * of some other SalesGroupRecordBean.</p>
111      * <p/>
112      * <p>The created SalesGroupRecordBean has no image states defined.</p>
113      *
114      * @param indentStyleClass
115      * @param rowStyleClass
116      */

117     public SalesGroupRecordBean(String JavaDoc indentStyleClass,
118                                 String JavaDoc rowStyleClass) {
119
120         this.indentStyleClass = indentStyleClass;
121         this.rowStyleClass = rowStyleClass;
122     }
123
124     /**
125      * Gets the renderable state of the contract/expand image toggle.
126      *
127      * @return true if images should be drawn; otherwise, false.
128      */

129     public boolean isRenderImage() {
130         return childSalesRecords != null && childSalesRecords.size() > 0;
131     }
132
133     /**
134      * Toggles the expanded state of this SalesGroup Record.
135      *
136      * @param event
137      */

138     public void toggleSubGroupAction(ActionEvent event) {
139         // toggle expanded state
140
isExpanded = !isExpanded;
141
142         // add sub elements to list
143
if (isExpanded) {
144             expandNodeAction();
145         }
146         // remove items from list
147
else {
148             contractNodeAction();
149         }
150     }
151
152     /**
153      * Adds a child sales record to this sales group.
154      *
155      * @param salesGroupRecord child sales record to add to this record.
156      */

157     public void addChildSalesGroupRecord(
158             SalesGroupRecordBean salesGroupRecord) {
159         if (this.childSalesRecords != null && salesGroupRecord != null) {
160             this.childSalesRecords.add(salesGroupRecord);
161             if (isExpanded) {
162                 // to keep elements in order, remove all
163
contractNodeAction();
164                 // then add them again.
165
expandNodeAction();
166             }
167         }
168     }
169
170     /**
171      * Removes the specified child sales record from this sales group.
172      *
173      * @param salesGroupRecord child sales record to remove.
174      */

175     public void removeChildSalesGroupRecord(
176             SalesGroupRecordBean salesGroupRecord) {
177         if (this.childSalesRecords != null && salesGroupRecord != null) {
178             if (isExpanded) {
179                 // remove all, make sure we are removing the specified one too.
180
contractNodeAction();
181             }
182             // remove the current node
183
this.childSalesRecords.remove(salesGroupRecord);
184             // update the list if needed.
185
if (isExpanded) {
186                 // to keep elements in order, remove all
187
contractNodeAction();
188                 // then add them again.
189
expandNodeAction();
190             }
191         }
192     }
193
194     /**
195      * Utility method to add all child nodes to the parent dataTable list.
196      */

197     private void expandNodeAction() {
198         if (childSalesRecords != null && childSalesRecords.size() > 0) {
199             // get index of current node
200
int index = parentInventoryList.indexOf(this);
201
202             // add all items in childSalesRecords to the parent list
203
parentInventoryList.addAll(index + 1, childSalesRecords);
204         }
205
206     }
207
208     /**
209      * Utility method to remove all child nodes from the parent dataTable list.
210      */

211     private void contractNodeAction() {
212         if (childSalesRecords != null && childSalesRecords.size() > 0) {
213             // add all items in childSalesRecords to the parent list
214
parentInventoryList.removeAll(childSalesRecords);
215         }
216     }
217
218     /**
219      * Gets the style class name used to define the first column of a sales
220      * record row. This first column is where a expand/contract image is
221      * placed.
222      *
223      * @return indent style class as defined in css file
224      */

225     public String JavaDoc getIndentStyleClass() {
226         return indentStyleClass;
227     }
228
229     /**
230      * Gets the style class name used to define all other columns in the sales
231      * record row, except the first column.
232      *
233      * @return style class as defined in css file
234      */

235     public String JavaDoc getRowStyleClass() {
236         return rowStyleClass;
237     }
238
239     /**
240      * Gets the image which will represent either the expanded or contracted
241      * state of the <code>SalesGroupRecordBean</code>.
242      *
243      * @return name of image to draw
244      */

245     public String JavaDoc getExpandContractImage() {
246         if(styleBean != null) {
247             String JavaDoc dir = styleBean.getImageDirectory();
248             String JavaDoc img = isExpanded ? contractImage : expandImage;
249             return dir + img;
250         }
251         return DEFAULT_IMAGE_DIR + SPACER_IMAGE;
252     }
253 }
Popular Tags