View Javadoc

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