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.XForm;
22  import com.eviware.x.form.XFormDialog;
23  import com.eviware.x.form.XFormField;
24  
25  import javax.swing.*;
26  import java.awt.*;
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.List;
30  
31  public class JTabbedFormDialog extends SwingXFormDialog
32  {
33     private JDialog dialog;
34     private List<SwingXFormImpl> forms = new ArrayList<SwingXFormImpl>();
35     private JTabbedPane tabs;
36     private JButtonBar buttons;
37  
38     public JTabbedFormDialog( String name, XForm[] forms, ActionList actions, String description, ImageIcon icon )
39     {
40        dialog = new JDialog( UISupport.getMainFrame(), name, true );
41        tabs = new JTabbedPane();
42        for( XForm form : forms )
43        {
44           SwingXFormImpl swingFormImpl = ( (SwingXFormImpl) form );
45           this.forms.add( swingFormImpl );
46  
47           JPanel panel = swingFormImpl.getPanel();
48           panel.setBorder( BorderFactory.createEmptyBorder( 0, 0, 5, 0 ) );
49  
50           tabs.addTab( form.getName(), panel );
51        }
52  
53        buttons = UISupport.initDialogActions( actions, dialog );
54  
55        if( description != null || icon != null )
56           dialog.getContentPane().add( UISupport.buildDescription( name, description, icon ), BorderLayout.NORTH );
57  
58        dialog.getContentPane().add( UISupport.createTabPanel( tabs, false ), BorderLayout.CENTER );
59  
60        buttons.setBorder( BorderFactory.createEmptyBorder( 3, 5, 3, 5 ) );
61        dialog.getContentPane().add( buttons, BorderLayout.SOUTH );
62        dialog.pack();
63        Dimension size = dialog.getSize();
64        if( size.getHeight() < 300 )
65        {
66           dialog.setSize( new Dimension( (int) size.getWidth(), 300 ) );
67        }
68     }
69  
70     public void setValues( StringToStringMap values )
71     {
72        for( XForm form : forms )
73           form.setValues( values );
74     }
75  
76     public void setOptions( String field, Object[] options )
77     {
78        for( XForm form : forms )
79           form.setOptions( field, options );
80     }
81  
82     public XFormField getFormField( String name )
83     {
84        for( XForm form : forms )
85        {
86           XFormField formField = form.getFormField( name );
87           if( formField != null )
88              return formField;
89        }
90  
91        return null;
92     }
93  
94     public void addAction( Action action )
95     {
96        DefaultActionList actions = new DefaultActionList();
97        actions.addAction( action );
98        buttons.addActions( actions );
99     }
100 
101    public StringToStringMap getValues()
102    {
103       StringToStringMap result = new StringToStringMap();
104 
105       for( XForm form : forms )
106          result.putAll( form.getValues() );
107 
108       return result;
109    }
110 
111    public void setVisible( boolean visible )
112    {
113       if( visible )
114          tabs.setSelectedIndex( 0 );
115 
116       UISupport.centerDialog( dialog );
117 
118       dialog.setVisible( visible );
119    }
120 
121    public boolean validate()
122    {
123       for( int i = 0; i < forms.size(); i++ )
124       {
125          XFormField[] formFields = forms.get( i ).getFormFields();
126          for( int c = 0; c < formFields.length; c++ )
127          {
128             ValidationMessage[] messages = formFields[c].validate();
129             if( messages != null && messages.length > 0 )
130             {
131                tabs.setSelectedIndex( i );
132                ( (AbstractSwingXFormField<?>) messages[0].getFormField() ).getComponent().requestFocus();
133                UISupport.showErrorMessage( messages[0].getMessage() );
134                return false;
135             }
136          }
137       }
138 
139       return true;
140    }
141 
142    public void setFormFieldProperty( String name, Object value )
143    {
144       for( XForm form : forms )
145          form.setFormFieldProperty( name, value );
146    }
147 
148    public String getValue( String field )
149    {
150       for( XForm form : forms )
151       {
152          if( form.getComponent( field ) != null )
153             return form.getComponent( field ).getValue();
154       }
155 
156       return null;
157    }
158 
159    public void setValue( String field, String value )
160    {
161       for( XForm form : forms )
162       {
163          if( form.getComponent( field ) != null )
164             form.getComponent( field ).setValue( value );
165       }
166    }
167 
168    public int getValueIndex( String name )
169    {
170       for( SwingXFormImpl form : forms )
171       {
172          if( form.getComponent( name ) != null )
173          {
174             String[] options = form.getOptions( name );
175             if( options == null )
176                return -1;
177 
178             return Arrays.asList( options ).indexOf( form.getComponentValue( name ) );
179          }
180       }
181 
182       return -1;
183    }
184 
185    public boolean show()
186    {
187       setReturnValue( XFormDialog.CANCEL_OPTION );
188       show( new StringToStringMap() );
189       return getReturnValue() == XFormDialog.OK_OPTION;
190    }
191 
192    public void setWidth( int i )
193    {
194       dialog.setPreferredSize( new Dimension( i, (int) dialog.getPreferredSize().getHeight() ) );
195    }
196 
197    public void release()
198    {
199       dialog.dispose();
200    }
201 }