KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > repo > component > UIMultiValueEditor


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License as
5  * published by the Free Software Foundation; either version
6  * 2.1 of the License, or (at your option) any later version.
7  * You may obtain a copy of the License at
8  *
9  * http://www.gnu.org/licenses/lgpl.txt
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the
16  * License.
17  */

18 package org.alfresco.web.ui.repo.component;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.UIInput;
26 import javax.faces.context.FacesContext;
27 import javax.faces.el.ValueBinding;
28 import javax.faces.event.AbortProcessingException;
29 import javax.faces.event.ActionEvent;
30 import javax.faces.event.FacesEvent;
31
32 import org.alfresco.web.app.Application;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /**
37  * This component wraps a standard component to give it multi value capabilities.
38  *
39  * A list of existing values are available, items can be removed from this list
40  * or new items added to the list. To add new items the component dynamically
41  * shows the child component this one wraps.
42  *
43  * @author gavinc
44  */

45 public class UIMultiValueEditor extends UIInput
46 {
47    private static final Log logger = LogFactory.getLog(UIMultiValueEditor.class);
48    private static final String JavaDoc MSG_SELECTED_ITEMS = "selected_items";
49    private static final String JavaDoc MSG_NO_SELECTED_ITEMS = "no_selected_items";
50    private static final String JavaDoc MSG_SELECT_ITEM = "select_an_item";
51    
52    
53    public final static String JavaDoc ACTION_SEPARATOR = ";";
54    public final static int ACTION_NONE = -1;
55    public final static int ACTION_REMOVE = 0;
56    public final static int ACTION_SELECT = 1;
57    public final static int ACTION_ADD = 2;
58    
59    private Boolean JavaDoc addingNewItem = Boolean.FALSE;
60    private Boolean JavaDoc readOnly;
61    private Object JavaDoc lastItemAdded;
62    private String JavaDoc selectItemMsg;
63    private String JavaDoc selectedItemsMsg;
64    private String JavaDoc noSelectedItemsMsg;
65    
66    // ------------------------------------------------------------------------------
67
// Component implementation
68

69    /**
70     * Default constructor
71     */

72    public UIMultiValueEditor()
73    {
74       setRendererType("org.alfresco.faces.List");
75    }
76    
77    /**
78     * @see javax.faces.component.UIComponent#getFamily()
79     */

80    public String JavaDoc getFamily()
81    {
82       return "org.alfresco.faces.MultiValueEditor";
83    }
84    
85    /**
86     * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
87     */

88    public void restoreState(FacesContext context, Object JavaDoc state)
89    {
90       Object JavaDoc values[] = (Object JavaDoc[])state;
91       // standard component attributes are restored by the super class
92
super.restoreState(context, values[0]);
93       this.lastItemAdded = values[1];
94       this.readOnly = (Boolean JavaDoc)values[2];
95       this.addingNewItem = (Boolean JavaDoc)values[3];
96       this.selectItemMsg = (String JavaDoc)values[4];
97       this.selectedItemsMsg = (String JavaDoc)values[5];
98       this.noSelectedItemsMsg = (String JavaDoc)values[6];
99    }
100    
101    /**
102     * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
103     */

104    public Object JavaDoc saveState(FacesContext context)
105    {
106       Object JavaDoc values[] = new Object JavaDoc[7];
107       // standard component attributes are saved by the super class
108
values[0] = super.saveState(context);
109       values[1] = this.lastItemAdded;
110       values[2] = this.readOnly;
111       values[3] = this.addingNewItem;
112       values[4] = this.selectItemMsg;
113       values[5] = this.selectedItemsMsg;
114       values[6] = this.noSelectedItemsMsg;
115       return (values);
116    }
117
118    /**
119     * Returns the last item added by the user
120     *
121     * @return The last item added
122     */

123    public Object JavaDoc getLastItemAdded()
124    {
125       ValueBinding vb = getValueBinding("lastItemAdded");
126       if (vb != null)
127       {
128          this.lastItemAdded = vb.getValue(getFacesContext());
129       }
130       
131       return this.lastItemAdded;
132    }
133
134    /**
135     * Sets the last item to be added by the user
136     *
137     * @param lastItemAdded The last item added
138     */

139    public void setLastItemAdded(Object JavaDoc lastItemAdded)
140    {
141       this.lastItemAdded = lastItemAdded;
142    }
143    
144    /**
145     * Returns the message to display for the selected items, if one hasn't been
146     * set it defaults to the message in the bundle under key 'selected_items'.
147     *
148     * @return The message
149     */

150    public String JavaDoc getSelectedItemsMsg()
151    {
152       ValueBinding vb = getValueBinding("selectedItemsMsg");
153       if (vb != null)
154       {
155          this.selectedItemsMsg = (String JavaDoc)vb.getValue(getFacesContext());
156       }
157       
158       if (this.selectedItemsMsg == null)
159       {
160          this.selectedItemsMsg = Application.getMessage(getFacesContext(), MSG_SELECTED_ITEMS);
161       }
162       
163       return this.selectedItemsMsg;
164    }
165
166    /**
167     * Sets the selected items message to display in the UI
168     *
169     * @param selectedItemsMsg The message
170     */

171    public void setSelectedItemsMsg(String JavaDoc selectedItemsMsg)
172    {
173       this.selectedItemsMsg = selectedItemsMsg;
174    }
175    
176    /**
177     * Returns the message to display when no items have been selected, if one hasn't been
178     * set it defaults to the message in the bundle under key 'no_selected_items'.
179     *
180     * @return The message
181     */

182    public String JavaDoc getNoSelectedItemsMsg()
183    {
184       ValueBinding vb = getValueBinding("noSelectedItemsMsg");
185       if (vb != null)
186       {
187          this.noSelectedItemsMsg = (String JavaDoc)vb.getValue(getFacesContext());
188       }
189       
190       if (this.noSelectedItemsMsg == null)
191       {
192          this.noSelectedItemsMsg = Application.getMessage(getFacesContext(), MSG_NO_SELECTED_ITEMS);
193       }
194       
195       return this.noSelectedItemsMsg;
196    }
197
198    /**
199     * Sets the no selected items message to display in the UI
200     *
201     * @param noSelectedItemsMsg The message
202     */

203    public void setNoSelectedItemsMsg(String JavaDoc noSelectedItemsMsg)
204    {
205       this.noSelectedItemsMsg = noSelectedItemsMsg;
206    }
207
208    /**
209     * Returns the message to display for select an item, if one hasn't been
210     * set it defaults to the message in the bundle under key 'select_an_item'.
211     *
212     * @return The message
213     */

214    public String JavaDoc getSelectItemMsg()
215    {
216       ValueBinding vb = getValueBinding("selectItemMsg");
217       if (vb != null)
218       {
219          this.selectItemMsg = (String JavaDoc)vb.getValue(getFacesContext());
220       }
221       
222       if (this.selectItemMsg == null)
223       {
224          this.selectItemMsg = Application.getMessage(getFacesContext(), MSG_SELECT_ITEM);
225       }
226       
227       return this.selectItemMsg;
228    }
229
230    /**
231     * Sets the select an item message to display in the UI
232     *
233     * @param selectedItemsMsg The message
234     */

235    public void setSelectItemMsg(String JavaDoc selectItemMsg)
236    {
237       this.selectItemMsg = selectItemMsg;
238    }
239
240    /**
241     * Determines whether the component is in read only mode
242     *
243     * @return true if the component is in read only mode
244     */

245    public boolean getReadOnly()
246    {
247       ValueBinding vb = getValueBinding("readOnly");
248       if (vb != null)
249       {
250          this.readOnly = (Boolean JavaDoc)vb.getValue(getFacesContext());
251       }
252       
253       if (this.readOnly == null)
254       {
255          this.readOnly = Boolean.FALSE;
256       }
257       
258       return this.readOnly.booleanValue();
259    }
260
261    /**
262     * Sets the read only mode for the component
263     *
264     * @param readOnly true to set read only mode
265     */

266    public void setReadOnly(boolean readOnly)
267    {
268       this.readOnly = Boolean.valueOf(readOnly);
269    }
270    
271    /**
272     * Determines whether the component is adding a new item
273     *
274     * @return true if we are adding a new item
275     */

276    public boolean getAddingNewItem()
277    {
278       return this.addingNewItem.booleanValue();
279    }
280
281    /**
282     * @see javax.faces.component.UIComponent#broadcast(javax.faces.event.FacesEvent)
283     */

284    public void broadcast(FacesEvent event) throws AbortProcessingException
285    {
286       if (event instanceof MultiValueEditorEvent)
287       {
288          MultiValueEditorEvent assocEvent = (MultiValueEditorEvent)event;
289          List JavaDoc items = (List JavaDoc)getValue();
290          
291          switch (assocEvent.Action)
292          {
293             case ACTION_SELECT:
294             {
295                this.addingNewItem = Boolean.TRUE;
296                break;
297             }
298             case ACTION_ADD:
299             {
300                if (items == null)
301                {
302                   items = new ArrayList JavaDoc();
303                   setSubmittedValue(items);
304                }
305                
306                items.add(getLastItemAdded());
307                this.addingNewItem = Boolean.FALSE;
308                
309                // get hold of the value binding for the lastItemAdded property
310
// and set it to null to show it's been added to the list
311
ValueBinding vb = getValueBinding("lastItemAdded");
312                if (vb != null)
313                {
314                   vb.setValue(FacesContext.getCurrentInstance(), null);
315                }
316                
317                break;
318             }
319             case ACTION_REMOVE:
320             {
321                items.remove(assocEvent.RemoveIndex);
322                break;
323             }
324          }
325       }
326       else
327       {
328          super.broadcast(event);
329       }
330    }
331    
332    
333    /**
334     * @see javax.faces.component.UIComponent#getRendersChildren()
335     */

336    public boolean getRendersChildren()
337    {
338       // only show the wrapped component when the add button has been clicked
339

340       return !this.addingNewItem.booleanValue();
341    }
342    
343    // ------------------------------------------------------------------------------
344
// Inner classes
345

346    /**
347     * Class representing an action relevant to the ChildAssociationEditor component.
348     */

349    public static class MultiValueEditorEvent extends ActionEvent
350    {
351       public int Action;
352       public int RemoveIndex;
353       
354       public MultiValueEditorEvent(UIComponent component, int action, int removeIndex)
355       {
356          super(component);
357          this.Action = action;
358          this.RemoveIndex = removeIndex;
359       }
360    }
361 }
362
Popular Tags