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

The following examples show how to use org.netbeans.editor.BaseDocument#insertString() . 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: JPACompletionItem.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected void substituteText(JTextComponent c, int offset, int len, String toAdd) {
    BaseDocument doc = (BaseDocument) c.getDocument();
    CharSequence prefix = getInsertPrefix();
    String text = prefix.toString();
    if (toAdd != null) {
        text += toAdd;
    }

    doc.atomicLock();
    try {
        Position position = doc.createPosition(offset);
        doc.remove(offset, len);
        doc.insertString(position.getOffset(), text.toString(), null);
    } catch (BadLocationException ble) {
        // nothing can be done to update
    } finally {
        doc.atomicUnlock();
    }
}
 
Example 2
Source File: AbstractTestCase.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected static BaseDocument getResourceAsDocument(String path) throws Exception {
    InputStream in = AbstractTestCase.class.getResourceAsStream(path);
    BaseDocument sd = new BaseDocument(true, "text/xml"); //NOI18N
    BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
    StringBuffer sbuf = new StringBuffer();
    try {
        String line = null;
        while ((line = br.readLine()) != null) {
            sbuf.append(line);
            sbuf.append(System.getProperty("line.separator"));
        }
    } finally {
        br.close();
    }
    sd.insertString(0,sbuf.toString(),null);
    return sd;
}
 
Example 3
Source File: WSCompletionItem.java    From netbeans with Apache License 2.0 6 votes vote down vote up
void substituteText(JTextComponent c, int offset, int len, String toAdd) {
    BaseDocument doc = (BaseDocument)c.getDocument();
    String text = getInsertPrefix().toString();
    if (text != null) {
        // Update the text
        doc.atomicLock();
        try {
            String textToReplace = doc.getText(offset, len);
            if (text.equals(textToReplace)) {
                return;
            }                
            Position position = doc.createPosition(offset);
            doc.remove(offset, len);
            doc.insertString(position.getOffset(), text, null);
        } catch (BadLocationException e) {
            // Can't update
        } finally {
            doc.atomicUnlock();
        }
    }
}
 
Example 4
Source File: ToggleBlockCommentActionTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void testInFile(String file) throws Exception {
    FileObject fo = getTestFile(file);
    assertNotNull(fo);
    String source = readFile(fo);

    int sourcePos = source.indexOf('^');
    assertNotNull(sourcePos);
    String sourceWithoutMarker = source.substring(0, sourcePos) + source.substring(sourcePos+1);

    JEditorPane ta = getPane(sourceWithoutMarker);
    Caret caret = ta.getCaret();
    caret.setDot(sourcePos);
    BaseDocument doc = (BaseDocument) ta.getDocument();

    Action a = new ToggleBlockCommentAction();
    a.actionPerformed(new ActionEvent(ta, 0, null));

    doc.getText(0, doc.getLength());
    doc.insertString(caret.getDot(), "^", null);

    String target = doc.getText(0, doc.getLength());
    assertDescriptionMatches(file, target, false, ".toggleComment");
}
 
Example 5
Source File: GroovyTypedTextInterceptor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Called to add semicolon after bracket for some conditions
 *
 * @param context
 * @return relative caretOffset change
 * @throws BadLocationException
 */
private static boolean moveSemicolon(BaseDocument doc, int dotPos, Caret caret) throws BadLocationException {
    TokenSequence<GroovyTokenId> ts = LexUtilities.getPositionedSequence(doc, dotPos);
    if (ts == null || isStringOrComment(ts.token().id())) {
        return false;
    }
    int lastParenPos = dotPos;
    int index = ts.index();
    // Move beyond semicolon
    while (ts.moveNext() && !(ts.token().id() == GroovyTokenId.NLS)) {
        switch (ts.token().id()) {
            case RPAREN:
                lastParenPos = ts.offset();
                break;
            case WHITESPACE:
                break;
            default:
                return false;
        }
    }
    // Restore javaTS position
    ts.moveIndex(index);
    ts.moveNext();
    if (isForLoopOrTryWithResourcesSemicolon(ts) || posWithinAnyQuote(doc, dotPos, ts) || (lastParenPos == dotPos && !ts.token().id().equals(GroovyTokenId.RPAREN))) {
        return false;
    }
    
    doc.remove(dotPos, 1);
    doc.insertString(lastParenPos, ";", null); // NOI18N
    caret.setDot(lastParenPos + 1);
    return true;
}
 
Example 6
Source File: StrutsEditorUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static int writeString(BaseDocument doc, String text, int offset) throws BadLocationException {
    int formatLength = 0;
    Indent indent = Indent.get(doc);
    Reformat fmt = Reformat.get(doc);
    indent.lock();
    try {
        fmt.lock();
        try {
            doc.atomicLock();
            try{
                offset = indent.indentNewLine(offset + 1);
                doc.insertString(Math.min(offset, doc.getLength()), text, null );
                Position endPos = doc.createPosition(offset + text.length() - 1);
                fmt.reformat(offset, endPos.getOffset());
                formatLength = Math.max(0, endPos.getOffset() - offset);
            }
            finally{
                doc.atomicUnlock();
            }
        } finally {
            fmt.unlock();
        }
    } finally {
        indent.unlock();
    }
    return Math.min(offset + formatLength + 1, doc.getLength());
}
 
Example 7
Source File: CslTestBase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public BaseDocument getDocument(String s, final String mimeType, final Language language) {
    try {
        BaseDocument doc = new BaseDocument(true, mimeType) {
            @Override
            public boolean isIdentifierPart(char ch) {
                if (mimeType != null) {
                    org.netbeans.modules.csl.core.Language l = LanguageRegistry.getInstance().getLanguageByMimeType(mimeType);
                    if (l != null) {
                        GsfLanguage gsfLanguage = l.getGsfLanguage();
                        if (gsfLanguage != null) {
                            return gsfLanguage.isIdentifierChar(ch);
                        }
                    }
                }

                return super.isIdentifierPart(ch);
            }
        };

        //doc.putProperty("mimeType", mimeType);
        doc.putProperty(org.netbeans.api.lexer.Language.class, language);

        doc.insertString(0, s, null);

        return doc;
    }
    catch (Exception ex){
        fail(ex.toString());
        return null;
    }
}
 
Example 8
Source File: SanitizeCurlyTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void execute(String original, String expected, int expectedDelta) throws Exception {
    int originalLength = original.length();
    GSFPHPParser parser = new GSFPHPParser();
    BaseDocument doc = new BaseDocument(true, "text/x-php5");
    doc.insertString(0, original, null);
    Context context = new GSFPHPParser.Context(Source.create(doc).createSnapshot() , -1);
    parser.sanitizeCurly(context);
    assertEquals(expected, context.getSanitizedSource());
    assertEquals(originalLength+expectedDelta, context.getSanitizedSource().length());
}
 
Example 9
Source File: XhtmlElLexerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testDoubleQuotedClosingCurlyBracketWithEscape() throws BadLocationException {
    BaseDocument doc = createDocument();
    doc.insertString(0, "<div> #{\"te\\\"xt}text\"} </div>", null);
    TokenHierarchy th = TokenHierarchy.get(doc);
    TokenSequence<XhtmlElTokenId> ts = th.tokenSequence(XhtmlElTokenId.language());
    
    assertToken("<div> ", XhtmlElTokenId.HTML, ts);
    assertToken("#{\"te\\\"xt}text\"}", XhtmlElTokenId.EL, ts);
    assertToken(" </div>\n", XhtmlElTokenId.HTML, ts);
    
}
 
Example 10
Source File: FunctionCompletionItem.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void defaultAction(JTextComponent component) {
    BaseDocument doc = (BaseDocument) component.getDocument();
    try {
        doc.insertString(carretOffset, text.substring(correction) + "()", null);
        component.setCaretPosition(component.getCaretPosition() - 1);

    } catch (BadLocationException ex) {
        // shouldn't happen
    }
    //This statement will close the code completion box:
    Completion.get().hideAll();

}
 
Example 11
Source File: InsertSemicolonAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionEvent evt, final JTextComponent target) {
    if (target.isEditable() && target.isEnabled()) {
        final BaseDocument doc = (BaseDocument) target.getDocument();
        final Indent indenter = Indent.get(doc);
        final class R implements Runnable {

            @Override
            public void run() {
                try {
                    Caret caret = target.getCaret();
                    int caretPosition = caret.getDot();
                    int eolOffset = Utilities.getRowEnd(target, caretPosition);
                    doc.insertString(eolOffset, SEMICOLON, null);
                    newLineProcessor.processNewLine(eolOffset, caret, indenter);
                } catch (BadLocationException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
        indenter.lock();
        try {
            doc.runAtomicAsUser(new R());
        } finally {
            indenter.unlock();
        }
    }
}
 
Example 12
Source File: JsTypedTextInterceptor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Check for various conditions and possibly add a pairing bracket
 * to the already inserted.
 * @param doc the document
 * @param dotPos position of the opening bracket (already in the doc)
 * @param caret caret
 * @param bracket the bracket that was inserted
 */
private void completeOpeningBracket(BaseDocument doc, int dotPos, Caret caret, char bracket)
    throws BadLocationException {
    if (isCompletablePosition(doc, dotPos + 1)) {
        String matchingBracket = "" + matching(bracket);
        doc.insertString(dotPos + 1, matchingBracket, null);
        caret.setDot(dotPos + 1);
    }
}
 
Example 13
Source File: DTDFormatter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Inserts new line at given position and indents the new line with
    * spaces.
    *
    * @param doc the document to work on
    * @param offset the offset of a character on the line
    * @return new offset to place cursor to
    */
    public int indentNewLine (Document doc, int offset) {
//        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n+ XMLFormatter::indentNewLine: doc = " + doc); // NOI18N
//        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+             ::indentNewLine: offset = " + offset); // NOI18N

        if (doc instanceof BaseDocument) {
            BaseDocument bdoc = (BaseDocument)doc;

            bdoc.atomicLock();
            try {
                bdoc.insertString (offset, "\n", null); // NOI18N
                offset++;

                int fullLine = Utilities.getFirstNonWhiteBwd (bdoc, offset - 1);
                int indent = Utilities.getRowIndent (bdoc, fullLine);
                
//                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+             ::indentNewLine: fullLine = " + fullLine); // NOI18N
//                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+             ::indentNewLine: indent   = " + indent); // NOI18N
//
//                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+             ::indentNewLine: offset   = " + offset); // NOI18N
//                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+             ::indentNewLine: sb       = '" + sb.toString() + "'"); // NOI18N

                String indentation = getIndentString(bdoc, indent);
                bdoc.insertString (offset, indentation, null);
                offset += indentation.length();

//                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+             ::indentNewLine: offset = " + offset); // NOI18N
            } catch (BadLocationException e) {
                if (Boolean.getBoolean ("netbeans.debug.exceptions")) { // NOI18N
                    e.printStackTrace();
                }
            } finally {
                bdoc.atomicUnlock();
            }

            return offset;
        }

        return super.indentNewLine (doc, offset);
    }
 
Example 14
Source File: PHPNewLineIndenterTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void testIndentInFile(String file, IndentPrefs indentPrefs, int initialIndent) throws Exception {
    FileObject fo = getTestFile(file);
    assertNotNull(fo);
    String source = readFile(fo);

    int sourcePos = source.indexOf('^');
    assertNotNull(sourcePos);
    String sourceWithoutMarker = source.substring(0, sourcePos) + source.substring(sourcePos+1);
    Formatter formatter = getFormatter(indentPrefs);

    JEditorPane ta = getPane(sourceWithoutMarker);
    Caret caret = ta.getCaret();
    caret.setDot(sourcePos);
    BaseDocument doc = (BaseDocument) ta.getDocument();
    if (formatter != null) {
        configureIndenters(doc, formatter, true);
    }

    setupDocumentIndentation(doc, indentPrefs);


    Map<String, Object> options = new HashMap<String, Object>(FmtOptions.getDefaults());
    options.put(FmtOptions.INITIAL_INDENT, initialIndent);
    if (indentPrefs != null) {
        options.put(FmtOptions.INDENT_SIZE, indentPrefs.getIndentation());
    }
    Preferences prefs = CodeStylePreferences.get(doc).getPreferences();
    for (String option : options.keySet()) {
        Object value = options.get(option);
        if (value instanceof Integer) {
            prefs.putInt(option, ((Integer) value).intValue());
        } else if (value instanceof String) {
            prefs.put(option, (String) value);
        } else if (value instanceof Boolean) {
            prefs.put(option, ((Boolean) value).toString());
        } else if (value instanceof CodeStyle.BracePlacement) {
            prefs.put(option, ((CodeStyle.BracePlacement) value).name());
        } else if (value instanceof CodeStyle.WrapStyle) {
            prefs.put(option, ((CodeStyle.WrapStyle) value).name());
        }
    }

    runKitAction(ta, DefaultEditorKit.insertBreakAction, "\n");

    doc.getText(0, doc.getLength());
    doc.insertString(caret.getDot(), "^", null);

    String target = doc.getText(0, doc.getLength());
    assertDescriptionMatches(file, target, false, ".indented");
}
 
Example 15
Source File: ExtKit.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled()) {
            target.getToolkit().beep();
            return;
        }

        BaseDocument doc = (BaseDocument)target.getDocument();
        int dotPos = target.getCaret().getDot();
        try {
            // look for identifier around caret
            int[] block = org.netbeans.editor.Utilities.getIdentifierBlock(doc, dotPos);

            // If there is no identifier around, warn user
            if (block == null) {
                target.getToolkit().beep();
                return;
            }

            // Get the identifier to operate on
            CharSequence identifier = DocumentUtilities.getText(doc, block[0], block[1] - block[0]);

            // Handle the case we already have the work done - e.g. if we got called over 'getValue'
            if (CharSequenceUtilities.startsWith(identifier, prefix) && 
                    Character.isUpperCase(identifier.charAt(prefix.length()))) return;

            // Handle the case we have other type of known xEr: eg isRunning -> getRunning
            for (int i=0; i<prefixGroup.length; i++) {
                String actPref = prefixGroup[i];
                if (CharSequenceUtilities.startsWith(identifier, actPref)
                        && identifier.length() > actPref.length()
                        && Character.isUpperCase(identifier.charAt(actPref.length()))
                   ) {
                    doc.remove(block[0], actPref.length());
                    doc.insertString(block[0], prefix, null);
                    return;
                }
            }

            // Upcase the first letter
            Utilities.changeCase(doc, block[0], 1, Utilities.CASE_UPPER);
            // Prepend the prefix before it
            doc.insertString(block[0], prefix, null);
        } catch (BadLocationException e) {
            target.getToolkit().beep();
        }
    }
}
 
Example 16
Source File: JPACompletionItem.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public boolean substituteText(JTextComponent c, int offset, int len, boolean shifted) {
    BaseDocument doc = (BaseDocument) c.getDocument();
    String text = getSubstitutionText();

    if (text != null) {
        if (toAdd != null && !toAdd.equals("\n")) // NOI18N
        {
            text += toAdd;
        }
        // Update the text
        doc.atomicLock();
        try {
            String textToReplace = doc.getText(offset, len);
            if (text.equals(textToReplace)) {
                return false;
            }

            if(!shifted) {//we are not in part of literal completion
                //dirty hack for @Table(name=CUS|
                if (!text.startsWith("\"")) {
                    text = quoteText(text);
                }

                //check if there is already an end quote
                char ch = doc.getText(offset + len, 1).charAt(0);
                if (ch == '"') {
                    //remove also this end quote since the inserted value is always quoted
                    len++;
                }
            }

            doc.remove(offset, len);
            doc.insertString(offset, text, null);
        } catch (BadLocationException e) {
            // Can't update
        } finally {
            doc.atomicUnlock();
        }
        return true;

    } else {
        return false;
    }
}
 
Example 17
Source File: UndoRedoTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testIssue83963() throws Exception {
    SchemaModel model = Util.loadSchemaModel("resources/undoredo.xsd");
    BaseDocument doc = (BaseDocument) model.getModelSource().
            getLookup().lookup(BaseDocument.class);
    Schema s = model.getSchema();
    TestComponentListener listener = new TestComponentListener();
    model.addComponentListener(listener);
    UndoManager ur = new UndoManager();
    model.addUndoableEditListener(ur);
    
    String original = doc.getText(0, doc.getLength());
    //System.out.println("doc before add ComplexType"+doc.getText(0, doc.getLength()));
    GlobalComplexType gct = model.getFactory().createGlobalComplexType();
    model.startTransaction();
    s.addComplexType(gct);
    model.endTransaction();
    model.removeUndoableEditListener(ur);
    doc.addUndoableEditListener(ur);
    
    //System.out.println("doc after add ComplexType"+doc.getText(0, doc.getLength()));
    
    String stStr = "   <xsd:simpleType name=\"lend\">\n     <xsd:list>\n       <xsd:simpleType>\n         <xsd:restriction base=\"xsd:string\"/>\n       </xsd:simpleType>\n     </xsd:list>\n   </xsd:simpleType>";
    
    String afterInsert = doc.getText(0, doc.getLength());
    //System.out.println("doc after insert simpleType"+doc.getText(290, 10));
    // position was changing which is weird but doesn't matter for undo-redo testing
    int schemaTagPosition = afterInsert.length() - 10;
    doc.insertString(schemaTagPosition, "\n", null);
    model.sync();
    doc.insertString(schemaTagPosition + 1, stStr, null);
    model.sync();
    
    //System.out.println("doc after insert simpleType"+doc.getText(0, doc.getLength()));
    ur.undo();
    //System.out.println("doc after first undo"+doc.getText(0, doc.getLength()));
    ur.undo();
    assertEquals(afterInsert,doc.getText(0, doc.getLength()));
    //System.out.println("doc after second undo"+doc.getText(0, doc.getLength()));
    ur.undo();
    //System.out.println("doc after third undo"+doc.getText(0, doc.getLength()));
    assertEquals(original, doc.getText(0, doc.getLength()));
    
    ur.redo();
    assertEquals(afterInsert,doc.getText(0, doc.getLength()));
    //System.out.println("doc after first redo"+doc.getText(0, doc.getLength()));
    ur.redo();
    //System.out.println("doc after second redo"+doc.getText(0, doc.getLength()));
    ur.redo();
    //System.out.println("doc after third redo"+doc.getText(0, doc.getLength()));
}
 
Example 18
Source File: AnnotationViewDataImplTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testGetMainMarkForBlock() throws /*BadLocation*/Exception {
    JEditorPane editor = new JEditorPane();
    
    editor.setEditorKit(BaseKit.getKit(PlainKit.class));
    
    BaseDocument bd = (BaseDocument) editor.getDocument();

    bd.insertString(0, "\n\n\n\n\n\n\n\n\n\n", null);
    
    TestMark mark1 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {2, 2});
    TestMark mark2 = new TestMark(Status.STATUS_OK, null, null, new int[] {2, 2});
    TestMark mark3 = new TestMark(Status.STATUS_WARNING, null, null, new int[] {2, 4});
    
    AnnotationDesc test1 = new TestAnnotationDesc(bd, bd.createPosition(7), "test-annotation-1");
    AnnotationDesc test2 = new TestAnnotationDesc(bd, bd.createPosition(8), "test-annotation-2");
    AnnotationDesc test3 = new TestAnnotationDesc(bd, bd.createPosition(8), "test-annotation-8");
    AnnotationDesc test4 = new TestAnnotationDesc(bd, bd.createPosition(9), "test-annotation-8");
    
    bd.getAnnotations().addAnnotation(test1);
    bd.getAnnotations().addAnnotation(test2);
    bd.getAnnotations().addAnnotation(test3);
    bd.getAnnotations().addAnnotation(test4);
    
    List marks1 = Arrays.asList(new Mark[]{mark1, mark2, mark3});
    List marks2 = Arrays.asList(new Mark[]{mark1, mark3});
    List marks3 = Arrays.asList(new Mark[]{mark2, mark3});
    List marks4 = Arrays.asList(new Mark[]{mark1, mark2});
    List marks5 = Arrays.asList(new Mark[]{mark3});
    
    TestMarkProvider provider = new TestMarkProvider(marks1, UpToDateStatus.UP_TO_DATE_OK);
    TestMarkProviderCreator creator = TestMarkProviderCreator.getDefault();
    
    creator.setProvider(provider);
    
    AnnotationView aView = new AnnotationView(editor);
    AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView.getData();
    
    assertEquals(mark1, data.getMainMarkForBlock(2, 2));
    assertEquals(mark1, data.getMainMarkForBlock(2, 3));
    assertEquals(mark1, data.getMainMarkForBlock(2, 4));
    assertEquals(mark1, data.getMainMarkForBlock(2, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 3));
    assertEquals(null, data.getMainMarkForBlock(6, 6));
    assertEquals(Status.STATUS_ERROR, data.getMainMarkForBlock(7, 7).getStatus());
    assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8, 8).getStatus());
    bd.getAnnotations().activateNextAnnotation(8);
    assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8, 8).getStatus());
    bd.getAnnotations().activateNextAnnotation(8);
    assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8, 8).getStatus());
    assertNull(data.getMainMarkForBlock(9, 9));
    assertEquals(Status.STATUS_ERROR, data.getMainMarkForBlock(7, 9).getStatus());
    
    provider.setMarks(marks2);
    
    bd.getAnnotations().removeAnnotation(test3);
    
    assertEquals(mark1, data.getMainMarkForBlock(2, 2));
    assertEquals(mark1, data.getMainMarkForBlock(2, 3));
    assertEquals(mark1, data.getMainMarkForBlock(2, 4));
    assertEquals(mark1, data.getMainMarkForBlock(2, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 3));
    assertEquals(null, data.getMainMarkForBlock(6, 6));

    assertEquals(Status.STATUS_ERROR, data.getMainMarkForBlock(7, 7).getStatus());
    assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8, 8).getStatus());
    assertNull(data.getMainMarkForBlock(9, 9));
    assertEquals(Status.STATUS_ERROR, data.getMainMarkForBlock(7, 9).getStatus());
    
    provider.setMarks(marks3);
    
    assertEquals(mark3, data.getMainMarkForBlock(2, 2));
    assertEquals(mark3, data.getMainMarkForBlock(2, 3));
    assertEquals(mark3, data.getMainMarkForBlock(2, 4));
    assertEquals(mark3, data.getMainMarkForBlock(2, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 3));
    assertEquals(null, data.getMainMarkForBlock(6, 6));
    
    provider.setMarks(marks4);
    
    assertEquals(mark1, data.getMainMarkForBlock(2, 2));
    assertEquals(mark1, data.getMainMarkForBlock(2, 3));
    assertEquals(mark1, data.getMainMarkForBlock(2, 4));
    assertEquals(mark1, data.getMainMarkForBlock(2, 6));
    assertEquals(null, data.getMainMarkForBlock(3, 6));
    assertEquals(null, data.getMainMarkForBlock(3, 3));
    assertEquals(null, data.getMainMarkForBlock(6, 6));
    
    provider.setMarks(marks5);
    
    assertEquals(mark3, data.getMainMarkForBlock(2, 2));
    assertEquals(mark3, data.getMainMarkForBlock(2, 3));
    assertEquals(mark3, data.getMainMarkForBlock(2, 4));
    assertEquals(mark3, data.getMainMarkForBlock(2, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 3));
    assertEquals(null, data.getMainMarkForBlock(6, 6));
}
 
Example 19
Source File: PhpTypedTextInterceptor.java    From netbeans with Apache License 2.0 3 votes vote down vote up
/**
 * Check for various conditions and possibly add a pairing bracket to the
 * already inserted.
 *
 * @param doc the document
 * @param dotPos position of the opening bracket (already in the doc)
 * @param caret caret
 * @param bracket the bracket that was inserted
 */
private void completeOpeningBracket(BaseDocument doc, int dotPos, Caret caret, char bracket) throws BadLocationException {
    if (!bracketCompleted && isCompletablePosition(doc, dotPos + 1)) {
        String matchingBracket = "" + matching(bracket);
        doc.insertString(dotPos + 1, matchingBracket, null);
        caret.setDot(dotPos + 1);
    }
}
 
Example 20
Source File: AnnotationViewTest.java    From netbeans with Apache License 2.0 2 votes vote down vote up
private static void performTest(final Action action) throws Exception {
    JFrame f = new JFrame();
    JEditorPane editor = new JEditorPane();
    
    editor.setEditorKit(BaseKit.getKit(PlainKit.class));
    
    TestMark mark1 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {6, 6});
    TestMark mark2 = new TestMark(Status.STATUS_OK, null, null, new int[] {6, 6});
    
    List marks = Arrays.asList(new Mark[]{mark1, mark2});
    
    TestMarkProvider provider = new TestMarkProvider(Collections.EMPTY_LIST, UpToDateStatus.UP_TO_DATE_OK);
    TestMarkProviderCreator creator = TestMarkProviderCreator.getDefault();
    
    creator.setProvider(provider);
    
    AnnotationView aView = new AnnotationView(editor);
    
    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(new JScrollPane(editor), BorderLayout.CENTER);
    f.getContentPane().add(aView, BorderLayout.EAST);
    
    f.setSize(500, 500);
    
    f.setVisible(true);

    String[] contents = getContents();
    
    for (int index = 0; index < contents.length; index++) {
        BaseDocument bd = (BaseDocument) editor.getDocument();
        
        bd.insertString(0, contents[index], null);
        
        provider.setMarks(marks);
        
        action.test(aView, bd);
        
        provider.setMarks(Collections.EMPTY_LIST);
        
        bd.remove(0, bd.getLength());
    }
    
    f.setVisible(false);
}