Java Code Examples for org.netbeans.editor.BaseDocument#createPosition()
The following examples show how to use
org.netbeans.editor.BaseDocument#createPosition() .
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 |
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: WSCompletionItem.java From netbeans with Apache License 2.0 | 6 votes |
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 3
Source File: BeansCompletionItem.java From netbeans with Apache License 2.0 | 6 votes |
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 4
Source File: AbstractCompletionItem.java From netbeans with Apache License 2.0 | 6 votes |
protected void doSubstituteText(JTextComponent c, BaseDocument d, String text) throws BadLocationException { int offset = getSubstOffset(); String old = d.getText(offset, length); int nextOffset = ctx.getNextCaretPos(); Position p = null; if (nextOffset >= 0) { p = d.createPosition(nextOffset); } if (text.equals(old)) { if (p != null) { c.setCaretPosition(p.getOffset()); } else { c.setCaretPosition(offset + getCaretShift(d)); } } else { d.remove(offset, length); d.insertString(offset, text, null); if (p != null) { c.setCaretPosition(p.getOffset()); } else { c.setCaretPosition(offset + getCaretShift(d)); } } }
Example 5
Source File: CssTypedBreakInterceptor.java From netbeans with Apache License 2.0 | 6 votes |
@Override public void insert(MutableContext context) throws BadLocationException { int offset = context.getBreakInsertOffset(); BaseDocument doc = (BaseDocument) context.getDocument(); if (offset > 0 && offset < doc.getLength()) { //check corners String text = doc.getText(offset - 1, 2); //get char before and after if (TWO_CURLY_BRACES_IMAGE.equals(text)) { //NOI18N //context.setText("\n\n", 1, 1, 0, 2); //reformat workaround -- the preferred //won't work as the reformatter will not reformat the line with the closing tag int from = Utilities.getRowStart(doc, offset); int to = Utilities.getRowEnd(doc, offset); reformat = new Position[]{doc.createPosition(from), doc.createPosition(to)}; context.setText("\n\n", 1, 1); } } }
Example 6
Source File: JSFEditorUtilities.java From netbeans with Apache License 2.0 | 5 votes |
private static int writeString(BaseDocument doc, String text, int offset){ int formatLength = 0; Indent indenter = Indent.get(doc); Reformat formatter = Reformat.get(doc); indenter.lock(); formatter.lock(); try { doc.atomicLock(); try{ offset = indenter.indentNewLine(offset + 1); doc.insertString(offset, text, null ); Position endPos = doc.createPosition(offset + text.length() - 1); formatter.reformat(offset, endPos.getOffset()); formatLength = Math.max(0, endPos.getOffset() - offset); } catch(BadLocationException ex){ Exceptions.printStackTrace(ex); } finally { doc.atomicUnlock(); } } finally { formatter.unlock(); indenter.unlock(); } return offset + formatLength + 1; }
Example 7
Source File: StrutsEditorUtilities.java From netbeans with Apache License 2.0 | 5 votes |
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 8
Source File: AbstractCompletionItem.java From netbeans with Apache License 2.0 | 5 votes |
protected void substituteText(final JTextComponent c, final String text) { final Document d = c.getDocument(); BaseDocument bd = (BaseDocument)d; bd.extWriteLock(); try { substPos = bd.createPosition(substOffset); doSubstituteText(c, bd, text); } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } finally { bd.extWriteUnlock(); } }
Example 9
Source File: CssTypedTextInterceptor.java From netbeans with Apache License 2.0 | 5 votes |
private void reindentLater(final BaseDocument doc, int start, int end) throws BadLocationException { final Position from = doc.createPosition(Utilities.getRowStart(doc, start)); final Position to = doc.createPosition(Utilities.getRowEnd(doc, end)); Runnable rn = new Runnable() { @Override public void run() { final Indent indent = Indent.get(doc); indent.lock(); try { doc.runAtomic(new Runnable() { @Override public void run() { try { indent.reindent(from.getOffset(), to.getOffset()); } catch (BadLocationException ex) { //ignore } } }); } finally { indent.unlock(); } } }; if (inTest) { rn.run(); } else { SwingUtilities.invokeLater(rn); } }
Example 10
Source File: ExtFormatter.java From netbeans with Apache License 2.0 | 5 votes |
/** Fix of #5620 - same method exists in Formatter (predecessor */ public @Override int reformat(BaseDocument doc, int startOffset, int endOffset) throws BadLocationException { try { javax.swing.text.Position pos = doc.createPosition(endOffset); reformat(doc, startOffset, endOffset, false); return pos.getOffset() - startOffset; } catch (IOException e) { e.printStackTrace(); return 0; } }
Example 11
Source File: PositionRange.java From netbeans with Apache License 2.0 | 4 votes |
public PositionRange(BaseDocument doc, int from, int to) throws BadLocationException { //the constructor will simply throw BLE when the embedded to source offset conversion fails (returns -1) this.from = doc.createPosition(from); this.to = doc.createPosition(to); }
Example 12
Source File: StrutsEditorUtilities.java From netbeans with Apache License 2.0 | 4 votes |
private static int writeElementIntoFather(BaseDocument doc, BaseBean bean, String father, String fatherName, String element) throws IOException{ int possition = -1; ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport(); TokenItem token = null; try { int offset = 0; // find the name as an attribute value do{ offset = doc.getText(0, doc.getLength()).indexOf("\""+fatherName+"\"", offset+1); //NOI18N token = sup.getTokenChain(offset, offset+1); if (token != null && token.getTokenID().getNumericID() == XML_ATTRIBUTE_VALUE) while ( token != null && token.getTokenID().getNumericID() != XML_ELEMENT) token = token.getPrevious(); } while (offset > 0 && token != null && !(token.getTokenID().getNumericID() == XML_ELEMENT && token.getImage().equals("<"+father))); //NOI18N if (token != null && token.getTokenID().getNumericID() == XML_ELEMENT && token.getImage().equals("<"+father)){ //NOI18N token = token.getNext(); // find the /> or > while (token != null && token.getTokenID().getNumericID() != XML_ELEMENT ) token = token.getNext(); if (token != null && token.getImage().equals("/>")){ //NOI18N StringBuffer text = new StringBuffer(); offset = token.getOffset(); text.append(">"); //NOI18N text.append(END_LINE); text.append(addNewLines(bean)); text.append(END_LINE); text.append("</"); text.append(father); text.append(">"); //NOI18N Reformat fmt = Reformat.get(doc); fmt.lock(); try { doc.atomicLock(); try{ doc.remove(offset, 2); doc.insertString(offset, text.toString(), null); Position endPos = doc.createPosition(offset + text.length() - 1); fmt.reformat(offset, endPos.getOffset()); offset += Math.max(0, endPos.getOffset() - offset); possition = offset; } finally{ doc.atomicUnlock(); } } finally { fmt.unlock(); } } if (token != null && token.getImage().equals(">")){ //NOI18N offset = -1; while (token != null && !(token.getTokenID().getNumericID() == XML_ELEMENT && token.getImage().equals("</"+father))){ //NOI18N while(token != null && !(token.getTokenID().getNumericID() == XML_ELEMENT && (token.getImage().equals("<"+element) //NOI18N || token.getImage().equals("</"+father)))) //NOI18N token = token.getNext(); if (token != null && token.getImage().equals("<"+element)){ //NOI18N while (token!= null && !(token.getTokenID().getNumericID() == XML_ELEMENT && (token.getImage().equals("/>") //NOI18N || token.getImage().equals("</"+element)))) //NOI18N token = token.getNext(); if (token != null && token.getImage().equals("</"+element)) //NOI18N while (token!= null && !(token.getTokenID().getNumericID() == XML_ELEMENT && token.getImage().equals(">"))) //NOI18N token = token.getNext(); if (token != null ) offset = token.getOffset() + token.getImage().length()-1; } if (token != null && token.getImage().equals("</"+father) && offset == -1){ //NOI18N while (token!= null && !(token.getTokenID().getNumericID() == XML_ELEMENT && (token.getImage().equals("/>") //NOI18N || token.getImage().equals(">")))) //NOI18N token = token.getPrevious(); offset = token.getOffset()+token.getImage().length()-1; } } if (offset > 0) possition = writeString(doc, addNewLines(bean), offset); } } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } return possition; }
Example 13
Source File: AnnotationViewDataImplTest.java From netbeans with Apache License 2.0 | 4 votes |
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 14
Source File: HtmlTypedBreakInterceptor.java From netbeans with Apache License 2.0 | 4 votes |
@Override public void insert(MutableContext context) throws BadLocationException { BaseDocument doc = (BaseDocument) context.getDocument(); int offset = context.getBreakInsertOffset(); TokenSequence<HTMLTokenId> ts = LexUtilities.getTokenSequence((BaseDocument)doc, offset, HTMLTokenId.language()); if (ts == null) { return; } ts.move(offset); String closingTagName = null; int end = -1; if (ts.moveNext() && ts.token().id() == HTMLTokenId.TAG_OPEN_SYMBOL && ts.token().text().toString().equals("</")) { if (ts.moveNext() && ts.token().id() == HTMLTokenId.TAG_CLOSE) { closingTagName = ts.token().text().toString(); end = ts.offset()+ts.token().text().length(); ts.movePrevious(); ts.movePrevious(); } } if (closingTagName == null) { return; } boolean foundOpening = false; if (ts.token().id() == HTMLTokenId.TAG_CLOSE_SYMBOL && ts.token().text().toString().equals(">")) { while (ts.movePrevious()) { if (ts.token().id() == HTMLTokenId.TAG_OPEN) { if (ts.token().text().toString().equals(closingTagName)) { foundOpening = true; } break; } } } if (foundOpening) { context.setText("\n\n", 1, 1); //reformat workaround -- the preferred //context.setText("\n\n", 1, 1, 0, 2); //won't work as the reformatter will not reformat the line with the closing tag int from = Utilities.getRowStart(doc, offset); int to = Utilities.getRowEnd(doc, offset); reformat = new Position[]{doc.createPosition(from), doc.createPosition(to)}; } }
Example 15
Source File: AnnotationViewDataImplTest.java From netbeans with Apache License 2.0 | 3 votes |
public void testComputeTotalStatus() throws Exception { JFrame f = new JFrame(); JEditorPane editor = new JEditorPane(); editor.setEditorKit(BaseKit.getKit(PlainKit.class)); AnnotationView aView = new AnnotationView(editor); AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView.getData(); 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); BaseDocument bd = (BaseDocument) editor.getDocument(); bd.insertString(0, "\n\n\n\n\n\n\n\n\n\n", null); Position start = bd.createPosition(Utilities.getRowStartFromLineOffset(bd, 2)); AnnotationDesc a1 = new AnnotationTestUtilities.TestAnnotationDesc1(bd, start); AnnotationDesc a2 = new AnnotationTestUtilities.TestAnnotationDesc2(bd, start); bd.getAnnotations().addAnnotation(a1); bd.getAnnotations().addAnnotation(a2); assertEquals(Status.STATUS_ERROR, data.computeTotalStatus()); bd.getAnnotations().activateNextAnnotation(2); assertEquals(Status.STATUS_ERROR, data.computeTotalStatus()); f.setVisible(false); }
Example 16
Source File: AnnotationViewDataImplTest.java From netbeans with Apache License 2.0 | 2 votes |
public void testMarkPriorities() throws 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); //test marks: TestMark mark1 = new TestMark(Status.STATUS_OK, null, null, new int[] {2, 2}, 99); TestMark mark2 = new TestMark(Status.STATUS_OK, null, null, new int[] {2, 2}, 10); TestMark mark3 = new TestMark(Status.STATUS_OK, null, null, new int[] {3, 4}, 5); TestMark mark4 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {2, 2}, 1000); TestMark mark5 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {2, 2}, 100); TestMark mark6 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {3, 4}, 50); List marks1 = Arrays.asList(new Mark[]{mark1, mark2, mark3}); List marks2 = Arrays.asList(new Mark[]{mark2, mark1, mark3}); List marks3 = Arrays.asList(new Mark[]{mark1, mark2, mark3, mark4, mark5, mark6}); 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(mark2, data.getMainMarkForBlock(2, 2)); assertEquals(mark3, data.getMainMarkForBlock(2, 3)); assertEquals(mark3, data.getMainMarkForBlock(3, 4)); assertEquals(null, data.getMainMarkForBlock(6, 6)); provider.setMarks(marks2); assertEquals(mark2, data.getMainMarkForBlock(2, 2)); assertEquals(mark3, data.getMainMarkForBlock(2, 3)); assertEquals(mark3, data.getMainMarkForBlock(3, 4)); assertEquals(null, data.getMainMarkForBlock(6, 6)); provider.setMarks(marks3); assertEquals(mark5, data.getMainMarkForBlock(2, 2)); assertEquals(mark6, data.getMainMarkForBlock(2, 3)); assertEquals(mark6, data.getMainMarkForBlock(3, 4)); assertEquals(null, data.getMainMarkForBlock(6, 6)); provider.setMarks(Collections.EMPTY_LIST); //test annotations: AnnotationDesc test1 = new TestAnnotationDesc(bd, bd.createPosition(2), "test-annotation-priority-1"); AnnotationDesc test2 = new TestAnnotationDesc(bd, bd.createPosition(2), "test-annotation-priority-2"); AnnotationDesc test3 = new TestAnnotationDesc(bd, bd.createPosition(2), "test-annotation-priority-3"); AnnotationDesc test4 = new TestAnnotationDesc(bd, bd.createPosition(2), "test-annotation-priority-4"); bd.getAnnotations().addAnnotation(test1); bd.getAnnotations().addAnnotation(test2); assertEquals(test2, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc()); bd.getAnnotations().activateNextAnnotation(2); assertEquals(test2, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc()); bd.getAnnotations().activateNextAnnotation(2); bd.getAnnotations().addAnnotation(test3); bd.getAnnotations().addAnnotation(test4); assertEquals(test4, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc()); bd.getAnnotations().activateNextAnnotation(2); assertEquals(test4, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc()); bd.getAnnotations().activateNextAnnotation(2); assertEquals(test4, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc()); //test interaction between annotations and marks: List marks4 = Arrays.asList(new Mark[]{mark1}); provider.setMarks(marks4); bd.getAnnotations().removeAnnotation(test2); bd.getAnnotations().removeAnnotation(test3); bd.getAnnotations().removeAnnotation(test4); assertEquals(mark1, data.getMainMarkForBlock(2, 2)); bd.getAnnotations().addAnnotation(test2); assertEquals(test2, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc()); }