/* * [BWTokenPanel.java] * * Summary: Renders a string of tokens, usually representing Java source code. * * Copyright: (c) 2004-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 4.0 2009-04-12 shorter style names, improved highlighting. */ package com.mindprod.jdisplay; import com.mindprod.jtokens.NL; import com.mindprod.jtokens.Token; import javax.swing.JTextArea; /** * Renders a string of tokens, usually representing Java source code. * * @author Roedy Green, Canadian Mind Products * @version 4.0 2009-04-12 shorter style names, improved highlighting. * @since 2004 */ public final class BWTokenPanel extends JTextArea { /** * version number of this class */ static final long serialVersionUID = 1L; /** * accepts an array of tokens to display in plain black and white on an AWT TextArea, that support copy/paste, but * nothing fancy in fonts, colours etc. * * @param tokens array of Tokens to display. */ public void setTokens( Token[] tokens ) { // render all the tokens, some may be offscreen, but no matter. StringBuilder sb = new StringBuilder( tokens.length * 15 ); for ( Token t : tokens ) { if ( t instanceof NL ) { // remove excess blank lines. int nls = Math.min( ( ( NL ) t ).getCount(), 2 ); for ( int j = 0; j < nls; j++ ) { sb.append( '\n' ); } } else { // colour and font are immaterial sb.append( t.getText() ); } } this.setText( sb.toString() ); // will trigger a repaint. } }