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.soapui.support.components;
    14
    15  import java.awt.BorderLayout;
    16  import java.awt.Dimension;
    17  import java.awt.event.ActionEvent;
    18  import java.util.HashMap;
    19  import java.util.Map;
    20
    21  import javax.swing.AbstractAction;
    22  import javax.swing.BorderFactory;
    23  import javax.swing.DefaultComboBoxModel;
    24  import javax.swing.ImageIcon;
    25  import javax.swing.JComboBox;
    26  import javax.swing.JComponent;
    27  import javax.swing.JDialog;
    28  import javax.swing.JList;
    29  import javax.swing.JPanel;
    30  import javax.swing.JSeparator;
    31
    32  import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
    33  import com.eviware.soapui.support.UISupport;
    34  import com.eviware.soapui.support.action.swing.ActionList;
    35  import com.eviware.soapui.support.action.swing.DefaultActionList;
    36  import com.jgoodies.forms.layout.FormLayout;
    37
    38  /***
    39   * Utility for creating simple configuration dialogs
    40   *
    41   * @author Ole.Matzura
    42   */
    43
    44  public class SwingConfigurationDialogImpl implements ConfigurationDialog
    45  {
    46  	private JDialog dialog;
    47  	private SimpleForm form;
    48  	private boolean result;
    49  	private Map<String,String> values;
    50  	private Map<String,FieldType> fieldTypes = new HashMap<String,FieldType>();
    51  	private final String title;
    52  	private Dimension size;
    53  	private JComponent content;
    54  	private ActionList actions;
    55  	private String description;
    56  	private ImageIcon icon;
    57
    58  	public SwingConfigurationDialogImpl( String title, String helpUrl, String description, ImageIcon icon )
    59  	{
    60  		this.title = title;
    61  		this.description = description;
    62  		this.icon = icon;
    63  		form = new SimpleForm( new FormLayout( "10px,right:pref,10px,left:pref,5px" ) );
    64
    65  		actions = new DefaultActionList( "Actions" );
    66  		if( helpUrl != null )
    67  		{
    68  			actions.addAction( new ShowOnlineHelpAction( helpUrl ));
    69  			actions.addSeparator();
    70  		}
    71
    72  		actions.addAction( new OkAction() );
    73  		actions.addAction( new CancelAction() );
    74  	}
    75
    76  	public boolean show( Map<String,String> values )
    77  	{
    78  		if( dialog == null )
    79  			buildDialog();
    80
    81  		this.values = values;
    82
    83  		result = false;
    84
    85  		form.setValues( values );
    86
    87  		if( size == null )
    88  			dialog.pack();
    89  		else
    90  			dialog.setSize( size );
    91
    92  		UISupport.showDialog( dialog );
    93  		return result;
    94  	}
    95
    96  	public Dimension getSize()
    97  	{
    98  		return size;
    99  	}
    100
    101 	public void setSize(Dimension preferredSize)
    102 	{
    103 		this.size = preferredSize;
    104 	}
    105
    106 	public void addTextField( String name, String tooltip )
    107 	{
    108 	   addTextField( name, tooltip, FieldType.TEXT );
    109 	}
    110
    111 	public void addTextField( String name, String tooltip, FieldType type )
    112 	{
    113 		if( type == FieldType.DIRECTORY )
    114 			form.append( name, new DirectoryFormComponent( tooltip ) );
    115 		else
    116 			form.appendTextField( name, tooltip );
    117
    118 		fieldTypes.put( name, type );
    119 	}
    120
    121    public void addCheckBox( String caption, String label, boolean selected )
    122    {
    123    	form.appendCheckBox( caption, label, selected );
    124    }
    125
    126    public void setContent( JComponent content )
    127    {
    128 		this.content = content;
    129 	}
    130
    131 	private void buildDialog()
    132 	{
    133 		dialog = new JDialog( UISupport.getMainFrame(), title, true );
    134
    135 		form.getPanel().setBorder( BorderFactory.createEmptyBorder( 5, 0, 5, 0 ));
    136
    137 		JPanel panel = new JPanel( new BorderLayout() );
    138 		JComponent contentPanel = content == null ? form.getPanel() : content;
    139 		panel.add( contentPanel, BorderLayout.CENTER );
    140
    141 		JButtonBar buttons = UISupport.initDialogActions( actions, dialog );
    142 		buttons.setBorder( BorderFactory.createEmptyBorder( 3, 5, 3, 5 ) );
    143
    144 		if( content == null )
    145 		{
    146 			JPanel p = new JPanel( new BorderLayout() );
    147 			p.add( new JSeparator(), BorderLayout.NORTH );
    148 			p.add( buttons, BorderLayout.CENTER );
    149
    150 			panel.add( p, BorderLayout.SOUTH );
    151 		}
    152 		else
    153 		{
    154 			panel.add( buttons, BorderLayout.SOUTH );
    155 		}
    156
    157 		if( description != null || icon != null )
    158 			dialog.getContentPane().add( UISupport.buildDescription( title, description, icon ), BorderLayout.NORTH );
    159
    160 		dialog.getContentPane().add( panel );
    161 		dialog.pack();
    162 	}
    163
    164 	private class OkAction extends AbstractAction
    165 	{
    166 		public OkAction()
    167 		{
    168 			super( "Ok" );
    169 		}
    170
    171 		public void actionPerformed(ActionEvent e)
    172 		{
    173 			result = true;
    174
    175 			form.getValues( values );
    176 			dialog.setVisible( false );
    177 		}
    178 	}
    179
    180 	private class CancelAction extends AbstractAction
    181 	{
    182 		public CancelAction()
    183 		{
    184 			super( "Cancel" );
    185 		}
    186
    187 		public void actionPerformed(ActionEvent e)
    188 		{
    189 			dialog.setVisible( false );
    190 		}
    191 	}
    192
    193 	public void addComboBox( String label, Object[] objects, String tooltip)
    194 	{
    195 		form.appendComboBox( label, objects, tooltip );
    196 	}
    197
    198 	public void setValues(String id, String[] values)
    199 	{
    200 		JComponent component = form.getComponent( id );
    201 		if( component instanceof JComboBox )
    202 		{
    203 			((JComboBox)component).setModel( new DefaultComboBoxModel( values ));
    204 		}
    205 		else if( component instanceof JList )
    206 		{
    207 			((JList)component).setModel( new DefaultComboBoxModel( values ));
    208 		}
    209 		else throw new RuntimeException( "Could not set values on [" + component + "]" );
    210 	}
    211
    212 	public void addComboBox(String label, String tooltip)
    213 	{
    214 		form.appendComboBox( label, new String[] {}, tooltip );
    215 	}
    216
    217 	public void addComponent(JComponent component)
    218 	{
    219 		form.addComponent( component );
    220 	}
    221
    222 	public void getValues(Map<String, String> values)
    223 	{
    224 		form.getValues( values );
    225 	}
    226
    227 	public ActionList getActions()
    228 	{
    229 		return actions;
    230 	}
    231
    232 	public void hide()
    233 	{
    234 		dialog.setVisible( false );
    235 	}
    236 }