/*
 * 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;

import java.awt.BorderLayout;
import java.awt.Insets;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;

import org.apache.jmeter.gui.util.JSyntaxTextArea;
import org.apache.jmeter.gui.util.JTextScrollPane;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.LogEvent;
import org.apache.log.LogTarget;
import org.apache.log.format.PatternFormatter;

/**
 * Panel that shows log events
 */
public class LoggerPanel extends JPanel implements LogTarget {

    private static final long serialVersionUID = 6911128494402594429L;

    private JSyntaxTextArea textArea;

    private final PatternFormatter format;

    // Limit length of log content
    private static final int LOGGER_PANEL_MAX_LENGTH =
            JMeterUtils.getPropDefault("jmeter.loggerpanel.maxlength", 80000); // $NON-NLS-1$

    // Make panel handle event even if closed
    private static final boolean LOGGER_PANEL_RECEIVE_WHEN_CLOSED =
            JMeterUtils.getPropDefault("jmeter.loggerpanel.enable_when_closed", true); // $NON-NLS-1$

    /**
     * Pane for display JMeter log file
     */
    public LoggerPanel() {
        init();
        format = new PatternFormatter(LoggingManager.DEFAULT_PATTERN + "\n"); // $NON-NLS-1$
    }

    private void init() {
        this.setLayout(new BorderLayout());

        // TEXTAREA
        textArea = new JSyntaxTextArea(15, 50, true);
        textArea.setEditable(false);
        textArea.setLineWrap(false);
        textArea.setLanguage("text");
        textArea.setMargin(new Insets(2, 2, 2, 2)); // space between borders and text
        JScrollPane areaScrollPane = new JTextScrollPane(textArea);

        areaScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        areaScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        this.add(areaScrollPane, BorderLayout.CENTER); 
    }

    /* (non-Javadoc)
     * @see org.apache.log.LogTarget#processEvent(org.apache.log.LogEvent)
     */
    @Override
    public void processEvent(final LogEvent logEvent) {
        if(!LOGGER_PANEL_RECEIVE_WHEN_CLOSED && !GuiPackage.getInstance().getMenuItemLoggerPanel().getModel().isSelected()) {
            return;
        }
        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                synchronized (textArea) {
                    textArea.append(format.format(logEvent));
                    int currentLength = textArea.getText().length();
                    // If LOGGER_PANEL_MAX_LENGTH is 0, it means all log events are kept
                    if(LOGGER_PANEL_MAX_LENGTH != 0 && currentLength> LOGGER_PANEL_MAX_LENGTH) {
                        textArea.setText(textArea.getText().substring(Math.max(0, currentLength-LOGGER_PANEL_MAX_LENGTH), 
                                currentLength));
                    }
                    textArea.setCaretPosition(textArea.getText().length());
                }
            }
        });
    }

    /**
     * Clear panel content
     */
    public void clear() {
        this.textArea.setText(""); // $NON-NLS-1$
    }
}

@@ -1017,16 +1017,129 @@
 ilities;
+%0Aimport javax.swing.text.BadLocationException;%0Aimport javax.swing.text.Document;%0Aimport javax.swing.text.Element;
 %0A%0Aimport
@@ -1438,16 +1438,68 @@
 rmatter;
+%0Aimport org.fife.ui.rsyntaxtextarea.SyntaxConstants;
 %0A%0A/**%0A *
@@ -1930,25 +1930,139 @@
 $NON-NLS-1$%0A
+    %0A    private static final int LOGGER_PANEL_MAX_LINES_COUNT = LOGGER_PANEL_MAX_LENGTH / 80; // $NON-NLS-1$%0A    
 %0A
-
     // Make 
@@ -2599,17 +2599,17 @@
 rea(15, 
-5
+8
 0, true)
@@ -2606,24 +2606,194 @@
  80, true);%0A
+        textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);%0A        textArea.setCodeFoldingEnabled(false);%0A        textArea.setAntiAliasingEnabled(false);%0A
         text
@@ -3824,47 +3824,99 @@
     
-textArea.append(format.format(logEvent)
+Document doc;%0A                    try %7B%0A                        doc = textArea.getDocument(
 );%0A 
@@ -3938,216 +3938,158 @@
     
-int currentLength = textArea.getText().length();%0A                    // If LOGGER_PANEL_MAX_LENGTH is 0,
+    Element root = doc.getDefaultRootElement();%0A                       
  i
+n
 t 
-means all log events are kept%0A                    if(LOGGER_PANEL_MAX_LENGTH != 0 && currentLength%3E 
+lineCount = root.getElementCount();%0A                        if (lineCount%3E
 LOGG
@@ -4102,21 +4102,26 @@
 EL_MAX_L
-ENGTH
+INES_COUNT
 ) %7B%0A    
@@ -4144,106 +4144,95 @@
     
-textArea.setText(textArea.getText().substring(Math.max(0, currentLength-LOGGER_PANEL_MAX_LENGTH), 
+    int end = root.getElement(lineCount-LOGGER_PANEL_MAX_LINES_COUNT-1).getEndOffset();
 %0A   
@@ -4260,26 +4260,25 @@
     
-    currentLength)
+doc.remove(0, end
 );%0A 
@@ -4300,85 +4300,284 @@
     
-%7D%0A                    textArea.setCaretPosition(textArea.getText().length());
+     %7D%0A                        doc.insertString(doc.getLength(), format.format(logEvent), null);%0A                        textArea.setCaretPosition(doc.getLength()-1);%0A                    %7D catch (BadLocationException e) %7B%0A                        // NOOP %0A                    %7D
 %0A   

Improve content update