Java Code Examples for org.netbeans.editor.BaseDocument#replace()

The following examples show how to use org.netbeans.editor.BaseDocument#replace() . 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: AndroidStyleable.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
@Override
public void defaultAction(JTextComponent component) {
    if (component != null) {
        try {
            BaseDocument document = (BaseDocument) component.getDocument();
            int caretPosition = component.getCaretPosition();
            String text = Utilities.getIdentifierBefore(document, component.getCaretPosition());
            if (text == null) {
                text = "";
            }
            int startPosition = caretPosition - text.length();
            if (AndroidStyleableStore.ANDROID_NAMESPACE.equals(nameSpacePath)) {
                document.replace(startPosition, text.length(), name, null);
            } else {
                document.replace(startPosition, text.length(), fullClassName, null);
            }
            Completion.get().hideDocumentation();
            Completion.get().hideCompletion();
            RankingProvider.inserted(fullClassName.hashCode());
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
}
 
Example 2
Source File: AttrCompletionItem.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
@Override
public void defaultAction(JTextComponent component) {
    if (component != null) {
        try {
            BaseDocument document = (BaseDocument) component.getDocument();
            int caretPosition = component.getCaretPosition();
            String text = Utilities.getIdentifierBefore(document, component.getCaretPosition());
            if (text == null) {
                text = "";
            }
            int startPosition = caretPosition - text.length();
            document.replace(startPosition, text.length(), completionText + "=\"\"", null);
            Completion.get().hideAll();
            RankingProvider.inserted(attr.getName().hashCode());
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
}
 
Example 3
Source File: BasicValuesCompletionItem.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
@Override
public void defaultAction(JTextComponent component) {
    if (component != null) {
        try {
            BaseDocument document = (BaseDocument) component.getDocument();
            int caretPosition = component.getCaretPosition();
            int startPosition = caretPosition - 1;
            while ('\"' != (document.getChars(startPosition, 1)[0])) {
                startPosition--;
            }
            startPosition++;
            document.replace(startPosition, caretPosition - startPosition, completionText, null);
            Completion.get().hideAll();
            RankingProvider.inserted(completionText.hashCode());
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);

        }
    }

}
 
Example 4
Source File: BasicColorValuesCompletionItem.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
@Override
public CompletionDocumentation resolveLink(String link) {
    if ("picker".equals(link)) {
        Component component = findComponentUnderMouse();
        if (component != null) {
            Color color = JColorChooser.showDialog(component, "Color Picker", decodeAlfa(value));
            if (color != null) {
                try {
                    BaseDocument document = (BaseDocument) this.document;
                    int startPosition = caretOffset - 1;
                    while ('\"' != (document.getChars(startPosition, 1)[0])) {
                        startPosition--;
                    }
                    startPosition++;
                    document.replace(startPosition, caretOffset - startPosition, getHTMLColorString(color), null);
                    Completion.get().hideAll();
                    RankingProvider.inserted(completionText.hashCode());
                } catch (BadLocationException ex) {
                    Exceptions.printStackTrace(ex);

                }
            }
        }
    }
    return null;
}
 
Example 5
Source File: ValueResultItemTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Test
public void testValueReplacement() throws Exception {
    JTextField textField = new JTextField();
    BaseDocument doc = getDocument("/org/netbeans/modules/xml/text/completion/res/ValueReplacement.xml");
    BaseDocument referenceDoc = getDocument("/org/netbeans/modules/xml/text/completion/res/ValueReplacement.golden.xml");
    Finder ip1Finder = new FinderFactory.StringFwdFinder("##IP1##", true);
    int insertPos1 = doc.find(ip1Finder, doc.getStartPosition().getOffset(), doc.getEndPosition().getOffset());
    doc.replace(insertPos1, 7, "", null);
    textField.setDocument(doc);
    MockGrammarResult mgr = new MockGrammarResult("Middle", "Mid");
    ValueResultItem vri1 = new ValueResultItem(insertPos1, mgr, 0, null);
    textField.setCaretPosition(insertPos1);
    vri1.defaultAction(textField);

    Finder ip2Finder = new FinderFactory.StringFwdFinder("##IP2##", true);
    int insertPos2 = doc.find(ip2Finder, doc.getStartPosition().getOffset(), doc.getEndPosition().getOffset());
    doc.replace(insertPos2, 7, "", null);
    ValueResultItem vri2 = new ValueResultItem(insertPos2, mgr, 7, null);
    textField.setCaretPosition(insertPos2);
    vri2.defaultAction(textField);

    MockGrammarResult mgr2 = new MockGrammarResult("Middle", "");
    Finder ip3Finder = new FinderFactory.StringFwdFinder("##IP3##", true);
    int insertPos3 = doc.find(ip3Finder, doc.getStartPosition().getOffset(), doc.getEndPosition().getOffset());
    doc.replace(insertPos3, 7, "", null);
    ValueResultItem vri3 = new ValueResultItem(insertPos3, mgr2, 7, null);
    textField.setCaretPosition(insertPos3);
    vri3.defaultAction(textField);

    assertTrue("Result did not match reference", compare(referenceDoc, doc));
}