Java Code Examples for javax.swing.JTextArea#getDocument()

The following examples show how to use javax.swing.JTextArea#getDocument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocumentFinderTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Document getDocument(String str) {
    JTextArea ta = new JTextArea(str);
    if (str != null && str.length() > 0) {
        ta.setCaretPosition(1);
    }
    Document doc = ta.getDocument();
    return doc;
}
 
Example 2
Source File: ToStringGeneratorTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testToStringWithPlusOperator() throws Exception {
    FileObject javaFile = FileUtil.createData(fo, "NewClass.java");
    String what1 = ""
            + "public class NewClass {\n"
            + "    private final String test1 = \"test\";\n"
            + "    private final String test2 = \"test\";\n"
            + "    private final String test3 = \"test\";\n";

    String what2 = ""
            + "\n"
            + "}";
    String what = what1 + what2;
    GeneratorUtilsTest.writeIntoFile(javaFile, what);

    JavaSource javaSource = JavaSource.forFileObject(javaFile);
    assertNotNull("Created", javaSource);

    Document doc = getDocuemnt(javaFile);

    final JTextArea component = new JTextArea(doc);
    component.setCaretPosition(what1.length());

    class Task implements org.netbeans.api.java.source.Task<CompilationController> {

        private ToStringGenerator generator;

        @Override
        public void run(CompilationController controller) throws Exception {
            controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
            Element typeElement = controller.getElements().getTypeElement("NewClass");
            generator = ToStringGenerator.createToStringGenerator(component, controller, typeElement, false);
        }

        public void post() throws Exception {
            assertNotNull("Created", generator);

            assertEquals("Three fields", 3, generator.getDescription().getSubs().size());
            assertEquals("test1 field selected", true, generator.getDescription().getSubs().get(0).isSelected());
            assertEquals("test2 field selected", true, generator.getDescription().getSubs().get(1).isSelected());
            assertEquals("test3 field selected", true, generator.getDescription().getSubs().get(2).isSelected());
            assertEquals("Don't use StringBuilder", false, generator.useStringBuilder());
        }
    }
    final Task task = new Task();

    javaSource.runUserActionTask(task, false);
    task.post();

    SwingUtilities.invokeAndWait(() -> task.generator.invoke());

    Document document = component.getDocument();
    String text = document.getText(0, document.getLength());
    String expected = ""
            + "public class NewClass {\n"
            + "    private final String test1 = \"test\";\n"
            + "    private final String test2 = \"test\";\n"
            + "    private final String test3 = \"test\";\n"
            + "\n"
            + "    @Override\n"
            + "    public String toString() {\n"
            + "        return \"NewClass{\" + \"test1=\" + test1 + \", test2=\" + test2 + \", test3=\" + test3 + '}';\n"
            + "    }\n"
            + "\n"
            + "}";
    assertEquals(expected, text);
}
 
Example 3
Source File: ToStringGeneratorTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testToStringWithStringBuilder() throws Exception {
    FileObject javaFile = FileUtil.createData(fo, "NewClass.java");
    String what1 = ""
            + "public class NewClass {\n"
            + "    private final String test1 = \"test\";\n"
            + "    private final String test2 = \"test\";\n"
            + "    private final String test3 = \"test\";\n";

    String what2 = ""
            + "\n"
            + "}";
    String what = what1 + what2;
    GeneratorUtilsTest.writeIntoFile(javaFile, what);

    JavaSource javaSource = JavaSource.forFileObject(javaFile);
    assertNotNull("Created", javaSource);

    Document doc = getDocuemnt(javaFile);

    final JTextArea component = new JTextArea(doc);
    component.setCaretPosition(what1.length());

    class Task implements org.netbeans.api.java.source.Task<CompilationController> {

        private ToStringGenerator generator;

        @Override
        public void run(CompilationController controller) throws Exception {
            controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
            Element typeElement = controller.getElements().getTypeElement("NewClass");
            generator = ToStringGenerator.createToStringGenerator(component, controller, typeElement, true);
        }

        public void post() throws Exception {
            assertNotNull("Created", generator);

            assertEquals("Three fields", 3, generator.getDescription().getSubs().size());
            assertEquals("test1 field selected", true, generator.getDescription().getSubs().get(0).isSelected());
            assertEquals("test2 field selected", true, generator.getDescription().getSubs().get(1).isSelected());
            assertEquals("test3 field selected", true, generator.getDescription().getSubs().get(2).isSelected());
            assertEquals("Use StringBuilder", true, generator.useStringBuilder());
        }
    }
    final Task task = new Task();

    javaSource.runUserActionTask(task, false);
    task.post();

    SwingUtilities.invokeAndWait(() -> task.generator.invoke());

    Document document = component.getDocument();
    String text = document.getText(0, document.getLength());
    String expected = ""
            + "public class NewClass {\n"
            + "    private final String test1 = \"test\";\n"
            + "    private final String test2 = \"test\";\n"
            + "    private final String test3 = \"test\";\n"
            + "\n"
            + "    @Override\n"
            + "    public String toString() {\n"
            + "        StringBuilder sb = new StringBuilder();\n"
            + "        sb.append(\"NewClass{test1=\").append(test1);\n"
            + "        sb.append(\", test2=\").append(test2);\n"
            + "        sb.append(\", test3=\").append(test3);\n"
            + "        sb.append('}');\n"
            + "        return sb.toString();\n"
            + "    }\n"
            + "\n"
            + "}";
    assertEquals(expected, text);
}
 
Example 4
Source File: ToStringGeneratorTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testToStringCanNotUseStringBuilder() throws Exception {
    sourceLevel = "1.4";
    FileObject javaFile = FileUtil.createData(fo, "NewClass.java");
    String what1 = ""
            + "public class NewClass {\n"
            + "    private final String test1 = \"test\";\n"
            + "    private final String test2 = \"test\";\n"
            + "    private final String test3 = \"test\";\n";

    String what2 = ""
            + "\n"
            + "}";
    String what = what1 + what2;
    GeneratorUtilsTest.writeIntoFile(javaFile, what);

    JavaSource javaSource = JavaSource.forFileObject(javaFile);
    assertNotNull("Created", javaSource);

    Document doc = getDocuemnt(javaFile);

    final JTextArea component = new JTextArea(doc);
    component.setCaretPosition(what1.length());

    class Task implements org.netbeans.api.java.source.Task<CompilationController> {

        private ToStringGenerator generator;

        @Override
        public void run(CompilationController controller) throws Exception {
            controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
            Element typeElement = controller.getElements().getTypeElement("NewClass");
            generator = ToStringGenerator.createToStringGenerator(component, controller, typeElement, true);
        }

        public void post() throws Exception {
            assertNotNull("Created", generator);

            assertEquals("Three fields", 3, generator.getDescription().getSubs().size());
            assertEquals("test1 field selected", true, generator.getDescription().getSubs().get(0).isSelected());
            assertEquals("test2 field selected", true, generator.getDescription().getSubs().get(1).isSelected());
            assertEquals("test3 field selected", true, generator.getDescription().getSubs().get(2).isSelected());
            assertEquals("Don't use StringBuilder", true, generator.useStringBuilder());
        }
    }
    final Task task = new Task();

    javaSource.runUserActionTask(task, false);
    task.post();

    SwingUtilities.invokeAndWait(() -> task.generator.invoke());

    Document document = component.getDocument();
    String text = document.getText(0, document.getLength());
    String expected = ""
            + "public class NewClass {\n"
            + "    private final String test1 = \"test\";\n"
            + "    private final String test2 = \"test\";\n"
            + "    private final String test3 = \"test\";\n"
            + "\n"
            + "    public String toString() {\n"
            + "        return \"NewClass{\" + \"test1=\" + test1 + \", test2=\" + test2 + \", test3=\" + test3 + '}';\n"
            + "    }\n"
            + "\n"
            + "}";
    assertEquals(expected, text);
}
 
Example 5
Source File: MainView.java    From HiJson with Apache License 2.0 4 votes vote down vote up
/**
 * 文本内容查找定位
 * @param key 要查找的字符串
 * @param ignoreCase 是否区分大小写
 * @param down  查找方向(向上false,向下true)
 * @param isFirst 是否从开头开始查找
 * @return
 */
public boolean startSegmentFindOrReplaceOperation(JTextArea textArea, String key, boolean ignoreCase, boolean down,boolean isFirst) {
    int length = key.length();
    Document doc = textArea.getDocument();
    int offset = textArea.getCaretPosition();
    int charsLeft = doc.getLength() - offset;
    if(charsLeft <=0 ){
        offset = 0;
        charsLeft = doc.getLength() - offset;
    }
    if (!down) {
        offset -= length;
        offset--;
        charsLeft = offset;
    }
    if(isFirst){
        offset = 0;
        charsLeft = doc.getLength() - offset;
    }
    Segment text = new Segment();
    text.setPartialReturn(true);
    try {
        while (charsLeft > 0) {
            doc.getText(offset, length, text);
            if ((ignoreCase == true && text.toString().equalsIgnoreCase(key))
                    || (ignoreCase == false && text.toString().equals(key))) {
                textArea.requestFocus();////焦点,才能能看到效果
                textArea.setSelectionStart(offset);
                textArea.setSelectionEnd(offset + length);
                return true;
            }
            charsLeft--;
            if (down) {
                offset++;
            } else {
                offset--;
            }

        }
    } catch (Exception e) {

    }
    return false;
}