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.comment;
18  
19  import com.atlassian.theplugin.commons.crucible.ValueNotYetInitialized;
20  import com.atlassian.theplugin.commons.crucible.api.model.ReviewAdapter;
21  import com.atlassian.theplugin.idea.ui.tree.AtlassianClickAction;
22  import com.atlassian.theplugin.idea.ui.tree.AtlassianTreeNode;
23  import com.atlassian.theplugin.util.PluginUtil;
24  import com.intellij.ui.SimpleColoredComponent;
25  import com.intellij.ui.SimpleTextAttributes;
26  import com.jgoodies.forms.layout.CellConstraints;
27  import com.jgoodies.forms.layout.FormLayout;
28  
29  import javax.swing.*;
30  import javax.swing.tree.TreeCellRenderer;
31  import java.awt.*;
32  
33  /**
34   * Created by IntelliJ IDEA.
35   * User: lguminski
36   * Date: Jul 21, 2008
37   * Time: 3:57:40 PM
38   * To change this template use File | Settings | File Templates.
39   */
40  public class GeneralSectionNode extends AtlassianTreeNode {
41  	private static final String GENERAL_COMMENTS_SECTION = "General Comments";
42  
43  	private TreeCellRenderer myRenderer;
44  	private ReviewAdapter review;
45  
46  
47  	public GeneralSectionNode(ReviewAdapter review, AtlassianClickAction action) {
48  		super(action);
49  		this.review = review;
50  		initRenderer();
51  
52  	}
53  
54  	void initRenderer() {
55  			int noOfGeneralComments = 0;
56  		try {
57  			noOfGeneralComments = review.getGeneralComments().size();
58  		} catch (ValueNotYetInitialized valueNotYetInitialized) {
59  			PluginUtil.getLogger().error("No General Comments");
60  		}		
61  		this.myRenderer = new MyRenderer(noOfGeneralComments);
62  	}
63  
64  	public GeneralSectionNode(GeneralSectionNode node) {
65  		super(node.getAtlassianClickAction());
66  		this.review = node.review;
67  		initRenderer();
68  	}
69  
70  	@Override
71      public TreeCellRenderer getTreeCellRenderer() {
72  		return myRenderer;
73  	}
74  
75  	public ReviewAdapter getReview() {
76  		return review;
77  	}
78  
79  	@Override
80  	public AtlassianTreeNode getClone() {
81  		return new GeneralSectionNode(this);
82  	}
83  
84  	public int compareTo(Object o) {
85  		if (o instanceof GeneralSectionNode) {
86  			return 0;
87  		}
88  		return -1;
89  	}
90  
91  	private class MyRenderer implements TreeCellRenderer {
92  		private int noOfGeneralComments = 0;
93  
94  		MyRenderer(final int noOfGeneralComments) {
95  			this.noOfGeneralComments = noOfGeneralComments;
96  		}
97  		
98  		public Component getTreeCellRendererComponent(JTree tree, Object value, boolean isSelected, boolean expanded,
99                  boolean leaf, int row, boolean hasFocus) {
100 			GeneralSectionNode node = (GeneralSectionNode) value;
101 			JPanel panel = new JPanel(new FormLayout("4dlu, left:pref:grow, 4dlu", "4dlu, pref:grow, 4dlu"));
102 			SimpleColoredComponent component = new SimpleColoredComponent();
103 			component.append(getGeneralCommentsSection(), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
104 			panel.add(component, new CellConstraints(2, 2));
105 
106 			panel.setBorder(isSelected ? UIManager.getBorder("List.focusCellHighlightBorder")
107 					: BorderFactory.createEmptyBorder(1, 1, 1, 1));
108 
109 			return panel;
110 		}
111 
112 		private String getGeneralCommentsSection() {
113 			StringBuilder sb = new StringBuilder(GENERAL_COMMENTS_SECTION);
114 			sb.append(" (").append(noOfGeneralComments).append(")");
115 			return sb.toString();
116 		}
117 	}
118 }