KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
8 import javax.faces.event.PhaseId;
9 import org.exoplatform.faces.core.Util;
10 import org.exoplatform.faces.core.component.*;
11 import org.exoplatform.faces.core.component.model.*;
12 import org.exoplatform.faces.core.event.ExoActionEvent;
13 import org.exoplatform.faces.core.event.ExoActionListener;
14 import org.exoplatform.services.communication.message.Account;
15 import org.exoplatform.services.communication.message.MessageService;
16 /**
17  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
18  * @since Aug 27, 2004
19  * @version $Id: UIAccountForm.java,v 1.3 2004/10/14 23:40:27 tuan08 Exp $
20  */

21 public class UIAccountForm extends UISimpleForm {
22   final static public String JavaDoc CHANGE_PROTOCOL_ACTION =
23     Util.encodeActionPhase("changeProtocol", PhaseId.APPLY_REQUEST_VALUES) ;
24   static private List PROTOCOL_OPTIONS ;
25   static {
26     PROTOCOL_OPTIONS = new ArrayList() ;
27     PROTOCOL_OPTIONS .add(new SelectItem(MessageService.STANDALONE_PROTOCOL, MessageService.STANDALONE_PROTOCOL));
28     PROTOCOL_OPTIONS .add(new SelectItem(MessageService.IMAP_PROTOCOL, MessageService.IMAP_PROTOCOL));
29     PROTOCOL_OPTIONS .add(new SelectItem(MessageService.POP3_PROTOCOL, MessageService.POP3_PROTOCOL));
30   }
31   private MessageService service_ ;
32   private Account account_ ;
33   
34   private UIStringInput accountNameInput_;
35   private UIStringInput ownerNameInput_;
36   private UIStringInput replyToAddressInput_;
37   private UISelectBox protocolInput_;
38   private UIStringInput userNameInput_;
39   private UIStringInput passwordInput_;
40   private UIStringInput mailServerInput_ ;
41   private UITextArea signatureInput_;
42   
43   private Row userNameRow_ ;
44   private Row passwordRow_ ;
45   private Row mailServerRow_ ;
46   
47   public UIAccountForm(MessageService service) throws Exception JavaDoc {
48     super("accountForm", "post", null) ;
49     setId("UIAccountForm") ;
50     setClazz("UIAccountForm");
51     service_ = service ;
52     
53     accountNameInput_ = new UIStringInput("accountName", "") ;
54     ownerNameInput_ = new UIStringInput("ownerName", "") ;
55     replyToAddressInput_ = new UIStringInput("replyTo", "") ;
56     protocolInput_ = new UISelectBox("protocol", "", PROTOCOL_OPTIONS);
57     protocolInput_.setUpdateOnChangeAction(CHANGE_PROTOCOL_ACTION) ;
58     userNameInput_ = new UIStringInput("userName", "") ;
59     passwordInput_ = new UIStringInput("password", "") ;
60     mailServerInput_ = new UIStringInput("mailServer", "") ;
61     signatureInput_ = new UITextArea("signature", "") ;
62     
63     add(new HeaderRow().
64         add(new Cell("#{UIAccountForm.header.add-edit-account}").
65             addColspan("2")));
66     add(new Row().
67         add(new LabelCell("#{UIAccountForm.label.account-name}")).
68         add(new ComponentCell(this, accountNameInput_)));
69     add(new Row().
70         add(new LabelCell("#{UIAccountForm.label.owner-name}")).
71         add(new ComponentCell(this, ownerNameInput_)));
72     add(new Row().
73         add(new LabelCell("#{UIAccountForm.label.reply-to}")).
74         add(new ComponentCell(this, replyToAddressInput_)));
75     add(new Row().
76         add(new LabelCell("#{UIAccountForm.label.message-protocol}")).
77         add(new ComponentCell(this, protocolInput_)));
78     userNameRow_ = new Row().
79                    add(new LabelCell("#{UIAccountForm.label.user-name}")).
80                    add(new ComponentCell(this, userNameInput_)) ;
81     add(userNameRow_);
82     passwordRow_ = new Row().
83                   add(new LabelCell("#{UIAccountForm.label.password}")).
84                   add(new ComponentCell(this, passwordInput_)) ;
85     add(passwordRow_);
86     mailServerRow_ = new Row().
87                      add(new LabelCell("#{UIAccountForm.label.mail-server}")).
88                      add(new ComponentCell(this, mailServerInput_));
89     add(mailServerRow_);
90     add(new Row().
91         add(new LabelCell("#{UIAccountForm.label.signature}")).
92         add(new ComponentCell(this, signatureInput_)));
93     add(new Row().
94         add(new ListComponentCell().
95             add(new FormButton("#{UIAccountForm.button.save}", SAVE_ACTION)).
96             add(new FormButton("#{UIAccountForm.button.cancel}", CANCEL_ACTION)).
97             addColspan("2").addAlign("center"))) ;
98     addActionListener(SaveActionListener.class, SAVE_ACTION) ;
99     addActionListener(CancelActionListener.class, CANCEL_ACTION) ;
100     addActionListener(ChangeProtocolActionListener.class, CHANGE_PROTOCOL_ACTION) ;
101   }
102   
103   public void changeProtocol(String JavaDoc protocol) {
104     if(protocol == null || MessageService.STANDALONE_PROTOCOL.equals(protocol)) {
105       userNameRow_.setVisible(false) ;
106       passwordRow_.setVisible(false) ;
107       mailServerRow_.setVisible(false) ;
108     } else {
109       userNameRow_.setVisible(true) ;
110       passwordRow_.setVisible(true) ;
111       mailServerRow_.setVisible(true) ;
112     }
113   }
114   
115   public void setAccount(Account account) {
116     account_ = account ;
117     if(account == null) {
118       accountNameInput_.setValue("") ;
119       accountNameInput_.setEditable(true) ;
120       ownerNameInput_.setValue("") ;
121       replyToAddressInput_.setValue("") ;
122       userNameInput_.setValue("") ;
123       passwordInput_.setValue("") ;
124       mailServerInput_.setValue("") ;
125       protocolInput_.setValue(MessageService.STANDALONE_PROTOCOL) ;
126       changeProtocol(MessageService.STANDALONE_PROTOCOL) ;
127     } else {
128       accountNameInput_.setValue(account.getAccountName()) ;
129       accountNameInput_.setEditable(false) ;
130       ownerNameInput_.setValue(account.getOwnerName()) ;
131       replyToAddressInput_.setValue(account.getReplyToAddress()) ;
132       userNameInput_.setValue(account.getProperty(Account.SERVER_SETTING_USERNAME)) ;
133       passwordInput_.setValue(account.getProperty(Account.SERVER_SETTING_PASSWORD)) ;
134       mailServerInput_.setValue(account.getProperty(Account.SERVER_SETTING_HOSTNAME)) ;
135       protocolInput_.setValue(account.getProtocol()) ;
136       changeProtocol(account.getProtocol()) ;
137     }
138   }
139    
140   static public class SaveActionListener extends ExoActionListener {
141     public void execute(ExoActionEvent event) throws Exception JavaDoc {
142       UIAccountForm uiForm = (UIAccountForm) event.getSource();
143       UIAccountConfiguration uiAccConfig =
144         (UIAccountConfiguration) uiForm.getSibling(UIAccountConfiguration.class) ;
145       Account account = uiForm.account_ ;
146       boolean newAccount = false ;
147       if (account == null) {
148         account = uiForm.service_.createAccountInstance() ;
149         account.setAccountName(uiForm.accountNameInput_.getValue()) ;
150         account.setOwner(uiAccConfig.getAccountOwner()) ;
151         newAccount = true ;
152       }
153       account.setOwnerName(uiForm.ownerNameInput_.getValue()) ;
154       account.setReplyToAddress(uiForm.replyToAddressInput_.getValue()) ;
155       account.setProtocol(uiForm.protocolInput_.getValue()) ;
156       account.setSignature(uiForm.signatureInput_.getValue()) ;
157       
158       account.setProperty(Account.SERVER_SETTING_USERNAME,
159                           uiForm.userNameInput_.getValue()) ;
160       account.setProperty(Account.SERVER_SETTING_PASSWORD,
161                           uiForm.passwordInput_.getValue()) ;
162       account.setProperty(Account.SERVER_SETTING_HOSTNAME,
163                           uiForm.mailServerInput_.getValue()) ;
164       if(newAccount) {
165         uiForm.service_.createAccount(account) ;
166       } else {
167         uiForm.service_.updateAccount(account) ;
168       }
169       uiAccConfig.updateAccountList() ;
170       uiForm.setRenderedSibling(UIAccountConfiguration.class) ;
171     }
172   }
173   
174   static public class CancelActionListener extends ExoActionListener {
175     public void execute(ExoActionEvent event) throws Exception JavaDoc {
176       UIAccountForm uiForm = (UIAccountForm) event.getSource();
177       uiForm.setRenderedSibling(UIAccountConfiguration.class) ;
178     }
179   }
180   
181   static public class ChangeProtocolActionListener extends ExoActionListener {
182     public void execute(ExoActionEvent event) throws Exception JavaDoc {
183       UIAccountForm uiForm = (UIAccountForm) event.getSource();
184       uiForm.changeProtocol(uiForm.protocolInput_.getValue()) ;
185     }
186   }
187 }
Popular Tags