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