Package com.wadegasior.jconsole

Source Code of com.wadegasior.jconsole.JConsoleUtils

package com.wadegasior.jconsole;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

/**
* Some utilities and helper functions for the JConsole application
* @author Wade Gasior
*
*/
public class JConsoleUtils {

  /**
   * Parses a space delimited string into parts
   * @param cmd The command to parse
   * @return A List of elements in the command
   */
  public static List<String> parseCommandParts(String cmd) {
    return Arrays.asList(cmd.split(" "));
  }

  /**
   * Executes a process and prints its output on the passed JTextPane
   * @param command The external command to run
   * @param dir The working directory for the external command
   * @param output The JTextPane on which to print
   */
  public static void runProcessForResult(List<String> command, File dir,
      JTextPane output) {
    ProcessBuilder pb = new ProcessBuilder(command);
    pb.directory(dir);

    Process process;
    InputStream is;
    InputStreamReader isr;
    BufferedReader br;
    try {
      process = pb.start();
      is = process.getInputStream();
      isr = new InputStreamReader(is);
      br = new BufferedReader(isr);

      String line;
      while ((line = br.readLine()) != null) {
        appendToTextPane(output, "<span style='color:#FFFFFF'>" + line + "</span>");
        output.setCaretPosition(output.getDocument().getLength());
      }

      appendToTextPane(output, "<br/>");

      br.close();
    } catch (IOException e) {
      appendToTextPane(output, "<span style='color:#ffcc00'>" + "No command '</span><span style='color:#9ec538'>"
          + toDelimitedString(command) + "</span><span style='color:#ffcc00'>' found.</span><br/>");
      appendToTextPane(output, "<br/>");
    }
  }

  /**
   * Appends html text to a JTextPane
   * @param pane the pane to be appended to
   * @param s The html string to append
   */
  public static void appendToTextPane(JTextPane pane, String s) {
    HTMLDocument doc = (HTMLDocument) pane.getDocument();
    HTMLEditorKit kit = (HTMLEditorKit) pane.getEditorKit();

    try {
      kit.insertHTML(doc, doc.getLength(), s, 0, 0, null);
    } catch (BadLocationException e) {
    } catch (IOException e) {
    }
  }

  /**
   * Reconstructs a space-delimited string from a provided List of elements
   * @param in List of string items
   * @return A string of space-delimited items
   */
  public static String toDelimitedString(List<String> in) {
    StringBuilder sb = new StringBuilder();
    for (String s : in) {
      sb.append(s + " ");
    }
    sb.deleteCharAt(sb.length() - 1);

    return sb.toString();
  }

}
TOP

Related Classes of com.wadegasior.jconsole.JConsoleUtils

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.