View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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(
56  						UISupport.buildDescription( null, "Select the window to switch to below", null ),
57  						BorderLayout.NORTH );
58  			dialog.getContentPane().add( desktopPanelsList, BorderLayout.CENTER );
59  			
60  			UISupport.initDialogActions( null, dialog );
61  			dialog.addWindowListener( new WindowAdapter()
62  			{
63  				@Override
64  				public void windowOpened( WindowEvent e )
65  				{
66  					initOnOpen();
67  				}
68  
69  				private void initOnOpen()
70  				{
71  					SwingUtilities.invokeLater( new Runnable() {
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  				@Override
82  				public void windowDeactivated( WindowEvent e )
83  				{
84  					dialog.setVisible( false );
85  				}
86  
87  				@Override
88  				public void windowLostFocus( WindowEvent e )
89  				{
90  					dialog.setVisible( false );
91  				}
92  
93  			} );
94  			dialog.addMouseListener( new MouseAdapter()
95  			{
96  				@Override
97  				public void mouseClicked( MouseEvent e )
98  				{
99  					dialog.setVisible( false );
100 				}
101 			} );
102 
103 			desktopPanelsList.getDesktopPanelsList().addKeyListener( new KeyAdapter()
104 			{
105 				@Override
106 				public void keyPressed( KeyEvent e )
107 				{
108 					if( e.getKeyChar() == '\n' )
109 					{
110 						DesktopPanel dp = ( DesktopPanel ) desktopPanelsList.getDesktopPanelsList().getSelectedValue();
111 						if( dp != null )
112 						{
113 							UISupport.showDesktopPanel( dp );
114 							dialog.setVisible( false );
115 						}
116 					}
117 				}
118 			} );
119 			
120 			desktopPanelsList.getDesktopPanelsList().addMouseListener( new MouseAdapter() {
121 
122 				@Override
123 				public void mouseClicked( MouseEvent e )
124 				{
125 					if( e.getClickCount() > 1 )
126 					{
127 						DesktopPanel dp = ( DesktopPanel ) desktopPanelsList.getDesktopPanelsList().getSelectedValue();
128 						if( dp != null )
129 						{
130 							UISupport.showDesktopPanel( dp );
131 							dialog.setVisible( false );
132 						}
133 					}
134 				}} );
135 		}
136 		
137 		dialog.setSize( new Dimension( 300, 120 + desktopPanelsList.getItemsCount()*20 ) );
138 
139 		UISupport.showDialog( dialog );
140 	}
141 }