Java Code Examples for org.netbeans.editor.BaseDocument#atomicLock()
The following examples show how to use
org.netbeans.editor.BaseDocument#atomicLock() .
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: JspPaletteUtilities.java From netbeans with Apache License 2.0 | 5 votes |
/**************************************************************************/ private static void insertTagLibRef(JTextComponent target, String prefix, String uri) { Document doc = target.getDocument(); if (doc != null && doc instanceof BaseDocument) { BaseDocument baseDoc = (BaseDocument)doc; baseDoc.atomicLock(); try { int pos = 0; // FIXME: compute better where to insert tag lib definition? String definition = "<%@taglib prefix=\""+prefix+"\" uri=\""+uri+"\"%>\n"; //NOI18N //test for .jspx. FIXME: find better way to detect xml syntax?. FileObject fobj = getFileObject(target); if (fobj != null && "jspx".equals(fobj.getExt())) { int baseDocLength = baseDoc.getLength(); String text = baseDoc.getText(0, baseDocLength); String jspRootBegin = "<jsp:root "; //NOI18N int jspRootIndex = text.indexOf(jspRootBegin); if (jspRootIndex != -1) { pos = jspRootIndex + jspRootBegin.length(); definition = "xmlns:" + prefix + "=\"" + uri + "\" "; //NOI18N } } doc.insertString(pos, definition, null); } catch (BadLocationException e) { Exceptions.printStackTrace(e); } finally { baseDoc.atomicUnlock(); } } }
Example 5
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 6
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 7
Source File: DTDFormatter.java From netbeans with Apache License 2.0 | 5 votes |
/** 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 8
Source File: DocumentUndoTest.java From netbeans with Apache License 2.0 | 5 votes |
private void insertByAtomicChars(int offset, String text) throws Exception { BaseDocument doc = getDocument(); for (int i = 0; i < text.length(); i++) { doc.atomicLock(); try { doc.insertString(offset + i, text.substring(i, i + 1), null); } finally { doc.atomicUnlock(); } } }
Example 9
Source File: JPACompletionItem.java From netbeans with Apache License 2.0 | 4 votes |
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 10
Source File: JPACompletionItem.java From netbeans with Apache License 2.0 | 4 votes |
@Override public boolean substituteText(JTextComponent c, int offset, int len, boolean shift) { 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 (!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++; } //check if there is already an start quote ch = doc.getText(offset -1, 1).charAt(0); if (ch == '"') { //remove also this end quote since the inserted value is always quoted len++; offset--; } doc.remove(offset, len); doc.insertString(offset, text, null); } catch (BadLocationException e) { // Can't update } finally { doc.atomicUnlock(); } return true; } else { return false; } }
Example 11
Source File: JPACompletionItem.java From netbeans with Apache License 2.0 | 4 votes |
@Override public boolean substituteText(JTextComponent c, int offset, int len, boolean shift) { len = initialvalue.length() + (getQuoted() ? 2 : 0);//we replace entire query with new 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; } //dirty hack for @Table(name=CUS| if (toQuote && !text.startsWith("\"")) { text = quoteText(text); } //check if there is already an end quote char ch = doc.getText(offset + len, 1).charAt(0); if (toQuote && ch == '"') { //remove also this end quote since the inserted value is always quoted len++; } doc.remove(offset, getCutomPosition() - ((text.length() - (toQuote ? 2 : 0)) - initialvalue.length())); doc.insertString(offset, text.substring(0, getCutomPosition()), null); } catch (BadLocationException e) { // Can't update } finally { doc.atomicUnlock(); } return true; } else { return false; } }
Example 12
Source File: JspPaletteUtilities.java From netbeans with Apache License 2.0 | 4 votes |
public static void insert(String s, JTextComponent target, boolean reformat) throws BadLocationException { Document _doc = target.getDocument(); if (_doc == null || !(_doc instanceof BaseDocument)) { return; } //check whether we are not in a scriptlet // JspSyntaxSupport sup = (JspSyntaxSupport)(doc.getSyntaxSupport().get(JspSyntaxSupport.class)); // int start = target.getCaret().getDot(); // TokenItem token = sup.getTokenChain(start, start + 1); // if (token != null && token.getTokenContextPath().contains(JavaTokenContext.contextPath)) // we are in a scriptlet // return false; if (s == null) { s = ""; //NOI18N } BaseDocument doc = (BaseDocument) _doc; Indent indent = Indent.get(doc); indent.lock(); try { doc.atomicLock(); try { int cursor_offset = s.indexOf(CARET); if (cursor_offset != -1) { s = s.replace(CARET, ""); //NOI18N } int start = insert(s, target, _doc); if (cursor_offset != -1) { target.setCaretPosition(start + cursor_offset); } if (reformat && start >= 0 && _doc instanceof BaseDocument) { // format the inserted text int end = start + s.length(); indent.reindent(start, end); } } finally { doc.atomicUnlock(); } } finally { indent.unlock(); } }
Example 13
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 14
Source File: BeansCompletionItem.java From netbeans with Apache License 2.0 | 4 votes |
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; } }