View Javadoc

1   /*
2    * soapUI, copyright (C) 2004-2010 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, xmlEditor ) );
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 implements EditorModelListener
52  	{
53  		private EditorModel editorModel;
54  		private final JXEditTextArea xmlEditor;
55  
56  		public EditorModelDocumentListener( EditorModel editorModel, JXEditTextArea xmlEditor )
57  		{
58  			this.editorModel = editorModel;
59  			this.xmlEditor = xmlEditor;
60  			
61  			editorModel.addEditorModelListener( this );
62  		}
63  
64  		public void update( Document document )
65  		{
66  			editorModel.setEditorText( getText( document ) );
67  		}
68  		
69  		public void editorTextChanged( String oldText, String newText )
70  		{
71  			xmlEditor.getDocument().removeDocumentListener( this );
72  			xmlEditor.setText( newText );
73  			xmlEditor.getDocument().addDocumentListener( this );
74  		}
75  	}
76  	
77  	private static class JTextComponentEditorModelDocumentListener extends DocumentListenerAdapter implements EditorModelListener
78  	{
79  		private final JTextComponent textField;
80  		private final EditorModel editorModel;
81  
82  		public JTextComponentEditorModelDocumentListener( EditorModel editorModel, JTextComponent textField )
83  		{
84  			this.editorModel = editorModel;
85  			editorModel.addEditorModelListener( this );
86  			this.textField = textField;
87  		}
88  
89  		public void editorTextChanged( String oldText, String newText )
90  		{
91  			textField.getDocument().removeDocumentListener( this );
92  			textField.setText( newText );
93  			textField.getDocument().addDocumentListener( this );
94  		}
95  
96  		public void update( Document document )
97  		{
98  			editorModel.setEditorText( getText( document ) );
99  		}
100 	}
101 }