Skip to content

Commit 11de4be

Browse files
committed
Add a feature to delete a comment
1 parent cca035c commit 11de4be

5 files changed

Lines changed: 82 additions & 8 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.junichi11.netbeans.modules.github.issues.issue.ui.CommentTabbedPanel;
4646
import com.junichi11.netbeans.modules.github.issues.issue.ui.CommentsPanel;
4747
import com.junichi11.netbeans.modules.github.issues.issue.ui.GitHubIssuePanel;
48+
import com.junichi11.netbeans.modules.github.issues.repository.GitHubRepository;
4849
import com.junichi11.netbeans.modules.github.issues.utils.StringUtils;
4950
import com.junichi11.netbeans.modules.github.issues.utils.UiUtils;
5051
import java.awt.event.ActionEvent;
@@ -189,6 +190,9 @@ public void propertyChange(PropertyChangeEvent evt) {
189190
case CommentsPanel.PROP_COMMENT_EDITED:
190191
editComment();
191192
break;
193+
case CommentsPanel.PROP_COMMENT_DELETED:
194+
deleteComment();
195+
break;
192196
default:
193197
break;
194198
}
@@ -232,6 +236,39 @@ public void run() {
232236
}
233237
}
234238

239+
@NbBundle.Messages({
240+
"GitHubIssueController.delete.comment.fail=Can't delete this issue."
241+
})
242+
private void deleteComment() {
243+
final Comment deletedComment = getPanel().getDeletedComment();
244+
if (deletedComment == null) {
245+
return;
246+
}
247+
RequestProcessor rp = GitHubIssues.getInstance().getRequestProcessor();
248+
rp.post(new Runnable() {
249+
250+
@Override
251+
public void run() {
252+
GitHubRepository repository = getPanel().getIssue().getRepository();
253+
final boolean success = GitHubIssueSupport.deleteComment(repository, deletedComment);
254+
255+
SwingUtilities.invokeLater(new Runnable() {
256+
257+
@Override
258+
public void run() {
259+
if (success) {
260+
// remove comment panel
261+
getPanel().removeDeletedComment();
262+
} else {
263+
// show error message
264+
UiUtils.showErrorDialog(Bundle.GitHubIssueController_delete_comment_fail());
265+
}
266+
}
267+
});
268+
}
269+
});
270+
}
271+
235272
//~ inner classes
236273
public class SubmitIssueAction implements ActionListener {
237274

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,28 @@ public static Comment editComment(GitHubRepository repository, Comment comment)
170170
return null;
171171
}
172172

173+
/**
174+
* Delete comment.
175+
*
176+
* @param repository
177+
* @param comment
178+
* @return {@code true} if comment is deleted, otherwise {@code false}
179+
*/
180+
public static boolean deleteComment(GitHubRepository repository, Comment comment) {
181+
IssueService issueService = createIssueService(repository);
182+
if (issueService == null) {
183+
return false;
184+
}
185+
boolean success = true;
186+
try {
187+
issueService.deleteComment(repository.getRepository(), comment.getId());
188+
} catch (IOException ex) {
189+
success = false;
190+
LOGGER.log(Level.WARNING, ex.getMessage());
191+
}
192+
return success;
193+
}
194+
173195
/**
174196
* Show in browser.
175197
*

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ public CommentPanel(Comment comment) {
7171
this.comment = comment;
7272
initComponents();
7373
load();
74-
75-
// TODO
76-
deleteLinkButton.setEnabled(false);
7774
}
7875

7976
final void load() {

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,20 @@ public void addComment(Comment comment, String loginName) {
9292
return;
9393
}
9494
CommentPanel newPanel = new CommentPanel(comment);
95-
User user = comment.getUser();
96-
newPanel.setEditEnabled(isMyself(user, loginName));
95+
User owner = comment.getUser();
96+
boolean isMyself = isMyself(owner, loginName);
97+
newPanel.setEditEnabled(isMyself);
98+
newPanel.setDeleteEnabled(isMyself);
9799
newPanel.addPropertyChangeListener(this);
98100
commentPanels.add(newPanel);
99101
add(newPanel);
100102
}
101103

102-
private boolean isMyself(User user, String me) {
103-
if (user == null || StringUtils.isEmpty(me)) {
104+
private boolean isMyself(User owner, String me) {
105+
if (owner == null || StringUtils.isEmpty(me)) {
104106
return false;
105107
}
106-
return user.getLogin().equals(me);
108+
return owner.getLogin().equals(me);
107109
}
108110

109111
public void removeAllComments() {
@@ -156,6 +158,14 @@ public void loadComments() {
156158
}
157159
}
158160

161+
public void removeDeletedCommlent() {
162+
if (deletedCommentPanel != null) {
163+
deletedCommentPanel.setVisible(false);
164+
removeComment(deletedCommentPanel);
165+
deletedCommentPanel = null;
166+
}
167+
}
168+
159169
/**
160170
* This method is called from within the constructor to initialize the form.
161171
* 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,14 @@ public Comment getEditedComment() {
415415
return commentsPanel.getEditedComment();
416416
}
417417

418+
public Comment getDeletedComment() {
419+
return commentsPanel.getDeletedComment();
420+
}
421+
422+
public void removeDeletedComment() {
423+
commentsPanel.removeDeletedCommlent();
424+
}
425+
418426
public void addCommentsChangeListener(PropertyChangeListener listener) {
419427
commentsPanel.addPropertyChangeListener(listener);
420428
}

0 commit comments

Comments
 (0)