Example usage for javax.swing.text StyledEditorKit StyledEditorKit

List of usage examples for javax.swing.text StyledEditorKit StyledEditorKit

Introduction

In this page you can find the example usage for javax.swing.text StyledEditorKit StyledEditorKit.

Prototype

public StyledEditorKit() 

Source Link

Document

Creates a new EditorKit used for styled documents.

Usage

From source file:Main.java

public static void main(String args[]) throws BadLocationException {
    JTextPane textPane1 = new JTextPane();

    MutableAttributeSet black = new SimpleAttributeSet();
    MutableAttributeSet red = new SimpleAttributeSet();

    StyleConstants.setForeground(black, Color.black);
    StyleConstants.setForeground(red, Color.red);
    textPane1.setEditorKit(new StyledEditorKit());
    doc1 = textPane1.getDocument();/*w  ww . j a v a 2  s.  c  om*/

    append1("This is a Test!\n");

    attribute = red;
    append1("Hello world! Hello Stackoverflow\n");

    attribute = black;
    append1("the text is black again\n");

    StyledDocument styledDocument = textPane1.getStyledDocument();
    Element element;

    JTextPane textPane2 = new JTextPane();
    textPane2.setEditorKit(new StyledEditorKit());

    doc2 = textPane2.getDocument();
    for (int i = 0; i < styledDocument.getLength(); i++) {
        element = styledDocument.getCharacterElement(i);
        AttributeSet attributeNew = element.getAttributes();
        System.out.println(i);
        append2(styledDocument.getText(i, 1), attributeNew);
    }

    JFrame frame1 = new JFrame();
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setSize(400, 300);
    frame1.getContentPane().add(new JScrollPane(textPane1), BorderLayout.CENTER);
    frame1.setVisible(true);

    JFrame frame2 = new JFrame();
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setSize(400, 300);
    frame2.setLocation(300, 0);
    frame2.getContentPane().add(new JScrollPane(textPane2), BorderLayout.CENTER);
    frame2.setVisible(true);
}

From source file:org.owasp.jbrofuzz.fuzz.ui.FuzzingPanel.java

private JTextPane createEditablePane() {

    JTextPane textPane = new JTextPane();

    // Get the preferences for wrapping lines of text
    final boolean wrapText = JBroFuzz.PREFS.getBoolean(JBroFuzzPrefs.FUZZING[2].getId(), false);

    if (wrapText) {
        textPane = new JTextPane();
    } else {// w w  w. jav a 2 s .co  m
        textPane = new NonWrappingTextPane();
    }

    textPane.putClientProperty("charset", "UTF-8");
    textPane.setEditable(true);
    textPane.setVisible(true);
    textPane.setFont(new Font("Verdana", Font.PLAIN, 12));
    textPane.setMargin(new Insets(1, 1, 1, 1));
    textPane.setBackground(Color.WHITE);
    textPane.setForeground(Color.BLACK);

    // Set the editor kit responsible for highlighting
    textPane.setEditorKit(new StyledEditorKit() {

        private static final long serialVersionUID = -6085642347022880064L;

        public Document createDefaultDocument() {
            return new TextHighlighter();
        }

    });

    // Right click: Cut, Copy, Paste, Select All
    RightClickPopups.rightClickRequestTextComponent(this, textPane);

    return textPane;
}

From source file:org.squidy.designer.zoom.impl.SourceCodeShape.java

private JEditorPane createCodePane(URL sourceCodeURL) {

    codePane = new JEditorPane() {
        @Override//from  w w w . ja  va  2s .c o m
        protected void processComponentKeyEvent(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SPACE && e.isControlDown()) {
                System.out.println("Code completion");

                int caretPosition = codePane.getCaretPosition();

                String code = codePane.getText();
                switch (code.charAt(caretPosition - 1)) {
                case '.':
                    int pseudoCaret = caretPosition - 1;
                    StringBuilder word = new StringBuilder();
                    for (char c = code.charAt(--pseudoCaret); !isEndOfWord(c); c = code.charAt(--pseudoCaret)) {
                        word.append(c);
                    }

                    word = word.reverse();

                    System.out.println("WORD: " + word);

                    // Class<?> type =
                    // ReflectionUtil.loadClass(word.toString());
                    //                  
                    // System.out.println("TYPE: " + type);

                    JPopupMenu menu = new JPopupMenu("sdaf");
                    Point p = codePane.getCaret().getMagicCaretPosition();
                    System.out.println("CARET POS: " + p);
                    // Point p = codePane.get

                    // menu.setPreferredSize(new Dimension(200, 200));
                    menu.setLocation(30, 30);
                    menu.add("test");

                    codePane.add(menu);

                    // System.out.println(p);

                    // codePane.get

                    menu.show(codePane, p.x, p.y);

                    break;
                }
            }

            super.processComponentKeyEvent(e);
        }

        /**
         * @param c
         * @return
         */
        private boolean isEndOfWord(char c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t';
        }
    };

    EditorKit editorKit = new StyledEditorKit() {

        /**
         * 
         */
        private static final long serialVersionUID = 7024886168909204806L;

        public Document createDefaultDocument() {
            return new SyntaxDocument();
        }
    };

    codePane.setEditorKitForContentType("text/java", editorKit);
    codePane.setContentType("text/java");

    try {
        FileInputStream fis = new FileInputStream(sourceCodeURL.getPath());
        codePane.read(fis, null);
        originSourceCode = codePane.getText();

        computeHeightOfCodePane();
        codePane.setAutoscrolls(true);
    } catch (Exception e) {
        codePane.setText("File not found!");
    }

    codePane.requestFocus();
    codePane.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    codePane.addKeyListener(new KeyAdapter() {

        /*
         * (non-Javadoc)
         * 
         * @see
         * java.awt.event.KeyAdapter#keyPressed(java.awt.event.KeyEvent)
         */
        @Override
        public void keyPressed(KeyEvent e) {
            super.keyPressed(e);

            switch (e.getKeyCode()) {
            case KeyEvent.VK_ENTER:
            case KeyEvent.VK_DELETE:
            case KeyEvent.VK_BACK_SPACE:
            case KeyEvent.VK_SPACE:
                computeHeightOfCodePane();
                break;
            }

            markDirty();
        }
    });

    // final JPopupMenu menu = new JPopupMenu();
    //
    // JMenuItem i = new JMenuItem("Option 1");
    // JMenuItem i2 = new JMenuItem("Option 2");
    // menu.add(i);
    // menu.add(i2);
    // edit.add(menu);
    // getComponent().addKeyListener(new KeyAdapter() {
    //      
    // public void keyTyped(KeyEvent e) {
    // if(e.getModifiers() == 2 && e.getKeyChar() == ' ') {
    // Point popupLoc = edit.getCaret().getMagicCaretPosition();
    // System.out.println(popupLoc);
    // menu.setLocation(new
    // Point((int)popupLoc.getX(),(int)popupLoc.getY()));
    // menu.setVisible(true);
    // }
    //      
    // }
    // });

    return codePane;
}