KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.exoplatform.faces.core.component.*;
9 import org.exoplatform.faces.core.component.model.*;
10 import org.exoplatform.faces.core.event.ExoActionEvent;
11 import org.exoplatform.faces.core.event.ExoActionListener;
12 import org.exoplatform.services.communication.message.*;
13 import org.exoplatform.services.communication.message.Message;
14 /**
15  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
16  * @since Aug 27, 2004
17  * @version $Id: UIMessageForm.java,v 1.6 2004/10/23 01:58:56 tuan08 Exp $
18  */

19 public class UIMessageForm extends UISimpleForm {
20   public static String JavaDoc SEND_MESSAGE_ACTION = "sendMessage" ;
21   private MessageService service_ ;
22   private Account account_ ;
23   private Message messageToReply_ ;
24   private Message message_ ;
25   
26   private UIStringInput toInput_;
27   private UISelectBox predefinedSubjectInput_;
28   private UIStringInput subjectInput_;
29   private UITextArea bodyInput_ ;
30   
31   public UIMessageForm(MessageService service) {
32     super("messageForm", "post", null) ;
33     setId("UIMessagseForm") ;
34     setClazz("UIMessageForm");
35     setRendererType("SimpleFormButtonRenderer") ;
36     service_ = service ;
37     
38     toInput_ = new UIStringInput("to", "") ;
39     subjectInput_ = new UIStringInput("subject", "") ;
40     bodyInput_ = new UITextArea("body", "") ;
41     //String[] predifinedSubject = res.getStringArray("label.predefined-subject");
42
String JavaDoc[] predifinedSubject = {
43         "select a subject or enter your subject in the box below" ,
44         "subject 1" ,
45         "subject 2" ,
46         "subject 3"
47     } ;
48     List options = new ArrayList() ;
49     for(int i = 0; i < predifinedSubject.length ; i++) {
50       options.add(new SelectItem(predifinedSubject[i], predifinedSubject[i]));
51     }
52     predefinedSubjectInput_= new UISelectBox("predefinedSubject", "", options);
53     add(new Row().
54         add(new Cell("#{UIMessageForm.info.send-message-instruction}").
55             addColspan("2")));
56     add(new Row().setClazz("bottom-line").
57         add(new LabelCell("#{UIMessageForm.label.to}")).
58         add(new ComponentCell(this, toInput_)));
59   
60     add(new Row().setClazz("bottom-line").
61         add(new LabelCell("#{UIMessageForm.label.subject}")).
62         add(new ListComponentCell().
63             add(this, predefinedSubjectInput_).
64             add("<br>").
65             add(this, subjectInput_)));
66     Cell actions =
67       new ListComponentCell().
68       add("<div style='float: right'>").
69       add(new FormButton("#{UIMessageForm.button.send-message}", SEND_MESSAGE_ACTION)).
70       add(new FormButton("#{UIMessageForm.button.cancel}", CANCEL_ACTION)).
71       add("</div>").
72       addColspan("2") ;
73     add(new Row().add(actions)) ;
74     add(new Row().
75         add(new ComponentCell(this, bodyInput_).addColspan("2")));
76     add(new Row(). add(actions)) ;
77     
78     addActionListener(SendMessageActionListener.class, SEND_MESSAGE_ACTION) ;
79     addActionListener(CancelActionListener.class, CANCEL_ACTION) ;
80   }
81   public void setFormData(Account account, Message messageToReply) {
82     account_ = account ;
83     messageToReply_ = messageToReply ;
84     message_ = service_.createMessageInstance() ;
85     if(messageToReply_ != null) {
86       toInput_.setValue(messageToReply.getFrom()) ;
87       predefinedSubjectInput_.setValue("") ;
88       subjectInput_.setValue("Re: " + messageToReply.getSubject()) ;
89       bodyInput_.setValue(messageToReply.getBody()) ;
90     } else {
91       toInput_.setValue("") ;
92       predefinedSubjectInput_.setValue("") ;
93       subjectInput_.setValue("") ;
94       bodyInput_.setValue("") ;
95     }
96   }
97   static public class SendMessageActionListener extends ExoActionListener {
98     public void execute(ExoActionEvent event) throws Exception JavaDoc {
99       UIMessageForm uiForm = (UIMessageForm) event.getSource();
100       Message message = uiForm.message_ ;
101       String JavaDoc subject = uiForm.subjectInput_.getValue() ;
102       if(subject == null || subject.length() == 0) {
103         subject = uiForm.predefinedSubjectInput_.getValue() ;
104       }
105       message.setTo(uiForm.toInput_.getValue()) ;
106       message.setSubject(subject) ;
107       message.setBody(uiForm.bodyInput_.getValue()) ;
108       uiForm.service_.sendMessage(uiForm.account_, message) ;
109       UISummary uiFolder =
110         (UISummary)uiForm.getSibling(UISummary.class) ;
111       uiFolder.update() ;
112       uiForm.setRenderedSibling(UISummary.class) ;
113     }
114   }
115
116   static public class CancelActionListener extends ExoActionListener {
117     public void execute(ExoActionEvent event) throws Exception JavaDoc {
118       UIMessageForm uiForm = (UIMessageForm) event.getSource();
119       uiForm.setRenderedSibling(UISummary.class) ;
120     }
121   }
122 }
Popular Tags