Example usage for javax.swing JTextArea getDocument

List of usage examples for javax.swing JTextArea getDocument

Introduction

In this page you can find the example usage for javax.swing JTextArea getDocument.

Prototype

public Document getDocument() 

Source Link

Document

Fetches the model associated with the editor.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextArea comp = new JTextArea();
    String actionName = "Lowercase";

    JFrame f = new JFrame();
    f.add(new JScrollPane(comp));
    f.setSize(300, 300);/*from   w w w.j  av a  2 s . c  om*/
    f.setVisible(true);
    comp.getInputMap().put(KeyStroke.getKeyStroke("F2"), actionName);

    comp.getActionMap().put(actionName, new TextAction(actionName) {
        public void actionPerformed(ActionEvent evt) {
            JTextComponent comp = getTextComponent(evt);

            if (comp.getSelectionStart() == comp.getSelectionEnd()) {
                if (comp.getCaretPosition() < comp.getDocument().getLength()) {
                    try {
                        int pos = comp.getCaretPosition();
                        Document doc = comp.getDocument();
                        String str = doc.getText(pos, 1).toLowerCase();

                        doc.remove(pos, 1);
                        doc.insertString(pos, str, null);
                        comp.moveCaretPosition(pos + 1);
                    } catch (Exception e) {
                        System.out.println();
                    }
                }
            } else {
                int s = comp.getSelectionStart();
                int e = comp.getSelectionEnd();

                comp.replaceSelection(comp.getSelectedText().toLowerCase());
                comp.select(s, e);
            }
        }
    });
}

From source file:Main.java

private static List<String> getLines(JTextArea textArea) {
    int lineHeight = getLineHeight(textArea);
    List<String> list = new ArrayList<String>();
    for (int num = 0;; num++) {
        int i = textArea.viewToModel(new Point(0, num * lineHeight));
        int j = textArea.viewToModel(new Point(0, (num + 1) * lineHeight));
        if (i == 0 && j == 0) {
            continue;
        }/*from  w w  w  . ja  v  a2 s. c o  m*/
        if (textArea.getDocument().getLength() == i && i == j) {
            break;
        }
        String s = removeTrailingNewLine(textArea.getText().substring(i, j));
        list.add(s);
    }
    return list;
}

From source file:com.ansorgit.plugins.bash.editor.inspections.inspections.FixShebangInspection.java

@Override
public JComponent createOptionsPanel() {
    FixShebangSettings settings = new FixShebangSettings();
    JTextArea textArea = settings.getValidCommandsTextArea();
    textArea.setText(Joiner.on('\n').join(validShebangCommands));

    textArea.getDocument().addDocumentListener(new DocumentListener() {
        @Override//w  w w . j  a  v a 2 s . c  o  m
        public void insertUpdate(DocumentEvent documentEvent) {
            updateShebangLines(documentEvent);
        }

        @Override
        public void removeUpdate(DocumentEvent documentEvent) {
            updateShebangLines(documentEvent);
        }

        @Override
        public void changedUpdate(DocumentEvent documentEvent) {
            updateShebangLines(documentEvent);
        }
    });

    return settings.getSettingsPanel();
}

From source file:com.mindcognition.mindraider.ui.swing.concept.annotation.renderer.RichTextAnnotationRenderer.java

private void insertStringToAnnotation(String stringToInsert) {
    JTextArea editor = richTextAnnotationRenderer.editor;
    try {//ww w .  j a v  a 2 s .  c o m
        final int caretPosition = editor.getCaretPosition();
        // link & htmlize
        String linked = editor.getDocument().getText(0, caretPosition) + stringToInsert
                + editor.getDocument().getText(caretPosition, editor.getDocument().getLength() - caretPosition);
        linked = linked.trim();
        // set to UI again
        editor.setText(linked);
        editor.setCaretPosition(caretPosition);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.mindcognition.mindraider.ui.swing.concept.annotation.renderer.AbstractTextAnnotationRenderer.java

private void configureEditor(JTextArea annotationTextArea) {
    // TODO use colors from the configuration
    annotationTextArea.setForeground(Color.WHITE);
    annotationTextArea.setBackground(Color.BLACK);
    annotationTextArea.setCaretColor(Color.RED);
    annotationTextArea.setSelectionColor(Color.YELLOW);
    annotationTextArea.setLineWrap(true);
    annotationTextArea.setWrapStyleWord(true);
    annotationTextArea.setFont(TEXTAREA_FONT);
    ;/*from   ww w.  ja  v a 2 s .  c  o  m*/

    // undo and redo
    undoManager = new UndoManager();
    annotationTextArea.getDocument().addUndoableEditListener(new EditorUndoListner(undoManager, toolbar));
}

From source file:hermes.renderers.DefaultMessageRenderer.java

/**
 * Show the TextMessage in a JTextArea./* ww  w . j a  va 2 s  . c  o m*/
 * 
 * @param textMessage
 * @return
 * @throws JMSException
 */
protected JComponent handleTextMessage(final TextMessage textMessage) throws JMSException {
    //
    // Show the text in a JTextArea, you can edit the message in place and
    // then drop it onto another queue/topic.

    final String text = textMessage.getText();
    final JTextArea textPane = new JTextArea();

    // final CharBuffer bytes = CharBuffer.wrap(text.subSequence(0,
    // text.length())) ;
    // final JTextArea textPane = new MyTextArea(new PlainDocument(new
    // MappedStringContent(bytes))) ;

    textPane.setEditable(false);
    textPane.setFont(Font.decode("Monospaced-PLAIN-12"));
    textPane.setLineWrap(true);
    textPane.setWrapStyleWord(true);

    textPane.append(text);

    textPane.getDocument().addDocumentListener(new DocumentListener() {
        public void doChange() {
            try {
                textMessage.setText(textPane.getText());
            } catch (JMSException e) {
                JOptionPane.showMessageDialog(textPane, "Unable to update the TextMessage: " + e.getMessage(),
                        "Error modifying message content", JOptionPane.ERROR_MESSAGE);

                try {
                    textPane.setText(textMessage.getText());
                } catch (JMSException e1) {
                    log.error(e1.getMessage(), e1);
                }

                textPane.setEditable(false);
                textPane.getDocument().removeDocumentListener(this);
            }
        }

        @Override
        public void changedUpdate(DocumentEvent arg0) {
            doChange();
        }

        @Override
        public void insertUpdate(DocumentEvent arg0) {
            doChange();
        }

        @Override
        public void removeUpdate(DocumentEvent arg0) {
            doChange();
        }
    });

    textPane.setCaretPosition(0);

    return textPane;
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.FormPane.java

@Override
public void swapTextFieldType(final InputPanel inputPanel, final short fieldLen) {
    JTextComponent oldComp;//from ww w  . j a  va2s .  c o m
    short fieldType;
    short rows;
    if (inputPanel.getComp() instanceof JTextField) {
        JTextField tf = (JTextField) inputPanel.getComp();
        tf.getDocument().removeDocumentListener(docListener);
        fieldType = WorkbenchTemplateMappingItem.TEXTAREA;
        oldComp = tf;
        rows = DEFAULT_TEXTAREA_ROWS;
    } else {
        JTextArea ta = (JTextArea) inputPanel.getComp();
        ta.getDocument().removeDocumentListener(docListener);
        fieldType = WorkbenchTemplateMappingItem.TEXTFIELD;
        oldComp = ta;
        rows = DEFAULT_TEXTFIELD_ROWS;
    }

    WorkbenchTemplateMappingItem wbtmi = inputPanel.getWbtmi();
    inputPanel.setComp(createUIComp(WorkbenchTask.getDataType(wbtmi), wbtmi.getCaption(), wbtmi.getFieldName(),
            fieldType, wbtmi.getDataFieldLength(), fieldLen, rows, wbtmi));

    ignoreChanges = true;
    ((JTextComponent) inputPanel.getComp()).setText(oldComp.getText());
    ignoreChanges = false;

    hasChanged = true;
    workbenchPane.setChanged(true);
}