View Javadoc

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