Skip to content

Commit cca035c

Browse files
committed
Add a feature to edit a comment
1 parent bcd0b88 commit cca035c

6 files changed

Lines changed: 129 additions & 9 deletions

File tree

src/main/java/com/junichi11/netbeans/modules/github/issues/issue/GitHubIssueController.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
package com.junichi11.netbeans.modules.github.issues.issue;
4343

4444
import com.junichi11.netbeans.modules.github.issues.GitHubIssues;
45+
import com.junichi11.netbeans.modules.github.issues.issue.ui.CommentTabbedPanel;
4546
import com.junichi11.netbeans.modules.github.issues.issue.ui.CommentsPanel;
4647
import com.junichi11.netbeans.modules.github.issues.issue.ui.GitHubIssuePanel;
4748
import com.junichi11.netbeans.modules.github.issues.utils.StringUtils;
@@ -63,6 +64,8 @@
6364
import org.openide.util.HelpCtx;
6465
import org.openide.util.NbBundle;
6566
import org.openide.util.RequestProcessor;
67+
import org.pegdown.Extensions;
68+
import org.pegdown.PegDownProcessor;
6669

6770
/**
6871
*
@@ -183,11 +186,52 @@ public void propertyChange(PropertyChangeEvent evt) {
183186
String quoteComment = StringUtils.toQuoteComment(p.getQuoteComment()) + "\n"; // NOI18N
184187
p.appendNewComment(quoteComment);
185188
break;
189+
case CommentsPanel.PROP_COMMENT_EDITED:
190+
editComment();
191+
break;
186192
default:
187193
break;
188194
}
189195
}
190196

197+
@NbBundle.Messages({
198+
"GitHubIssueController.edit.comment.title=Edit Comment"
199+
})
200+
private void editComment() {
201+
final Comment comment = getPanel().getEditedComment();
202+
final String originalBody = comment.getBody();
203+
final String editedBody = CommentTabbedPanel.showDialog(Bundle.GitHubIssueController_edit_comment_title(), comment.getBody());
204+
if (editedBody != null) {
205+
final GitHubIssue issue = getPanel().getIssue();
206+
if (issue != null) {
207+
RequestProcessor rp = GitHubIssues.getInstance().getRequestProcessor();
208+
rp.post(new Runnable() {
209+
210+
@Override
211+
public void run() {
212+
comment.setBody(editedBody);
213+
Comment editComment = GitHubIssueSupport.editComment(issue.getRepository(), comment);
214+
if (editComment != null) {
215+
PegDownProcessor processor = new PegDownProcessor(Extensions.FENCED_CODE_BLOCKS);
216+
String body = editComment.getBody();
217+
String bodyHtml = processor.markdownToHtml(body);
218+
comment.setBodyHtml(String.format("<html>%s</html>", bodyHtml)); // NOI18N
219+
SwingUtilities.invokeLater(new Runnable() {
220+
221+
@Override
222+
public void run() {
223+
getPanel().loadComments();
224+
}
225+
});
226+
} else {
227+
comment.setBody(originalBody);
228+
}
229+
}
230+
});
231+
}
232+
}
233+
}
234+
191235
//~ inner classes
192236
public class SubmitIssueAction implements ActionListener {
193237

src/main/java/com/junichi11/netbeans/modules/github/issues/issue/GitHubIssueSupport.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,27 @@ private static Comment createComment(GitHubRepository gitHubRepository, Issue is
149149
return issueService.createComment(gitHubRepository.getRepository(), issue.getNumber(), comment);
150150
}
151151

152+
/**
153+
* Edit comment.
154+
*
155+
* @param repository GitHubRepository
156+
* @param comment Comment
157+
* @return comment if editing successfully, otherwise {@code null}
158+
*/
159+
@CheckForNull
160+
public static Comment editComment(GitHubRepository repository, Comment comment) {
161+
IssueService issueService = createIssueService(repository);
162+
if (issueService == null) {
163+
return null;
164+
}
165+
try {
166+
return issueService.editComment(repository.getRepository(), comment);
167+
} catch (IOException ex) {
168+
LOGGER.log(Level.WARNING, ex.getMessage());
169+
}
170+
return null;
171+
}
172+
152173
/**
153174
* Show in browser.
154175
*

src/main/java/com/junichi11/netbeans/modules/github/issues/issue/ui/CommentPanel.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,19 @@ private CommentPanel() {
7070
public CommentPanel(Comment comment) {
7171
this.comment = comment;
7272
initComponents();
73-
setUserName(comment.getUser().getLogin());
74-
setCreatedDate(comment.getCreatedAt());
75-
setUpdatedDate(comment.getUpdatedAt());
76-
setContent(comment.getBodyHtml());
73+
load();
7774

7875
// TODO
7976
deleteLinkButton.setEnabled(false);
80-
editLinkButton.setEnabled(false);
77+
}
78+
79+
final void load() {
80+
if (comment != null) {
81+
setUserName(comment.getUser().getLogin());
82+
setCreatedDate(comment.getCreatedAt());
83+
setUpdatedDate(comment.getUpdatedAt());
84+
setContent(comment.getBodyHtml());
85+
}
8186
}
8287

8388
private void setUserName(String name) {

src/main/java/com/junichi11/netbeans/modules/github/issues/issue/ui/CommentTabbedPanel.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
package com.junichi11.netbeans.modules.github.issues.issue.ui;
4343

4444
import java.awt.Component;
45+
import java.awt.Dialog;
46+
import org.openide.DialogDescriptor;
47+
import org.openide.DialogDisplayer;
4548
import org.pegdown.Extensions;
4649
import org.pegdown.PegDownProcessor;
4750

@@ -80,6 +83,27 @@ public void setEditable(boolean isEditable) {
8083
commentWriteTextArea.setEditable(isEditable);
8184
}
8285

86+
/**
87+
* Show dialog.
88+
*
89+
* @param title title
90+
* @param text comment
91+
* @return String if OK Button is clicked, otherwise {@code null}
92+
*/
93+
public static String showDialog(String title, String text) {
94+
CommentTabbedPanel panel = new CommentTabbedPanel();
95+
panel.setText(text);
96+
DialogDescriptor descriptor = new DialogDescriptor(panel, title, true, DialogDescriptor.OK_CANCEL_OPTION, null, null);
97+
Dialog dialog = DialogDisplayer.getDefault().createDialog(descriptor);
98+
dialog.pack();
99+
dialog.setVisible(true);
100+
dialog.dispose();
101+
if (descriptor.getValue() == DialogDescriptor.OK_OPTION) {
102+
return panel.getText();
103+
}
104+
return null;
105+
}
106+
83107
/**
84108
* This method is called from within the constructor to initialize the form.
85109
* WARNING: Do NOT modify this code. The content of this method is always

src/main/java/com/junichi11/netbeans/modules/github/issues/issue/ui/CommentsPanel.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.Collections;
5050
import java.util.List;
5151
import org.eclipse.egit.github.core.Comment;
52+
import org.eclipse.egit.github.core.User;
5253

5354
/**
5455
*
@@ -80,22 +81,31 @@ public List<Comment> getComments() {
8081
return comments;
8182
}
8283

83-
public void addComments(Collection<Comment> comments) {
84+
public void addComments(Collection<Comment> comments, String loginName) {
8485
for (Comment comment : comments) {
85-
addComment(comment);
86+
addComment(comment, loginName);
8687
}
8788
}
8889

89-
public void addComment(Comment comment) {
90+
public void addComment(Comment comment, String loginName) {
9091
if (comment == null) {
9192
return;
9293
}
9394
CommentPanel newPanel = new CommentPanel(comment);
95+
User user = comment.getUser();
96+
newPanel.setEditEnabled(isMyself(user, loginName));
9497
newPanel.addPropertyChangeListener(this);
9598
commentPanels.add(newPanel);
9699
add(newPanel);
97100
}
98101

102+
private boolean isMyself(User user, String me) {
103+
if (user == null || StringUtils.isEmpty(me)) {
104+
return false;
105+
}
106+
return user.getLogin().equals(me);
107+
}
108+
99109
public void removeAllComments() {
100110
synchronized (commentPanels) {
101111
for (CommentPanel comment : commentPanels) {
@@ -138,6 +148,14 @@ public void resetChangedPanels() {
138148
editedCommentPanel = null;
139149
}
140150

151+
public void loadComments() {
152+
synchronized (commentPanels) {
153+
for (CommentPanel commentPanel : commentPanels) {
154+
commentPanel.load();
155+
}
156+
}
157+
}
158+
141159
/**
142160
* This method is called from within the constructor to initialize the form.
143161
* WARNING: Do NOT modify this code. The content of this method is always

src/main/java/com/junichi11/netbeans/modules/github/issues/issue/ui/GitHubIssuePanel.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public void update() {
246246
String bodyHtml = processor.markdownToHtml(body);
247247
comment.setBodyHtml(String.format("<html>%s</html>", bodyHtml)); // NOI18N
248248
}
249-
commentsPanel.addComments(comments);
249+
commentsPanel.addComments(comments, repository.getUserName());
250250
}
251251
}
252252

@@ -258,6 +258,10 @@ public void update() {
258258
fireChange();
259259
}
260260

261+
public void loadComments() {
262+
commentsPanel.loadComments();
263+
}
264+
261265
private void setAssigneeSelected(User assignee) {
262266
int size = assigneeComboBoxModel.getSize();
263267
for (int i = 0; i < size; i++) {
@@ -407,6 +411,10 @@ public String getQuoteComment() {
407411
return commentsPanel.getQuoteComment();
408412
}
409413

414+
public Comment getEditedComment() {
415+
return commentsPanel.getEditedComment();
416+
}
417+
410418
public void addCommentsChangeListener(PropertyChangeListener listener) {
411419
commentsPanel.addPropertyChangeListener(listener);
412420
}

0 commit comments

Comments
 (0)