1 5 package org.exoplatform.portlets.communication.forum.component; 6 7 import java.util.ResourceBundle ; 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 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 { 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 { 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 { 110 UIPostForm uiForm = (UIPostForm) event.getComponent() ; 111 String user = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser() ; 112 String subject = uiForm.subject_.getText() ; 113 String message = uiForm.message_.getText() ; 114 if(subject == null || subject.length() == 0) { 115 ResourceBundle 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 |