本文整理汇总了Java中org.openide.text.NbDocument.createPosition方法的典型用法代码示例。如果您正苦于以下问题:Java NbDocument.createPosition方法的具体用法?Java NbDocument.createPosition怎么用?Java NbDocument.createPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.openide.text.NbDocument
的用法示例。
在下文中一共展示了NbDocument.createPosition方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findResult
import org.openide.text.NbDocument; //导入方法依赖的package包/类
private void findResult() {
if (isCanceled())
return;
int len = sdoc.getLength();
if (startOffset >= len || endOffset > len) {
if (!isCanceled() && ERR.isLoggable(ErrorManager.WARNING)) {
ERR.log(ErrorManager.WARNING, "document changed, but not canceled?" );
ERR.log(ErrorManager.WARNING, "len = " + len );
ERR.log(ErrorManager.WARNING, "startOffset = " + startOffset );
ERR.log(ErrorManager.WARNING, "endOffset = " + endOffset );
}
cancel();
return;
}
try {
result[0] = NbDocument.createPosition(sdoc, startOffset, Bias.Forward);
result[1] = NbDocument.createPosition(sdoc, endOffset, Bias.Backward);
} catch (BadLocationException e) {
ERR.notify(ErrorManager.ERROR, e);
}
}
示例2: makeFxNamespaceCreator
import org.openide.text.NbDocument; //导入方法依赖的package包/类
/**
* Adds fx: namespace declaration to the root of the document. Fails with
* IllegalStateException, if a fx with a conflicting namespace URI is already
* used in the root element.
*
* @param doc document to modify
* @param h token hierarchy - to find the root element
*/
public static Callable<String> makeFxNamespaceCreator(CompletionContext ctx) {
final String existingPrefix = ctx.findFxmlNsPrefix();
if (existingPrefix != null) {
return new Callable<String>() {
@Override
public String call() throws Exception {
return existingPrefix;
}
};
}
final String prefix = ctx.findPrefixString(JavaFXEditorUtils.FXML_FX_NAMESPACE_CURRENT,
JavaFXEditorUtils.FXML_FX_PREFIX);
final Document doc = ctx.getDoc();
Position pos;
try {
pos = NbDocument.createPosition(doc, ctx.getRootAttrInsertOffset(), Position.Bias.Forward);
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
pos = null;
}
final Position finalPos = pos;
return new Callable<String>() {
public String call() throws Exception {
if (finalPos == null) {
return prefix;
}
doc.insertString(finalPos.getOffset(), "xmlns:" + prefix + "=\"" +
JavaFXEditorUtils.FXML_FX_NAMESPACE_CURRENT + "\" ", null);
return prefix;
}
};
}
示例3: actionPerformed
import org.openide.text.NbDocument; //导入方法依赖的package包/类
@Override
public void actionPerformed(ActionEvent e) {
if (ignoreComboAction)
return; // not invoked by user, ignore
GuardedBlock gBlock = codeData.getGuardedBlock(category, blockIndex);
GuardBlockInfo gInfo = getGuardInfos(category)[blockIndex];
int[] blockBounds = getGuardBlockBounds(category, blockIndex);
int startOffset = blockBounds[0];
int endOffset = blockBounds[1];
int gHead = gBlock.getHeaderLength();
int gFoot = gBlock.getFooterLength();
JTextComponent editor = getEditor(category);
StyledDocument doc = (StyledDocument) editor.getDocument();
changed = true;
JComboBox combo = (JComboBox) e.getSource();
try {
docListener.setActive(false);
if (combo.getSelectedIndex() == 1) { // changing from default to custom
NbDocument.unmarkGuarded(doc, startOffset, endOffset - startOffset);
// keep last '\n' so we don't destroy next editable block's position
doc.remove(startOffset, endOffset - startOffset - 1);
// insert the custom code into the document
String customCode = gBlock.getCustomCode();
int customLength = customCode.length();
if (gInfo.customizedCode != null) { // already was edited before
customCode = customCode.substring(0, gHead)
+ gInfo.customizedCode
+ customCode.substring(customLength - gFoot);
customLength = customCode.length();
}
if (customCode.endsWith("\n")) // NOI18N
customCode = customCode.substring(0, customLength-1);
doc.insertString(startOffset, customCode, null);
gInfo.customized = true;
// make guarded "header" and "footer", select the text in between
NbDocument.markGuarded(doc, startOffset, gHead);
NbDocument.markGuarded(doc, startOffset + customLength - gFoot, gFoot);
editor.setSelectionStart(startOffset + gHead);
editor.setSelectionEnd(startOffset + customLength - gFoot);
editor.requestFocus();
combo.setToolTipText(gBlock.getCustomEntry().getToolTipText());
}
else { // changing from custom to default
// remember the customized code
gInfo.customizedCode = doc.getText(startOffset + gHead,
endOffset - gFoot - gHead - startOffset);
NbDocument.unmarkGuarded(doc, endOffset - gFoot, gFoot);
NbDocument.unmarkGuarded(doc, startOffset, gHead);
// keep last '\n' so we don't destroy next editable block's position
doc.remove(startOffset, endOffset - startOffset - 1);
String defaultCode = gBlock.getDefaultCode();
if (defaultCode.endsWith("\n")) // NOI18N
defaultCode = defaultCode.substring(0, defaultCode.length()-1);
doc.insertString(startOffset, defaultCode, null);
gInfo.customized = false;
// make the whole text guarded, cancel selection
NbDocument.markGuarded(doc, startOffset, defaultCode.length()+1); // including '\n'
if (editor.getSelectionStart() >= startOffset && editor.getSelectionEnd() <= endOffset)
editor.setCaretPosition(startOffset);
combo.setToolTipText(NbBundle.getMessage(CustomCodeData.class, "CTL_GuardCombo_Default_Hint")); // NOI18N
}
// we must create a new Position - current was moved away by inserting new string on it
gInfo.position = NbDocument.createPosition(doc, startOffset, Position.Bias.Forward);
docListener.setActive(true);
}
catch (BadLocationException ex) { // should not happen
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
}
}
示例4: getLine0
import org.openide.text.NbDocument; //导入方法依赖的package包/类
private Position[] getLine0(Error d, final Document doc, int startOffset, int endOffset) {
StyledDocument sdoc = (StyledDocument) doc;
int lineNumber = NbDocument.findLineNumber(sdoc, startOffset);
int lineOffset = NbDocument.findLineOffset(sdoc, lineNumber);
String text = DataLoadersBridge.getDefault().getLine(doc, lineNumber);
if (text == null) {
return new Position[2];
}
if (d.isLineError()) {
int column = 0;
int length = text.length();
while (column < text.length() && Character.isWhitespace(text.charAt(column))) {
column++;
}
while (length > 0 && Character.isWhitespace(text.charAt(length - 1))) {
length--;
}
startOffset = lineOffset + column;
endOffset = lineOffset + length;
if (startOffset > endOffset) {
// Space only on the line
startOffset = lineOffset;
}
}
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "startOffset = " + startOffset );
LOG.log(Level.FINE, "endOffset = " + endOffset );
}
final int startOffsetFinal = startOffset;
final int endOffsetFinal = endOffset;
final Position[] result = new Position[2];
int len = doc.getLength();
if (startOffsetFinal > len || endOffsetFinal > len) {
if (!cancel.isCancelled() && LOG.isLoggable(Level.WARNING)) {
LOG.log(Level.WARNING, "document changed, but not canceled?" );
LOG.log(Level.WARNING, "len = " + len );
LOG.log(Level.WARNING, "startOffset = " + startOffsetFinal );
LOG.log(Level.WARNING, "endOffset = " + endOffsetFinal );
}
cancel();
return result;
}
try {
result[0] = NbDocument.createPosition(doc, startOffsetFinal, Bias.Forward);
result[1] = NbDocument.createPosition(doc, endOffsetFinal, Bias.Backward);
} catch (BadLocationException e) {
LOG.log(Level.WARNING, null, e);
}
return result;
}
示例5: InstantRenamePerformer
import org.openide.text.NbDocument; //导入方法依赖的package包/类
/** Creates a new instance of InstantRenamePerformer */
private InstantRenamePerformer(JTextComponent target, Set<OffsetRange> highlights, int caretOffset) throws BadLocationException {
this.target = target;
doc = target.getDocument();
MutablePositionRegion mainRegion = null;
List<MutablePositionRegion> regions = new ArrayList<MutablePositionRegion>();
for (OffsetRange h : highlights) {
Position start = NbDocument.createPosition(doc, h.getStart(), Bias.Backward);
Position end = NbDocument.createPosition(doc, h.getEnd(), Bias.Forward);
MutablePositionRegion current = new MutablePositionRegion(start, end);
if (isIn(current, caretOffset)) {
mainRegion = current;
} else {
regions.add(current);
}
}
if (mainRegion == null) {
Logger.getLogger(InstantRenamePerformer.class.getName()).warning("No highlight contains the caret (" + caretOffset + "; highlights=" + highlights + ")"); //NOI18N
// Attempt to use another region - pick the one closest to the caret
if (regions.size() > 0) {
mainRegion = regions.get(0);
int mainDistance = Integer.MAX_VALUE;
for (MutablePositionRegion r : regions) {
int distance = caretOffset < r.getStartOffset() ? (r.getStartOffset()-caretOffset) : (caretOffset-r.getEndOffset());
if (distance < mainDistance) {
mainRegion = r;
mainDistance = distance;
}
}
} else {
return;
}
}
regions.add(0, mainRegion);
region = new SyncDocumentRegion(doc, regions);
if (doc instanceof BaseDocument) {
((BaseDocument) doc).addPostModificationDocumentListener(this);
}
target.addKeyListener(this);
target.putClientProperty("NetBeansEditor.navigateBoundaries", mainRegion); // NOI18N
target.putClientProperty(InstantRenamePerformer.class, this);
requestRepaint();
target.select(mainRegion.getStartOffset(), mainRegion.getEndOffset());
}
示例6: createRegion
import org.openide.text.NbDocument; //导入方法依赖的package包/类
public static MutablePositionRegion createRegion(final Document doc, int start, int end) throws BadLocationException {
Position startPos = NbDocument.createPosition(doc, start, Position.Bias.Backward);
Position endPos = NbDocument.createPosition(doc, end, Position.Bias.Forward);
MutablePositionRegion current = new MutablePositionRegion(startPos, endPos);
return current;
}
示例7: addAttribute
import org.openide.text.NbDocument; //导入方法依赖的package包/类
private void addAttribute(JTextComponent c, Document d, String text) throws BadLocationException {
String nsPrefix = ctx.findFxmlNsPrefix();
boolean addNsDecl = false;
// declare the namespace, if not yet present
if (nsPrefix == null) {
nsPrefix = ctx.findPrefixString(JavaFXEditorUtils.FXML_FX_NAMESPACE_CURRENT, JavaFXEditorUtils.FXML_FX_PREFIX);
addNsDecl = true;
}
int start = getStartOffset() + text.length();
// fix the position against mutation
Position startPos = NbDocument.createPosition(d, start, Position.Bias.Backward);
StringBuilder sb = new StringBuilder();
sb.append(" "); // tag-attribute separator
if (ctx.isRootElement() && addNsDecl) {
// append NS declaration
sb.append(createNsDecl(nsPrefix));
}
sb.append(ctx.createNSName(nsPrefix, getAttributeName())).append("=\"");
int l = sb.length();
sb.append("\"");
if (!ctx.isTagFinished()) {
if (shouldClose) {
sb.append("/>");
} else {
sb.append(">");
}
}
d.insertString(start, sb.toString(), null);
if (!ctx.isRootElement() && addNsDecl) {
d.insertString(
ctx.getRootAttrInsertOffset(),
createNsDecl(nsPrefix), null);
}
// position the caret inside '' for the user to enter the value
c.setCaretPosition(startPos.getOffset() + l);
}
示例8: InstantRenamePerformer
import org.openide.text.NbDocument; //导入方法依赖的package包/类
/** Creates a new instance of InstantRenamePerformer */
private InstantRenamePerformer(JTextComponent target, Set<Token> highlights, int caretOffset) throws BadLocationException {
this.target = target;
doc = target.getDocument();
MutablePositionRegion mainRegion = null;
List<MutablePositionRegion> regions = new ArrayList<MutablePositionRegion>();
for (Token h : highlights) {
// type parameter name is represented as ident -> ignore surrounding <> in rename
int delta = h.id() == JavadocTokenId.IDENT && h.text().charAt(0) == '<' && h.text().charAt(h.length() - 1) == '>' ? 1 : 0;
Position start = NbDocument.createPosition(doc, h.offset(null) + delta, Bias.Backward);
Position end = NbDocument.createPosition(doc, h.offset(null) + h.length() - delta, Bias.Forward);
MutablePositionRegion current = new MutablePositionRegion(start, end);
if (isIn(current, caretOffset)) {
mainRegion = current;
} else {
regions.add(current);
}
}
if (mainRegion == null) {
throw new IllegalArgumentException("No highlight contains the caret.");
}
regions.add(0, mainRegion);
region = new SyncDocumentRegion(doc, regions);
if (doc instanceof BaseDocument) {
BaseDocument bdoc = ((BaseDocument) doc);
bdoc.setPostModificationDocumentListener(this);
UndoableEdit undo = new CancelInstantRenameUndoableEdit(this);
for (UndoableEditListener l : bdoc.getUndoableEditListeners()) {
l.undoableEditHappened(new UndoableEditEvent(doc, undo));
}
}
target.addKeyListener(this);
target.putClientProperty(InstantRenamePerformer.class, this);
target.putClientProperty("NetBeansEditor.navigateBoundaries", mainRegion); // NOI18N
requestRepaint();
target.select(mainRegion.getStartOffset(), mainRegion.getEndOffset());
span = region.getFirstRegionLength();
registry.add(this);
sendUndoableEdit(doc, CloneableEditorSupport.BEGIN_COMMIT_GROUP);
}
示例9: performAction
import org.openide.text.NbDocument; //导入方法依赖的package包/类
/**
* Open I18nPanel and grab user response then update Document.
* @param activatedNodes currently activated nodes
*/
protected void performAction (final Node[] activatedNodes) {
try {
final EditorCookie editorCookie = (activatedNodes[0]).getCookie(EditorCookie.class);
if (editorCookie == null) {
Util.debug(new IllegalArgumentException("Missing editor cookie!")); // NOI18N
return;
}
// Set data object.
dataObject = activatedNodes[0].getCookie(DataObject.class);
if (dataObject == null) {
Util.debug(new IllegalArgumentException("Missing DataObject!")); // NOI18N
return;
}
JEditorPane[] panes = editorCookie.getOpenedPanes();
if (panes == null || panes.length == 0) {
//??? should it be tools action at all, once launched
// from node it may raise this exception or it inserts
// string in latest caret position in possibly hidden source
Util.debug(new IllegalArgumentException("Missing editor pane!")); // NOI18N
return;
}
// Set insert position.
position = NbDocument.createPosition(panes[0].getDocument(), panes[0].getCaret().getDot(), Position.Bias.Backward);
// If there is a i18n action in run on the same editor, cancel it.
I18nManager.getDefault().cancel();
try {
showModalPanel();
} catch(IOException ex) {
String msg = "Document loading failure " + dataObject.getName(); // NOI18N
Util.debug(msg, ex);
return;
}
// Ensure caret is visible.
panes[0].getCaret().setVisible(true);
} catch (BadLocationException blex) {
ErrorManager.getDefault().notify(blex);
} finally {
dataObject = null;
support = null;
i18nPanel = null;
position = null;
}
}