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  
17  package com.atlassian.theplugin.idea.ui.tree.file;
18  
19  import com.atlassian.theplugin.commons.crucible.ValueNotYetInitialized;
20  import com.atlassian.theplugin.commons.crucible.api.model.GeneralComment;
21  import com.atlassian.theplugin.idea.crucible.ReviewData;
22  import com.atlassian.theplugin.idea.ui.tree.AtlassianClickAction;
23  import com.atlassian.theplugin.idea.ui.tree.AtlassianTreeNode;
24  import com.intellij.ui.ColoredTreeCellRenderer;
25  import com.intellij.ui.SimpleTextAttributes;
26  import static com.intellij.ui.SimpleTextAttributes.STYLE_ITALIC;
27  import com.intellij.util.Icons;
28  
29  import javax.swing.*;
30  import javax.swing.tree.TreeCellRenderer;
31  import java.awt.*;
32  import java.util.List;
33  
34  /**
35   * Created by IntelliJ IDEA.
36   * User: lguminski
37   * Date: Jul 15, 2008
38   * Time: 6:03:26 AM
39   * To change this template use File | Settings | File Templates.
40   */
41  public class CrucibleChangeSetTitleNode extends FileNode {
42  	private ReviewData review;
43  	private static final TreeCellRenderer MY_RENDERER = new CrucibleChangeSetTitleNodeRenderer();
44  
45  	public CrucibleChangeSetTitleNode(ReviewData review, AtlassianClickAction action) {
46  		super(review.getName(), action);
47  		this.review = review;
48  	}
49  
50  	public CrucibleChangeSetTitleNode(final CrucibleChangeSetTitleNode node) {
51  		super(node.getReview().getName(), node.getAtlassianClickAction());
52  		this.review = node.review;
53  	}
54  
55  	@Override
56  	public TreeCellRenderer getTreeCellRenderer() {
57  		return MY_RENDERER;
58  	}
59  
60  	public ReviewData getReview() {
61  		return review;
62  	}
63  
64  	private static class CrucibleChangeSetTitleNodeRenderer extends ColoredTreeCellRenderer {
65  		private static final SimpleTextAttributes TEXT_ITALIC = new SimpleTextAttributes(STYLE_ITALIC, null);
66  		private static final SimpleTextAttributes RED_ITALIC = new SimpleTextAttributes(STYLE_ITALIC, Color.red);
67  
68  		public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded,
69  										  boolean leaf, int row, boolean hasFocus) {
70  			StringBuilder sb = new StringBuilder();
71  			CrucibleChangeSetTitleNode node = (CrucibleChangeSetTitleNode) value;
72  			append(node.getReview().getPermId().getId(), new SimpleTextAttributes(SimpleTextAttributes.STYLE_BOLD,
73  					Color.red));
74  			append(" ", SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
75  			append(node.getReview().getName(), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
76  			try {
77  				List<GeneralComment> generalComments = node.getReview().getGeneralComments();
78  				if (generalComments.size() > 0) {
79  					int noOfDefects = 0;
80  					for (GeneralComment comment : generalComments) {
81  						if (comment.isDefectRaised()) {
82  							noOfDefects++;
83  						}
84  					}
85  					append(" ",
86  							TEXT_ITALIC);
87  					append(String.valueOf(generalComments.size()),
88  							TEXT_ITALIC);
89  					append(" comment", TEXT_ITALIC);
90  					if (generalComments.size() != 1) {
91  						append("s", TEXT_ITALIC);
92  					}
93  					if (noOfDefects > 0) {
94  						append(" (", TEXT_ITALIC);
95  						append(String.valueOf(noOfDefects),
96  								RED_ITALIC);
97  						append(" defect", RED_ITALIC);
98  						if (noOfDefects != 1) {
99  							append("s", RED_ITALIC);
100 						}
101 						append(")", TEXT_ITALIC);
102 					}
103 				}
104 			} catch (ValueNotYetInitialized valueNotYetInitialized) {
105 				// ignore
106 			}
107 			setIcon(expanded ? Icons.DIRECTORY_OPEN_ICON : Icons.DIRECTORY_CLOSED_ICON);
108 		}
109 	}
110 
111 	public AtlassianTreeNode getClone() {
112 		return new CrucibleChangeSetTitleNode(this);
113 	}
114 }