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.crucible.ui;
17  
18  import com.atlassian.theplugin.idea.ui.tree.comment.CommentTreeNode;
19  import com.atlassian.theplugin.idea.util.IdeaIconProvider;
20  import com.atlassian.theplugin.util.ui.IconProvider;
21  import com.intellij.util.ui.UIUtil;
22  
23  import javax.swing.*;
24  import javax.swing.plaf.TreeUI;
25  import javax.swing.plaf.basic.BasicTreeUI;
26  import javax.swing.tree.DefaultMutableTreeNode;
27  import javax.swing.tree.DefaultTreeCellRenderer;
28  import javax.swing.tree.TreeCellRenderer;
29  import java.awt.*;
30  
31  public class ReviewCommentRenderer extends DefaultTreeCellRenderer implements TreeCellRenderer {
32  
33  	/**
34  	 * Useful for injecting your own IconProvider. Facilitates testing outside IDEA framework
35  	 *
36  	 * @param iconProvider provider used for retrieving icons
37  	 */
38  	public ReviewCommentRenderer(final IconProvider iconProvider) {
39  		reviewCommentPanel = new ReviewCommentPanel(iconProvider);
40  	}
41  
42  	/**
43  	 * uses default IDEA-specific icon provider. Not testable outside IDEA
44  	 */
45  	public ReviewCommentRenderer() {
46  		this(new IdeaIconProvider());
47  	}
48  
49  	private final ReviewCommentPanel reviewCommentPanel;
50  
51  	@Override
52  	public Component getTreeCellRendererComponent(JTree tree, Object value, final boolean isSelected, boolean expanded,
53  			boolean leaf, int row, boolean aHasFocus) {
54  		if (value instanceof CommentTreeNode) {
55  			final CommentTreeNode node = (CommentTreeNode) value;
56  			reviewCommentPanel.update(node.getReview(), node.getComment(), getAvailableWidth(node, tree),
57  					node.isExpanded(), isSelected, tree.getFont());
58  			return reviewCommentPanel;
59  		} else {
60  			return super.getTreeCellRendererComponent(tree, value, isSelected, expanded, leaf, row, aHasFocus);
61  		}
62  	}
63  
64  	private int getAvailableWidth(DefaultMutableTreeNode obj, JTree jtree) {
65  		int i1 = jtree.getInsets().left + jtree.getInsets().right + getNesting(jtree) * (obj.getLevel() + 1);
66  		return jtree.getVisibleRect().width - i1 - 2;
67  	}
68  
69  	private int getNesting(JTree jtree) {
70  		TreeUI treeui = jtree.getUI();
71  		if (treeui instanceof BasicTreeUI) {
72  			BasicTreeUI basictreeui = (BasicTreeUI) treeui;
73  			return basictreeui.getLeftChildIndent() + basictreeui.getRightChildIndent();
74  		} else {
75  			return (Integer) UIUtil.getTreeLeftChildIndent() + (Integer) UIUtil.getTreeRightChildIndent();
76  		}
77  	}
78  
79  }
80