View Javadoc

1   /*
2    * soapUI, copyright (C) 2004-2009 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;
14  
15  import javax.swing.JComponent;
16  import javax.swing.JScrollPane;
17  import javax.swing.text.Document;
18  import javax.swing.text.JTextComponent;
19  
20  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
21  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditorModel;
22  import com.eviware.soapui.support.EditorModel.EditorModelListener;
23  import com.eviware.soapui.support.components.JUndoableTextArea;
24  import com.eviware.soapui.support.xml.JXEditTextArea;
25  
26  public class DefaultEditorFactory implements EditorFactory
27  {
28  	public JComponent buildXPathEditor( EditorModel editorModel )
29  	{
30  		JUndoableTextArea textArea = new JUndoableTextArea();
31  		textArea.setText( editorModel.getEditorText() );
32  		textArea.getDocument().addDocumentListener( new JTextComponentEditorModelDocumentListener( editorModel, textArea ) );
33  		return new JScrollPane( textArea );
34  	}
35  
36  	public JComponent buildXmlEditor( EditorModel editorModel )
37  	{
38  		JXEditTextArea xmlEditor = JXEditTextArea.createXmlEditor( true );
39  		xmlEditor.setText( editorModel.getEditorText() );
40  		xmlEditor.getDocument().addDocumentListener( new EditorModelDocumentListener( editorModel ) );
41  		JScrollPane scrollPane = new JScrollPane( xmlEditor );
42  		UISupport.addPreviewCorner( scrollPane, false );
43  		return scrollPane;
44  	}
45  
46  	public JComponent buildGroovyEditor( GroovyEditorModel editorModel )
47  	{
48  		return new GroovyEditor( editorModel );
49  	}
50  
51  	private static class EditorModelDocumentListener extends DocumentListenerAdapter
52  	{
53  		private EditorModel editorModel;
54  
55  		public EditorModelDocumentListener( EditorModel editorModel )
56  		{
57  			this.editorModel = editorModel;
58  		}
59  
60  		public void update( Document document )
61  		{
62  			editorModel.setEditorText( getText( document ) );
63  		}
64  	}
65  	
66  	private static class JTextComponentEditorModelDocumentListener extends EditorModelDocumentListener implements EditorModelListener
67  	{
68  		private final JTextComponent textField;
69  
70  		public JTextComponentEditorModelDocumentListener( EditorModel editorModel, JTextComponent textField )
71  		{
72  			super( editorModel );
73  			
74  			editorModel.addEditorModelListener( this );
75  			this.textField = textField;
76  		}
77  
78  		public void editorTextChanged( String oldText, String newText )
79  		{
80  			textField.getDocument().removeDocumentListener( this );
81  			textField.setText( newText );
82  			textField.getDocument().addDocumentListener( this );
83  		}
84  	}
85  }