Skip to content

Instantly share code, notes, and snippets.

@GlasFrost
Created December 26, 2014 13:48
Show Gist options
  • Save GlasFrost/1fe35b34a822bd1c79f3 to your computer and use it in GitHub Desktop.
Save GlasFrost/1fe35b34a822bd1c79f3 to your computer and use it in GitHub Desktop.
Helper class for various tasks
/*
* Copyright (c) 2014 Luis Hartmann
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.glasfrost;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;
/**
* Helper class by Luis Hartmann aka. GlasFrost
* @author Luis Hartmann
*/
public class GlasfrostUtils {
/**
* Throws UnsupportedOperationException
* @param args the command line arguments
*/
public static void main(String[] args) {
L.log(Level.SEVERE, "Do not run main()", new UnsupportedOperationException("com.glasfrost.GlasfrostUtils.main() is not supposed to be run as stand-alone application."));
}
public static GlasfrostUtils getInstance(){
return new GlasfrostUtils();
}
/**
* Redirect System.err to jTextArea
* @param textArea
*/
public void errToTextArea(JTextArea textArea){
PrintStream printStream = new PrintStream(new TextAreaOutputStream(textArea));
System.setErr(printStream);
}
/**
* Redirect System.out to jTextArea
* @param textArea
*/
public void outToTextArea(JTextArea textArea){
PrintStream printStream = new PrintStream(new TextAreaOutputStream(textArea));
System.setOut(printStream);
}
/**
* Outputs data to given javax.swing.JTextArea
*/
public class TextAreaOutputStream extends OutputStream {
private JTextArea textArea;
/**
* Constructor
* @param textArea jTextArea to output data into
*/
public TextAreaOutputStream(JTextArea textArea) {
this.textArea = textArea;
}
@Override
public void write(int b) throws IOException {
// Append data to jTextArea
textArea.append(String.valueOf((char)b));
// Scroll to the very end
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
/**
* Logger for class GlasfrostUtils
*/
public static final Logger L = Logger.getLogger(GlasfrostUtils.class.getName());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment