Skip to content

Commit c0a43e3

Browse files
committed
Show user avatar #20
1 parent ded305a commit c0a43e3

7 files changed

Lines changed: 117 additions & 19 deletions

File tree

src/main/java/com/junichi11/netbeans/modules/github/issues/GitHubCache.java

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

4444
import com.junichi11.netbeans.modules.github.issues.repository.GitHubRepository;
45+
import java.awt.Image;
46+
import java.awt.image.BufferedImage;
4547
import java.io.IOException;
48+
import java.net.URL;
4649
import java.util.Collections;
4750
import java.util.HashMap;
4851
import java.util.List;
4952
import java.util.Map;
5053
import java.util.logging.Level;
5154
import java.util.logging.Logger;
55+
import javax.imageio.ImageIO;
56+
import javax.swing.Icon;
57+
import javax.swing.ImageIcon;
5258
import org.eclipse.egit.github.core.Label;
5359
import org.eclipse.egit.github.core.Milestone;
5460
import org.eclipse.egit.github.core.Repository;
@@ -58,6 +64,7 @@
5864
import org.eclipse.egit.github.core.service.LabelService;
5965
import org.eclipse.egit.github.core.service.MilestoneService;
6066
import org.eclipse.egit.github.core.service.UserService;
67+
import org.netbeans.api.annotations.common.CheckForNull;
6168
import org.netbeans.api.annotations.common.NonNull;
6269

6370
/**
@@ -71,6 +78,7 @@ public final class GitHubCache {
7178
private List<Milestone> milestones;
7279
private List<Label> labels;
7380
private User myself;
81+
private final Map<String, Icon> userIcons = new HashMap<>();
7482
private final GitHubRepository repository;
7583
private static final Logger LOGGER = Logger.getLogger(GitHubCache.class.getName());
7684

@@ -218,4 +226,43 @@ public User getMySelf() {
218226
return myself;
219227
}
220228

229+
/**
230+
* Get user icon. Icon size is 16x16.
231+
*
232+
* @param user User
233+
* @return user icon if it was got, otherwise {@code null}
234+
*/
235+
@CheckForNull
236+
public Icon getUserIcon(User user) {
237+
if (user == null) {
238+
return null;
239+
}
240+
String login = user.getLogin();
241+
Icon icon = userIcons.get(login);
242+
if (icon != null) {
243+
return icon;
244+
}
245+
GitHubClient client = repository.createGitHubClient();
246+
if (client == null) {
247+
return null;
248+
}
249+
UserService userService = new UserService(client);
250+
try {
251+
user = userService.getUser(login);
252+
String avatarUrl = user.getAvatarUrl();
253+
if (avatarUrl != null && !avatarUrl.isEmpty()) {
254+
URL url = new URL(avatarUrl);
255+
// resize to 16x16
256+
BufferedImage image = ImageIO.read(url);
257+
Image resizedImage = image.getScaledInstance(16, 16, Image.SCALE_SMOOTH);
258+
icon = new ImageIcon(resizedImage);
259+
userIcons.put(login, icon);
260+
return icon;
261+
}
262+
} catch (IOException ex) {
263+
LOGGER.log(Level.WARNING, ex.getMessage());
264+
}
265+
return null;
266+
}
267+
221268
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ public class GitHubIssueController implements IssueController, ChangeListener, P
7474

7575
private GitHubIssuePanel panel;
7676
private String errorMessage;
77+
private final String repositoryId;
7778

7879
public GitHubIssueController(GitHubIssue gitHubIssue) {
80+
repositoryId = gitHubIssue.getRepository().getID();
7981
getPanel().setIssue(gitHubIssue);
8082
getPanel().update();
8183
}
@@ -125,7 +127,7 @@ public void removePropertyChangeListener(PropertyChangeListener listener) {
125127

126128
private GitHubIssuePanel getPanel() {
127129
if (panel == null) {
128-
panel = new GitHubIssuePanel();
130+
panel = new GitHubIssuePanel(repositoryId);
129131
panel.addChangeListener(this);
130132
panel.addAction(getSubmitIssueAction());
131133
panel.addAction(getCommentAction());

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
*/
4242
package com.junichi11.netbeans.modules.github.issues.issue.ui;
4343

44+
import com.junichi11.netbeans.modules.github.issues.GitHubCache;
45+
import com.junichi11.netbeans.modules.github.issues.repository.GitHubRepository;
4446
import com.junichi11.netbeans.modules.github.issues.utils.DateUtils;
4547
import java.awt.Color;
4648
import java.awt.Font;
@@ -76,7 +78,7 @@ public AttributesViewPanel() {
7678
@NbBundle.Messages({
7779
"AttributesViewPanel.LBL.dueDate=Due date"
7880
})
79-
public void setAttributes(Issue issue) {
81+
public void setAttributes(Issue issue, GitHubRepository repository) {
8082
initAttributes();
8183
if (issue == null) {
8284
return;
@@ -88,6 +90,10 @@ public void setAttributes(Issue issue) {
8890
String login = assignee.getLogin();
8991
assigneeNameLabel.setText(login);
9092
assigneeNameLabel.setToolTipText(login);
93+
if (repository != null) {
94+
GitHubCache cache = GitHubCache.create(repository);
95+
assigneeNameLabel.setIcon(cache.getUserIcon(assignee));
96+
}
9197
}
9298

9399
// milestone

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.junichi11.netbeans.modules.github.issues.utils.UiUtils;
4646
import java.awt.Font;
4747
import java.util.Date;
48+
import javax.swing.Icon;
4849
import org.eclipse.egit.github.core.Comment;
4950
import org.openide.util.NbBundle;
5051

@@ -70,11 +71,11 @@ private CommentPanel() {
7071
initComponents();
7172
}
7273

73-
public CommentPanel(Comment comment) {
74+
public CommentPanel(Comment comment, Icon icon) {
7475
this.comment = comment;
7576
initComponents();
7677
init();
77-
load();
78+
load(icon);
7879
}
7980

8081
private void init() {
@@ -85,9 +86,10 @@ private void init() {
8586
previewLinkButton.setText(Bundle.CommentPanel_previewLinkButton_title_html());
8687
}
8788

88-
final void load() {
89+
final void load(Icon icon) {
8990
if (comment != null) {
9091
setUserName(comment.getUser().getLogin());
92+
setUserIcon(icon);
9193
setCreatedDate(comment.getCreatedAt());
9294
setUpdatedDate(comment.getUpdatedAt());
9395
setContent(comment.getBody());
@@ -98,6 +100,10 @@ private void setUserName(String name) {
98100
userLinkButton.setText(name);
99101
}
100102

103+
private void setUserIcon(Icon icon) {
104+
userLinkButton.setIcon(icon);
105+
}
106+
101107
private void setCreatedDate(Date date) {
102108
if (date != null) {
103109
createdDateLabel.setText(DateUtils.DEFAULT_DATE_TIME_FORMAT.format(date));

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
*/
4242
package com.junichi11.netbeans.modules.github.issues.issue.ui;
4343

44+
import com.junichi11.netbeans.modules.github.issues.GitHubCache;
45+
import com.junichi11.netbeans.modules.github.issues.repository.GitHubRepository;
4446
import com.junichi11.netbeans.modules.github.issues.utils.StringUtils;
4547
import java.beans.PropertyChangeEvent;
4648
import java.beans.PropertyChangeListener;
@@ -81,18 +83,20 @@ public List<Comment> getComments() {
8183
return comments;
8284
}
8385

84-
public void addComments(Collection<Comment> comments, String loginName) {
86+
public void addComments(Collection<Comment> comments, GitHubRepository repository) {
8587
for (Comment comment : comments) {
86-
addComment(comment, loginName);
88+
addComment(comment, repository);
8789
}
8890
}
8991

90-
public void addComment(Comment comment, String loginName) {
91-
if (comment == null) {
92+
public void addComment(Comment comment, GitHubRepository repository) {
93+
if (comment == null || repository == null) {
9294
return;
9395
}
94-
CommentPanel newPanel = new CommentPanel(comment);
96+
GitHubCache cache = GitHubCache.create(repository);
9597
User owner = comment.getUser();
98+
String loginName = repository.getUserName();
99+
CommentPanel newPanel = new CommentPanel(comment, cache.getUserIcon(owner));
96100
boolean isMyself = isMyself(owner, loginName);
97101
newPanel.setEditEnabled(isMyself);
98102
newPanel.setDeleteEnabled(isMyself);
@@ -150,10 +154,14 @@ public void resetChangedPanels() {
150154
editedCommentPanel = null;
151155
}
152156

153-
public void loadComments() {
157+
public void loadComments(GitHubRepository repository) {
158+
assert repository != null;
159+
GitHubCache cache = GitHubCache.create(repository);
154160
synchronized (commentPanels) {
155161
for (CommentPanel commentPanel : commentPanels) {
156-
commentPanel.load();
162+
Comment comment = commentPanel.getComment();
163+
User user = comment.getUser();
164+
commentPanel.load(cache.getUserIcon(user));
157165
}
158166
}
159167
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,21 @@ public class GitHubIssuePanel extends JPanel {
105105
private static final Icon ICON_32 = ImageUtilities.loadImageIcon("com/junichi11/netbeans/modules/github/issues/resources/icon_32.png", true); // NOI18N
106106
private static final Logger LOGGER = Logger.getLogger(GitHubIssuePanel.class.getName());
107107
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // NOI18N
108+
private final String repositoryId;
108109

109110
/**
110111
* Creates new form GitHubIssuePanel
111112
*/
112-
public GitHubIssuePanel() {
113+
public GitHubIssuePanel(String repositoryId) {
114+
this.repositoryId = repositoryId;
113115
initComponents();
114116
init();
115117
}
116118

117119
private void init() {
118120
// set cell renderer
119121
milestoneComboBox.setRenderer(new AttributesListCellRenderer(milestoneComboBox.getRenderer()));
120-
assigneeComboBox.setRenderer(new AttributesListCellRenderer(assigneeComboBox.getRenderer()));
122+
assigneeComboBox.setRenderer(new AttributesListCellRenderer(assigneeComboBox.getRenderer(), repositoryId));
121123
labelsList.setCellRenderer(new AttributesListCellRenderer(labelsList.getCellRenderer()));
122124
milestoneComboBox.setModel(milestoneComboBoxModel);
123125
assigneeComboBox.setModel(assigneeComboBoxModel);
@@ -197,10 +199,16 @@ public void update() {
197199
Issue issue = gitHubIssue.getIssue();
198200
if (issue != null) {
199201
// set existing info
202+
// user infomation
203+
User user = issue.getUser();
204+
GitHubCache cache = GitHubCache.create(repository);
205+
Icon userIcon = cache.getUserIcon(user);
206+
200207
// header
201208
headerCreatedDateLabel.setText(DATE_FORMAT.format(issue.getCreatedAt()));
202209
headerUpdatedDateLabel.setText(DATE_FORMAT.format(issue.getUpdatedAt()));
203-
headerCreatedByUserLabel.setText(issue.getUser().getLogin());
210+
headerCreatedByUserLabel.setText(user.getLogin());
211+
headerCreatedByUserLabel.setIcon(userIcon);
204212

205213
// title
206214
titleTextField.setText(issue.getTitle());
@@ -230,7 +238,7 @@ public void update() {
230238
}
231239

232240
// set attributes
233-
attributesViewPanel.setAttributes(issue);
241+
attributesViewPanel.setAttributes(issue, repository);
234242

235243
// new comment
236244
GitHubIssueState state = GitHubIssueState.toEnum(issue.getState());
@@ -250,7 +258,7 @@ public void update() {
250258
String bodyHtml = processor.markdownToHtml(body);
251259
comment.setBodyHtml(String.format("<html>%s</html>", bodyHtml)); // NOI18N
252260
}
253-
commentsPanel.addComments(comments, repository.getUserName());
261+
commentsPanel.addComments(comments, repository);
254262
}
255263
}
256264

@@ -281,7 +289,7 @@ private void updateLables(GitHubCache cache, boolean force) {
281289
}
282290

283291
public void loadComments() {
284-
commentsPanel.loadComments();
292+
commentsPanel.loadComments(getRepository());
285293
fireChange();
286294
}
287295

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@
4141
*/
4242
package com.junichi11.netbeans.modules.github.issues.ui;
4343

44+
import com.junichi11.netbeans.modules.github.issues.GitHubCache;
45+
import com.junichi11.netbeans.modules.github.issues.repository.GitHubRepository;
46+
import com.junichi11.netbeans.modules.github.issues.repository.GitHubRepositoryManager;
4447
import java.awt.Component;
4548
import javax.swing.DefaultListCellRenderer;
49+
import javax.swing.Icon;
50+
import javax.swing.JLabel;
4651
import javax.swing.JList;
4752
import javax.swing.ListCellRenderer;
4853
import org.eclipse.egit.github.core.Label;
@@ -58,28 +63,44 @@ public class AttributesListCellRenderer extends DefaultListCellRenderer {
5863
private static final long serialVersionUID = -7361150610203379058L;
5964

6065
private final ListCellRenderer renderer;
66+
private final String repositoryId;
6167

6268
public AttributesListCellRenderer(ListCellRenderer renderer) {
69+
this(renderer, null);
70+
}
71+
72+
public AttributesListCellRenderer(ListCellRenderer renderer, String repositoryId) {
6373
this.renderer = renderer;
74+
this.repositoryId = repositoryId;
6475
}
6576

6677
@Override
6778
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
6879
String text = null;
80+
Icon icon = null;
6981
if (value instanceof Milestone) {
7082
Milestone milestone = (Milestone) value;
7183
text = milestone.getTitle();
7284
} else if (value instanceof User) {
7385
User user = (User) value;
7486
text = user.getLogin();
87+
if (text != null && repositoryId != null && !repositoryId.isEmpty()) {
88+
GitHubRepository repository = GitHubRepositoryManager.getInstance().getRepository(repositoryId);
89+
if (repository != null) {
90+
GitHubCache cache = GitHubCache.create(repository);
91+
icon = cache.getUserIcon(user);
92+
}
93+
}
7594
} else if (value instanceof Label) {
7695
Label label = (Label) value;
7796
text = label.getName();
7897
}
7998
if (text == null) {
8099
text = " "; // NOI18N
81100
}
82-
return renderer.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
101+
JLabel label = (JLabel) renderer.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
102+
label.setIcon(icon);
103+
return label;
83104
}
84105

85106
}

0 commit comments

Comments
 (0)