/**
 * *****************************************************************************
 * Copyright (c) 2016
 *
 * Licensed 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 com.exalttech.trex.ui.views.logs;

import com.exalttech.trex.util.Util;
import java.util.Date;
import javafx.application.Platform;
import javafx.scene.control.TextArea;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.AnchorPane;
import static javafx.scene.layout.AnchorPane.setBottomAnchor;
import static javafx.scene.layout.AnchorPane.setLeftAnchor;
import static javafx.scene.layout.AnchorPane.setRightAnchor;
import static javafx.scene.layout.AnchorPane.setTopAnchor;

/**
 *
 * @author Georgekh
 */
public class ConsoleLogView extends AnchorPane {

    TextArea logsContent;
    StringBuffer sb;
    /**
     *
     */
    public ConsoleLogView() {
        setTopAnchor(this, 0d);
        setLeftAnchor(this, 0d);
        setBottomAnchor(this, 0d);
        setRightAnchor(this, 0d);
        buildUI();
    }

    private void buildUI() {
        logsContent = new TextArea();
        logsContent.setWrapText(true);
        logsContent.getStyleClass().add("consoleLogsContainer");
        setTopAnchor(logsContent, 0d);
        setLeftAnchor(logsContent, 0d);
        setBottomAnchor(logsContent, 0d);
        setRightAnchor(logsContent, 0d);
        sb = new StringBuffer();
        getChildren().add(logsContent);
    }

    /**
     *
     * @param textToAppend
     */
    public void append(String textToAppend) {
        if (textToAppend != null) {
            sb.append(LogType.INFO.getDisplayedText()).append(" ").append(Util.formatDate(new Date())).append(" ").append(textToAppend).append("\n");
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    logsContent.appendText(sb.toString());
                    sb.setLength(0);
                }
            });

        }
    }

    /**
     * Copy console log to clipboard
     */
    public void copyToClipboard() {
        // select all text
        logsContent.selectAll();
        final Clipboard clipboard = Clipboard.getSystemClipboard();
        final ClipboardContent content = new ClipboardContent();
        content.putString(logsContent.getText());
        clipboard.setContent(content);
    }
    
    /**
     * Clear log view
     */
    public void clear(){
        logsContent.setText("");
        logsContent.clear();
        Util.optimizeMemory();
    }
}

@@ -830,31 +830,8 @@
 il;%0A
-import java.util.Date;%0A
 impo
@@ -1024,189 +1024,131 @@
 ne;%0A
+%0A
 import 
-static javafx.scene.layout.AnchorPane.setBottomAnchor;%0Aimport static javafx.scene.layout.AnchorPane.setLeftAnchor;%0Aimport static javafx.scene.layout.AnchorPane.setRightAnch
+java.util.Date;%0Aimport java.util.LinkedList;%0Aimport java.util.function.Consumer;%0Aimport java.util.stream.Collect
 or
+s
 ;%0Aim
@@ -1156,58 +1156,31 @@
 ort 
-static javafx.scene.layout.AnchorPane.setTopAnchor
+java.util.stream.Stream
 ;%0A%0A/
@@ -1263,16 +1263,24 @@
  %7B%0A%0A    
+private 
 TextArea
@@ -1301,24 +1301,278 @@
     
-StringBuffer sb;
+private MessageQueue queue = new MessageQueue(5);%0A    private Consumer%3CVoid%3E logUpdater = (e) -%3E Platform.runLater(() -%3E %7B%0A        String text = queue.stream().collect(Collectors.joining(%22%5Cn%22));%0A        logsContent.clear();%0A        logsContent.appendText(text);%0A    %7D);%0A
 %0A   
@@ -2118,41 +2118,8 @@
 d);%0A
-        sb = new StringBuffer();%0A
     
@@ -2253,24 +2253,25 @@
 ToAppend) %7B%0A
+%0A
         if (
@@ -2310,18 +2310,47 @@
     
-sb.append(
+String msg = String.format(%22%25s %25s %25s%22, 
 LogT
@@ -2376,37 +2376,18 @@
 edText()
-).append(%22 %22).append(
+, 
 Util.for
@@ -2409,297 +2409,90 @@
 e())
-).append(%22 %22).append(textToAppend).append(%22%5Cn%22);%0A            Platform.runLater(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    logsContent.appendText(sb.toString());%0A                    sb.setLength(0);%0A                %7D%0A            %7D
+, textToAppend);%0A            queue.add(msg);%0A            logUpdater.accept(null
 );%0A
-%0A
     
@@ -3047,11 +3047,537 @@
 );%0A    %7D
+%0A%0A    private class MessageQueue %7B%0A        private int size;%0A%0A        private LinkedList%3CString%3E queue;%0A        %0A        public MessageQueue(int size) %7B%0A            this.size = size;%0A            this.queue = new LinkedList%3C%3E();%0A        %7D%0A        %0A        public void add(String element) %7B%0A            if (queue.size() == size) %7B%0A                queue.removeFirst();%0A            %7D%0A            queue.addLast(element);%0A        %7D%0A        %0A        public Stream%3CString%3E stream() %7B%0A            return queue.stream();%0A        %7D%0A    %7D
 %0A%7D%0A

Fix memory leak in ConsoleLogView