View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.impl.wsdl.panels.teststeps.support;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Font;
17  import java.awt.event.ActionListener;
18  
19  import javax.swing.JPanel;
20  import javax.swing.event.CaretListener;
21  import javax.swing.text.Document;
22  
23  import org.syntax.jedit.KeywordMap;
24  import org.syntax.jedit.tokenmarker.CTokenMarker;
25  import org.syntax.jedit.tokenmarker.GroovyTokenMarker;
26  import org.syntax.jedit.tokenmarker.Token;
27  
28  import com.eviware.soapui.model.settings.Settings;
29  import com.eviware.soapui.model.settings.SettingsListener;
30  import com.eviware.soapui.settings.UISettings;
31  import com.eviware.soapui.support.DocumentListenerAdapter;
32  import com.eviware.soapui.support.components.JEditorStatusBar.JEditorStatusBarTarget;
33  import com.eviware.soapui.support.xml.JXEditTextArea;
34  
35  public class GroovyEditor extends JPanel implements JEditorStatusBarTarget
36  {
37  	private JXEditTextArea editArea;
38  	private final GroovyEditorModel model;
39  	private InternalSettingsListener settingsListener;
40  
41  	public GroovyEditor( GroovyEditorModel model )
42  	{
43  		super( new BorderLayout() );
44  		this.model = model;
45  		
46  		editArea = new JXEditTextArea( new CTokenMarker( false, initKeywords() ) );
47  		
48  		Settings settings = model.getSettings();
49  		String editorFont = settings.getString( UISettings.EDITOR_FONT, UISettings.DEFAULT_EDITOR_FONT );
50  		if( editorFont != null && editorFont.length() > 0 )
51  			editArea.setFont(Font.decode(editorFont));
52  		else
53  			editArea.setFont( Font.decode( UISettings.DEFAULT_EDITOR_FONT ));
54  
55  		editArea.setText( model.getScript() );
56  		ActionListener runAction = model.getRunAction();
57  		if( runAction != null )
58  			editArea.getInputHandler().addKeyBinding( "A+ENTER", runAction );
59  		
60  		editArea.getDocument().addDocumentListener( new DocumentListenerAdapter() {
61  
62  			public void update(Document document)
63  			{
64  				GroovyEditor.this.model.setScript( editArea.getText() );
65  			}} );
66  		
67  		settingsListener = new InternalSettingsListener();
68  		settings.addSettingsListener( settingsListener );
69  		
70  		add( editArea );
71  	}
72  	
73  	public JXEditTextArea getEditArea()
74  	{
75  		return editArea;
76  	}
77  
78  	public void release()
79  	{
80  		model.getSettings().removeSettingsListener( settingsListener );
81  	}
82  
83  	public void selectError(String message)
84  	{
85  		int ix = message == null ? -1 : message.indexOf( "@ line " );
86  		if( ix >= 0 )
87  		{
88  			try
89  			{
90  				int ix2 = message.indexOf(',', ix);
91  				int line = ix2 == -1 ? Integer.parseInt(message.substring(ix + 6).trim()) : Integer.parseInt(message
92  						.substring(ix + 6, ix2).trim());
93  				int column = 0;
94  				if (ix2 != -1)
95  				{
96  					ix = message.indexOf("column ", ix2);
97  					if (ix >= 0)
98  					{
99  						ix2 = message.indexOf('.', ix);
100 						column = ix2 == -1 ? Integer.parseInt(message.substring(ix + 7).trim()) : Integer
101 								.parseInt(message.substring(ix + 7, ix2).trim());
102 					}
103 				}
104 				
105 				editArea.setCaretPosition(editArea.getLineStartOffset(line - 1) + column - 1);
106 			}
107 			catch (Exception ex)
108 			{
109 			}					
110 			
111 			editArea.requestFocus();
112 		}
113 	}
114 	
115 	private KeywordMap initKeywords()
116 	{
117 		KeywordMap keywords = GroovyTokenMarker.getKeywords();
118 		
119 		for( String keyword : model.getKeywords() )
120 			keywords.add(keyword,Token.KEYWORD2);
121 		return keywords;
122 	}
123 	
124 	private final class InternalSettingsListener implements SettingsListener
125 	{
126 		public void settingChanged(String name, String newValue, String oldValue)
127 		{
128 			if( name.equals( UISettings.EDITOR_FONT ))
129 			{
130 				editArea.setFont( Font.decode( newValue ));
131 				invalidate();
132 			}
133 		}
134 	}
135 
136 	public void addCaretListener( CaretListener listener )
137 	{
138 		editArea.addCaretListener( listener );
139 	}
140 
141 	public int getCaretPosition()
142 	{
143 		return editArea.getCaretPosition();
144 	}
145 
146 	public int getLineOfOffset( int offset ) throws Exception
147 	{
148 		return editArea.getLineOfOffset( offset );
149 	}
150 
151 	public int getLineStartOffset( int line ) throws Exception
152 	{
153 		return editArea.getLineStartOffset( line );
154 	}
155 
156 	public void removeCaretListener( CaretListener listener )
157 	{
158 		editArea.removeCaretListener( listener );
159 	}
160 }