1
2
3
4
5
6
7
8
9
10
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 /***
38 * Groovy editor wrapper
39 *
40 * @author ole.matzura
41 */
42
43 public class GroovyEditor extends JPanel implements JEditorStatusBarTarget
44 {
45 private JXEditTextArea editArea;
46 private GroovyEditorModel model;
47 private InternalSettingsListener settingsListener;
48 private GroovyDocumentListener groovyDocumentListener;
49
50 public GroovyEditor( GroovyEditorModel model )
51 {
52 super( new BorderLayout() );
53 this.model = model;
54
55 editArea = new JXEditTextArea( new CTokenMarker( false, initKeywords() ) );
56
57 Settings settings = model.getSettings();
58 String editorFont = settings.getString( UISettings.EDITOR_FONT, UISettings.DEFAULT_EDITOR_FONT );
59 if( editorFont != null && editorFont.length() > 0 )
60 editArea.setFont(Font.decode(editorFont));
61 else
62 editArea.setFont( Font.decode( UISettings.DEFAULT_EDITOR_FONT ));
63
64 editArea.setText( model.getScript() );
65 editArea.setCaretPosition( 0 );
66 ActionListener runAction = model.getRunAction();
67 if( runAction != null )
68 editArea.getInputHandler().addKeyBinding( "A+ENTER", runAction );
69
70 groovyDocumentListener = new GroovyDocumentListener();
71 editArea.getDocument().addDocumentListener( groovyDocumentListener );
72
73 settingsListener = new InternalSettingsListener();
74 settings.addSettingsListener( settingsListener );
75
76 add( editArea );
77
78 addFocusListener( new FocusAdapter() {
79
80 public void focusGained( FocusEvent e )
81 {
82 editArea.requestFocusInWindow();
83 }}
84 );
85 }
86
87 public JXEditTextArea getEditArea()
88 {
89 return editArea;
90 }
91
92 public void release()
93 {
94 model.getSettings().removeSettingsListener( settingsListener );
95 model = null;
96 editArea.getDocument().removeDocumentListener( groovyDocumentListener );
97 editArea.getInputHandler().removeAllKeyBindings();
98 }
99
100 public void selectError(String message)
101 {
102 int ix = message == null ? -1 : message.indexOf( "@ line " );
103 if( ix >= 0 )
104 {
105 try
106 {
107 int ix2 = message.indexOf(',', ix);
108 int line = ix2 == -1 ? Integer.parseInt(message.substring(ix + 6).trim()) : Integer.parseInt(message
109 .substring(ix + 6, ix2).trim());
110 int column = 0;
111 if (ix2 != -1)
112 {
113 ix = message.indexOf("column ", ix2);
114 if (ix >= 0)
115 {
116 ix2 = message.indexOf('.', ix);
117 column = ix2 == -1 ? Integer.parseInt(message.substring(ix + 7).trim()) : Integer
118 .parseInt(message.substring(ix + 7, ix2).trim());
119 }
120 }
121
122 editArea.setCaretPosition(editArea.getLineStartOffset(line - 1) + column - 1);
123 }
124 catch (Exception ex)
125 {
126 }
127
128 editArea.requestFocus();
129 }
130 }
131
132 private KeywordMap initKeywords()
133 {
134 KeywordMap keywords = GroovyTokenMarker.getKeywords();
135
136 for( String keyword : model.getKeywords() )
137 keywords.add(keyword,Token.KEYWORD2);
138 return keywords;
139 }
140
141 private final class GroovyDocumentListener extends DocumentListenerAdapter
142 {
143 public void update(Document document)
144 {
145 GroovyEditor.this.model.setScript( editArea.getText() );
146 }
147 }
148
149 private final class InternalSettingsListener implements SettingsListener
150 {
151 public void settingChanged(String name, String newValue, String oldValue)
152 {
153 if( name.equals( UISettings.EDITOR_FONT ))
154 {
155 editArea.setFont( Font.decode( newValue ));
156 invalidate();
157 }
158 }
159 }
160
161 public void addCaretListener( CaretListener listener )
162 {
163 editArea.addCaretListener( listener );
164 }
165
166 public int getCaretPosition()
167 {
168 return editArea.getCaretPosition();
169 }
170
171 public int getLineOfOffset( int offset ) throws Exception
172 {
173 return editArea.getLineOfOffset( offset );
174 }
175
176 public int getLineStartOffset( int line ) throws Exception
177 {
178 return editArea.getLineStartOffset( line );
179 }
180
181 public void removeCaretListener( CaretListener listener )
182 {
183 editArea.removeCaretListener( listener );
184 }
185 }