View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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,
36  			int i, boolean c )
37  	{
38  		return tree.getCellRenderer().getTreeCellRendererComponent( tree, lastPathComponent, b, object, object2, i, c );
39  	}
40  
41  	public JComponent getComponent()
42  	{
43  		return tree;
44  	}
45  
46  	public void setDragInfo( String dropInfo )
47  	{
48  		tree.setToolTipText( dropInfo );
49  	}
50  
51  	public Rectangle getModelItemBounds( T path )
52  	{
53  		return tree.getRowBounds( getRowForModelItem( path ) );
54  	}
55  
56  	public T getModelItemForLocation( int x, int y )
57  	{
58  		int rowForLocation = tree.getRowForLocation( x, y );
59  		if( rowForLocation == -1 )
60  			rowForLocation = tree.getClosestRowForLocation( x, y );
61  
62  		return getModelItemAtRow( rowForLocation );
63  	}
64  
65  	public void selectModelItem( T path )
66  	{
67  		int row = getRowForModelItem( path );
68  		tree.setSelectionRow( row );
69  	}
70  
71  	public void toggleExpansion( T last )
72  	{
73  		int row = getRowForModelItem( last );
74  		if( tree.isExpanded( row ) )
75  			tree.collapseRow( row );
76  		else
77  			tree.expandRow( row );
78  	}
79  
80  	public abstract int getRowForModelItem( T modelItem );
81  
82  	public abstract T getModelItemAtRow( int row );
83  
84  }