001package com.ganteater.ae.desktop.ui;
002
003import java.awt.event.ActionEvent;
004import java.awt.event.ActionListener;
005import java.awt.event.KeyAdapter;
006import java.awt.event.KeyEvent;
007import java.awt.event.WindowEvent;
008import java.awt.event.WindowListener;
009
010import javax.swing.AbstractButton;
011import javax.swing.JCheckBox;
012import javax.swing.JFrame;
013
014import org.apache.commons.lang.StringUtils;
015
016import com.ganteater.ae.desktop.util.UIUtils;
017import com.ganteater.ae.desktop.view.ListLogPresenter.LogRecord;
018import com.ganteater.ae.desktop.view.TextLogPresenter;
019import com.ganteater.ae.processor.Processor;
020
021public class LogFrame extends JFrame implements WindowListener {
022
023        private static final long serialVersionUID = 1L;
024        private static final String LOG_MSG_ICON = "log-message.png";
025        private TextLogPresenter logPanel;
026        private JCheckBox onTop = new JCheckBox();
027
028        private String NOT_ALWAYS_ON_TOP_ICON = "off-always-on-top.png";
029        private String ALWAYS_ON_TOP_TOP = "on-always-on-top.png";
030
031        public LogFrame(TextLogPresenter logPanel) {
032                super("Log message");
033                this.logPanel = logPanel;
034                setIconImage(AEFrame.getIcon(LOG_MSG_ICON).getImage());
035
036                getContentPane().add(logPanel);
037
038                logPanel.getLogTextArea().addKeyListener(new KeyAdapter() {
039                        @Override
040                        public void keyReleased(KeyEvent e) {
041                                if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
042                                        setVisible(false);
043                        }
044                });
045
046                onTop.setIcon(AEFrame.getIcon(NOT_ALWAYS_ON_TOP_ICON));
047                onTop.setSelectedIcon(AEFrame.getIcon(ALWAYS_ON_TOP_TOP));
048                onTop.addActionListener(new ActionListener() {
049                        @Override
050                        public void actionPerformed(ActionEvent e) {
051                                setAlwaysOnTop(onTop.isSelected());
052                        }
053                });
054
055                onTop.setEnabled(true);
056                logPanel.addButton((AbstractButton) onTop);
057
058                pack();
059                UIUtils.applyPreferedView(this, "log_win", 400, 600);
060
061                addWindowListener(this);
062        }
063
064        public void showText(LogRecord text, Processor taskEditor, boolean autoformat) {
065                setVisible(true);
066                try {
067                        String source = text.getSource();
068                        if (source == null) {
069                                source = text.getText();
070                        }
071                        setTitle(StringUtils.abbreviate(source, 64));
072                } catch (Exception e) {
073                        // TODO: handle exception
074                }
075                out(text, taskEditor);
076                if (autoformat) {
077                        logPanel.format();
078                }
079        }
080
081        public void out(LogRecord text, Processor taskEditor) {
082                logPanel.info(text, taskEditor);
083        }
084
085        @Override
086        public void windowOpened(WindowEvent e) {
087                // TODO Auto-generated method stub
088
089        }
090
091        @Override
092        public void windowClosing(WindowEvent e) {
093                UIUtils.saveDialogPreferedView(this, "log_win");
094        }
095
096        @Override
097        public void windowClosed(WindowEvent e) {
098
099        }
100
101        @Override
102        public void windowIconified(WindowEvent e) {
103                // TODO Auto-generated method stub
104
105        }
106
107        @Override
108        public void windowDeiconified(WindowEvent e) {
109                // TODO Auto-generated method stub
110
111        }
112
113        @Override
114        public void windowActivated(WindowEvent e) {
115                // TODO Auto-generated method stub
116
117        }
118
119        @Override
120        public void windowDeactivated(WindowEvent e) {
121                UIUtils.saveDialogPreferedView(this, "log_win");
122        }
123
124        @Override
125        public void setVisible(boolean b) {
126                super.setVisible(b);
127                logPanel.getLogTextArea().requestFocus();
128        }
129}