/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.gui.util;

import java.util.Properties;

import org.apache.jmeter.util.JMeterUtils;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RUndoManager;

/**
 * Utility class to handle RSyntaxTextArea code
 */
public class JSyntaxTextArea extends RSyntaxTextArea {

    private static final long serialVersionUID = 210L;

    private final Properties languageProperties = JMeterUtils.loadProperties("org/apache/jmeter/gui/util/textarea.properties"); //$NON-NLS-1$;

    private static final boolean WRAP_STYLE_WORD = JMeterUtils.getPropDefault("jsyntaxtextarea.wrapstyleword", true);
    private static final boolean LINE_WRAP       = JMeterUtils.getPropDefault("jsyntaxtextarea.linewrap", true);
    private static final boolean CODE_FOLDING    = JMeterUtils.getPropDefault("jsyntaxtextarea.codefolding", true);
    private static final int MAX_UNDOS           = JMeterUtils.getPropDefault("jsyntaxtextarea.maxundos", 50);

    @Deprecated
    public JSyntaxTextArea() {
        // For use by test code only
    }

    /**
     * Creates the default syntax highlighting text area.
     * The following are set:
     * <ul>
     * <li>setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA)</li>
     * <li>setCodeFoldingEnabled(true)</li>
     * <li>setAntiAliasingEnabled(true)</li>
     * <li>setLineWrap(true)</li>
     * <li>setWrapStyleWord(true)</li>
     * </ul>
     * @param rows
     * @param cols
     */
    public JSyntaxTextArea(int rows, int cols) {
        super(rows, cols);
        super.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
        super.setCodeFoldingEnabled(CODE_FOLDING);
        super.setAntiAliasingEnabled(true);
        super.setLineWrap(LINE_WRAP);
        super.setWrapStyleWord(WRAP_STYLE_WORD);
    }

    /**
     * Sets the language of the text area.
     * @param language
     */
    public void setLanguage(String language) {
        final String style = languageProperties.getProperty(language);
        if (style == null) {
            super.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);
        } else {
            super.setSyntaxEditingStyle(style);
        }
    }

    /**
     * Override UndoManager to allow disabling if feature causes issues
     * See https://github.com/bobbylight/RSyntaxTextArea/issues/19
     */
    @Override
    protected RUndoManager createUndoManager() {
        RUndoManager undoManager = super.createUndoManager();
        undoManager.setLimit(MAX_UNDOS);
        return undoManager;
    }

    /**
     * Sets initial text resetting undo history
     * @param string
     */
	public void setInitialText(String string) {
		setText(string);
		discardAllEdits();
	}
}

@@ -1368,16 +1368,53 @@
 LS-1$;%0A%0A
+%09private final boolean disableUndo;%0A%0A
     priv
@@ -1948,16 +1948,42 @@
 de only%0A
+    %09this(30, 50, false);%0A
     %7D%0A%0A 
@@ -2428,24 +2428,610 @@
 int cols) %7B%0A
+        this(rows, cols, false);%0A    %7D%0A%0A    /**%0A     * Creates the default syntax highlighting text area.%0A     * The following are set:%0A     * %3Cul%3E%0A     * %3Cli%3EsetSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA)%3C/li%3E%0A     * %3Cli%3EsetCodeFoldingEnabled(true)%3C/li%3E%0A     * %3Cli%3EsetAntiAliasingEnabled(true)%3C/li%3E%0A     * %3Cli%3EsetLineWrap(true)%3C/li%3E%0A     * %3Cli%3EsetWrapStyleWord(true)%3C/li%3E%0A     * %3C/ul%3E%0A     * @param rows%0A     * @param cols%0A     * @param disableUndo true to disable undo manager, defaults to false%0A     */%0A    public JSyntaxTextArea(int rows, int cols, boolean disableUndo) %7B%0A
         supe
@@ -3299,31 +3299,62 @@
 _WORD);%0A    
-%7D%0A%0A    
+%09this.disableUndo = disableUndo;%0A%09%7D%0A%0A%09
 /**%0A     * S
@@ -4012,16 +4012,103 @@
         
+if(disableUndo) %7B%0A        %09undoManager.setLimit(0);        %09%0A        %7D else %7B%0A        %09
 undoMana
@@ -4132,16 +4132,26 @@
 UNDOS);%0A
+        %7D%0A
         

Add constructor parameter to disable undo