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;
18  
19  import javax.swing.*;
20  import javax.swing.plaf.basic.BasicTreeUI;
21  import javax.swing.tree.AbstractLayoutCache;
22  import javax.swing.tree.DefaultTreeCellRenderer;
23  import javax.swing.tree.TreeCellRenderer;
24  import java.awt.*;
25  
26  /**
27  	 * Author: Craig Wood
28   * Piece of code from http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=2&t=015891
29   */
30  class BasicWideNodeTreeUI extends BasicTreeUI {
31  	private int lastWidth;
32  	private boolean leftToRight;
33  	protected JTree tree;
34  
35  	@Override
36  	public void installUI(JComponent c) {
37  		if (c == null) {
38  			throw new NullPointerException("null component passed to BasicTreeUI.installUI()");
39  		}
40  		tree = (JTree) c;
41  		super.installUI(c);
42  	}
43  
44  	@Override
45  	protected void prepareForUIInstall() {
46  		super.prepareForUIInstall();
47  		leftToRight = tree.getComponentOrientation().isLeftToRight();
48  		Container parent = tree.getParent();
49  		if (parent != null) {
50  			lastWidth = parent.getWidth();
51  		}
52  	}
53  
54  	@Override
55  	protected TreeCellRenderer createDefaultCellRenderer() {
56  		return new DefaultTreeCellRenderer();
57  	}
58  
59  	@Override
60  	protected AbstractLayoutCache.NodeDimensions createNodeDimensions() {
61  		return new NodeDimensionsHandler();
62  	}
63  
64  	public class NodeDimensionsHandler extends AbstractLayoutCache.NodeDimensions {
65  		public Rectangle getNodeDimensions(Object value, int row, int depth,
66  										   boolean expanded, Rectangle size) {
67  
68  			// Return size of editing component, if editing and asking
69  			// for editing row.
70  			if (editingComponent != null && editingRow == row) {
71  				Dimension prefSize = editingComponent.getPreferredSize();
72  				int rh = getRowHeight();
73  
74  				if (rh > 0 && rh != prefSize.height) {
75  					prefSize.height = rh;
76  }
77  if (size != null) {
78  					size.x = getRowX(row, depth);
79  					size.width = prefSize.width;
80  					size.height = prefSize.height;
81  				} else {
82  					size = new Rectangle(getRowX(row, depth), 0,
83  							prefSize.width, prefSize.height);
84  				}
85  
86  				if (!leftToRight) {
87  					size.x = lastWidth - size.width - size.x - 2;
88  				}
89  				return size;
90  			}
91  			// Not editing, use renderer.
92  			if (currentCellRenderer != null) {
93  				Component aComponent;
94  
95  				aComponent = currentCellRenderer.getTreeCellRendererComponent(tree, value, tree.isRowSelected(row),
96  						expanded, treeModel.isLeaf(value), row, false);
97  if (tree != null) {
98  					// Only ever removed when UI changes, this is OK!
99  					rendererPane.add(aComponent);
100 					aComponent.validate();
101 				}
102 				Dimension prefSize = aComponent.getPreferredSize();
103 
104 				if (size != null) {
105 					size.x = getRowX(row, depth);
106 					size.width = //prefSize.width;
107 							lastWidth - size.x; // <*** the only change
108 					size.height = prefSize.height;
109 				} else {
110 					size = new Rectangle(getRowX(row, depth), 0,
111 							prefSize.width, prefSize.height);
112 				}
113 
114 				if (!leftToRight) {
115 					size.x = lastWidth - size.width - size.x - 2;
116 				}
117 				return size;
118 			}
119 			return null;
120 		}
121 	}
122 }