Java Code Examples for javax.swing.text.StyledDocument#getParagraphElement()
The following examples show how to use
javax.swing.text.StyledDocument#getParagraphElement() .
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: PUCompletionProvider.java From netbeans with Apache License 2.0 | 6 votes |
static int getRowFirstNonWhite(StyledDocument doc, int offset) throws BadLocationException { Element lineElement = doc.getParagraphElement(offset); int start = lineElement.getStartOffset(); while (start + 1 < lineElement.getEndOffset()) { try { if (doc.getText(start, 1).charAt(0) != ' ') { break; } } catch (BadLocationException ex) { throw (BadLocationException) new BadLocationException( "calling getText(" + start + ", " + (start + 1) + ") on doc of length: " + doc.getLength(), start).initCause(ex); } start++; } return start; }
Example 2
Source File: GradleCliCompletionProvider.java From netbeans with Apache License 2.0 | 6 votes |
static int getRowFirstNonWhite(StyledDocument doc, int offset) throws BadLocationException { Element lineElement = doc.getParagraphElement(offset); int start = lineElement.getStartOffset(); while (start + 1 < lineElement.getEndOffset()) { try { if (doc.getText(start, 1).charAt(0) != ' ') { break; } } catch (BadLocationException ex) { throw (BadLocationException) new BadLocationException( "calling getText(" + start + ", " + (start + 1) + ") on doc of length: " + doc.getLength(), start ).initCause(ex); } start++; } return start; }
Example 3
Source File: BeansCompletionProvider.java From netbeans with Apache License 2.0 | 6 votes |
static int getRowFirstNonWhite(StyledDocument doc, int offset) throws BadLocationException { Element lineElement = doc.getParagraphElement(offset); int start = lineElement.getStartOffset(); while (start + 1 < lineElement.getEndOffset()) { try { if (doc.getText(start, 1).charAt(0) != ' ') { break; } } catch (BadLocationException ex) { throw (BadLocationException) new BadLocationException( "calling getText(" + start + ", " + (start + 1) + ") on doc of length: " + doc.getLength(), start).initCause(ex); } start++; } return start; }
Example 4
Source File: AnnotationBar.java From netbeans with Apache License 2.0 | 6 votes |
/** * Locates AnnotateLine associated with given line. The * line is translated to Element that is used as map lookup key. * The map is initially filled up with Elements sampled on * annotate() method. * * <p>Key trick is that Element's identity is maintained * until line removal (and is restored on undo). * * @param line * @return found AnnotateLine or <code>null</code> */ private AnnotateLine getAnnotateLine(int line) { StyledDocument sd = (StyledDocument) doc; int lineOffset = NbDocument.findLineOffset(sd, line); Element element = sd.getParagraphElement(lineOffset); AnnotateLine al = elementAnnotations.get(element); if (al != null) { int startOffset = element.getStartOffset(); int endOffset = element.getEndOffset(); try { int len = endOffset - startOffset; String text = doc.getText(startOffset, len -1); String content = al.getContent(); if (text.equals(content)) { return al; } } catch (BadLocationException e) { Mercurial.LOG.log(Level.INFO, "HG.AB: can not locate line annotation."); // NOI18N } } return null; }
Example 5
Source File: FormI18nSupport.java From netbeans with Apache License 2.0 | 5 votes |
/** Implements superclass abstract method. */ protected void setHardCodedString(HardCodedString hcString, StyledDocument document) { getStringText().setText(hcString == null ? "" : hcString.getText()); // NOI18N int pos; String hardLine; if (hcString.getStartPosition() == null) { hardLine = ""; // NOI18N } else { pos = hcString.getStartPosition().getOffset(); try { Element paragraph = document.getParagraphElement(pos); hardLine = document.getText(paragraph.getStartOffset(), paragraph.getEndOffset()-paragraph.getStartOffset()).trim(); } catch (BadLocationException ble) { hardLine = ""; // NOI18N } } getFoundInText().setText(hardLine); if(hcString instanceof FormHardCodedString) { getComponentText().setText( ((FormHardCodedString)hcString).getValidProperty().getRADComponentName()); getPropertyText().setText( ((FormHardCodedString)hcString).getNodeProperty().getName()); } else { remove(getComponentLabel()); remove(getComponentText()); remove(getPropertyLabel()); remove(getPropertyText()); } }
Example 6
Source File: JavaI18nSupport.java From netbeans with Apache License 2.0 | 5 votes |
/** Implements superclass abstract method. */ protected void setHardCodedString(HardCodedString hcString, StyledDocument document) { getStringText().setText(hcString == null ? "" //NOI18N : hcString.getText()); int pos; String hardLine; if (hcString.getStartPosition() == null) { hardLine = ""; //NOI18N } else { pos = hcString.getStartPosition().getOffset(); try { Element paragraph = document.getParagraphElement(pos); hardLine = document.getText(paragraph.getStartOffset(), paragraph.getEndOffset() - paragraph.getStartOffset()) .trim(); } catch (BadLocationException ble) { hardLine = ""; // NOI18N } } getFoundInText().setText(hardLine); remove(getComponentLabel()); remove(getComponentText()); remove(getPropertyLabel()); remove(getPropertyText()); }
Example 7
Source File: CfgPropsCompletionQuery.java From nb-springboot with Apache License 2.0 | 5 votes |
@Override protected void query(CompletionResultSet completionResultSet, Document document, int caretOffset) { logger.finer("Starting completion"); final StyledDocument styDoc = (StyledDocument) document; Element lineElement = styDoc.getParagraphElement(caretOffset); int lineStartOffset = lineElement.getStartOffset(); try { String lineToCaret = styDoc.getText(lineStartOffset, caretOffset - lineStartOffset); if (!lineToCaret.contains("#")) { String[] parts = lineToCaret.split("="); //property name extraction from part before = Matcher matcher = PATTERN_PROP_NAME.matcher(parts[0]); String propPrefix = null; int propPrefixOffset = 0; while (matcher.find()) { propPrefix = matcher.group(); propPrefixOffset = matcher.start(); } // check which kind of completion final int equalSignOffset = lineToCaret.indexOf('='); if (parts.length > 1) { //value completion String valPrefix = parts[1].trim(); completePropValue(completionResultSet, propPrefix, valPrefix, lineStartOffset + lineToCaret.indexOf(valPrefix, equalSignOffset), caretOffset); } else if (equalSignOffset >= 0) { //value completion with empty filter completePropValue(completionResultSet, propPrefix, "", lineStartOffset + equalSignOffset + 1, caretOffset); } else { // property completion completePropName(completionResultSet, propPrefix, lineStartOffset + propPrefixOffset, caretOffset); } } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } completionResultSet.finish(); }
Example 8
Source File: CfgPropsDocAndTooltipQuery.java From nb-springboot with Apache License 2.0 | 5 votes |
@Override protected void query(CompletionResultSet completionResultSet, Document document, int caretOffset) { final StyledDocument styDoc = (StyledDocument) document; Element lineElement = styDoc.getParagraphElement(caretOffset); int lineStartOffset = lineElement.getStartOffset(); int lineEndOffset = lineElement.getEndOffset(); try { String line = styDoc.getText(lineStartOffset, lineEndOffset - lineStartOffset); if (line.endsWith("\n")) { line = line.substring(0, line.length() - 1); } if (showTooltip) { logger.log(FINER, "Tooltip on: {0}", line); } else { logger.log(FINER, "Documentation on: {0}", line); } if (!line.contains("#")) { //property name extraction from line Matcher matcher = PATTERN_PROP_NAME.matcher(line); if (matcher.matches()) { String propPrefix = matcher.group(1); ConfigurationMetadataProperty propMeta = sbs.getPropertyMetadata(propPrefix); if (propMeta != null) { if (showTooltip) { final JToolTip toolTip = new JToolTip(); toolTip.setTipText(Utils.shortenJavaType(propMeta.getType())); completionResultSet.setToolTip(toolTip); } else { completionResultSet.setDocumentation(new CfgPropCompletionDocumentation(propMeta)); } } } } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } completionResultSet.finish(); }
Example 9
Source File: KeyCompletionItem.java From nb-springboot with Apache License 2.0 | 5 votes |
@Override public void defaultAction(JTextComponent jtc) { logger.log(Level.FINER, "Accepted key value hint: {0}", hint.toString()); try { StyledDocument doc = (StyledDocument) jtc.getDocument(); // calculate the amount of chars to remove (by default from dot up to caret position) int lenToRemove = caretOffset - dotOffset; int equalSignIndex = -1; int colonIndex = -1; if (overwrite) { // NOTE: the editor removes by itself the word at caret when ctrl + enter is pressed // the document state here is different from when the completion was invoked thus we have to // find again the offset of the equal sign in the line Element lineElement = doc.getParagraphElement(caretOffset); String line = doc.getText(lineElement.getStartOffset(), lineElement.getEndOffset() - lineElement.getStartOffset()); equalSignIndex = line.indexOf('='); colonIndex = line.indexOf(':'); if (equalSignIndex >= 0) { // from dot to equal sign lenToRemove = lineElement.getStartOffset() + equalSignIndex - dotOffset; } else if (colonIndex >= 0) { // from dot to colon lenToRemove = lineElement.getStartOffset() + colonIndex - dotOffset; } } // remove characters from dot then insert new text doc.remove(dotOffset, lenToRemove); if (equalSignIndex < 0 && colonIndex < 0) { logger.log(Level.FINER, "Adding equal sign and continuing completion"); doc.insertString(dotOffset, hint.getValue().toString().concat("="), null); } else { logger.log(Level.FINER, "Finish completion with no added chars"); doc.insertString(dotOffset, hint.getValue().toString(), null); Completion.get().hideAll(); } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } }
Example 10
Source File: TextEditorGUI.java From trygve with GNU General Public License v2.0 | 5 votes |
public void setBreakpointToEOLAt(int byteOffset, int lineNumber) { final StyledDocument doc = (StyledDocument)editPane.getDocument(); final Element paragraphElement = doc.getParagraphElement(byteOffset); if (paragraphElement.getClass() == BranchElement.class) { final SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setBackground(sas, Color.cyan); // Look for ending delimiter int length = 1; try { for (int i = byteOffset; ; i++) { if (i >= doc.getLength()) { length = i - byteOffset + 1; break; } else if (doc.getText(i, 1).equals("\n")) { length = i - byteOffset; break; } } } catch (BadLocationException ble) { length = 0; } if (0 < length) { doc.setCharacterAttributes(byteOffset, length, sas, false); } } }
Example 11
Source File: CfgPropCompletionItem.java From nb-springboot with Apache License 2.0 | 4 votes |
@Override public void defaultAction(JTextComponent jtc) { logger.log(Level.FINER, "Accepted name completion: {0}", configurationMeta.getId()); try { StyledDocument doc = (StyledDocument) jtc.getDocument(); // calculate the amount of chars to remove (by default from property start up to caret position) int lenToRemove = caretOffset - propStartOffset; int equalSignIndex = -1; if (overwrite) { // NOTE: the editor removes by itself the word at caret when ctrl + enter is pressed // the document state here is different from when the completion was invoked thus we have to // find again the offset of the equal sign in the line Element lineElement = doc.getParagraphElement(caretOffset); String line = doc.getText(lineElement.getStartOffset(), lineElement.getEndOffset() - lineElement.getStartOffset()); equalSignIndex = line.indexOf('='); int colonIndex = line.indexOf(':'); if (equalSignIndex >= 0) { // from property start to equal sign lenToRemove = lineElement.getStartOffset() + equalSignIndex - propStartOffset; } else if (colonIndex >= 0) { // from property start to colon lenToRemove = lineElement.getStartOffset() + colonIndex - propStartOffset; } else { // from property start to end of line (except line terminator) lenToRemove = lineElement.getEndOffset() - 1 - propStartOffset; } } // remove characters from the property name start offset doc.remove(propStartOffset, lenToRemove); // add some useful chars depending on data type and presence of successive equal signs final String dataType = configurationMeta.getType(); final boolean isSequence = dataType.contains("List") || dataType.contains("Set") || dataType.contains("[]"); final boolean preferArray = NbPreferences.forModule(PrefConstants.class) .getBoolean(PrefConstants.PREF_ARRAY_NOTATION, false); final boolean needEqualSign = !(overwrite && equalSignIndex >= 0); StringBuilder sb = new StringBuilder(getText()); boolean continueCompletion = false; int goBack = 0; if (dataType.contains("Map")) { sb.append("."); continueCompletion = canCompleteKey(); } else if (isSequence) { if (preferArray) { sb.append("[]"); goBack = 1; if (needEqualSign) { sb.append("="); goBack++; } } else { if (needEqualSign) { sb.append("="); continueCompletion = canCompleteValue(); } } } else if (needEqualSign) { sb.append("="); continueCompletion = canCompleteValue(); } doc.insertString(propStartOffset, sb.toString(), null); if (goBack != 0) { jtc.setCaretPosition(jtc.getCaretPosition() - goBack); } // optinally close the code completion box if (!continueCompletion) { Completion.get().hideAll(); } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } }
Example 12
Source File: ValueCompletionItem.java From nb-springboot with Apache License 2.0 | 4 votes |
@Override public void defaultAction(JTextComponent jtc) { logger.log(Level.FINER, "Accepted value completion: {0}", hint.toString()); try { StyledDocument doc = (StyledDocument) jtc.getDocument(); // calculate the amount of chars to remove (by default from dot up to caret position) int lenToRemove = caretOffset - dotOffset; if (overwrite) { // NOTE: the editor removes by itself the word at caret when ctrl + enter is pressed // the document state here is different from when the completion was invoked thus we have to // find again the offset of the equal sign in the line Element lineElement = doc.getParagraphElement(caretOffset); String line = doc.getText(lineElement.getStartOffset(), lineElement.getEndOffset() - lineElement.getStartOffset()); int equalSignIndex = line.indexOf('='); int colonIndex = line.indexOf(':'); int commaIndex = line.indexOf(',', dotOffset - lineElement.getStartOffset()); if (equalSignIndex >= 0 && dotOffset < equalSignIndex) { // from dot to equal sign lenToRemove = lineElement.getStartOffset() + equalSignIndex - dotOffset; } else if (colonIndex >= 0 && dotOffset < colonIndex) { // from dot to colon lenToRemove = lineElement.getStartOffset() + colonIndex - dotOffset; } else if (commaIndex >= 0) { // from dot to comma lenToRemove = lineElement.getStartOffset() + commaIndex - dotOffset; } else { // from dot to end of line (except line terminator) lenToRemove = lineElement.getEndOffset() - 1 - dotOffset; } } // remove characters from dot then insert new text doc.remove(dotOffset, lenToRemove); doc.insertString(dotOffset, hint.getValue().toString(), null); // close the code completion box if (!continueCompletion) { Completion.get().hideAll(); } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } }
Example 13
Source File: JavaTypeCompletionItem.java From nb-springboot with Apache License 2.0 | 4 votes |
@Override public void defaultAction(JTextComponent jtc) { logger.log(Level.FINER, "Accepted java type completion: {0} {1}", new Object[]{elementKind.toString(), name}); try { StyledDocument doc = (StyledDocument) jtc.getDocument(); // calculate the amount of chars to remove (by default from dot up to caret position) int lenToRemove = caretOffset - dotOffset; if (overwrite) { // NOTE: the editor removes by itself the word at caret when ctrl + enter is pressed // the document state here is different from when the completion was invoked thus we have to // find again the offset of the equal sign in the line Element lineElement = doc.getParagraphElement(caretOffset); String line = doc.getText(lineElement.getStartOffset(), lineElement.getEndOffset() - lineElement.getStartOffset()); int equalSignIndex = line.indexOf('='); int colonIndex = line.indexOf(':'); int commaIndex = line.indexOf(',', dotOffset - lineElement.getStartOffset()); if (equalSignIndex >= 0 && dotOffset < equalSignIndex) { // from dot to equal sign lenToRemove = lineElement.getStartOffset() + equalSignIndex - dotOffset; } else if (colonIndex >= 0 && dotOffset < colonIndex) { // from dot to colon lenToRemove = lineElement.getStartOffset() + colonIndex - dotOffset; } else if (commaIndex >= 0) { // from dot to comma lenToRemove = lineElement.getStartOffset() + commaIndex - dotOffset; } else { // from dot to end of line (except line terminator) lenToRemove = lineElement.getEndOffset() - 1 - dotOffset; } } // remove characters from dot then insert new text doc.remove(dotOffset, lenToRemove); if (isKeyCompletion) { // insert and continue completion doc.insertString(dotOffset, name.concat("="), null); } else { // insert and close the code completion box doc.insertString(dotOffset, name, null); Completion.get().hideAll(); } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } }
Example 14
Source File: FileObjectCompletionItem.java From nb-springboot with Apache License 2.0 | 4 votes |
@Override public void defaultAction(JTextComponent jtc) { logger.log(Level.FINER, "Accepted file object completion: {0}", FileUtil.getFileDisplayName(fileObj)); try { StyledDocument doc = (StyledDocument) jtc.getDocument(); // calculate the amount of chars to remove (by default from dot up to caret position) int lenToRemove = caretOffset - dotOffset; if (overwrite) { // NOTE: the editor removes by itself the word at caret when ctrl + enter is pressed // the document state here is different from when the completion was invoked thus we have to // find again the offset of the equal sign in the line Element lineElement = doc.getParagraphElement(caretOffset); String line = doc.getText(lineElement.getStartOffset(), lineElement.getEndOffset() - lineElement.getStartOffset()); int equalSignIndex = line.indexOf('='); int colonIndex = line.indexOf(':'); int commaIndex = line.indexOf(',', dotOffset - lineElement.getStartOffset()); if (equalSignIndex >= 0 && dotOffset < equalSignIndex) { // from dot to equal sign lenToRemove = lineElement.getStartOffset() + equalSignIndex - dotOffset; } else if (colonIndex >= 0 && dotOffset < colonIndex) { // from dot to colon lenToRemove = lineElement.getStartOffset() + colonIndex - dotOffset; } else if (commaIndex >= 0) { // from dot to comma lenToRemove = lineElement.getStartOffset() + commaIndex - dotOffset; } else { // from dot to end of line (except line terminator) lenToRemove = lineElement.getEndOffset() - 1 - dotOffset; } } // remove characters from dot then insert new text doc.remove(dotOffset, lenToRemove); if (fileObj.isRoot()) { logger.log(Level.FINER, "Adding filesystem root and continuing completion"); doc.insertString(dotOffset, getText(), null); } else if (fileObj.isFolder()) { logger.log(Level.FINER, "Adding folder and continuing completion"); doc.insertString(dotOffset, getText().concat("/"), null); } else { logger.log(Level.FINER, "Adding file and finishing completion"); doc.insertString(dotOffset, getText(), null); Completion.get().hideAll(); } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } }