View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.wcm.webui.selector;
18  
19  import org.exoplatform.ecm.webui.selector.ComponentSelector;
20  import org.exoplatform.ecm.webui.selector.UISelectable;
21  import org.exoplatform.webui.config.annotation.ComponentConfig;
22  import org.exoplatform.webui.config.annotation.EventConfig;
23  import org.exoplatform.webui.core.UIComponent;
24  import org.exoplatform.webui.core.UIContainer;
25  import org.exoplatform.webui.core.UIPopupWindow;
26  import org.exoplatform.webui.core.lifecycle.UIContainerLifecycle;
27  import org.exoplatform.webui.event.Event;
28  import org.exoplatform.webui.event.EventListener;
29  import org.exoplatform.webui.organization.account.UIUserSelector;
30  
31  /**
32   * Created by The eXo Platform SAS
33   * Author : eXoPlatform
34   * chuong.phan@exoplatform.com, phan.le.thanh.chuong@gmail.com
35   * June 10, 2009
36   */
37  @ComponentConfig(
38      lifecycle = UIContainerLifecycle.class,
39      events = {@EventConfig(listeners = UIUserMemberSelector.AddUserActionListener.class)}
40  )
41  public class UIUserMemberSelector extends UIContainer implements ComponentSelector  {
42  
43    /** The ui component. */
44    private UIComponent uiComponent;
45  
46    /** The return field. */
47    private String returnField;
48  
49    /** The is use popup. */
50    private boolean isUsePopup = true;
51  
52    /** The is multi. */
53    private boolean isMulti = true;
54  
55    /** The is show search user. */
56    private boolean isShowSearchUser = true;
57  
58    /** The is show search. */
59    private boolean isShowSearch;
60  
61    /**
62     * Instantiates a new uIWCM user container.
63     */
64    public UIUserMemberSelector() {}
65  
66    /**
67     * Inits the.
68     *
69     * @throws Exception the exception
70     */
71    public void init() throws Exception {
72      UIUserSelector uiUserSelector = getChild(UIUserSelector.class);
73      if (uiUserSelector == null) {
74        uiUserSelector = addChild(UIUserSelector.class, null, null);
75      }
76      uiUserSelector.setMulti(isMulti);
77      uiUserSelector.setShowSearchUser(isShowSearchUser);
78      uiUserSelector.setShowSearch(isShowSearch);
79    }
80  
81    /**
82     * Checks if is use popup.
83     *
84     * @return true, if is use popup
85     */
86    public boolean isUsePopup() {
87      return isUsePopup;
88    }
89  
90    /**
91     * Sets the use popup.
92     *
93     * @param isUsePopup the new use popup
94     */
95    public void setUsePopup(boolean isUsePopup) {
96      this.isUsePopup = isUsePopup;
97    }
98  
99    /**
100    * Checks if is multi.
101    *
102    * @return true, if is multi
103    */
104   public boolean isMulti() {
105     return isMulti;
106   }
107 
108   /**
109    * Sets the multi.
110    *
111    * @param isMulti the new multi
112    */
113   public void setMulti(boolean isMulti) {
114     this.isMulti = isMulti;
115   }
116 
117   /**
118    * Checks if is show search user.
119    *
120    * @return true, if is show search user
121    */
122   public boolean isShowSearchUser() {
123     return isShowSearchUser;
124   }
125 
126   /**
127    * Sets the show search user.
128    *
129    * @param isShowSearchUser the new show search user
130    */
131   public void setShowSearchUser(boolean isShowSearchUser) {
132     this.isShowSearchUser = isShowSearchUser;
133   }
134 
135   /**
136    * Checks if is show search.
137    *
138    * @return true, if is show search
139    */
140   public boolean isShowSearch() {
141     return isShowSearch;
142   }
143 
144   /**
145    * Sets the show search.
146    *
147    * @param isShowSearch the new show search
148    */
149   public void setShowSearch(boolean isShowSearch) {
150     this.isShowSearch = isShowSearch;
151   }
152 
153   /* (non-Javadoc)
154    * @see org.exoplatform.ecm.webui.selector.ComponentSelector#getSourceComponent()
155    */
156   public UIComponent getSourceComponent() {
157     return uiComponent;
158   }
159 
160   /**
161    * Gets the return field.
162    *
163    * @return the return field
164    */
165   public String getReturnField() {
166     return returnField;
167   }
168 
169   /*
170    * (non-Javadoc)
171    * @see
172    * org.exoplatform.ecm.webui.selector.ComponentSelector#setSourceComponent
173    * (org.exoplatform.webui.core.UIComponent, java.lang.String[])
174    */
175   public void setSourceComponent(UIComponent uicomponent, String[] initParams) {
176     uiComponent = uicomponent;
177     if (initParams == null || initParams.length == 0)
178       return;
179     for (int i = 0; i < initParams.length; i++) {
180       if (initParams[i].indexOf("returnField") > -1) {
181         String[] array = initParams[i].split("=");
182         returnField = array[1];
183         break;
184       }
185       returnField = initParams[0];
186     }
187   }
188 
189   /**
190    * The listener interface for receiving addUserAction events.
191    * The class that is interested in processing a addUserAction
192    * event implements this interface, and the object created
193    * with that class is registered with a component using the
194    * component's <code>addAddUserActionListener</code> method. When
195    * the addUserAction event occurs, that object's appropriate
196    * method is invoked.
197    */
198   static  public class AddUserActionListener extends EventListener<UIUserMemberSelector> {
199 
200     /* (non-Javadoc)
201      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
202      */
203     public void execute(Event<UIUserMemberSelector> event) throws Exception {
204       UIUserMemberSelector userMemberSelector = event.getSource();
205       UIUserSelector userSelector = userMemberSelector.getChild(UIUserSelector.class);
206       String returnField = userMemberSelector.getReturnField();
207       ((UISelectable)userMemberSelector.getSourceComponent()).doSelect(returnField, userSelector.getSelectedUsers());
208       if (userMemberSelector.isUsePopup) {
209         UIPopupWindow uiPopup = userMemberSelector.getParent();
210         uiPopup.setShow(false);
211         UIComponent uicomp = userMemberSelector.getSourceComponent().getParent();
212         event.getRequestContext().addUIComponentToUpdateByAjax(uicomp);
213         if (!uiPopup.getId().equals("PopupComponent"))
214           event.getRequestContext().addUIComponentToUpdateByAjax(uiPopup);
215       } else {
216         event.getRequestContext().addUIComponentToUpdateByAjax(
217             userMemberSelector.getSourceComponent());
218       }
219     }
220   }
221 }