Commit 95c98930 authored by Matija Obreza's avatar Matija Obreza

Improved log display & log scrolling

parent 98ddec75
/**
* Copyright 2014 Global Crop Diversity Trust
*
* 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 org.genesys2.anno.gui;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import swing2swt.layout.BorderLayout;
public class LogViewer extends Composite {
private Text txtLogtext;
/**
* Create the composite.
*
* @param parent
* @param style
*/
public LogViewer(Composite parent, int style) {
super(parent, style);
setLayout(new BorderLayout(0, 0));
ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
toolBar.setLayoutData(BorderLayout.NORTH);
ToolItem tltmClear = new ToolItem(toolBar, SWT.NONE);
tltmClear.setText("Clear");
ToolItem tltmSave = new ToolItem(toolBar, SWT.NONE);
tltmSave.setText("Save...");
txtLogtext = new Text(this, SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL | SWT.MULTI);
txtLogtext.setLayoutData(BorderLayout.CENTER);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
......@@ -15,16 +15,12 @@
*/
package org.genesys2.anno.gui;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.log4j.LogManager;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.WriterAppender;
import org.apache.log4j.spi.LoggingEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
......@@ -33,9 +29,8 @@ public class SwtLogAppender extends WriterAppender {
private static final String DEFAULT_LAYOUT_PATTERN = "%d{yyyy-MM-dd HH:mm:ss} %t %-5p %c{1}:%L - %m%n";
private Text text;
private Display display;
private int maxBufferSize = 100000;
private int maxBufferSize = 1000000;
private StringBuffer logBuffer = new StringBuffer();
private AtomicBoolean resetText = new AtomicBoolean(true);
public SwtLogAppender(Text text2) {
this.text = text2;
......@@ -45,15 +40,6 @@ public class SwtLogAppender extends WriterAppender {
close();
}
});
text.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
if (resetText.getAndSet(false)) {
text.setText(logBuffer.toString());
text.setSelection(logBuffer.length());
}
}
});
display = text.getDisplay();
LogManager.getRootLogger().addAppender(this);
PatternLayout patternLayout = new org.apache.log4j.PatternLayout();
......@@ -98,14 +84,15 @@ public class SwtLogAppender extends WriterAppender {
logBuffer.replace(0, pos + 1, "");
}
}
resetText.set(true);
// System.err.println(mes);
display.asyncExec(new Runnable() {
@Override
public void run() {
if (text != null) {
text.redraw();
display.asyncExec(() -> {
if (text != null) {
int scrollPos = text.getLineCount() - 1;
text.setText(logBuffer.toString());
int topIndex = text.getTopIndex();
if ((scrollPos <= 0 && topIndex == 0) || topIndex >= scrollPos - 3) {
text.setSelection(text.getText().length() * 2);
}
}
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment