KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > communication > message > component > UISelectAccount


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlets.communication.message.component;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9 import org.exoplatform.container.SessionContainer;
10 import org.exoplatform.faces.core.component.UISelectBox;
11 import org.exoplatform.faces.core.component.UISimpleForm;
12 import org.exoplatform.faces.core.component.model.ComponentCell;
13 import org.exoplatform.faces.core.component.model.Row;
14 import org.exoplatform.faces.core.component.model.SelectItem;
15 import org.exoplatform.faces.core.event.ExoActionEvent;
16 import org.exoplatform.faces.core.event.ExoActionListener;
17 import org.exoplatform.services.communication.message.Account;
18 import org.exoplatform.services.communication.message.MessageService;
19 /**
20  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
21  * @since Aug 27, 2004
22  * @version $Id: UISelectAccount.java,v 1.5 2004/09/28 18:19:22 tuan08 Exp $
23  */

24 public class UISelectAccount extends UISimpleForm {
25   private UISelectBox accountInput_;
26   private Account selectAccount_ ;
27   private List JavaDoc accounts_ ;
28   
29   public UISelectAccount(MessageService service) throws Exception JavaDoc {
30     super("selectAccountForm", "post", null) ;
31     setId("UISelectAccount") ;
32     setClazz("UISelectAccount");
33     String JavaDoc userName = SessionContainer.getInstance().getOwner();
34     accounts_ = service.getAccounts(userName) ;
35     if(accounts_.size() > 0) selectAccount_ = (Account) accounts_.get(0) ;
36     List JavaDoc options = new ArrayList JavaDoc() ;
37     options.add(new SelectItem("Select an account", ""));
38     for(int i = 0; i < accounts_.size(); i++) {
39       Account account = (Account) accounts_.get(i);
40       String JavaDoc name = account.getAccountName() ;
41       options.add(new SelectItem(name, name));
42     }
43     accountInput_ = new UISelectBox("account", "", options);
44     accountInput_.setUpdateOnChangeAction("changeAccount");
45     add(new Row().
46         add(new ComponentCell(this, accountInput_)));
47     addActionListener(ChangeAccountActionListener.class, "changeAccount") ;
48   }
49   
50   public Account getSelectAccount() { return selectAccount_ ; }
51   
52   static public class ChangeAccountActionListener extends ExoActionListener {
53     public void execute(ExoActionEvent event) throws Exception JavaDoc {
54       UISelectAccount uiSelectAccount = (UISelectAccount) event.getSource() ;
55       String JavaDoc accountName = uiSelectAccount.accountInput_.getValue() ;
56       List JavaDoc accounts = uiSelectAccount.accounts_ ;
57       for (int i = 0; i < accounts.size(); i++) {
58         Account account = (Account) accounts.get(i) ;
59         if(account.getAccountName().equals(accountName)) {
60           uiSelectAccount.selectAccount_ = account ;
61           UIAccount uiAccount = (UIAccount) uiSelectAccount.getSibling(UIAccount.class) ;
62           uiAccount.setAccount(account) ;
63           break ;
64         }
65       }
66       uiSelectAccount.accountInput_.setValue("") ;
67     }
68   }
69 }
Popular Tags