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.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 ) == 
61           JOptionPane.OK_OPTION;
62     }
63  
64     public String prompt(String question, String title, String value)
65     {
66        return (String) JOptionPane.showInputDialog( parent, question, title, JOptionPane.QUESTION_MESSAGE, 
67              null, null, value );
68     }
69  
70     public String prompt(String question, String title)
71     {
72        return JOptionPane.showInputDialog(parent, question, title, JOptionPane.QUESTION_MESSAGE );
73     }
74  
75     public void showInfoMessage( String message )
76     {
77        showInfoMessage( message, "Information" );
78     }
79     
80     public void showInfoMessage(String message, String title)
81     {
82        JOptionPane.showMessageDialog( parent, message, title, JOptionPane.INFORMATION_MESSAGE );
83     }
84  
85     public Object prompt(String question, String title, Object[] objects)
86     {
87        Object result = JOptionPane.showInputDialog( parent, question, title, 
88              JOptionPane.OK_CANCEL_OPTION, null, objects, null );
89        return result;
90     }
91     
92     public Object prompt(String question, String title, Object[] objects, String value )
93     {
94        Object result = JOptionPane.showInputDialog( parent, question, title, 
95              JOptionPane.OK_CANCEL_OPTION, null, objects, value );
96        return result;
97     }
98  
99     public Boolean confirmOrCancel(String question, String title )
100    {
101       int result = JOptionPane.showConfirmDialog( parent, question, title, JOptionPane.YES_NO_CANCEL_OPTION );
102       
103       if( result == JOptionPane.CANCEL_OPTION )
104          return null;
105       
106       return Boolean.valueOf( result == JOptionPane.YES_OPTION );
107    }
108 
109    public XProgressDialog createProgressDialog(String label, int length, String initialValue, boolean canCancel)
110    {
111    	return new ProgressDialog( "Progress", label, length, initialValue, canCancel );
112    }
113 
114 	public void showExtendedInfo( String title, String description, String content, Dimension size  )
115 	{
116 		JPanel buttonBar = ButtonBarFactory.buildRightAlignedBar( new JButton( new OkAction( "OK" )));
117 		
118 		showExtendedInfo( title, description, content, buttonBar, size );
119 	}
120 
121 	private void showExtendedInfo( String title, String description, String content, JPanel buttonBar, Dimension size  )
122 	{
123 		extendedInfoDialog = new JDialog( UISupport.getMainFrame(), title );
124 		extendedInfoDialog.setModal( true );
125 		JPanel panel = new JPanel( new BorderLayout() );
126 		
127 		if( description != null )
128 		{
129 			panel.add( UISupport.buildDescription( title, description, null ), BorderLayout.NORTH );
130 		}
131 		
132 		JEditorPane editorPane = new JEditorPane( "text/html", content );
133 		editorPane.setCaretPosition( 0 );
134 		editorPane.setEditable( false );
135 		editorPane.addHyperlinkListener( new DefaultHyperlinkListener( editorPane ) );
136 		
137 		JScrollPane scrollPane = new JScrollPane( editorPane);
138 		scrollPane.setBorder( BorderFactory.createCompoundBorder( 
139 					BorderFactory.createEmptyBorder( 5, 5, 5, 5), scrollPane.getBorder() ));
140 		
141 		panel.add( scrollPane);
142 		buttonBar.setBorder( BorderFactory.createEmptyBorder( 0, 0, 5, 5 ) );
143 		panel.add( buttonBar, BorderLayout.SOUTH );
144 		
145 		extendedInfoDialog.getRootPane().setContentPane( panel);
146 		if( size == null )
147 			extendedInfoDialog.setSize( 400, 300 );
148 		else
149 			extendedInfoDialog.setSize( size );
150 		
151 		extendedInfoResult = null;
152 		UISupport.showDialog( extendedInfoDialog );
153 	}
154 
155 	public boolean confirmExtendedInfo( String title, String description, String content, Dimension size  )
156 	{
157 		JPanel buttonBar = ButtonBarFactory.buildRightAlignedBar( 
158 					new JButton( new OkAction( "OK" )), new JButton( new CancelAction( "Cancel" )));
159 		
160 		showExtendedInfo( title, description, content, buttonBar, size );
161 		
162 		return extendedInfoResult == null ? false : extendedInfoResult;
163 	}
164 
165 	public Boolean confirmOrCancleExtendedInfo( String title, String description, String content, Dimension size  )
166 	{
167 		JPanel buttonBar = ButtonBarFactory.buildRightAlignedBar( 
168 					new JButton( new OkAction( "Yes" )), new JButton( new NoAction( "No" )), 
169 					new JButton( new CancelAction( "Cancel" )));
170 		
171 		showExtendedInfo( title, description, content, buttonBar, size );
172 		
173 		return extendedInfoResult;
174 	}
175 	
176 	private final class OkAction extends AbstractAction
177 	{
178 		public OkAction( String name )
179 		{
180 			super( name );
181 		}
182 		
183 		public void actionPerformed( ActionEvent e )
184 		{
185 			extendedInfoResult = true;
186 			extendedInfoDialog.setVisible( false );
187 		}
188 	}
189 	
190 	private final class NoAction extends AbstractAction
191 	{
192 		public NoAction( String name )
193 		{
194 			super( name );
195 		}
196 		
197 		public void actionPerformed( ActionEvent e )
198 		{
199 			extendedInfoResult = false;
200 			extendedInfoDialog.setVisible( false );
201 		}
202 	}
203 	
204 	private final class CancelAction extends AbstractAction
205 	{
206 		public CancelAction( String name )
207 		{
208 			super( name );
209 		}
210 		
211 		public void actionPerformed( ActionEvent e )
212 		{
213 			extendedInfoResult = null;
214 			extendedInfoDialog.setVisible( false );
215 		}
216 	}
217 
218 	public String selectXPath( String title, String info, String xml, String xpath )
219 	{
220 		return prompt( "Specify XPath expression", "Select XPath", xpath );
221 	}
222 
223 	/* (non-Javadoc)
224 	 * @see com.eviware.x.dialogs.XDialogs#promptPassword(java.lang.String, java.lang.String)
225 	 */
226 	public char[] promptPassword(String question, String title) {
227 		JPasswordField passwordField = new JPasswordField();
228 		JLabel qLabel = new JLabel(question);
229 		JOptionPane.showConfirmDialog(null, new Object[] {qLabel, passwordField}, title, JOptionPane.OK_CANCEL_OPTION);
230 		return passwordField.getPassword();
231 	}
232 }