View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.JOptionPane;
26  import javax.swing.JPanel;
27  import javax.swing.JScrollPane;
28  
29  import com.eviware.soapui.support.DefaultHyperlinkListener;
30  import com.eviware.soapui.support.UISupport;
31  import com.eviware.soapui.support.components.ProgressDialog;
32  import com.eviware.x.dialogs.XDialogs;
33  import com.eviware.x.dialogs.XProgressDialog;
34  import com.jgoodies.forms.factories.ButtonBarFactory;
35  
36  /***
37   * 
38   * @author Lars
39   */
40  public class SwingDialogs implements XDialogs
41  {
42  	private Component parent;
43  	private JDialog extendedInfoDialog;
44  	private Boolean extendedInfoResult;
45     
46     public SwingDialogs(Component parent)
47     {
48        this.parent = parent;
49     }
50  
51     public void showErrorMessage(String message)
52     {
53        if( parent == null )
54           return;
55           
56        JOptionPane.showMessageDialog( parent, message, "Error", JOptionPane.ERROR_MESSAGE );
57     }
58  
59     public boolean confirm(String question, String title)
60     {
61        return JOptionPane.showConfirmDialog( parent, question, title, JOptionPane.YES_NO_OPTION ) == 
62           JOptionPane.OK_OPTION;
63     }
64  
65     public String prompt(String question, String title, String value)
66     {
67        return (String) JOptionPane.showInputDialog( parent, question, title, JOptionPane.QUESTION_MESSAGE, 
68              null, null, value );
69     }
70  
71     public String prompt(String question, String title)
72     {
73        return JOptionPane.showInputDialog(parent, question, title, JOptionPane.QUESTION_MESSAGE );
74     }
75  
76     public void showInfoMessage( String message )
77     {
78        showInfoMessage( message, "Information" );
79     }
80     
81     public void showInfoMessage(String message, String title)
82     {
83        JOptionPane.showMessageDialog( parent, message, title, JOptionPane.INFORMATION_MESSAGE );
84     }
85  
86     public Object prompt(String question, String title, Object[] objects)
87     {
88        Object result = JOptionPane.showInputDialog( parent, question, title, 
89              JOptionPane.OK_CANCEL_OPTION, null, objects, null );
90        return result;
91     }
92     
93     public Object prompt(String question, String title, Object[] objects, String value )
94     {
95        Object result = JOptionPane.showInputDialog( parent, question, title, 
96              JOptionPane.OK_CANCEL_OPTION, null, objects, value );
97        return result;
98     }
99  
100    public Boolean confirmOrCancel(String question, String title )
101    {
102       int result = JOptionPane.showConfirmDialog( parent, question, title, JOptionPane.YES_NO_CANCEL_OPTION );
103       
104       if( result == JOptionPane.CANCEL_OPTION )
105          return null;
106       
107       return Boolean.valueOf( result == JOptionPane.YES_OPTION );
108    }
109 
110    public XProgressDialog createProgressDialog(String label, int length, String initialValue, boolean canCancel)
111    {
112    	return new ProgressDialog( "Progress", label, length, initialValue, canCancel );
113    }
114 
115 	public void showExtendedInfo( String title, String description, String content, Dimension size  )
116 	{
117 		JPanel buttonBar = ButtonBarFactory.buildRightAlignedBar( new JButton( new OkAction( "OK" )));
118 		
119 		showExtendedInfo( title, description, content, buttonBar, size );
120 	}
121 
122 	private void showExtendedInfo( String title, String description, String content, JPanel buttonBar, Dimension size  )
123 	{
124 		extendedInfoDialog = new JDialog( UISupport.getMainFrame(), title );
125 		extendedInfoDialog.setModal( true );
126 		JPanel panel = new JPanel( new BorderLayout() );
127 		
128 		if( description != null )
129 		{
130 			panel.add( UISupport.buildDescription( title, description, null ), BorderLayout.NORTH );
131 		}
132 		
133 		JEditorPane editorPane = new JEditorPane( "text/html", content );
134 		editorPane.setCaretPosition( 0 );
135 		editorPane.setEditable( false );
136 		editorPane.addHyperlinkListener( new DefaultHyperlinkListener( editorPane ) );
137 		
138 		JScrollPane scrollPane = new JScrollPane( editorPane);
139 		scrollPane.setBorder( BorderFactory.createCompoundBorder( 
140 					BorderFactory.createEmptyBorder( 5, 5, 5, 5), scrollPane.getBorder() ));
141 		
142 		panel.add( scrollPane);
143 		buttonBar.setBorder( BorderFactory.createEmptyBorder( 0, 0, 5, 5 ) );
144 		panel.add( buttonBar, BorderLayout.SOUTH );
145 		
146 		extendedInfoDialog.getRootPane().setContentPane( panel);
147 		if( size == null )
148 			extendedInfoDialog.setSize( 400, 300 );
149 		else
150 			extendedInfoDialog.setSize( size );
151 		
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 }