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.actions;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Dimension;
17  import java.awt.event.ActionEvent;
18  import java.awt.event.KeyAdapter;
19  import java.awt.event.KeyEvent;
20  import java.awt.event.MouseAdapter;
21  import java.awt.event.MouseEvent;
22  import java.awt.event.WindowAdapter;
23  import java.awt.event.WindowEvent;
24  
25  import javax.swing.AbstractAction;
26  import javax.swing.BorderFactory;
27  import javax.swing.JDialog;
28  import javax.swing.SwingUtilities;
29  
30  import com.eviware.soapui.support.UISupport;
31  import com.eviware.soapui.ui.JDesktopPanelsList;
32  import com.eviware.soapui.ui.desktop.DesktopPanel;
33  
34  public class SwitchDesktopPanelAction extends AbstractAction
35  {
36  	private JDialog dialog;
37  	private final JDesktopPanelsList desktopPanelsList;
38  
39  	public SwitchDesktopPanelAction( JDesktopPanelsList desktopPanelsList )
40  	{
41  		super( "Switch Window" );
42  		this.desktopPanelsList = desktopPanelsList;
43  
44  		putValue( SHORT_DESCRIPTION, "Prompts to switch to an open editor window" );
45  		putValue( ACCELERATOR_KEY, UISupport.getKeyStroke( "menu W" ) );
46  	}
47  
48  	public void actionPerformed( ActionEvent e )
49  	{
50  		if( dialog == null )
51  		{
52  			desktopPanelsList.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
53  
54  			dialog = new JDialog( UISupport.getMainFrame(), "Switch Window", false );
55  			dialog.getContentPane().add( UISupport.buildDescription( null, "Select the window to switch to below", null ),
56  					BorderLayout.NORTH );
57  			dialog.getContentPane().add( desktopPanelsList, BorderLayout.CENTER );
58  
59  			UISupport.initDialogActions( null, dialog );
60  			dialog.addWindowListener( new WindowAdapter()
61  			{
62  				@Override
63  				public void windowOpened( WindowEvent e )
64  				{
65  					initOnOpen();
66  				}
67  
68  				private void initOnOpen()
69  				{
70  					SwingUtilities.invokeLater( new Runnable()
71  					{
72  
73  						public void run()
74  						{
75  							desktopPanelsList.getDesktopPanelsList().requestFocus();
76  							if( desktopPanelsList.getDesktopPanels().size() > 0 )
77  								desktopPanelsList.getDesktopPanelsList().setSelectedIndex( 0 );
78  						}
79  					} );
80  				}
81  
82  				@Override
83  				public void windowDeactivated( WindowEvent e )
84  				{
85  					dialog.setVisible( false );
86  				}
87  
88  				@Override
89  				public void windowLostFocus( WindowEvent e )
90  				{
91  					dialog.setVisible( false );
92  				}
93  
94  			} );
95  			dialog.addMouseListener( new MouseAdapter()
96  			{
97  				@Override
98  				public void mouseClicked( MouseEvent e )
99  				{
100 					dialog.setVisible( false );
101 				}
102 			} );
103 
104 			desktopPanelsList.getDesktopPanelsList().addKeyListener( new KeyAdapter()
105 			{
106 				@Override
107 				public void keyPressed( KeyEvent e )
108 				{
109 					if( e.getKeyChar() == '\n' )
110 					{
111 						DesktopPanel dp = ( DesktopPanel )desktopPanelsList.getDesktopPanelsList().getSelectedValue();
112 						if( dp != null )
113 						{
114 							UISupport.showDesktopPanel( dp );
115 							dialog.setVisible( false );
116 						}
117 					}
118 				}
119 			} );
120 
121 			desktopPanelsList.getDesktopPanelsList().addMouseListener( new MouseAdapter()
122 			{
123 
124 				@Override
125 				public void mouseClicked( MouseEvent e )
126 				{
127 					if( e.getClickCount() > 1 )
128 					{
129 						DesktopPanel dp = ( DesktopPanel )desktopPanelsList.getDesktopPanelsList().getSelectedValue();
130 						if( dp != null )
131 						{
132 							UISupport.showDesktopPanel( dp );
133 							dialog.setVisible( false );
134 						}
135 					}
136 				}
137 			} );
138 		}
139 
140 		dialog.setSize( new Dimension( 300, 120 + desktopPanelsList.getItemsCount() * 20 ) );
141 
142 		UISupport.showDialog( dialog );
143 	}
144 }