Coverage Report - org.kuali.student.core.comments.ui.client.widgets.commenttool.CommentTool
 
Classes in this File Line Coverage Branch Coverage Complexity
CommentTool
0%
0/172
0%
0/32
2.045
CommentTool$1
0%
0/3
0%
0/2
2.045
CommentTool$10
0%
0/5
N/A
2.045
CommentTool$10$1
0%
0/6
N/A
2.045
CommentTool$10$1$1
0%
0/6
N/A
2.045
CommentTool$11
0%
0/7
0%
0/4
2.045
CommentTool$11$1
0%
0/6
0%
0/4
2.045
CommentTool$12
0%
0/18
0%
0/6
2.045
CommentTool$13
0%
0/1
N/A
2.045
CommentTool$2
0%
0/7
0%
0/4
2.045
CommentTool$3
0%
0/3
N/A
2.045
CommentTool$4
0%
0/3
N/A
2.045
CommentTool$5
0%
0/29
0%
0/5
2.045
CommentTool$5$1
0%
0/5
N/A
2.045
CommentTool$5$2
0%
0/6
N/A
2.045
CommentTool$6
0%
0/13
0%
0/6
2.045
CommentTool$7
0%
0/7
0%
0/4
2.045
CommentTool$8
0%
0/8
0%
0/3
2.045
CommentTool$9
0%
0/7
0%
0/4
2.045
CommentTool$EditMode
0%
0/2
N/A
2.045
 
 1  
 package org.kuali.student.core.comments.ui.client.widgets.commenttool;
 2  
 
 3  
 import java.text.DateFormat;
 4  
 import java.text.SimpleDateFormat;
 5  
 import java.util.ArrayList;
 6  
 import java.util.Collections;
 7  
 import java.util.Comparator;
 8  
 import java.util.Date;
 9  
 import java.util.HashMap;
 10  
 import java.util.List;
 11  
 import java.util.Map;
 12  
 
 13  
 import org.kuali.student.common.dto.DtoConstants.DtoState;
 14  
 import org.kuali.student.common.dto.RichTextInfo;
 15  
 import org.kuali.student.common.dto.StatusInfo;
 16  
 import org.kuali.student.common.ui.client.application.Application;
 17  
 import org.kuali.student.common.ui.client.application.KSAsyncCallback;
 18  
 import org.kuali.student.common.ui.client.configurable.mvc.HasReferenceId;
 19  
 import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle;
 20  
 import org.kuali.student.common.ui.client.mvc.Callback;
 21  
 import org.kuali.student.common.ui.client.mvc.Controller;
 22  
 import org.kuali.student.common.ui.client.mvc.ModelRequestCallback;
 23  
 import org.kuali.student.common.ui.client.mvc.dto.ReferenceModel;
 24  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 25  
 import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle;
 26  
 import org.kuali.student.common.ui.client.widgets.KSLabel;
 27  
 import org.kuali.student.common.ui.client.widgets.KSLightBox;
 28  
 import org.kuali.student.common.ui.client.widgets.KSTextArea;
 29  
 import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations.OkEnum;
 30  
 import org.kuali.student.common.ui.client.widgets.buttongroups.OkGroup;
 31  
 import org.kuali.student.common.ui.client.widgets.dialog.ConfirmationDialog;
 32  
 import org.kuali.student.common.ui.client.widgets.layout.VerticalFlowPanel;
 33  
 import org.kuali.student.core.comment.dto.CommentInfo;
 34  
 import org.kuali.student.core.comments.ui.client.service.CommentRpcService;
 35  
 import org.kuali.student.core.comments.ui.client.service.CommentRpcServiceAsync;
 36  
 
 37  
 import com.google.gwt.core.client.GWT;
 38  
 import com.google.gwt.dom.client.Style;
 39  
 import com.google.gwt.event.dom.client.ClickEvent;
 40  
 import com.google.gwt.event.dom.client.ClickHandler;
 41  
 import com.google.gwt.event.dom.client.KeyUpEvent;
 42  
 import com.google.gwt.event.dom.client.KeyUpHandler;
 43  
 import com.google.gwt.user.client.Window;
 44  
 import com.google.gwt.user.client.rpc.AsyncCallback;
 45  
 import com.google.gwt.user.client.ui.FlexTable;
 46  
 import com.google.gwt.user.client.ui.FlowPanel;
 47  
 import com.google.gwt.user.client.ui.HTML;
 48  
 import com.google.gwt.user.client.ui.HorizontalPanel;
 49  
 import com.google.gwt.user.client.ui.Widget;
 50  
 
 51  0
 public class CommentTool implements HasReferenceId {
 52  
 
 53  0
     private CommentRpcServiceAsync commentServiceAsync = GWT.create(CommentRpcService.class);
 54  
     private String referenceId;
 55  
     private String referenceTypeKey;
 56  
     private String referenceType;
 57  
     private String referenceState;
 58  
     private KSLightBox commentLightBox;
 59  0
     private HTML loggedInUserNameHTML = new HTML();
 60  
     private String loggedInUserId;
 61  0
     private KSTextArea commentTextArea = new KSTextArea();
 62  0
     private KSButton cancelEditButton = new KSButton("Cancel");
 63  0
     private KSButton submitCommentButton = new KSButton("Submit");
 64  0
     private FlexTable commentsTableLayout = new FlexTable();
 65  
     //private static final DateFormat df = new SimpleDateFormat("MMMM dd, yyyy - hh:mmaaa");
 66  
     private Controller controller;    
 67  
     private Enum<?> viewEnum;
 68  
     private String viewName;    //View name is being used as menu item label   
 69  0
     VerticalFlowPanel loggedInLabelsPanel = new VerticalFlowPanel();
 70  0
     VerticalFlowPanel commentEditPanel = new VerticalFlowPanel();
 71  0
     KSLabel notAuthorizedToAddComments = new KSLabel("The document must be saved before Comments can be added.");
 72  0
     private EditMode editMode = EditMode.ADD_COMMENT;
 73  
     private CommentInfo selectedComment;
 74  0
     private List<Callback<EditMode>> editControlsCallbacks = new ArrayList<Callback<EditMode>>();
 75  
     private String commentTypeKey;
 76  
     private Map<String, String> referenceAttributes;
 77  0
     private KSLabel proposalTitle = new KSLabel();
 78  
     private String title;
 79  
     private HTML htmlLabel;
 80  
     private SectionTitle leaveACommentTitle;
 81  
     private HorizontalPanel commentSectionPanel;
 82  
     private KSButton editButton;
 83  
     private KSButton deleteButton;
 84  0
     private Map<Integer, KSButton> editButtonMap = new HashMap<Integer, KSButton>();
 85  0
     private Map<Integer, KSButton> deleteButtonMap = new HashMap<Integer, KSButton>();
 86  
 
 87  0
     public enum EditMode {
 88  0
         ADD_COMMENT, UPDATE_COMMENT, VIEW_COMMENT
 89  
     }
 90  
     
 91  0
     public CommentTool(Enum<?> viewEnum, String viewName, String commentTypeKey, String title) {
 92  0
         this.viewName = viewName;
 93  0
         this.viewEnum = viewEnum;
 94  0
         this.commentTypeKey = commentTypeKey;
 95  0
         this.title = title;
 96  0
         init();
 97  
         
 98  0
     }
 99  
     
 100  
     public Controller getController() {
 101  0
         return this.controller;
 102  
     }
 103  
 
 104  
 
 105  
     public void setController(Controller controller){
 106  0
         this.controller = controller;
 107  0
     }
 108  
 
 109  
     
 110  
     public Widget asWidget(){
 111  0
         return commentLightBox.getWidget();
 112  
     }
 113  
     
 114  0
     private OkGroup buttonPanel = new OkGroup(new Callback<OkEnum>(){
 115  
 
 116  
         @Override
 117  
         public void exec(OkEnum result) {
 118  0
             if(result == OkEnum.Ok){
 119  
             }
 120  
 
 121  0
         }
 122  
     });
 123  
     
 124  
     private void init() {
 125  0
         commentLightBox = new KSLightBox();
 126  0
         VerticalFlowPanel contentPanel = new VerticalFlowPanel();
 127  
         // light box title and instructions
 128  0
         SectionTitle title = SectionTitle.generateH2Title(this.title);
 129  0
         title.addStyleName("ks-layout-header");
 130  0
         htmlLabel = new HTML("<b>All comments posted here will be visible to authors, and " +
 131  
                         "to reviewers after you submit the proposal.</b>");
 132  0
         title.setStyleName("cluProposalTitleSection");
 133  0
         proposalTitle.setVisible(false);
 134  0
         contentPanel.add(proposalTitle);
 135  0
         commentLightBox.setNonCaptionHeader(title);
 136  0
         contentPanel.add(htmlLabel);
 137  
         
 138  
         // comments section title
 139  0
         leaveACommentTitle = SectionTitle.generateH3Title("Leave a Comment");
 140  0
         leaveACommentTitle.getElement().getStyle().setProperty("borderBottom", "1px solid #D8D8D8");
 141  0
         leaveACommentTitle.getElement().getStyle().setProperty("marginTop", "2em");
 142  0
         contentPanel.add(leaveACommentTitle);
 143  
         
 144  
         // comments section
 145  0
         HTML loggedInAsLabel = new HTML("<b>Logged in as:<b/>");
 146  0
         loggedInLabelsPanel.add(loggedInAsLabel);
 147  0
         final String userId = Application.getApplicationContext().getSecurityContext().getUserId();
 148  0
         commentServiceAsync.getUserRealName(userId, new AsyncCallback<String>() {
 149  
             @Override
 150  
             public void onFailure(Throwable caught) {
 151  0
                 loggedInUserNameHTML.setHTML("<b>" + userId + "</b>");
 152  0
             }
 153  
             @Override
 154  
             public void onSuccess(String result) {
 155  0
                 if (result != null && !result.isEmpty()) {
 156  0
                     loggedInUserNameHTML.setHTML("<b>" + result + "</b>");
 157  
                 } else {
 158  0
                     loggedInUserNameHTML.setHTML("<b>" + userId + "</b>");
 159  
                 }
 160  0
             }
 161  
         });
 162  
         
 163  0
         loggedInUserId = userId;
 164  0
         loggedInLabelsPanel.add(loggedInUserNameHTML);
 165  0
         commentTextArea.setSize("500", "100");
 166  0
         commentEditPanel.add(commentTextArea);
 167  0
         FlowPanel buttonsPanel = new FlowPanel();
 168  0
         buttonsPanel.add(cancelEditButton);
 169  0
         cancelEditButton.addClickHandler(new ClickHandler() {
 170  
             @Override
 171  
             public void onClick(ClickEvent event) {
 172  0
                 setEditMode(EditMode.ADD_COMMENT);
 173  0
             }
 174  
         });
 175  0
         buttonsPanel.add(submitCommentButton);
 176  0
         submitCommentButton.setEnabled(false);
 177  0
         commentEditPanel.add(buttonsPanel);
 178  0
         buttonsPanel.addStyleName("KS-Comment-Button-Panel");
 179  
         
 180  0
         commentTextArea.addKeyUpHandler(new KeyUpHandler() {
 181  
             @Override
 182  
             public void onKeyUp(KeyUpEvent event) {
 183  0
                 submitCommentButton.setEnabled(true);
 184  0
             }
 185  
         });
 186  
         
 187  
         // if edit mode is add coment call addComment
 188  
         // if edit mode is edit comment call updateComment
 189  0
         submitCommentButton.addClickHandler(new ClickHandler() {
 190  
             @Override
 191  
             public void onClick(ClickEvent event) {
 192  0
                 switch (editMode) {
 193  
                     case ADD_COMMENT: {
 194  0
                         CommentInfo newComment = new CommentInfo();
 195  
 
 196  0
                         RichTextInfo text = new RichTextInfo();
 197  0
                         text.setFormatted(commentTextArea.getText());
 198  0
                         text.setPlain(commentTextArea.getText());
 199  0
                         newComment.setType(commentTypeKey);
 200  0
                         newComment.setReferenceId(referenceId);
 201  0
                         newComment.setReferenceTypeKey(referenceTypeKey);
 202  0
                         newComment.setState(DtoState.ACTIVE.toString());
 203  0
                         newComment.setCommentText(text);
 204  
 
 205  
                         try {
 206  0
                             commentServiceAsync.addComment(referenceId, referenceTypeKey, newComment, new KSAsyncCallback<CommentInfo>(){
 207  
 
 208  
                                 @Override
 209  
                                 public void handleFailure(Throwable caught) {
 210  0
                                     GWT.log("Add Comment Failed", caught);
 211  0
                                 }
 212  
 
 213  
                                 @Override
 214  
                                 public void onSuccess(CommentInfo result) {
 215  0
                                     refreshComments();
 216  0
                                 }
 217  
                             });
 218  0
                         } catch (Exception e) {
 219  0
                             GWT.log("Add Comment Failed", e);
 220  0
                         }
 221  0
                         break;
 222  
                     }
 223  
                     case UPDATE_COMMENT: {
 224  0
                         if (selectedComment != null) {
 225  0
                             RichTextInfo text = new RichTextInfo();
 226  0
                             text.setFormatted(commentTextArea.getText());
 227  0
                             text.setPlain(commentTextArea.getText());
 228  0
                             selectedComment.setReferenceId(referenceId);
 229  0
                             selectedComment.setReferenceTypeKey(referenceTypeKey);
 230  0
                             selectedComment.setCommentText(text);
 231  
 //                            selectedComment.setType("commentType." + referenceType + "." + referenceState);
 232  0
                             selectedComment.setType(commentTypeKey);
 233  
                         }
 234  
 
 235  
                         try {
 236  0
                             commentServiceAsync.updateComment(referenceId, referenceTypeKey, selectedComment, new KSAsyncCallback<CommentInfo>(){
 237  
 
 238  
                                 @Override
 239  
                                 public void handleFailure(Throwable caught) {
 240  0
                                     GWT.log("Add Comment Failed", caught);
 241  0
                                 }
 242  
 
 243  
                                 @Override
 244  
                                 public void onSuccess(CommentInfo result) {
 245  0
                                     refreshComments();
 246  0
                                     setEditMode(EditMode.ADD_COMMENT);
 247  0
                                 }
 248  
                             });
 249  0
                         } catch (Exception e) {
 250  0
                             GWT.log("Add Comment Failed", e);
 251  0
                         }
 252  
                         break;
 253  
                     }
 254  
                 }
 255  0
             }
 256  
         });
 257  
         
 258  0
         commentSectionPanel = new HorizontalPanel();
 259  0
         commentSectionPanel.add(loggedInLabelsPanel);
 260  0
         commentSectionPanel.add(commentEditPanel);
 261  0
         commentSectionPanel.add(notAuthorizedToAddComments);
 262  0
         contentPanel.add(commentSectionPanel);
 263  
         
 264  
         // comments table
 265  0
         contentPanel.add(commentsTableLayout);
 266  
         
 267  
         
 268  
 //        scrollPanel.setHeight(height)
 269  
         
 270  0
         commentLightBox.setWidget(contentPanel);
 271  0
         setEditMode(EditMode.ADD_COMMENT);
 272  0
     }
 273  
     
 274  
     private void checkPermissionsAndRedrawTable(final List<CommentInfo> commentInfos) {
 275  
         // check permission to see if user can comment
 276  0
         commentServiceAsync.isAuthorizedAddComment(referenceId, referenceTypeKey, new KSAsyncCallback<Boolean>() {
 277  
 
 278  
             @Override
 279  
             public void onFailure(Throwable caught) {
 280  0
                 GWT.log("Error checking permission for adding comments: ", caught);
 281  0
                 throw new RuntimeException("Error checking Permissions: ", caught);
 282  
             }
 283  
 
 284  
             @Override
 285  
             public void onSuccess(Boolean result) {
 286  0
                 GWT.log("User is " + ((result) ? "" : "not ") + 
 287  
                         "authorized to add comment.", null);
 288  0
                 if(referenceId != null && !(referenceId.isEmpty())){
 289  0
                     notAuthorizedToAddComments.setVisible(false);
 290  0
                     commentEditPanel.setVisible(result);
 291  0
                     commentsTableLayout.setVisible(true);
 292  0
                     redrawCommentsTable(commentInfos);
 293  
                 }
 294  
                 else{
 295  0
                     notAuthorizedToAddComments.setVisible(true);
 296  0
                     commentEditPanel.setVisible(false);
 297  0
                     commentsTableLayout.setVisible(false);
 298  
                 }
 299  0
             }
 300  
 
 301  
         });
 302  0
     }
 303  
     
 304  
     private void redrawCommentsTable(List<CommentInfo> commentInfos) {
 305  0
         commentsTableLayout.clear();
 306  0
         editControlsCallbacks.clear();
 307  
         
 308  0
         if (commentInfos != null) {
 309  0
             int rowIndex = 0;
 310  0
             int commentCounter = 0;
 311  0
             for (final CommentInfo commentInfo : commentInfos) {
 312  0
                 int columnIndex = 0;
 313  0
                 editButton = new KSButton("Edit", ButtonStyle.DEFAULT_ANCHOR);
 314  0
                 deleteButton = new KSButton("Delete", ButtonStyle.DEFAULT_ANCHOR);
 315  0
                 if (commentInfo.getType() != null && 
 316  
                         commentInfo.getType().startsWith("kuali.comment.type.workflowDecisionRationale")) {
 317  
                     // do not display comments for workflow decision rationale.
 318  0
                     continue;
 319  
                 }
 320  0
                 if (rowIndex == 0) {
 321  0
                     StringBuilder titleTextSb = new StringBuilder();
 322  0
                     titleTextSb.append("Comments (").append(commentInfos.size()).append(")");
 323  0
                     SectionTitle commentsSectionHeader = SectionTitle.generateH3Title(titleTextSb.toString());
 324  0
                     commentsSectionHeader.getElement().getStyle().setProperty("borderBottom", "1px solid #D8D8D8");
 325  0
                     commentsTableLayout.setWidget(rowIndex, columnIndex, commentsSectionHeader);
 326  0
                     commentsTableLayout.getFlexCellFormatter().setColSpan(rowIndex, columnIndex, 3);
 327  0
                     rowIndex++;
 328  
                 }
 329  0
                 if (commentCounter > 0) {
 330  0
                     VerticalFlowPanel space = new VerticalFlowPanel();
 331  0
                     space.getElement().getStyle().setPaddingBottom(5d, Style.Unit.PX);
 332  0
                     commentsTableLayout.setWidget(rowIndex, columnIndex, space);
 333  0
                     commentsTableLayout.getFlexCellFormatter().setColSpan(rowIndex, columnIndex, 3);
 334  0
                     rowIndex++;
 335  
                 }
 336  0
                 VerticalFlowPanel userNameAndTime = new VerticalFlowPanel();
 337  0
                 final HTML userNameLabel = new HTML();
 338  
                 // TODO use user id for now change to user name
 339  0
                 final String userId = commentInfo.getMetaInfo().getUpdateId();
 340  0
                 commentServiceAsync.getUserRealName(userId, new AsyncCallback<String>() {
 341  
                     @Override
 342  
                     public void onFailure(Throwable caught) {
 343  0
                         userNameLabel.setHTML("<b>" + userId + "</b>");
 344  0
                     }
 345  
                     @Override
 346  
                     public void onSuccess(String result) {
 347  0
                         if (result != null && !result.isEmpty()) {
 348  0
                             userNameLabel.setHTML("<b>" + result + "</b>");
 349  
                         } else {
 350  0
                             userNameLabel.setHTML("<b>" + userId + "</b>");
 351  
                         }
 352  0
                     }
 353  
                 });
 354  0
                 Date createTime = commentInfo.getMetaInfo().getCreateTime();
 355  0
                 userNameAndTime.add(userNameLabel);
 356  0
                 DateFormat df = new SimpleDateFormat("MMMM dd, yyyy - hh:mmaaa");
 357  0
                 userNameAndTime.add(new KSLabel(df.format(createTime)));
 358  0
                 userNameAndTime.getElement().getStyle().setPaddingRight(20d, Style.Unit.PX);
 359  0
                 commentsTableLayout.setWidget(rowIndex, columnIndex, userNameAndTime);
 360  0
                 columnIndex++;
 361  
                 
 362  0
                 RichTextInfo commentRT = commentInfo.getCommentText();
 363  0
                 String commentText = commentRT.getPlain();
 364  0
                 KSLabel commentTextLabel = new KSLabel(commentText);
 365  0
                 commentTextLabel.getElement().getStyle().setPaddingRight(20d, Style.Unit.PX);
 366  0
                 commentTextLabel.setWidth("120px");
 367  0
                 commentTextLabel.getElement().getStyle().setProperty("wordWrap", "break-word");
 368  0
                 commentsTableLayout.setWidget(rowIndex, columnIndex, commentTextLabel);
 369  0
                 columnIndex++;
 370  0
                 editButton.getElement().getStyle().setPadding(5d, Style.Unit.PX);
 371  0
                 editControlsCallbacks.add(new Callback<EditMode>() {
 372  
                     @Override
 373  
                     public void exec(EditMode result) {
 374  0
                         switch (editMode) {
 375  
                             case UPDATE_COMMENT:
 376  
 //                                editButton.setStyleName("KS-CommentButtonDisabled");
 377  
 //                                deleteButton.setStyleName("KS-CommentButtonDisabled");
 378  0
                                 editButton.setEnabled(false);
 379  0
                                 deleteButton.setEnabled(false);
 380  0
                                 break;
 381  
                             case ADD_COMMENT:
 382  
 //                                editButton.setStyleName("KS-CommentButton");
 383  
 //                                deleteButton.setStyleName("KS-CommentButton");
 384  0
                                 editButton.setEnabled(true);
 385  0
                                 deleteButton.setEnabled(true);
 386  
                                 break;
 387  
                         }
 388  0
                     }
 389  
                 });
 390  
                 
 391  0
                 editButton.addClickHandler(new ClickHandler() {
 392  
                     @Override
 393  
                     public void onClick(ClickEvent event) {
 394  0
                         String commentText = (commentInfo == null ||
 395  
                                 commentInfo.getCommentText() == null)? "" :
 396  
                                     commentInfo.getCommentText().getPlain();
 397  0
                         selectedComment = commentInfo;
 398  0
                         commentTextArea.setText(commentText);
 399  0
                         commentTextArea.setFocus(true);
 400  0
                         setEditMode(EditMode.UPDATE_COMMENT);
 401  0
                     }
 402  
                 });
 403  0
                 deleteButton.addClickHandler(new ClickHandler() {
 404  
                     @Override
 405  
                     public void onClick(ClickEvent event) {
 406  0
                             final ConfirmationDialog confirmDeletion =
 407  
                                 new ConfirmationDialog("Delete Comment",  
 408  
                                     "You are about to delete a comment.  Are you sure?");
 409  0
                             confirmDeletion.getConfirmButton().addClickHandler(new ClickHandler(){
 410  
                                 @Override
 411  
                                 public void onClick(ClickEvent event) {
 412  
                                     try {
 413  0
                                         commentServiceAsync.removeComment(commentInfo.getId(), 
 414  
                                                 referenceId, referenceTypeKey, 
 415  0
                                                 new KSAsyncCallback<StatusInfo>(){
 416  
 
 417  
                                             @Override
 418  
                                             public void handleFailure(Throwable caught) {
 419  0
                                                 GWT.log("remove Comment Failed", caught);
 420  0
                                             }
 421  
 
 422  
                                             @Override
 423  
                                             public void onSuccess(StatusInfo result) {
 424  0
                                                 confirmDeletion.hide();
 425  0
                                                 refreshComments();
 426  0
                                             }
 427  
 
 428  
                                         });
 429  0
                                     } catch (Exception e) {
 430  0
                                         GWT.log("remove Comment Failed", e);
 431  0
                                     }
 432  0
                                 }
 433  
                             });
 434  0
                             confirmDeletion.show();
 435  0
                     }
 436  
                 });
 437  0
                 commentsTableLayout.setWidget(rowIndex, columnIndex, editButton);
 438  0
                 editButtonMap.put(commentCounter, editButton);
 439  0
                 columnIndex++;
 440  0
                 commentsTableLayout.setWidget(rowIndex, columnIndex, deleteButton);
 441  0
                 deleteButtonMap.put(commentCounter, deleteButton);
 442  0
                 columnIndex++;
 443  0
                 if (userId == null || !userId.equals(this.loggedInUserId)) {
 444  0
                     editButton.setVisible(false);
 445  0
                     deleteButton.setVisible(false);
 446  
                 }
 447  
                 
 448  0
                 rowIndex++;
 449  
                 
 450  0
                 commentCounter++;
 451  0
             }
 452  0
             setEditMode(EditMode.ADD_COMMENT);
 453  
         }
 454  0
     }
 455  
     
 456  
     public void setEditMode(EditMode editMode) {
 457  0
         this.editMode = editMode;
 458  0
         switch (editMode) {
 459  
             case UPDATE_COMMENT:
 460  0
                 cancelEditButton.setVisible(true);
 461  0
                 if (editControlsCallbacks != null) {
 462  0
                     for (Callback<EditMode> callback : editControlsCallbacks) {
 463  0
                         callback.exec(EditMode.UPDATE_COMMENT);
 464  
                     }
 465  
                 }
 466  
                 break;
 467  
             case ADD_COMMENT:
 468  0
                 cancelEditButton.setVisible(false);
 469  0
                 commentTextArea.setText("");
 470  0
                 if (editControlsCallbacks != null) {
 471  0
                     for (Callback<EditMode> callback : editControlsCallbacks) {
 472  0
                         callback.exec(EditMode.ADD_COMMENT);
 473  
                     }
 474  
                 }
 475  
                 break;
 476  
             case VIEW_COMMENT:
 477  0
                 htmlLabel.setVisible(false);
 478  0
                 leaveACommentTitle.setVisible(false);
 479  0
                 commentSectionPanel.setVisible(false);
 480  
                 
 481  0
                 for (int i = 0; i < editButtonMap.size(); i++) {
 482  0
                     editButtonMap.get(i).setVisible(false);                    
 483  
                 }
 484  0
                 for (int i = 0; i < deleteButtonMap.size(); i++) {
 485  0
                     deleteButtonMap.get(i).setVisible(false);                    
 486  
                 }
 487  
 
 488  
                 break;
 489  
         }
 490  0
     }
 491  
     
 492  
     public void refreshComments(){
 493  
         //rpc call to get all current comments and populate into comment list
 494  
         try {
 495  0
             commentServiceAsync.getComments(referenceId, referenceTypeKey, new KSAsyncCallback<List<CommentInfo>>(){
 496  
 
 497  
                 @Override
 498  
                 public void handleFailure(Throwable caught) {
 499  0
                     GWT.log("getComments failed", caught);
 500  0
                 }
 501  
 
 502  
                 @Override
 503  
                 public void onSuccess(List<CommentInfo> result) {
 504  0
                     if (result != null && !result.isEmpty()) {
 505  0
                         Collections.sort(result, new Comparator<CommentInfo>(){
 506  
 
 507  
                             @Override
 508  
                             public int compare(CommentInfo comment1, CommentInfo comment2) {
 509  
 
 510  0
                                 if(comment1.getMetaInfo().getCreateTime().after(comment2.getMetaInfo().getCreateTime())){
 511  0
                                     return -1;
 512  
                                 }
 513  0
                                 else if(comment1.getMetaInfo().getCreateTime().before(comment2.getMetaInfo().getCreateTime())){
 514  0
                                     return 1;
 515  
                                 }
 516  
                                 else{
 517  
                                     //equal
 518  0
                                     return 0;
 519  
                                 }
 520  
 
 521  
                             }
 522  
                         });
 523  
                     }
 524  0
                     checkPermissionsAndRedrawTable(result);
 525  0
                 }
 526  
             });
 527  0
         } catch (Exception e) {
 528  0
             Window.alert("Failed to refresh Comments");
 529  0
             GWT.log("refresh Comments Failed",e);
 530  0
         }
 531  
 
 532  
 
 533  0
     }
 534  
 //    private void redrawCommentsTable(FlexTable tableLayout) {
 535  
 //        StringBuilder titleTextSb = new StringBuilder();
 536  
 //        titleTextSb.append("Comments (").append(docInfos.size()).append(")");
 537  
 //        SectionTitle uploadedFileSectionHeader = SectionTitle.generateH3Title(titleTextSb.toString());
 538  
 //        uploadedFileSectionHeader.getElement().getStyle().setProperty("borderBottom", "1px solid #D8D8D8");
 539  
 //        tableLayout.setWidget(rowIndex, columnIndex, uploadedFileSectionHeader);
 540  
 //        tableLayout.getFlexCellFormatter().setColSpan(rowIndex, columnIndex, 3);
 541  
 //    }
 542  
     
 543  
     @Override
 544  
     public String getReferenceId() {
 545  0
         return referenceId;
 546  
     }
 547  
 
 548  
     @Override
 549  
     public String getReferenceState() {
 550  0
         return referenceState;
 551  
     }
 552  
 
 553  
     @Override
 554  
     public String getReferenceType() {
 555  0
         return referenceType;
 556  
     }
 557  
 
 558  
     @Override
 559  
     public String getReferenceTypeKey() {
 560  0
         return referenceTypeKey;
 561  
     }
 562  
 
 563  
     @Override
 564  
     public void setReferenceId(String id) {
 565  0
         this.referenceId = id;
 566  0
     }
 567  
 
 568  
     @Override
 569  
     public void setReferenceState(String state) {
 570  0
         this.referenceState = state;
 571  0
     }
 572  
 
 573  
     @Override
 574  
     public void setReferenceType(String type) {
 575  0
         this.referenceType = type;
 576  0
     }
 577  
 
 578  
     @Override
 579  
     public void setReferenceTypeKey(String key) {
 580  0
         this.referenceTypeKey = key;
 581  0
     }
 582  
     
 583  
     public void setReferenceAttributes(Map<String, String> referenceAttributes) {
 584  0
         this.referenceAttributes = referenceAttributes;
 585  0
     }
 586  
     
 587  
     public void show() {
 588  0
         controller.requestModel(ReferenceModel.class, 
 589  0
                 new ModelRequestCallback<ReferenceModel>(){
 590  
             public void onModelReady(ReferenceModel model) {
 591  0
                 String proposalTitleString = null;
 592  0
                 setReferenceId(model.getReferenceId());
 593  0
                 setReferenceTypeKey(model.getReferenceTypeKey());
 594  0
                 setReferenceType(model.getReferenceType());
 595  0
                 setReferenceState(model.getReferenceState());
 596  0
                 setReferenceAttributes(model.getReferenceAttributes());
 597  0
                 proposalTitleString = (model.getReferenceAttributes() == null)? null : 
 598  
                     model.getReferenceAttributes().get("name");
 599  0
                 if (proposalTitleString != null && !proposalTitleString.trim().isEmpty()) {
 600  0
                     proposalTitle.setText(proposalTitleString);
 601  0
                     proposalTitle.setVisible(true);
 602  
                 } else {
 603  0
                     proposalTitle.setText("");
 604  0
                     proposalTitle.setVisible(false);
 605  
                 }
 606  0
                 refreshComments();
 607  0
                 commentLightBox.show();
 608  0
             }
 609  
 
 610  
             public void onRequestFail(Throwable cause) {
 611  0
                 Window.alert(cause.toString());
 612  0
             }
 613  
         });
 614  0
     }
 615  
 
 616  
 }