View Javadoc

1   package com.atlassian.theplugin.idea.crucible;
2   
3   import com.intellij.openapi.util.IconLoader;
4   import com.atlassian.theplugin.commons.crucible.api.model.ReviewItem;
5   import com.atlassian.theplugin.commons.crucible.api.model.GeneralComment;
6   
7   import javax.swing.tree.DefaultTreeCellRenderer;
8   import javax.swing.*;
9   import java.awt.*;
10  
11  /**
12   * Created by IntelliJ IDEA.
13   * User: pmaruszak
14   * Date: Jun 12, 2008
15   * Time: 1:28:52 PM
16   * To change this template use File | Settings | File Templates.
17   */
18  public class CrucibleTreeRenderer extends DefaultTreeCellRenderer {
19  
20  	private final static String MODIFIED_FILE_STR = "(mod)";
21  	private final static String NEW_FILE_STR = "(new)";
22  	private final static String DELETED_FILE_STR = "(del)";
23  	private final static String UNKNOWN_FILE_STR = "(???)";
24  
25  
26  	private static Icon crucibleServersIcon;
27  	private static Icon crucibleServerEnabledIcon;
28  	private static Icon crucibleServerDisabledIcon;
29  	private static Icon crucibleSelectNoneIcon;
30  	private static Icon crucibleSelectAllIcon;
31  	private static final int DEFAULT_COMMENT_MESSAGE_LENGTH = 20;
32  
33  	static {
34  
35  		crucibleServersIcon = IconLoader.getIcon("/icons/crucible-blue-16.png");
36  		crucibleServerEnabledIcon = IconLoader.getIcon("/icons/crucible-blue-16.png");
37  		crucibleServerDisabledIcon = IconLoader.getIcon("/icons/crucible-grey-16.png");
38  		crucibleSelectNoneIcon = IconLoader.getIcon("/icons/select_none.gif");
39  		crucibleSelectAllIcon = IconLoader.getIcon("/icons/select_all.gif");
40  
41  	}
42  
43  
44  	public Component getTreeCellRendererComponent(
45              JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
46  
47  		JLabel label = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
48  		StringBuffer labelText = new StringBuffer() ;
49  
50  		if (value instanceof ReviewItemDataNode) {
51  			ReviewItem item =  ((ReviewItemDataNode)value).getReviewItem();
52  
53  			if (item.getFromPath().length() > 0 && item.getToPath().length() == 0){
54  				//new file
55  				labelText.append(getFileNameFromPath(item.getFromPath()));
56  				labelText.append(NEW_FILE_STR);
57  
58  			} else if (item.getFromPath().length() > 0 && item.getToPath().length() > 0){
59  
60  				labelText.append(getFileNameFromPath(item.getToPath()));
61  				labelText.append(" ").append(MODIFIED_FILE_STR);
62  
63  			} else if (item.getFromPath().length() == 0 && item.getToPath().length() > 0){
64  				labelText.append(item.getToPath());
65  				labelText.append(" ").append(DELETED_FILE_STR);
66  
67  			} else {
68  				labelText.append((item.getFromPath().length() > 0 ? getFileNameFromPath(item.getFromPath()) : getFileNameFromPath(item.getToPath())));
69  				labelText.append(" ").append(UNKNOWN_FILE_STR);
70  			}
71  
72  			label.setText(labelText.toString());
73  			label.setIcon(crucibleSelectNoneIcon);
74  
75  			}
76  		if (value instanceof CrucibleTreeRootNode) {
77  			ReviewDataInfoAdapter adapter = ((CrucibleTreeRootNode) value).getReviewDataInfoAdapter();
78  			if (adapter != null) {
79  
80  
81  				labelText.append(adapter.getName());
82  			}
83  			label.setText(labelText.toString());
84  			label.setIcon(crucibleServerEnabledIcon);
85  		}
86  
87  		if (value instanceof CommentNode){
88  			GeneralComment generalComment = ((CommentNode) value).getGeneralComment();
89  			if (generalComment != null) {
90  				labelText.append(generalComment.getMessage().substring(0, Math.min(generalComment.getMessage().length(),DEFAULT_COMMENT_MESSAGE_LENGTH)));
91  				if (generalComment.getMessage().length() > DEFAULT_COMMENT_MESSAGE_LENGTH) {
92  					labelText.append("...");
93  				}
94  				labelText.append(" (").append(generalComment.getUser()).append(")");
95  			}
96  			label.setIcon(crucibleSelectNoneIcon);
97  		}
98  		
99  
100 		return label;
101 	}
102 
103 	private String getFileNameFromPath(String filePath){
104 		
105 		return filePath.substring(filePath.lastIndexOf("/") + 1);
106 	};
107 
108 }
109