Menu

Docs

General
2007-01-16
2013-04-23
  • Jason Davis

    Jason Davis - 2007-01-16

    Docs are forth coming. Here is a quickstart.

    First add sdoc jar to your classpath.

    Here is a code example.

    package sdoc;

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.HashMap;
    import java.util.Map;

    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.text.Document;

    import sdoc.lexers.SQLLexer;

    public class Example
    {

        public static void main(String[] args)
        {
            //Configure the SyntaxDocumentFactory
            SyntaxDocumentFactory fac = SyntaxDocumentFactory.getInstance();
       
            //force Anti-Aliasing on - somewhat slow
            fac.forceAA(SyntaxDocumentFactory.FORCE_AA_ALWAYS_ON);
           
            //force Anti-Aliasing off - very fast - this is the default
            fac.forceAA(SyntaxDocumentFactory.FORCE_AA_ALWAYS_OFF);
       
            //not using aa is fast but makes italics look bad
            //you can enable AA just for italics - this is the default
            fac.forceAAItalics(SyntaxDocumentFactory.FORCE_AA_ALWAYS_ON);
       
            //highlight the line where the cursor is - this is on by default
            fac.highlightCurrent(true);
           
            //show print margin -- this is default
            fac.showPrintMargin(true);
           
            //enable built in basic undo/redo support
            //this is on by default
            fac.setUseDefaultUndoManager(true);
           
           
            //there are more config options check the javadoc
            // for SyntaxDocumentFactory
           
           
            //create a text area and add it to a scrollpane
            final JTextArea area = new JTextArea();
           
            JScrollPane sp = new JScrollPane(area);
           
           
            //set the font before making the gutter/line number area
            //highlighting needs a fixed width font
            area.setFont(new Font("dialoginput" , Font.PLAIN , 12));
           
            //if you want line numbering
            sp.setRowHeaderView(new Gutter(area));
           
            //Create a Document that will highlight SQL
            // this uses one of the built in lexers
            Document doc = fac.getDefaultDocument(SyntaxDocumentFactory.SQL_LEXER, area);
           
            //Create a Document that will highlight Java
            // this uses one of the built in lexers
            doc = fac.getDefaultDocument(SyntaxDocumentFactory.JAVA_LEXER, area);
           
           
            //add a lexer to SyntaxDocumentFactory
            // this method needs a Map<TokenType , SimpleStyle>
            // this method all needs a name or a file extension
            SimpleStyle keyword = new SimpleStyle();
            keyword.fontColor = new Color(127,0,85);
            keyword.bold = true;
           
            SimpleStyle plain = new SimpleStyle();
            plain.fontColor = Color.BLACK;
           
            SimpleStyle green = new SimpleStyle();
            green.fontColor = new Color(63,127,95);
            green.italic = true;
           
            SimpleStyle blue = new SimpleStyle();
            blue.fontColor = new Color(63,95,191);
            blue.italic = true;
           
            SimpleStyle literal = new SimpleStyle();
            literal.fontColor = new Color(0,0,128);
           
            Map sqlMap = new HashMap();
            sqlMap.put(SQLLexer.KEYWORD_STYLE, keyword);
            sqlMap.put(SQLLexer.OPERATOR_STYLE, plain);
            sqlMap.put(SQLLexer.PLAIN_STYLE, plain);
            sqlMap.put(SQLLexer.DATA_STYLE, plain);
            sqlMap.put(SQLLexer.DEFAULT, plain);
            sqlMap.put(SQLLexer.LITERAL_STYLE, literal);
            sqlMap.put(SQLLexer.COMMENT_STYLE, green);
            sqlMap.put(SQLLexer.MULTILINE_BEGIN_1, green);
            sqlMap.put(SQLLexer.MULTILINE_CONTENT_1, green);
            sqlMap.put(SQLLexer.MULTILINE_END_1, green);
           
            //add lexer to factory for reuse
            fac.addLexer(new SQLLexer(), sqlMap, "sql");
           
            //get a document using the above lexer that was added
            doc = fac.getDocumentForExtension("sql", area);
           
            //create a document who lexer and colors are not added to the cache
            doc = fac.getDocument(new SQLLexer(), sqlMap, area);
           
           
            //set the document on the text area
            area.setDocument(doc);
           
            //show it and use it
            JFrame frame = new JFrame("Highlight Test");
           
            frame.setContentPane(sp);
            frame.pack();
            frame.setSize(800,600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
            frame.addWindowListener(new WindowAdapter()
            {
                public void windowOpened(WindowEvent e)
                {
                    area.requestFocusInWindow();
                }
            });
           
           
       
        }
       
    }

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.