1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
public class DecConverterLog extends JTextPane {
/** the styled document */
private StyledDocument doc;
public DecConverterLog() {
super();
doc = this.getStyledDocument();
MutableAttributeSet standard = new SimpleAttributeSet();
StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, 0, standard, true);
this.setEditable(false);
addStylesToDocument(doc);
}
public DecConverterLog(StyledDocument s) {
super(s);
}
/**
* Adds some styles to the document to change the printed text
*
* @param doc
* The StyledDocument of the text pane
*/
protected void addStylesToDocument(StyledDocument doc) {
// Initialize some styles.
Style def = StyleContext.getDefaultStyleContext().getStyle(
StyleContext.DEFAULT_STYLE);
Style regular = doc.addStyle("normal", def);
StyleConstants.setFontFamily(def, "SansSerif");
Style s = doc.addStyle("error", regular);
StyleConstants.setForeground(s, new Color(250, 0, 0));
StyleConstants.setItalic(s, true);
StyleConstants.setBold(s, true);
s = doc.addStyle("info", regular);
StyleConstants.setForeground(s, new Color(0, 64, 128));
StyleConstants.setBold(s, true);
s = doc.addStyle("compare", regular);
StyleConstants.setForeground(s, new Color(240, 130, 30));
StyleConstants.setBold(s, true);
s = doc.addStyle("ok", regular);
StyleConstants.setForeground(s, new Color(0, 128, 0));
StyleConstants.setBold(s, true);
}
/**
* insertText <br>
* This method inserts the text in the log pane <br>
*
* @param txt :
* the text to insert
* @param type :
* the type of information : error, info, normal
*/
public void insertText(String txt, String type) {
try {
// insert text
this.doc.insertString(doc.getLength(), txt + "\n", doc
.getStyle(type));
} catch (BadLocationException e) {
}
this.setCaretPosition(doc.getLength());
}
} |
Partager