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.x.impl.swing;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Component;
17  import java.awt.Dimension;
18  import java.awt.event.ActionEvent;
19  
20  import javax.swing.AbstractAction;
21  import javax.swing.BorderFactory;
22  import javax.swing.JButton;
23  import javax.swing.JDialog;
24  import javax.swing.JEditorPane;
25  import javax.swing.JLabel;
26  import javax.swing.JOptionPane;
27  import javax.swing.JPanel;
28  import javax.swing.JPasswordField;
29  import javax.swing.JScrollPane;
30  
31  import com.eviware.soapui.support.DefaultHyperlinkListener;
32  import com.eviware.soapui.support.UISupport;
33  import com.eviware.soapui.support.components.ProgressDialog;
34  import com.eviware.x.dialogs.XDialogs;
35  import com.eviware.x.dialogs.XProgressDialog;
36  import com.jgoodies.forms.factories.ButtonBarFactory;
37  
38  /***
39   * 
40   * @author Lars
41   */
42  public class SwingDialogs implements XDialogs
43  {
44  	private Component parent;
45  	private JDialog extendedInfoDialog;
46  	private Boolean extendedInfoResult;
47  
48  	public SwingDialogs( Component parent )
49  	{
50  		this.parent = parent;
51  	}
52  
53  	public void showErrorMessage( String message )
54  	{
55  		JOptionPane.showMessageDialog( parent, message, "Error", JOptionPane.ERROR_MESSAGE );
56  	}
57  
58  	public boolean confirm( String question, String title )
59  	{
60  		return JOptionPane.showConfirmDialog( parent, question, title, JOptionPane.YES_NO_OPTION ) == JOptionPane.OK_OPTION;
61  	}
62  
63  	public String prompt( String question, String title, String value )
64  	{
65  		return ( String )JOptionPane.showInputDialog( parent, question, title, JOptionPane.QUESTION_MESSAGE, null, null,
66  				value );
67  	}
68  
69  	public String prompt( String question, String title )
70  	{
71  		return JOptionPane.showInputDialog( parent, question, title, JOptionPane.QUESTION_MESSAGE );
72  	}
73  
74  	public void showInfoMessage( String message )
75  	{
76  		showInfoMessage( message, "Information" );
77  	}
78  
79  	public void showInfoMessage( String message, String title )
80  	{
81  		JOptionPane.showMessageDialog( parent, message, title, JOptionPane.INFORMATION_MESSAGE );
82  	}
83  
84  	public Object prompt( String question, String title, Object[] objects )
85  	{
86  		Object result = JOptionPane.showInputDialog( parent, question, title, JOptionPane.OK_CANCEL_OPTION, null,
87  				objects, null );
88  		return result;
89  	}
90  
91  	public Object prompt( String question, String title, Object[] objects, String value )
92  	{
93  		Object result = JOptionPane.showInputDialog( parent, question, title, JOptionPane.OK_CANCEL_OPTION, null,
94  				objects, value );
95  		return result;
96  	}
97  
98  	public Boolean confirmOrCancel( String question, String title )
99  	{
100 		int result = JOptionPane.showConfirmDialog( parent, question, title, JOptionPane.YES_NO_CANCEL_OPTION );
101 
102 		if( result == JOptionPane.CANCEL_OPTION )
103 			return null;
104 
105 		return Boolean.valueOf( result == JOptionPane.YES_OPTION );
106 	}
107 
108 	public int yesYesToAllOrNo( String question, String title )
109 	{
110 		String[] buttons = { "Yes", "Yes to all", "No" };
111 		return JOptionPane.showOptionDialog( parent, question, title, 0, JOptionPane.QUESTION_MESSAGE, null, buttons,
112 				buttons[0] );
113 	}
114 
115 	public XProgressDialog createProgressDialog( String label, int length, String initialValue, boolean canCancel )
116 	{
117 		return new ProgressDialog( "Progress", label, length, initialValue, canCancel );
118 	}
119 
120 	public void showExtendedInfo( String title, String description, String content, Dimension size )
121 	{
122 		JPanel buttonBar = ButtonBarFactory.buildRightAlignedBar( new JButton( new OkAction( "OK" ) ) );
123 
124 		showExtendedInfo( title, description, content, buttonBar, size );
125 	}
126 
127 	private void showExtendedInfo( String title, String description, String content, JPanel buttonBar, Dimension size )
128 	{
129 		extendedInfoDialog = new JDialog( UISupport.getMainFrame(), title );
130 		extendedInfoDialog.setModal( true );
131 		JPanel panel = new JPanel( new BorderLayout() );
132 
133 		if( description != null )
134 		{
135 			panel.add( UISupport.buildDescription( title, description, null ), BorderLayout.NORTH );
136 		}
137 
138 		JEditorPane editorPane = new JEditorPane( "text/html", content );
139 		editorPane.setCaretPosition( 0 );
140 		editorPane.setEditable( false );
141 		editorPane.addHyperlinkListener( new DefaultHyperlinkListener( editorPane ) );
142 
143 		JScrollPane scrollPane = new JScrollPane( editorPane );
144 		scrollPane.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ),
145 				scrollPane.getBorder() ) );
146 
147 		panel.add( scrollPane );
148 		buttonBar.setBorder( BorderFactory.createEmptyBorder( 0, 0, 5, 5 ) );
149 		panel.add( buttonBar, BorderLayout.SOUTH );
150 
151 		extendedInfoDialog.getRootPane().setContentPane( panel );
152 		if( size == null )
153 			extendedInfoDialog.setSize( 400, 300 );
154 		else
155 			extendedInfoDialog.setSize( size );
156 
157 		extendedInfoResult = null;
158 		UISupport.showDialog( extendedInfoDialog );
159 	}
160 
161 	public boolean confirmExtendedInfo( String title, String description, String content, Dimension size )
162 	{
163 		JPanel buttonBar = ButtonBarFactory.buildRightAlignedBar( new JButton( new OkAction( "OK" ) ), new JButton(
164 				new CancelAction( "Cancel" ) ) );
165 
166 		showExtendedInfo( title, description, content, buttonBar, size );
167 
168 		return extendedInfoResult == null ? false : extendedInfoResult;
169 	}
170 
171 	public Boolean confirmOrCancleExtendedInfo( String title, String description, String content, Dimension size )
172 	{
173 		JPanel buttonBar = ButtonBarFactory.buildRightAlignedBar( new JButton( new OkAction( "Yes" ) ), new JButton(
174 				new NoAction( "No" ) ), new JButton( new CancelAction( "Cancel" ) ) );
175 
176 		showExtendedInfo( title, description, content, buttonBar, size );
177 
178 		return extendedInfoResult;
179 	}
180 
181 	private final class OkAction extends AbstractAction
182 	{
183 		public OkAction( String name )
184 		{
185 			super( name );
186 		}
187 
188 		public void actionPerformed( ActionEvent e )
189 		{
190 			extendedInfoResult = true;
191 			extendedInfoDialog.setVisible( false );
192 		}
193 	}
194 
195 	private final class NoAction extends AbstractAction
196 	{
197 		public NoAction( String name )
198 		{
199 			super( name );
200 		}
201 
202 		public void actionPerformed( ActionEvent e )
203 		{
204 			extendedInfoResult = false;
205 			extendedInfoDialog.setVisible( false );
206 		}
207 	}
208 
209 	private final class CancelAction extends AbstractAction
210 	{
211 		public CancelAction( String name )
212 		{
213 			super( name );
214 		}
215 
216 		public void actionPerformed( ActionEvent e )
217 		{
218 			extendedInfoResult = null;
219 			extendedInfoDialog.setVisible( false );
220 		}
221 	}
222 
223 	public String selectXPath( String title, String info, String xml, String xpath )
224 	{
225 		return prompt( "Specify XPath expression", "Select XPath", xpath );
226 	}
227 
228 	/*
229 	 * (non-Javadoc)
230 	 * 
231 	 * @see com.eviware.x.dialogs.XDialogs#promptPassword(java.lang.String,
232 	 * java.lang.String)
233 	 */
234 	public char[] promptPassword( String question, String title )
235 	{
236 		JPasswordField passwordField = new JPasswordField();
237 		JLabel qLabel = new JLabel( question );
238 		JOptionPane.showConfirmDialog( null, new Object[] { qLabel, passwordField }, title, JOptionPane.OK_CANCEL_OPTION );
239 		return passwordField.getPassword();
240 	}
241 }