View | Details | Raw Unified | Return to bug 52986
Collapse All | Expand All

(-)src/components/org/apache/jmeter/assertions/gui/BeanShellAssertionGui.java (-2 / +3 lines)
Lines 29-34 Link Here
29
import javax.swing.JTextField;
29
import javax.swing.JTextField;
30
30
31
import org.apache.jmeter.assertions.BeanShellAssertion;
31
import org.apache.jmeter.assertions.BeanShellAssertion;
32
import org.apache.jmeter.gui.util.BeanShellTextArea;
32
import org.apache.jmeter.testelement.TestElement;
33
import org.apache.jmeter.testelement.TestElement;
33
import org.apache.jmeter.testelement.property.BooleanProperty;
34
import org.apache.jmeter.testelement.property.BooleanProperty;
34
import org.apache.jmeter.util.JMeterUtils;
35
import org.apache.jmeter.util.JMeterUtils;
Lines 43-49 Link Here
43
44
44
    private JTextField parameters;// parameters to pass to script file (or script)
45
    private JTextField parameters;// parameters to pass to script file (or script)
45
46
46
    private JTextArea scriptField;// script area
47
    private BeanShellTextArea scriptField;// script area
47
48
48
    public BeanShellAssertionGui() {
49
    public BeanShellAssertionGui() {
49
        init();
50
        init();
Lines 136-142 Link Here
136
    }
137
    }
137
138
138
    private JPanel createScriptPanel() {
139
    private JPanel createScriptPanel() {
139
        scriptField = new JTextArea();
140
        scriptField = new BeanShellTextArea();
140
        scriptField.setRows(4);
141
        scriptField.setRows(4);
141
        scriptField.setLineWrap(true);
142
        scriptField.setLineWrap(true);
142
        scriptField.setWrapStyleWord(true);
143
        scriptField.setWrapStyleWord(true);
(-)src/core/org/apache/jmeter/gui/util/BeanShellTextArea.java (+108 lines)
Line 0 Link Here
1
package org.apache.jmeter.gui.util;
2
3
import java.awt.event.ActionEvent;
4
import java.awt.event.KeyEvent;
5
6
import javax.swing.AbstractAction;
7
import javax.swing.JComponent;
8
import javax.swing.JTextArea;
9
import javax.swing.KeyStroke;
10
import javax.swing.text.BadLocationException;
11
import javax.swing.text.Document;
12
13
public class BeanShellTextArea extends JTextArea {
14
	private static final long serialVersionUID = -2943059989300009359L;
15
16
	public BeanShellTextArea() {
17
		super();
18
		initListener();
19
	}
20
21
	private void initListener() {
22
		registerKeyboardAction(new AutoIndentAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_FOCUSED);
23
	}
24
	
25
	/* from http://www.jroller.com/santhosh/entry/autoindent_for_jtextarea */
26
	private class AutoIndentAction extends AbstractAction { 
27
		private static final long serialVersionUID = 1476242317203366836L;
28
29
		
30
		public void actionPerformed(ActionEvent ae) { 
31
	        JTextArea comp = (JTextArea)ae.getSource();
32
	        Document doc = comp.getDocument(); 
33
	 
34
	        if(!comp.isEditable()) 
35
	            return; 
36
	        try {
37
	        	int line = comp.getLineOfOffset(comp.getCaretPosition());
38
	            int start = comp.getLineStartOffset(line);
39
	            int end = comp.getLineEndOffset(line);
40
	            String str = doc.getText(start, end - start);
41
	            
42
	            String whiteSpace = getLeadingWhiteSpace(str);
43
	            
44
	            if(str.contains("{") && !str.contains("}")){
45
	            	whiteSpace += "\t";
46
	            } else if(!str.contains("{") && str.contains("}")) {
47
	            	whiteSpace = getMatchingWhitespace(comp, doc, line);
48
	            	doc.remove(start, end - start);
49
	            	doc.insertString(start, whiteSpace + str.trim(), null);
50
	            }
51
	            
52
	            doc.insertString(comp.getCaretPosition(), '\n' + whiteSpace, null);
53
	            
54
	            
55
	        } catch(BadLocationException ex) { 
56
	            // ignore
57
	        } 
58
	    } 
59
	 
60
	    private String getMatchingWhitespace(JTextArea comp, Document doc, int line) {
61
			int count = 0;
62
	    	for(; line >= 0; line--) {
63
				try {
64
		            int start = comp.getLineStartOffset(line);
65
		            int end = comp.getLineEndOffset(line);
66
		            String str = doc.getText(start, end - start);
67
					if (str.contains("{"))
68
						count--;
69
					if (str.contains("}"))
70
						count++;
71
					
72
					if(count == 0) {
73
						return getLeadingWhiteSpace(str);
74
					}
75
					
76
				} catch(Exception e) {
77
					// ignore
78
				}
79
			}
80
	    	
81
			return "";
82
		}
83
84
		/** 
85
	     *  Returns leading white space characters in the specified string. 
86
	     */ 
87
	    private String getLeadingWhiteSpace(String str) { 
88
	        return str.substring(0, getLeadingWhiteSpaceWidth(str)); 
89
	    } 
90
	 
91
	    /** 
92
	     *  Returns the number of leading white space characters in the specified string. 
93
	     */ 
94
	    private int getLeadingWhiteSpaceWidth(String str) { 
95
	        int whitespace = 0; 
96
	        while(whitespace<str.length()) { 
97
	            char ch = str.charAt(whitespace); 
98
	            if(ch==' ' || ch=='\t') 
99
	                whitespace++; 
100
	            else 
101
	                break; 
102
	        } 
103
	        return whitespace; 
104
	    }
105
106
	} 
107
	
108
}

Return to bug 52986