Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log status at the bottom #734

Merged
merged 9 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ public static void handleRestart() {

}

public static void applyLogPreferences() {
logManager.applyPreferences();
}

public static void showLogView() {
logManager.showLogView();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.protege.editor.core.log;

import javax.swing.SizeRequirements;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BoxView;
import javax.swing.text.ComponentView;
import javax.swing.text.Element;
import javax.swing.text.IconView;
import javax.swing.text.LabelView;
import javax.swing.text.ParagraphView;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;

/**
* A {@link StyledEditorKit} using which one can set line wrapping of the text
*
* @author Yevgeny Kazakov
*/
public class LogEditorKit extends StyledEditorKit implements ViewFactory {

private static final long serialVersionUID = -6193302665953492671L;
private boolean lineWrap_ = false;

public boolean isLineWrap() {
return lineWrap_;

}

public void setLineWrap(boolean wrap) {
this.lineWrap_ = wrap;
}

private class LogParagraphView extends ParagraphView {
public LogParagraphView(Element elem) {
super(elem);
}

@Override
protected SizeRequirements calculateMinorAxisRequirements(int axis,
SizeRequirements r) {

if (lineWrap_)
return super.calculateMinorAxisRequirements(axis, r);

SizeRequirements req = super.calculateMinorAxisRequirements(axis,
r);
req.minimum = req.preferred;
return req;
}

@Override
public int getFlowSpan(int index) {

if (lineWrap_)
return super.getFlowSpan(index);

return Integer.MAX_VALUE;
}
}

@Override
public ViewFactory getViewFactory() {
return this;
}

@Override
public View create(Element elem) {
String kind = elem.getName();
if (kind != null)
if (kind.equals(AbstractDocument.ContentElementName)) {
return new LabelView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new LogParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new BoxView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
return new LabelView(elem);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package org.protege.editor.core.log;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.AppenderBase;
import org.protege.editor.core.ui.error.ErrorLogListener;
import org.slf4j.Logger;
import ch.qos.logback.core.Context;

import org.protege.editor.core.FileUtils;
import org.protege.editor.core.ui.action.TimestampOutputAction;
import org.slf4j.LoggerFactory;

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -18,64 +26,132 @@
*/
public class LogManager {

private final LogView logView;

private final AppenderBase<ILoggingEvent> appender;
private final LogView logView;
private final Appender<ILoggingEvent> appender;

private final List<ErrorLogListener> listenerList = new ArrayList<>();

private final LoggingEventTranslator translator = new LoggingEventTranslator();
private final List<LogStatusListener> listenerList = new ArrayList<>();

private final JDialog logViewDialog;

public LogManager(LogView logView) {
this.logView = logView;

this.logView = logView;

appender = new AppenderBase<ILoggingEvent>() {

@Override
public void start() {
logView.start();
super.start();
}

@Override
public void stop() {
super.stop();
logView.stop();
}

@Override
public void setContext(Context context) {
logView.setContext(context);
super.setContext(context);
}

@Override
protected void append(ILoggingEvent event) {
LogManager.this.logView.append(translator.toLogRecord(event));
if(event.getLevel() == Level.ERROR) {
fireErrorLogged();
}
}
protected void append(ILoggingEvent event) {
logView.doAppend(event);
fireEvent(event);
}
};

JOptionPane op = new JOptionPane(logView.asJComponent(), JOptionPane.PLAIN_MESSAGE);
JComponent holder = new JPanel(new BorderLayout(7, 7));
holder.setPreferredSize(new Dimension(800, 600));
JScrollPane sp = new JScrollPane(logView.asJComponent());
sp.getVerticalScrollBar().setUnitIncrement(15);
holder.add(sp);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton clearLogButton = new JButton("Clear log");
clearLogButton.setToolTipText("Remove all log messages");
clearLogButton.addActionListener(e -> logView.clearView());
JButton showLogFile = new JButton("Show log file");
showLogFile.setToolTipText("Show the log file in the system file browser");
showLogFile.addActionListener(e -> FileUtils.showLogFile());
JButton preferencesButton = new JButton("Preferences");
preferencesButton.addActionListener(e -> showPreferences());
preferencesButton.setToolTipText("Display log preferences");
JButton timeStampButton = new JButton("Time stamp");
timeStampButton.addActionListener(e -> TimestampOutputAction.createTimeStamp(holder));
timeStampButton.setToolTipText("Print a timestamp and optional message into the logs or console");
buttonPanel.add(showLogFile);
buttonPanel.add(preferencesButton);
buttonPanel.add(timeStampButton);
buttonPanel.add(clearLogButton);
holder.add(buttonPanel, BorderLayout.SOUTH);

JOptionPane op = new JOptionPane(holder, JOptionPane.PLAIN_MESSAGE);
logViewDialog = op.createDialog(null, "Log");
logViewDialog.setModal(false);
logViewDialog.setResizable(true);
}

public void addErrorLogListener(ErrorLogListener listener) {
public synchronized void addErrorLogListener(LogStatusListener listener) {
listenerList.add(listener);
}

public void removeErrorLogListener(ErrorLogListener listener) {
listenerList.remove(listener);
}
public synchronized void removeErrorLogListener(LogStatusListener listener) {
listenerList.remove(listener);
}

private void fireErrorLogged() {
new ArrayList<>(listenerList)
.stream()
.forEach(ErrorLogListener::errorLogged);
}
private synchronized void fireEvent(ILoggingEvent event) {
for (int i = 0; i < listenerList.size(); i++) {
listenerList.get(i).eventLogged(event);
}
}

private synchronized void fireErrorsCleared() {
listenerList.stream().forEach(LogStatusListener::statusCleared);
}

private ch.qos.logback.classic.Logger getRootLogger() {
return (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
private Logger getRootLogger() {
return (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
}

public void bind() {
ch.qos.logback.classic.Logger rootLogger = getRootLogger();
rootLogger.addAppender(appender);
appender.start();
applyPreferences();
Logger rootLogger = getRootLogger();
appender.setContext(rootLogger.getLoggerContext());
appender.start();
rootLogger.addAppender(appender);
}

public void unbind() {
appender.stop();
public void unbind() {
getRootLogger().detachAppender(appender);
appender.stop();
}

public void showLogView() {
logViewDialog.setVisible(true);
fireErrorsCleared();
}

private void showPreferences() {
LogPreferencesPanel panel = new LogPreferencesPanel();
panel.initialise();
JOptionPane op = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE,
JOptionPane.OK_CANCEL_OPTION);
JDialog dlg = op.createDialog(logViewDialog, "Log Preferences");
dlg.setResizable(true);
dlg.setVisible(true);
Object value = op.getValue();
if (value != null && (int) value == JOptionPane.OK_OPTION) {
panel.applyChanges();
}
}

public void applyPreferences() {
getRootLogger().setLevel(
Level.toLevel(LogPreferences.create().load().logLevel));
logView.applyPreferences();
}
}
Loading