View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.support.dnd;
14  
15  import java.awt.Component;
16  import java.awt.Rectangle;
17  
18  import javax.swing.JComponent;
19  import javax.swing.JTree;
20  
21  public abstract class JTreeDragAndDropable<T> implements SoapUIDragAndDropable<T>
22  {
23  	private JTree tree;
24  	
25  	public JTreeDragAndDropable( JTree tree )
26  	{
27  		this.tree = tree;
28  	}
29  	
30  	public JTree getTree()
31  	{
32  		return tree;
33  	}
34  	
35  	public Component getCellRendererComponent( Object lastPathComponent, boolean b, boolean object, boolean object2, int i, boolean c )
36  	{
37  		return tree.getCellRenderer().getTreeCellRendererComponent( tree, lastPathComponent, b, object, object2, i, c );
38  	}
39  
40  	public JComponent getComponent()
41  	{
42  		return tree;
43  	}
44  
45  	public void setDragInfo( String dropInfo )
46  	{
47  		tree.setToolTipText( dropInfo );
48  	}
49  
50  	public Rectangle getModelItemBounds( T path )
51  	{
52  		return tree.getRowBounds( getRowForModelItem( path ) );
53  	}
54  
55  	public T getModelItemForLocation( int x, int y )
56  	{
57  		int rowForLocation = tree.getRowForLocation( x, y );
58  		if( rowForLocation == -1 )
59  			rowForLocation = tree.getClosestRowForLocation( x, y );
60  		
61  		return getModelItemAtRow( rowForLocation);
62  	}
63  
64  	public void selectModelItem( T path )
65  	{
66  		int row = getRowForModelItem( path );
67  		tree.setSelectionRow( row );
68  	}
69  
70  	public void toggleExpansion( T last )
71  	{
72  		int row = getRowForModelItem( last );
73  		if( tree.isExpanded( row ))
74  			tree.collapseRow( row );
75  		else
76  			tree.expandRow( row );
77  	}
78  	
79  	public abstract int getRowForModelItem( T modelItem );
80  	
81  	public abstract T getModelItemAtRow( int row );
82  	
83  }