View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.atlassian.theplugin.idea.ui.tree.file;
17  
18  import com.atlassian.theplugin.commons.crucible.api.model.CrucibleFileInfo;
19  import com.atlassian.theplugin.commons.crucible.api.model.CrucibleFileInfoImpl;
20  import com.atlassian.theplugin.commons.crucible.api.model.CrucibleReviewItemInfo;
21  import com.atlassian.theplugin.idea.crucible.ReviewAdapter;
22  import com.atlassian.theplugin.idea.ui.tree.AtlassianClickAction;
23  import com.atlassian.theplugin.idea.ui.tree.AtlassianTreeNode;
24  import com.intellij.openapi.fileTypes.FileType;
25  import com.intellij.openapi.fileTypes.FileTypeManager;
26  import com.intellij.openapi.vcs.FileStatus;
27  import com.intellij.ui.ColoredTreeCellRenderer;
28  import com.intellij.ui.SimpleTextAttributes;
29  import org.apache.commons.io.FilenameUtils;
30  
31  import javax.swing.*;
32  import javax.swing.tree.TreeCellRenderer;
33  import java.awt.*;
34  
35  public class CrucibleFileNode extends FileNode {
36  
37  	private CrucibleFileInfo file;
38  	private static final ColoredTreeCellRenderer MY_RENDERER = new CrucibleFileNodeRenderer();
39  	private ReviewAdapter review;
40  
41  	public CrucibleFileNode(final ReviewAdapter review, final CrucibleFileInfo file) {
42  		this(review, file, AtlassianClickAction.EMPTY_ACTION);
43  	}
44  
45  	public CrucibleFileNode(final ReviewAdapter review, final CrucibleFileInfo file,
46  			final AtlassianClickAction action) {
47  		super(FilenameUtils.getName(file.getFileDescriptor().getUrl()), action);
48  		this.review = review;
49  		this.file = file;
50  	}
51  
52  	public CrucibleFileNode(final CrucibleFileNode node) {
53  		this(node.getReview(), node.getFile(), node.getAtlassianClickAction());
54  	}
55  
56  	@Override
57  	public TreeCellRenderer getTreeCellRenderer() {
58  		return MY_RENDERER;
59  	}
60  
61  	public CrucibleFileInfo getFile() {
62  		return file;
63  	}
64  
65  	public ReviewAdapter getReview() {
66  		return review;
67  	}
68  
69  	public void setReview(ReviewAdapter review) {
70  		this.review = review;
71  		for (CrucibleReviewItemInfo info : review.getReviewItems()) {
72  			if (info.getId().equals(file.getItemInfo().getId())) {
73  				((CrucibleFileInfoImpl) file).setItemInfo(info);
74  				break;
75  			}
76  		}
77  	}
78  
79  	private static class CrucibleFileNodeRenderer extends ColoredTreeCellRenderer {
80  		private static final SimpleTextAttributes TEXT_ITALIC =
81  				new SimpleTextAttributes(SimpleTextAttributes.STYLE_ITALIC, null);
82  		private static final SimpleTextAttributes RED_ITALIC =
83  				new SimpleTextAttributes(SimpleTextAttributes.STYLE_ITALIC, Color.red);
84  
85  		public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded,
86  				boolean leaf, int row, boolean hasFocus) {
87  			CrucibleFileNode node = (CrucibleFileNode) value;
88  			append(node.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
89  
90  			StringBuilder txt = new StringBuilder();
91  			txt.append(" (rev: ");
92  			switch (node.getFile().getCommitType()) {
93  				case Added:
94  					txt.append(node.getFile().getFileDescriptor().getRevision());
95  					break;
96  				case Deleted:
97  					txt.append(node.getFile().getOldFileDescriptor().getRevision());
98  					break;
99  				case Modified:
100 				case Copied:
101 				case Moved:
102 					txt.append(node.getFile().getOldFileDescriptor().getRevision());
103 					txt.append("-");
104 					txt.append(node.getFile().getFileDescriptor().getRevision());
105 					break;
106 				case Unknown:
107 				default:
108 					txt.append(node.getFile().getOldFileDescriptor().getRevision());
109 					txt.append("-");
110 					txt.append(node.getFile().getFileDescriptor().getRevision());
111 					break;
112 			}
113 			txt.append(")");
114 			append(txt.toString(), SimpleTextAttributes.GRAY_ITALIC_ATTRIBUTES);
115 
116 			int noOfComments = node.getFile().getItemInfo().getNumberOfComments();
117 			if (noOfComments > 0) {
118 				int noOfDefects = node.getFile().getItemInfo().getNumberOfDefects();
119 				append(" ",
120 						TEXT_ITALIC);
121 				append(String.valueOf(noOfComments),
122 						TEXT_ITALIC);
123 				append(" comment", TEXT_ITALIC);
124 				if (noOfComments != 1) {
125 					append("s", TEXT_ITALIC);
126 				}
127 
128 				if (noOfDefects > 0) {
129 					append(" (", TEXT_ITALIC);
130 					append(String.valueOf(noOfDefects),
131 							RED_ITALIC);
132 					append(" defect",
133 							RED_ITALIC);
134 					if (noOfDefects != 1) {
135 						append("s",
136 								RED_ITALIC);
137 					}
138 					append(")", TEXT_ITALIC);
139 				}
140 			}
141 
142 			FileTypeManager mgr = FileTypeManager.getInstance();
143 			FileType type = mgr.getFileTypeByFileName(node.getName());
144 			setIcon(type.getIcon());
145 			switch (node.getFile().getCommitType()) {
146 				case Added:
147 					setForeground(FileStatus.COLOR_ADDED);
148 					break;
149 				case Deleted:
150 					setForeground(FileStatus.COLOR_MISSING);
151 					break;
152 				case Modified:
153 				case Moved:
154 				case Copied:
155 					setForeground(FileStatus.COLOR_MODIFIED);
156 					break;
157 				case Unknown:
158 				default:
159 					setForeground(FileStatus.COLOR_UNKNOWN);
160 					break;
161 			}
162 		}
163 	}
164 
165 	public AtlassianTreeNode getClone() {
166 		return new CrucibleFileNode(this);
167 	}
168 
169 	public boolean isCompactable() {
170 		return false;
171 	}
172 }