View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.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   * 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 = UISupport.createImageIcon( "/up_arrow.gif" );
41  	private final static ImageIcon leftArrow = UISupport.createImageIcon( "/left_arrow.gif" );
42  	private final static ImageIcon rightArrow = UISupport.createImageIcon( "/right_arrow.gif" );
43  	private final static ImageIcon downArrow = UISupport.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  	public 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 					if( leftButton != null )
161 					{
162 						leftButton.setIcon( getOrientation() == JSplitPane.VERTICAL_SPLIT ? upArrow : leftArrow );
163 						leftButton.setMinimumSize( getOrientation() == JSplitPane.VERTICAL_SPLIT ? new Dimension( 8, 6 )
164 								: new Dimension( 6, 8 ) );
165 					}
166 
167 					if( rightButton != null )
168 					{
169 						rightButton.setIcon( getOrientation() == JSplitPane.VERTICAL_SPLIT ? downArrow : rightArrow );
170 						rightButton.setMinimumSize( getOrientation() == JSplitPane.VERTICAL_SPLIT ? new Dimension( 8, 6 )
171 								: new Dimension( 6, 8 ) );
172 					}
173 
174 					lastOrientation = getOrientation();
175 				}
176 
177 				if( getOrientation() == JSplitPane.VERTICAL_SPLIT )
178 				{
179 					if( leftButton != null )
180 						leftButton.setBounds( 2, 2, 8, 6 );
181 
182 					if( rightButton != null )
183 						rightButton.setBounds( 12, 2, 8, 6 );
184 				}
185 				else
186 				{
187 					if( leftButton != null )
188 						leftButton.setBounds( 2, 2, 6, 8 );
189 
190 					if( rightButton != null )
191 						rightButton.setBounds( 2, 12, 6, 8 );
192 				}
193 			}
194 
195 			public Dimension preferredLayoutSize( Container c )
196 			{
197 				return minimumLayoutSize( c );
198 			}
199 
200 			public void removeLayoutComponent( Component c )
201 			{
202 			}
203 
204 			public void addLayoutComponent( String string, Component c )
205 			{
206 			}
207 
208 			public Dimension minimumLayoutSize( Container parent )
209 			{
210 				return new Dimension( 10, 10 );
211 			}
212 		}
213 
214 		public JButton getLeftButton()
215 		{
216 			return leftButton;
217 		}
218 
219 		public JButton getRightButton()
220 		{
221 			return rightButton;
222 		}
223 	}
224 
225 	public void setDividerLocation( JSplitPane jc, int location )
226 	{
227 		super.setDividerLocation( jc, location );
228 		enableOneTouchButtons( jc, location );
229 	}
230 
231 	public void update( Graphics g, JComponent c )
232 	{
233 		super.update( g, c );
234 		enableOneTouchButtons( getSplitPane(), getSplitPane().getDividerLocation() );
235 	}
236 
237 	private void enableOneTouchButtons( JSplitPane jc, int location )
238 	{
239 		JButton leftButton = ( ( SoapUIDivider )getDivider() ).getLeftButton();
240 		JButton rightButton = ( ( SoapUIDivider )getDivider() ).getRightButton();
241 
242 		if( leftButton != null )
243 			leftButton.setEnabled( location > jc.getMinimumDividerLocation() && jc.getRightComponent() != null
244 					&& jc.getRightComponent().isVisible() );
245 
246 		if( rightButton != null )
247 			rightButton.setEnabled( location < jc.getMaximumDividerLocation() && jc.getLeftComponent() != null
248 					&& jc.getLeftComponent().isVisible() );
249 	}
250 }