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 com.eviware.soapui.support.UISupport;
16  import com.eviware.soapui.support.action.swing.ActionList;
17  import com.eviware.soapui.support.action.swing.DefaultActionList;
18  import com.eviware.soapui.support.components.JButtonBar;
19  import com.eviware.soapui.support.types.StringToStringMap;
20  import com.eviware.x.form.ValidationMessage;
21  import com.eviware.x.form.XFormDialog;
22  import com.eviware.x.form.XFormField;
23  
24  import javax.swing.*;
25  import java.awt.*;
26  import java.util.Arrays;
27  
28  public class JFormDialog extends SwingXFormDialog
29  {
30     private JDialog dialog;
31     private SwingXFormImpl form;
32     private JButtonBar buttons;
33  
34     public JFormDialog( String name, SwingXFormImpl form, ActionList actions, String description, ImageIcon icon )
35     {
36        dialog = new JDialog( UISupport.getMainFrame(), name, true );
37  
38        buttons = UISupport.initDialogActions( actions, dialog );
39        buttons.setBorder( BorderFactory.createEmptyBorder( 5, 0, 0, 0 ) );
40  
41        JPanel panel = new JPanel( new BorderLayout() );
42        this.form = (SwingXFormImpl) form;
43        panel.add( ( this.form.getPanel() ), BorderLayout.CENTER );
44        panel.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) );
45  
46        if( description != null || icon != null )
47           dialog.getContentPane().add( UISupport.buildDescription( name, description, icon ), BorderLayout.NORTH );
48  
49        dialog.getContentPane().add( panel, BorderLayout.CENTER );
50  
51        buttons.setBorder(
52                BorderFactory.createCompoundBorder(
53                        BorderFactory.createCompoundBorder(
54                                BorderFactory.createMatteBorder( 1, 0, 0, 0, Color.GRAY ),
55                                BorderFactory.createMatteBorder( 1, 0, 0, 0, Color.WHITE ) ),
56                        BorderFactory.createEmptyBorder( 3, 5, 3, 5 ) ) );
57  
58        dialog.getContentPane().add( buttons, BorderLayout.SOUTH );
59     }
60  
61     public void setValues( StringToStringMap values )
62     {
63        form.setValues( values );
64     }
65  
66     public StringToStringMap getValues()
67     {
68        StringToStringMap result = new StringToStringMap();
69        result.putAll( form.getValues() );
70  
71        return result;
72     }
73  
74     public void setOptions( String field, Object[] options )
75     {
76        form.setOptions( field, options );
77     }
78  
79     public void setVisible( boolean visible )
80     {
81        dialog.pack();
82        if( dialog.getHeight() < 270 )
83        {
84           dialog.setSize( new Dimension( dialog.getWidth(), 270 ) );
85        }
86  
87        if( dialog.getWidth() < 450 )
88        {
89           dialog.setSize( new Dimension( 450, dialog.getHeight() ) );
90        }
91  
92        UISupport.centerDialog( dialog );
93        dialog.setVisible( visible );
94     }
95  
96     public void addAction( Action action )
97     {
98        DefaultActionList actions = new DefaultActionList();
99        actions.addAction( action );
100       buttons.addActions( actions );
101    }
102 
103    public boolean validate()
104    {
105       XFormField[] formFields = form.getFormFields();
106       for( int c = 0; c < formFields.length; c++ )
107       {
108          ValidationMessage[] messages = formFields[c].validate();
109          if( messages != null && messages.length > 0 )
110          {
111             ( (AbstractSwingXFormField<?>) messages[0].getFormField() ).getComponent().requestFocus();
112             UISupport.showErrorMessage( messages[0].getMessage() );
113             return false;
114          }
115       }
116 
117       return true;
118    }
119 
120    public void setFormFieldProperty( String name, Object value )
121    {
122       form.setFormFieldProperty( name, value );
123    }
124 
125    public String getValue( String field )
126    {
127       return form.getComponentValue( field );
128    }
129 
130    public void setValue( String field, String value )
131    {
132       form.setComponentValue( field, value );
133    }
134 
135    public int getValueIndex( String name )
136    {
137       String[] options = form.getOptions( name );
138       if( options == null )
139          return -1;
140 
141       return Arrays.asList( options ).indexOf( form.getComponentValue( name ) );
142    }
143 
144    public boolean show()
145    {
146       setReturnValue( XFormDialog.CANCEL_OPTION );
147       show( new StringToStringMap() );
148       return getReturnValue() == XFormDialog.OK_OPTION;
149    }
150 
151    public XFormField getFormField( String name )
152    {
153       return form.getFormField( name );
154    }
155 
156    public void setWidth( int i )
157    {
158       dialog.setPreferredSize( new Dimension( i, (int) dialog.getPreferredSize().getHeight() ) );
159    }
160 
161    public void release()
162    {
163       dialog.dispose();
164    }
165 }