/*≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
PROJECT: GenUtils
AUTHOR: DCR
DATE: 2012-10-24
FILENAME: DCRAdvancedTextConsole.java
PURPOSE: A styled text console for output, whatever.
VERSION: 20141009-1424
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡*/
package IRP; //Added 2014.10.08 for IRP project
//=[BEGIN IMPORTS]==============================================================
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
//=[END IMPORTS]================================================================
//=[BEGIN CLASS DCRAdvancedTextConsole]=========================================
public class DCRAdvancedTextConsole extends JTextPane
{
//-[BEGIN DATA MEMBERS]-----------------------------------------------------
public static final int INS_BEG = 0;
public static final int INS_END = 1;
private final boolean insertAtTop;
private final Style dStyle;
private Style cStyle;
private StyleContext styleContext;
// private final StyledDocument doc;
private DefaultStyledDocument doc;
//-[END DATA MEMBERS]-------------------------------------------------------
//-[BEGIN CONSTRUCTOR(S)]---------------------------------------------------
DCRAdvancedTextConsole(int insertMode)
{
insertAtTop = insertMode == INS_BEG;
// doc = getStyledDocument();
// dStyle = addStyle("Default", null);
// cStyle = addStyle("Custom", null);
styleContext = new StyleContext();
dStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE);
cStyle = styleContext.addStyle("CUSTOM", dStyle);
StyleConstants.setLeftIndent(cStyle, 50f);
StyleConstants.setFirstLineIndent(cStyle, -50f);
doc = new DefaultStyledDocument(styleContext);
this.setDocument(doc);
setEditable(false);
}
//-[END CONSTRUCTOR(S)]-----------------------------------------------------
//-[BEGIN METHOD addLine]---------------------------------------------------
public final void addLine(String s, Color c, boolean bold, boolean italic)
{
StyleConstants.setForeground(cStyle, c);
StyleConstants.setBold(cStyle, bold);
StyleConstants.setItalic(cStyle, italic);
String ts = new SimpleDateFormat("[hh:mm:ss a] ").format(new Date());
try
{
if(insertAtTop)
{
doc.setLogicalStyle(0, dStyle);
doc.insertString(0, ts, (AttributeSet)null);
doc.setLogicalStyle(0, cStyle);
doc.insertString(0, s + "\n", (AttributeSet)null);
setCaretPosition(0);
}
else
{
doc.setLogicalStyle(doc.getLength(), dStyle);
doc.insertString(doc.getLength(), ts, (AttributeSet)null);
doc.setLogicalStyle(doc.getLength(), cStyle);
doc.insertString(doc.getLength(), s + "\n", (AttributeSet)null);
setCaretPosition(doc.getLength());
}
}
catch(BadLocationException e)
{
System.out.println("Caught BadLocationException within "
+ "DCRAdvancedTextConsole.addLine(). This should never happen, "
+ "but stranger things have occurred.");
}
}
//-[END METHOD addLine]-----------------------------------------------------
//-[BEGIN METHOD printTestStrings]------------------------------------------
public void printTestStrings()
{
addLine("Blue text, no bold, no italic.", Color.BLUE, false, false);
addLine("Red text, bold, no italic.", Color.RED, true, false);
addLine("Green text, no bold, italic.", Color.GREEN, false, true);
}
//-[END METHOD printTestStrings]--------------------------------------------
//-[BEGIN METHOD setFontFamily]---------------------------------------------
public void setFontFamily(String family)
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ArrayList al = new ArrayList(Arrays.asList(ge.getAvailableFontFamilyNames()));
if(al.contains(family))
{
StyleConstants.setFontFamily(dStyle, family);
StyleConstants.setFontFamily(cStyle, family);
}
else
{
StyleConstants.setFontFamily(dStyle, "Dialog");
StyleConstants.setFontFamily(cStyle, "Dialog");
}
}
//-[END METHOD setFontFamily]-----------------------------------------------
//-[BEGIN METHOD setFontSize]-----------------------------------------------
public void setFontSize(int size)
{
StyleConstants.setFontSize(dStyle, size);
StyleConstants.setFontSize(cStyle, size);
}
//-[END METHOD setFontSize]-------------------------------------------------
}
//=[END CLASS DCRAdvancedTextConsole]===========================================
//≡[EOF]≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡