View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / eviware.com 
3    *
4    *  SoapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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;
14  
15  import java.awt.Component;
16  import java.awt.Container;
17  import java.awt.Cursor;
18  import java.awt.Dimension;
19  import java.awt.Graphics;
20  import java.awt.LayoutManager;
21  
22  import javax.swing.ImageIcon;
23  import javax.swing.JButton;
24  import javax.swing.JComponent;
25  import javax.swing.JSplitPane;
26  import javax.swing.plaf.basic.BasicSplitPaneDivider;
27  import javax.swing.plaf.basic.BasicSplitPaneUI;
28  
29  import com.eviware.soapui.SoapUI;
30  
31  /***
32   * SplitPaneUI that draws nicer buttons and enables/disables them appropriately
33   *
34   * @author Ole.Matzura
35   */
36  
37  public class SoapUISplitPaneUI extends BasicSplitPaneUI
38  {
39  	private boolean hasBeenDragged;
40     private final static ImageIcon upArrow = SoapUI.createImageIcon( "/up_arrow.gif");
41     private final static ImageIcon leftArrow = SoapUI.createImageIcon( "/left_arrow.gif");
42     private final static ImageIcon rightArrow = SoapUI.createImageIcon( "/right_arrow.gif");
43     private final static ImageIcon downArrow = SoapUI.createImageIcon( "/down_arrow.gif");
44  
45     public SoapUISplitPaneUI( )
46     {
47        super();
48     }
49  
50  	protected void finishDraggingTo(int location)
51  	{
52  		super.finishDraggingTo(location);
53  		
54  		hasBeenDragged = true;
55  	}
56  
57  	public void resetToPreferredSizes(JSplitPane jc)
58  	{
59  		super.resetToPreferredSizes(jc);
60  		hasBeenDragged = false;
61  	}
62  
63  	public boolean hasBeenDragged()
64  	{
65  		return hasBeenDragged;
66  	}
67  
68  	public void setHasBeenDragged(boolean hasBeenDragged)
69  	{
70  		this.hasBeenDragged = hasBeenDragged;
71  	}
72  
73  	public BasicSplitPaneDivider createDefaultDivider()
74     {
75        return new SoapUIDivider( this );
76     }
77     
78     private class SoapUIDivider extends BasicSplitPaneDivider
79     {
80        public SoapUIDivider( BasicSplitPaneUI ui )
81        {
82           super( ui );
83  
84           setLayout( new SoapUIDividerLayout() );
85        }
86        
87        protected JButton createLeftOneTouchButton()
88        {
89        	if (getSplitPane().getOrientation() == JSplitPane.VERTICAL_SPLIT)
90        	{
91              JButton b = new JButton( upArrow );
92  
93              b.setMinimumSize( new Dimension( 8, 6 ) );
94              b.setFocusPainted( false );
95              b.setBorderPainted( false );
96              b.setRequestFocusEnabled( false );
97              b.setBorder( null );
98              b.setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) );
99  
100             return b;
101       	}
102       	else
103       	{
104             JButton b = new JButton( leftArrow );
105 
106             b.setMinimumSize( new Dimension( 6, 8 ) );
107             b.setFocusPainted( false );
108             b.setBorderPainted( false );
109             b.setRequestFocusEnabled( false );
110             b.setBorder( null );
111             b.setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) );
112 
113             return b;
114       	}
115       }
116 
117       protected JButton createRightOneTouchButton()
118       {
119       	if (getSplitPane().getOrientation() == JSplitPane.VERTICAL_SPLIT)
120       	{
121             JButton b = new JButton( downArrow );
122 
123             b.setMinimumSize( new Dimension( 8, 6 ) );
124             b.setFocusPainted( false );
125             b.setBorderPainted( false );
126             b.setRequestFocusEnabled( false );
127             b.setBorder( null );
128             b.setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) );
129 
130             return b;
131       	}
132       	else
133       	{
134             JButton b = new JButton( rightArrow );
135 
136             b.setMinimumSize( new Dimension( 6, 8 ) );
137             b.setFocusPainted( false );
138             b.setBorderPainted( false );
139             b.setRequestFocusEnabled( false );
140             b.setBorder( null );
141             b.setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) );
142 
143             return b;
144       	}
145       }
146 
147       protected class SoapUIDividerLayout implements LayoutManager
148       {
149       	private int lastOrientation;
150       	
151       	public SoapUIDividerLayout()
152       	{
153       		lastOrientation = getOrientation();
154       	}
155       	
156          public void layoutContainer( Container c )
157          {
158             if( lastOrientation != getOrientation() )
159             {
160             	leftButton.setIcon( getOrientation() == JSplitPane.VERTICAL_SPLIT ? upArrow : leftArrow );
161             	leftButton.setMinimumSize( getOrientation() == JSplitPane.VERTICAL_SPLIT ? 
162             			new Dimension( 8, 6 ) : new Dimension( 6, 8 ));
163             	
164             	rightButton.setIcon( getOrientation() == JSplitPane.VERTICAL_SPLIT ? downArrow : rightArrow );
165             	rightButton.setMinimumSize( getOrientation() == JSplitPane.VERTICAL_SPLIT ? 
166             			new Dimension( 8, 6 ) : new Dimension( 6, 8 ));
167             	
168             	lastOrientation = getOrientation();
169             }
170 
171             if (getOrientation() == JSplitPane.VERTICAL_SPLIT)
172          	{
173                leftButton.setBounds( 2, 2, 8, 6 );
174                rightButton.setBounds( 12, 2, 8, 6 );
175          	}
176             else
177             {
178                leftButton.setBounds( 2, 2, 6, 8 );
179                rightButton.setBounds( 2, 12, 6, 8 );
180          	}
181          }
182 
183          public Dimension preferredLayoutSize( Container c )
184          {
185             return minimumLayoutSize( c );
186          }
187 
188          public void removeLayoutComponent( Component c )
189          {
190          }
191 
192          public void addLayoutComponent( String string, Component c )
193          {
194          }
195 
196          public Dimension minimumLayoutSize( Container parent )
197          {
198             return new Dimension( 10, 10 );
199          }
200       }
201 
202 		public JButton getLeftButton()
203 		{
204 			return leftButton;
205 		}
206 		
207 		public JButton getRightButton()
208 		{
209 			return rightButton;
210 		}
211    }
212 
213 	public void setDividerLocation(JSplitPane jc, int location)
214 	{
215 		super.setDividerLocation(jc, location);
216 		enableOneTouchButtons(jc, location);
217 	}
218 	
219 	public void update(Graphics g, JComponent c)
220 	{
221 		super.update(g, c);
222 		enableOneTouchButtons( getSplitPane(), getSplitPane().getDividerLocation());
223 	}
224 
225 	private void enableOneTouchButtons(JSplitPane jc, int location)
226 	{
227 		JButton leftButton = ((SoapUIDivider)getDivider()).getLeftButton();
228 		JButton rightButton = ((SoapUIDivider)getDivider()).getRightButton();
229 		
230 		if( leftButton != null )
231 			leftButton.setEnabled( location > jc.getMinimumDividerLocation() && jc.getRightComponent().isVisible());
232 		
233 		if( rightButton != null )
234 			rightButton.setEnabled( location < jc.getMaximumDividerLocation() && jc.getLeftComponent().isVisible());
235 	}
236 }