package de.jowisoftware.sshclient.log;

import static de.jowisoftware.sshclient.i18n.Translation.t;

import java.awt.EventQueue;
import java.util.Observable;
import java.util.Observer;

import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

import de.jowisoftware.sshclient.ui.tabpanel.Tab;
import de.jowisoftware.sshclient.ui.tabpanel.TabPanel;
import de.jowisoftware.sshclient.ui.tabpanel.closable.ClosableTabListener;
import de.jowisoftware.sshclient.ui.tabpanel.closable.ClosableTabTitleComponent;

public final class LogTab implements Tab, Observer {
    private static class Content extends JScrollPane {
        private static final long serialVersionUID = -5153117403605046800L;

        private static final int MAX_LENGTH = 2048;
        private final DefaultListModel listModel = new DefaultListModel();

        public Content() {
            super(new JList());
            ((JList) getViewport().getView()).setModel(listModel);
        }

        public void addMessage(final String message) {
            listModel.addElement(message);
            if (listModel.getSize() > MAX_LENGTH) {
                listModel.remove(0);
            }

            validate();
            scrollToBottom();
        }

        private void scrollToBottom() {
            final JScrollBar vertical = getVerticalScrollBar();
            vertical.setValue(vertical.getMaximum());
        }
    }

    private final ClosableTabTitleComponent title;
    private final Content content = new Content();

    public LogTab(final TabPanel parent) {
        title = new ClosableTabTitleComponent(this, new JLabel(t("mainwindow.tabs.logs", "logs")));
        title.addListener(new ClosableTabListener() {
            @Override
            public void closeTab(final Tab tab) {
                parent.closeTab(LogTab.this);
            }
        });

        LogObserver.getInstance().addObserver(this);
    }

    public void dispose() {
        LogObserver.getInstance().deleteObserver(this);
    }

    @Override
    public JComponent getContent() {
        return content;
    }

    @Override
    public JComponent getTitleContent() {
        return title;
    }

    @Override public void freeze() { }
    @Override public void unfreeze() { }

    @Override
    public void update(final Observable o, final Object arg) {
        final LogMessageContainer message = (LogMessageContainer) arg;

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                content.addMessage(message.toHTML());
            }
        });
    }
}
@@ -138,52 +138,260 @@
 ava.
-util.Observable;%0Aimport java.util.Observer;%0A
+awt.Toolkit;%0Aimport java.awt.datatransfer.Clipboard;%0Aimport java.awt.datatransfer.StringSelection;%0Aimport java.awt.event.ActionEvent;%0Aimport java.util.Observable;%0Aimport java.util.Observer;%0A%0Aimport javax.swing.AbstractAction;%0Aimport javax.swing.Action;
 %0Aimp
@@ -1218,17 +1218,35 @@
         
-(
+final JList list = 
 (JList) 
@@ -1268,17 +1268,34 @@
 etView()
-)
+;%0A            list
 .setMode
@@ -1308,16 +1308,1222 @@
 Model);%0A
+            final Action action = createCopyAction(list);%0A            list.getActionMap().put(%22copy%22, action);%0A        %7D%0A%0A        private Action createCopyAction(final JList list) %7B%0A            return new AbstractAction() %7B%0A                private static final long serialVersionUID = 2681501502636412512L;%0A%0A                @Override%0A                public void actionPerformed(final ActionEvent e) %7B%0A                    final Clipboard clipboard = Toolkit.getDefaultToolkit()%0A                            .getSystemClipboard();%0A                    final StringBuffer content = new StringBuffer();%0A%0A                    for (final Object entry : list.getSelectedValues())%0A                    %7B%0A                        String line = (String) entry;%0A                        line = line.replaceAll(%22&nbsp;%22, %22 %22);%0A                        line = line.replaceAll(%22%3Cbr /%3E%22, %22%5Cn%22);%0A                        line = line.replaceAll(%22%5C%5Cs*%3C%5B%5E%3E%5D+%3E%5C%5Cs*%22, %22%22);%0A                        content.append(line).append(%22%5Cn%22);%0A                    %7D%0A%0A                    clipboard.setContents(%0A                            new StringSelection(content.toString()),%0A                            null);%0A                %7D%0A            %7D;%0A
         

copy logs in plain text format