Package com.mophie.android.logclient

Source Code of com.mophie.android.logclient.Main$ButtonListener

package com.mophie.android.logclient;

/*
* 2013 Fredrik T Lillejordet Released under MIT Licence, see file.
*/

import static javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;

import org.omg.CORBA.SystemException;

import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;

public class Main extends JFrame {

    private static final long serialVersionUID = 1L;

    JButton startButton, clearButton, stopButton, saveButton;
    JTextField packageField, serverField, portField, filterField, cmdField;
    JTextArea textArea;
    JCheckBox eLevel, wLevel, dLevel, iLevel, vLevel, pkgonly;
    boolean bE, bW, bD, bI, bV;
    ButtonListener listener;
    UpdateText task;
    Socket socket;
   
    String path;

    int buffer = 0;
    String pkg = "";
    String server = "";
    String port = "";
    String filter = "";

    public Main() {
        initUI();
    }

    public void initUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        Container content = new Container();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

        Container line1 = new Container();
        line1.setLayout(new FlowLayout(FlowLayout.LEFT));

        line1.add(new JLabel("Package: "));
        packageField = new JTextField(20);
        packageField.setText("com.mophie.space.android");
        line1.add(packageField);

        line1.add(new JLabel("Package Only"));
        pkgonly = new JCheckBox();
        pkgonly.setSelected(false);
        line1.add(pkgonly);

        content.add(line1);

        Container line2 = new Container();
        line2.setLayout(new FlowLayout(FlowLayout.LEFT));
        line2.add(new JLabel("Verbose"));
        vLevel = new JCheckBox();
        vLevel.setSelected(true);
        vLevel.setMargin(new Insets(0, 5, 0, 0));
        line2.add(vLevel);

        line2.add(new JLabel("Debug"));
        dLevel = new JCheckBox();
        dLevel.setMargin(new Insets(0, 0, 0, 5));
        dLevel.setSelected(true);
        line2.add(dLevel);

        line2.add(new JLabel("Info"));
        iLevel = new JCheckBox();
        iLevel.setMargin(new Insets(0, 0, 0, 5));
        iLevel.setSelected(true);
        line2.add(iLevel);

        line2.add(new JLabel("Warn"));
        wLevel = new JCheckBox();
        wLevel.setMargin(new Insets(0, 0, 0, 5));
        wLevel.setSelected(true);
        line2.add(wLevel);

        line2.add(new JLabel("Error"));
        eLevel = new JCheckBox();
        eLevel.setMargin(new Insets(0, 0, 0, 5));
        eLevel.setSelected(true);
        line2.add(eLevel);

        line2.add(new JLabel("Filter:"));
        filterField = new JTextField(15);
        filterField.setText("");
        line2.add(filterField);

        content.add(line2);

        Container line3 = new Container();
        line3.setLayout(new FlowLayout(FlowLayout.LEFT));

        line3.add(new JLabel("Server:"));
        serverField = new JTextField(10);
        serverField.setText("10.0.20.97");
        line3.add(serverField);

        line3.add(new JLabel("Port:"));
        portField = new JTextField(5);
        portField.setText("1234");
        line3.add(portField);

        content.add(line3);

        Container line4 = new Container();
        line4.setLayout(new FlowLayout(FlowLayout.LEFT));

        line4.add(new JLabel("Logcat cmd:"));
        cmdField = new JTextField(40);
        cmdField.setText("logcat -v time ");
        line4.add(cmdField);

        content.add(line4);

        Container line5 = new Container();
        line5.setLayout(new FlowLayout());

        listener = new ButtonListener();
        startButton = new JButton("Start");
        startButton.addActionListener(listener);
        line5.add(startButton);

        stopButton = new JButton("Stop");
        stopButton.addActionListener(listener);
        line5.add(stopButton);

        clearButton = new JButton("Clear");
        clearButton.addActionListener(listener);
        line5.add(clearButton);
       
        saveButton = new JButton("Save");
        saveButton.addActionListener(listener);
        line5.add(saveButton);

        content.add(line5);

        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

        textArea = new JTextArea(50, 80);
        textArea.setEditable(false);

        DefaultCaret caret = (DefaultCaret) textArea.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        JScrollPane sp = new JScrollPane(textArea);
        sp.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
        content.add(sp);

        content.setSize(d.width / 2, 100);
        sp.setSize(d.width / 2, (d.height / 3) * 2);

        add(content);

        setTitle("Logcat Viewer");
        setVisible(true);
        setPreferredSize(new Dimension(d.width / 2, d.height));
        setResizable(true);
        pack();
    }

    private void start() {
        pkg = packageField.getText().toLowerCase();
        server = serverField.getText().toLowerCase();
        port = portField.getText().toLowerCase();
        filter = filterField.getText().toLowerCase();

        packageField.setEditable(false);
        serverField.setEditable(false);
        portField.setEditable(false);
        filterField.setEditable(false);
        cmdField.setEditable(false);

        bE = eLevel.isSelected();
        bW = wLevel.isSelected();
        bD = dLevel.isSelected();
        bI = iLevel.isSelected();
        bV = vLevel.isSelected();

        startButton.setVisible(false);
        (task = new UpdateText()).execute();
    }

    private void stop() {
        if (task != null) {
            task.cancel(true);
            task = null;
        }
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e1) {
            } finally {
                socket = null;
            }
        }
        startButton.setVisible(true);
        packageField.setEditable(true);
        serverField.setEditable(true);
        portField.setEditable(true);
        filterField.setEditable(true);
        cmdField.setEditable(true);
    }

    class UpdateText extends SwingWorker<String, String> {

        @Override
        public String doInBackground() throws Exception {
            if (isCancelled()) {
                return null;
            }
            try {
                System.out.println("server " + server + " port:" + port);
                socket = new Socket(server, Integer.parseInt(port));

                // write logcat cmd to server
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                        socket.getOutputStream()));

                String cmd = cmdField.getText();
                if (cmd != null && cmd.startsWith("logcat") && cmd.length() > "logcat".length()) {
                    out.write(cmd);
                    out.write("\r\n");
                }
                if (pkgonly.isSelected()) {
                    out.write("Package:" + pkg);
                    out.write("\r\n");
                }
                out.write("\r\n");
                out.flush();

                System.out.println("socket connected to server " + server);
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));

                String line = null;
                while ((line = in.readLine()) != null) {
                    if (line.length() <= 0) {
                        continue;
                    }
                    line = filter(line);
                    if (line != null)
                        publish(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                stop();
            }
            return null;
        }

        private String filter(String line) {
            String lowercase = line.toLowerCase();

            if (filter != null && filter.length() > 0) {
                if (!lowercase.contains(filter))
                    return null;
            }

            if (!bD && line.contains("D/")) {
                return null;
            }
            if (!bE && line.contains("E/")) {
                return null;
            }
            if (!bI && line.contains("I/")) {
                return null;
            }
            if (!bW && line.contains("W/")) {
                return null;
            }
            if (!bV && line.contains("V/")) {
                return null;
            }
            return line;
        }

        @Override
        public void process(List<String> chunks) {
            for (String s : chunks) {
                if (textArea.getDocument().getLength() > 0) {
                    textArea.append("\n");
                }
                // textArea.append((lineNumber++) + " : " + s);
                textArea.append(s);
            }
            try {
                textArea.setCaretPosition(textArea.getLineStartOffset(textArea.getLineCount() - 1));
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
        }
    }

    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == startButton) {
                start();
            } else if (e.getSource() == clearButton) {
                textArea.setText("");
                // lineNumber = 0;
            } else if (e.getSource() == stopButton) {
                stop();
            } else if (e.getSource() == saveButton) {
                System.out.println("Click save");
                if (path == null || !(new File(path).exists())) {
                    JFileChooser jChooser = new JFileChooser();
                    jChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    int index = jChooser.showDialog(null, "Save to");
                    if (index == JFileChooser.APPROVE_OPTION) {
                        path = jChooser.getSelectedFile().getAbsolutePath();
                    }
                }
                StringBuilder sb = new StringBuilder();
                sb.append(path);
                sb.append("/log-");
                sb.append(System.currentTimeMillis());
                sb.append(".txt");
                FileWriter fw = null;
                try {
                    fw = new FileWriter(new File(sb.toString()));
                    fw.write(textArea.getText());
                    fw.flush();
                    textArea.setText("");
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                } finally {
                    if (fw != null) {
                        try {
                            fw.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        final Main g = new Main();
        g.setSize(d.width / 2, d.height);
    }
}
TOP

Related Classes of com.mophie.android.logclient.Main$ButtonListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.