KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > communication > forum > component > UIPostForm


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.forum.component;
6
7 import java.util.ResourceBundle JavaDoc;
8 import javax.faces.context.FacesContext ;
9 import org.exoplatform.container.SessionContainer;
10 import org.exoplatform.faces.core.component.UITextArea ;
11 import org.exoplatform.faces.core.component.UISimpleForm;
12 import org.exoplatform.faces.core.component.UIStringInput;
13 import org.exoplatform.faces.core.component.model.*;
14 import org.exoplatform.faces.core.event.ExoActionEvent;
15 import org.exoplatform.faces.core.event.ExoActionListener;
16 import org.exoplatform.services.communication.forum.Forum;
17 import org.exoplatform.services.communication.forum.ForumService;
18 import org.exoplatform.services.communication.forum.Post;
19 import org.exoplatform.services.communication.forum.Topic;
20 /**
21  * Sat, Jan 03, 2004 @ 11:16
22  * @author: Tuan Nguyen
23  * @email: tuan08@users.sourceforge.net
24  * @version: $Id: UIPostForm.java,v 1.12 2004/10/20 23:39:59 benjmestrallet Exp $
25  */

26 public class UIPostForm extends UISimpleForm {
27   private ForumService service_ ;
28   private UIStringInput subject_ ;
29   private UITextArea message_ ;
30   private Post post_ ;
31   private Topic topic_ ;
32   private Forum forum_ ;
33   
34   public UIPostForm(ForumService service) throws Exception JavaDoc {
35     super("postForm", "post", null) ;
36     setId("UIPostForm") ;
37     setClazz("UIPostForm");
38     service_ = service ;
39     message_ = new UITextArea("message", "") ;
40     subject_ = new UIStringInput("subject", "") ;
41         
42     add(new HeaderRow().
43         add(new Cell("#{UIPostForm.header.title}").
44             addColspan("2")));
45     add(new Row().
46         add(new LabelCell("#{UIPostForm.label.subject}")).
47         add(new ComponentCell(this, subject_))) ;
48     add(new Row().
49         add(new LabelCell("#{UIPostForm.label.message}").addValign("top")).
50         add(new ComponentCell(this, message_)));
51     add(new Row().
52         add(new ListComponentCell().
53             add(new FormButton("#{UIPostForm.button.save}", SAVE_ACTION)).
54             add(new FormButton("#{UIPostForm.button.cancel}", CANCEL_ACTION)).
55             addColspan("2").addAlign("center"))) ;
56     addActionListener(CancelActionListener.class, CANCEL_ACTION) ;
57     addActionListener(SaveActionListener.class, SAVE_ACTION) ;
58   }
59   
60   public void setNewTopicInfo(Forum forum) {
61     forum_ = forum ;
62     topic_ = null ;
63     post_ = null ;
64     subject_.setText("") ;
65     message_.setText("") ;
66   }
67
68   public void setNewPostInfo(Topic topic) {
69     forum_ = null ;
70     topic_ = topic ;
71     post_ = null ;
72     subject_.setText("") ;
73     message_.setText("") ;
74   }
75
76   public void setQuotePostInfo(Topic topic, Post post) {
77     forum_ = null ;
78     topic_ = topic ;
79     post_ = null ;
80     subject_.setText(post.getSubject()) ;
81     message_.setText(post.getMessage()) ;
82   }
83
84   public void setEditPostInfo(Topic topic, Post post) {
85     forum_ = null ;
86     topic_ = topic ;
87     post_ = post ;
88     subject_.setText(post.getSubject()) ;
89     message_.setText(post.getMessage()) ;
90   }
91   
92   public boolean canDecodeInvalidState() { return forum_ != null || topic_ != null; }
93   
94   static public class CancelActionListener extends ExoActionListener {
95         public void execute(ExoActionEvent event) throws Exception JavaDoc {
96             UIPostForm uiForm = (UIPostForm) event.getComponent() ;
97       UIForumPortlet forumPortlet = (UIForumPortlet)uiForm.getAncestorOfType(UIForumPortlet.class);
98       forumPortlet.addHistoryElement(uiForm);
99             if (uiForm.topic_ == null) {
100         forumPortlet.setRenderedComponent(UITopics.class);
101       } else {
102         forumPortlet.setRenderedComponent(UIPosts.class);
103       }
104       ((UIToolbarPanel)forumPortlet.getChildComponentOfType(UIToolbarPanel.class)).setRendered(true);
105         }
106   }
107   
108   static public class SaveActionListener extends ExoActionListener {
109         public void execute(ExoActionEvent event) throws Exception JavaDoc {
110             UIPostForm uiForm = (UIPostForm) event.getComponent() ;
111              String JavaDoc user = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser() ;
112        String JavaDoc subject = uiForm.subject_.getText() ;
113        String JavaDoc message = uiForm.message_.getText() ;
114        if(subject == null || subject.length() == 0) {
115          ResourceBundle JavaDoc res = event.getApplicationResourceBundle() ;
116          subject = res.getString("UIPostForm.msg.no-subject");
117        }
118        Topic topic = uiForm.topic_ ;
119        boolean isNewTopic = false ;
120        if (topic == null) {
121          topic = uiForm.service_.createTopicInstance() ;
122          topic.setOwner(user) ;
123          topic.setModifiedBy(user) ;
124          topic.setTopic(subject) ;
125          topic.setDescription("") ;
126          uiForm.service_.addTopic(uiForm.forum_, topic) ;
127          isNewTopic = true ;
128        }
129        
130        boolean isNewPost = false ;
131        Post post = uiForm.post_ ;
132        if(post == null) {
133          post = uiForm.service_.createPostInstance() ;
134          post.setOwner(user) ;
135          isNewPost = true ;
136        }
137
138        post.setModifiedBy(user) ;
139        post.setSubject(subject) ;
140        post.setMessage(message) ;
141        post.setRemoteAddr(SessionContainer.getInstance().getMonitor().getIPAddress()) ;
142        if(isNewPost) uiForm.service_.addPost(topic, post) ;
143        else uiForm.service_.updatePost(post) ;
144        UITopics uiTopics = (UITopics)uiForm.getSibling(UITopics.class) ;
145        if(isNewTopic || isNewPost) {
146          uiTopics.reload() ;
147        }
148              uiTopics.visit( topic.getId() );
149        UIPosts uiPosts = (UIPosts) uiForm.getSibling(UIPosts.class) ;
150        uiPosts.setUIPostsData(uiTopics.getForum() , topic) ;
151            
152        UIForumPortlet forumPortlet = (UIForumPortlet)uiForm.getAncestorOfType(UIForumPortlet.class);
153        forumPortlet.addHistoryElement(uiForm);
154        forumPortlet.setRenderedComponent(UIPosts.class);
155        ((UIToolbarPanel)forumPortlet.getChildComponentOfType(UIToolbarPanel.class)).setRendered(true);
156         }
157   }
158   
159 }
Popular Tags