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.ValueNotYetInitialized;
19  import com.atlassian.theplugin.commons.crucible.api.model.CrucibleFileInfo;
20  import com.atlassian.theplugin.idea.crucible.ReviewData;
21  import com.atlassian.theplugin.idea.ui.tree.AtlassianClickAction;
22  import com.atlassian.theplugin.idea.ui.tree.AtlassianTreeNode;
23  import com.intellij.openapi.fileTypes.FileType;
24  import com.intellij.openapi.fileTypes.FileTypeManager;
25  import com.intellij.openapi.vcs.FileStatus;
26  import com.intellij.ui.ColoredTreeCellRenderer;
27  import com.intellij.ui.SimpleTextAttributes;
28  import org.apache.commons.io.FilenameUtils;
29  
30  import javax.swing.*;
31  import javax.swing.tree.TreeCellRenderer;
32  import java.awt.*;
33  
34  /**
35   * Created by IntelliJ IDEA.
36   * User: lguminski
37   * Date: Jul 14, 2008
38   * Time: 11:07:42 PM
39   * To change this template use File | Settings | File Templates.
40   */
41  public class CrucibleFileNode extends FileNode {
42  
43  	private CrucibleFileInfo file;
44  	private static final ColoredTreeCellRenderer MY_RENDERER = new CrucibleFileNodeRenderer();
45  	private ReviewData review;
46  
47  	public CrucibleFileNode(final ReviewData review, final CrucibleFileInfo file) {
48  		this(review, file, AtlassianClickAction.EMPTY_ACTION);
49  	}
50  
51  	public CrucibleFileNode(final ReviewData review, final CrucibleFileInfo file,
52  			final AtlassianClickAction action) {
53  		super(FilenameUtils.getName(file.getFileDescriptor().getUrl()), action);
54  		this.review = review;
55  		this.file = file;
56  	}
57  
58  	public CrucibleFileNode(final CrucibleFileNode node) {
59  		this(node.getReview(), node.getFile(), node.getAtlassianClickAction());
60  	}
61  
62  	@Override
63  	public TreeCellRenderer getTreeCellRenderer() {
64  		return MY_RENDERER;
65  	}
66  
67  	public CrucibleFileInfo getFile() {
68  		return file;
69  	}
70  
71  	public ReviewData getReview() {
72  		return review;
73  	}
74  
75  	private static class CrucibleFileNodeRenderer extends ColoredTreeCellRenderer {
76  		private static final SimpleTextAttributes TEXT_ITALIC =
77  				new SimpleTextAttributes(SimpleTextAttributes.STYLE_ITALIC, null);
78  		private static final SimpleTextAttributes RED_ITALIC =
79  				new SimpleTextAttributes(SimpleTextAttributes.STYLE_ITALIC, Color.red);
80  
81  		public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded,
82  				boolean leaf, int row, boolean hasFocus) {
83  			CrucibleFileNode node = (CrucibleFileNode) value;
84  			append(node.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
85  
86  			StringBuilder txt = new StringBuilder();
87  			txt.append(" (rev: ");
88  			switch (node.getFile().getCommitType()) {
89  				case Added:
90  					txt.append(node.getFile().getFileDescriptor().getRevision());
91  					break;
92  				case Deleted:
93  					txt.append(node.getFile().getOldFileDescriptor().getRevision());
94  					break;
95  				case Modified:
96  				case Copied:
97  				case Moved:
98  					txt.append(node.getFile().getOldFileDescriptor().getRevision());
99  					txt.append("-");
100 					txt.append(node.getFile().getFileDescriptor().getRevision());
101 					break;
102 				case Unknown:
103 				default:
104 					txt.append(node.getFile().getOldFileDescriptor().getRevision());
105 					txt.append("-");
106 					txt.append(node.getFile().getFileDescriptor().getRevision());
107 					break;
108 			}
109 			txt.append(")");
110 			append(txt.toString(), SimpleTextAttributes.GRAY_ITALIC_ATTRIBUTES);
111 			try {
112 
113 				int noOfComments = node.getFile().getNumberOfComments();
114 				if (noOfComments > 0) {
115 					int noOfDefects = node.getFile().getNumberOfDefects();
116 					append(" ",
117 							TEXT_ITALIC);
118 					append(String.valueOf(noOfComments),
119 							TEXT_ITALIC);
120 					append(" comment", TEXT_ITALIC);
121 					if (noOfComments != 1) {
122 						append("s", TEXT_ITALIC);
123 					}
124 
125 					if (noOfDefects > 0) {
126 						append(" (", TEXT_ITALIC);
127 						append(String.valueOf(noOfDefects),
128 								RED_ITALIC);
129 						append(" defect",
130 								RED_ITALIC);
131 						if (noOfDefects != 1) {
132 							append("s",
133 									RED_ITALIC);
134 						}
135 						append(")", TEXT_ITALIC);
136 					}
137 				}
138 			} catch (ValueNotYetInitialized valueNotYetInitialized) {
139 				// TODO lguminsk do something reasonable with this
140 				valueNotYetInitialized.printStackTrace();
141 			}
142 
143 			FileTypeManager mgr = FileTypeManager.getInstance();
144 			FileType type = mgr.getFileTypeByFileName(node.getName());
145 			setIcon(type.getIcon());
146 			switch (node.getFile().getCommitType()) {
147 				case Added:
148 					setForeground(FileStatus.COLOR_ADDED);
149 					break;
150 				case Deleted:
151 					setForeground(FileStatus.COLOR_MISSING);
152 					break;
153 				case Modified:
154 				case Moved:
155 				case Copied:
156 					setForeground(FileStatus.COLOR_MODIFIED);
157 					break;
158 				case Unknown:
159 				default:
160 					setForeground(FileStatus.COLOR_UNKNOWN);
161 					break;
162 			}
163 		}
164 	}
165 
166 	public AtlassianTreeNode getClone() {
167 		return new CrucibleFileNode(this);
168 	}
169 }