本文整理汇总了Java中javax.swing.text.Document.createPosition方法的典型用法代码示例。如果您正苦于以下问题:Java Document.createPosition方法的具体用法?Java Document.createPosition怎么用?Java Document.createPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.Document
的用法示例。
在下文中一共展示了Document.createPosition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: get
import javax.swing.text.Document; //导入方法依赖的package包/类
/**
* Gets a <code>Position</code> in the document at the given offset. The
* position is automatically updated when the document's contents is modified.
* The <code>Position</code> does not reference the document in anyway that
* would prevent the document from being garbage collected.
*
* @param doc The document to get a position in.
* @param offset The initial offset of the position.
*
* @return A <code>Position</code> inside the document.
* @throws BadLocationException If the offset is not valid for the document.
*/
public static Position get(Document doc, int offset) throws BadLocationException {
// Check that the offset is valid. This should excercise any rule imposed by
// the document on its positions.
doc.createPosition(offset);
synchronized (OGLS) {
OffsetGapList<WeakP> ogl = OGLS.get(doc);
if (ogl == null) {
ogl = new OffsetGapList<WeakPositions.WeakP>();
OGLS.put(doc, ogl);
doc.addDocumentListener(WeakListeners.document(documentsTracker, doc));
}
int index = ogl.findElementIndex(offset);
WeakP pos = index >= 0 ? ogl.get(index) : null;
if (pos == null) {
pos = new WeakP(offset);
ogl.add(pos);
}
return pos;
}
}
示例2: indentLine
import javax.swing.text.Document; //导入方法依赖的package包/类
@Override
public int indentLine(Document doc, int offset) {
Indent indent = Indent.get(doc);
indent.lock();
try {
if (doc instanceof BaseDocument) {
((BaseDocument) doc).atomicLock();
}
try {
Position pos = doc.createPosition(offset);
indent.reindent(offset);
return pos.getOffset();
} catch (BadLocationException ble) {
return offset;
} finally {
if (doc instanceof BaseDocument) {
((BaseDocument) doc).atomicUnlock();
}
}
} finally {
indent.unlock();
}
}
示例3: addMarkAllHighlight
import javax.swing.text.Document; //导入方法依赖的package包/类
/**
* Adds a special "marked occurrence" highlight.
*
* @param start
* @param end
* @param p
* @return A tag to reference the highlight later.
* @throws BadLocationException
* @see #clearMarkAllHighlights()
*/
Object addMarkAllHighlight(int start, int end, HighlightPainter p)
throws BadLocationException {
Document doc = textArea.getDocument();
TextUI mapper = textArea.getUI();
// Always layered highlights for marked occurrences.
HighlightInfoImpl i = new LayeredHighlightInfoImpl();
i.setPainter(p);
i.p0 = doc.createPosition(start);
// HACK: Use "end-1" to prevent chars the user types at the "end" of
// the highlight to be absorbed into the highlight (default Highlight
// behavior).
i.p1 = doc.createPosition(end-1);
markAllHighlights.add(i);
mapper.damageRange(textArea, start, end);
return i;
}
示例4: createPosition
import javax.swing.text.Document; //导入方法依赖的package包/类
public static Position createPosition(Context context, int offset, boolean backwardBias) throws Exception {
final Document expectedDoc = getExpectedDocument(context);
final Document doc = getDocument(context);
if (context.isLogOp()) {
StringBuilder sb = context.logOpBuilder();
sb.append(backwardBias ? "CREATE_BACKWARD_BIAS_POSITION(" : "CREATE_POSITION(");
sb.append(offset).append("): \n");
context.logOp(sb);
}
Position testPos = (expectedDoc instanceof ExpectedDocument)
? ((ExpectedDocument)expectedDoc).createSyncedTestPosition(offset, backwardBias)
: (backwardBias
? ((TestEditorDocument)doc).createBackwardBiasPosition(offset)
:doc.createPosition(offset));
// Since EditorDocumentContent implements position sharing the test should obey
// and if shared position is returned it should only check that the existing position has the right offset.
return testPos;
}
示例5: testPos
import javax.swing.text.Document; //导入方法依赖的package包/类
@Test
public void testPos() throws Exception {
Document doc = new PlainDocument();
doc.insertString(0, "\t\t\n\n", null);
Position pos1 = doc.createPosition(1);
Position pos2 = doc.createPosition(2);
Position pos10 = ComplexPositions.create(pos1, 0);
Position pos11 = ComplexPositions.create(pos1, 1);
Position pos20 = ComplexPositions.create(pos2, 0);
Position pos21 = ComplexPositions.create(pos2, 1);
assertEquals(0, ComplexPositions.getSplitOffset(pos1));
assertEquals(0, ComplexPositions.getSplitOffset(pos10));
assertEquals(1, ComplexPositions.getSplitOffset(pos11));
comparePos(pos1, pos10, 0);
comparePos(pos10, pos11, -1);
comparePos(pos1, pos2, -1);
comparePos(pos10, pos20, -1);
comparePos(pos20, pos21, -1);
}
示例6: getToolTip
import javax.swing.text.Document; //导入方法依赖的package包/类
@Override
public JComponent getToolTip(double x, double y, Shape allocation) {
Container container = getContainer();
if (container instanceof JEditorPane) {
JEditorPane editorPane = (JEditorPane) getContainer();
JEditorPane tooltipPane = new JEditorPane();
EditorKit kit = editorPane.getEditorKit();
Document doc = getDocument();
if (kit != null && doc != null) {
Element lineRootElement = doc.getDefaultRootElement();
tooltipPane.putClientProperty(FoldViewFactory.DISPLAY_ALL_FOLDS_EXPANDED_PROPERTY, true);
try {
// Start-offset of the fold => line start => position
int lineIndex = lineRootElement.getElementIndex(fold.getStartOffset());
Position pos = doc.createPosition(
lineRootElement.getElement(lineIndex).getStartOffset());
// DocumentView.START_POSITION_PROPERTY
tooltipPane.putClientProperty("document-view-start-position", pos); // NOI18N
// End-offset of the fold => line end => position
lineIndex = lineRootElement.getElementIndex(fold.getEndOffset());
pos = doc.createPosition(lineRootElement.getElement(lineIndex).getEndOffset());
// DocumentView.END_POSITION_PROPERTY
tooltipPane.putClientProperty("document-view-end-position", pos); // NOI18N
tooltipPane.putClientProperty("document-view-accurate-span", true); // NOI18N
// Set the same kit and document
tooltipPane.setEditorKit(kit);
tooltipPane.setDocument(doc);
tooltipPane.setEditable(false);
return new FoldToolTip(editorPane, tooltipPane, getBorderColor());
} catch (BadLocationException e) {
// => return null
}
}
}
return null;
}
示例7: TextTransferable
import javax.swing.text.Document; //导入方法依赖的package包/类
TextTransferable(JTextComponent c, int start, int end) {
this.c = c;
Document doc = c.getDocument();
try {
p0 = doc.createPosition(start);
p1 = doc.createPosition(end);
plainData = c.getSelectedText();
} catch (BadLocationException ble) {
}
}
示例8: setEndOffset
import javax.swing.text.Document; //导入方法依赖的package包/类
void setEndOffset(Document doc, int endOffset)
throws BadLocationException {
if (isRootFold()) {
throw new IllegalStateException("Cannot set endOffset of root fold"); // NOI18N
} else {
this.endPos = doc.createPosition(endOffset);
}
}
示例9: actionPerformed
import javax.swing.text.Document; //导入方法依赖的package包/类
public void actionPerformed(ActionEvent e) {
JTextComponent tc = (JTextComponent) getInvoker();
String sel = tc.getSelectedText();
if (e.getSource() == cutItem) {
tc.cut();
} else if (e.getSource() == copyItem) {
tc.copy();
} else if (e.getSource() == pasteItem) {
tc.paste();
} else if (e.getSource() == selectAllItem) {
tc.selectAll();
} else if (e.getSource() == deleteItem) {
Document doc = tc.getDocument();
int start = tc.getSelectionStart();
int end = tc.getSelectionEnd();
try {
Position p0 = doc.createPosition(start);
Position p1 = doc.createPosition(end);
if ((p0 != null) && (p1 != null)
&& (p0.getOffset() != p1.getOffset())) {
doc.remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
}
} catch (BadLocationException be) {
}
}
}
示例10: createPosition
import javax.swing.text.Document; //导入方法依赖的package包/类
private Position createPosition(Document doc, int offset) {
try {
return doc.createPosition(offset);
} catch (BadLocationException ex) {
throw new IndexOutOfBoundsException("Position creation failed at offset=" + offset + // NOI18N
", doc=" + doc + "\n" + ex.getLocalizedMessage()); // NOI18N
}
}
示例11: testCustomBounds
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testCustomBounds() throws Exception {
loggingOn();
JEditorPane pane = ViewUpdatesTesting.createPane();
Document doc = pane.getDocument();
doc.insertString(0, "hello\nworld\ngood\nmorning", null);
Position startPos = doc.createPosition(3);
pane.putClientProperty(DocumentView.START_POSITION_PROPERTY, startPos);
pane.modelToView(0); // Force rebuild of VH
doc.insertString(startPos.getOffset(), "a", null);
doc.insertString(startPos.getOffset() - 1, "a", null);
Element line0 = doc.getDefaultRootElement().getElement(0);
Position endPos = doc.createPosition(line0.getEndOffset() + 3); // Middle of line 1
pane.putClientProperty(DocumentView.END_POSITION_PROPERTY, endPos);
pane.modelToView(0); // Force rebuild of VH
TestHighlightsViewFactory testFactory = ViewUpdatesTesting.getTestFactory(pane);
List<TestHighlight> hlts = ViewUpdatesTesting.getHighlightsCopy(testFactory);
int hlStartOffset = startPos.getOffset() + 1;
int hlEndOffset = endPos.getOffset() - 1;
TestHighlight hi = TestHighlight.create(doc, hlStartOffset, hlEndOffset, colorAttrs[0]);
hlts.add(hi);
testFactory.setHighlights(hlts);
testFactory.fireChange(hlStartOffset, hlEndOffset);
pane.modelToView(0); // Force rebuild of VH
doc.insertString(doc.getLength(), "test\ntest2", null);
Position endPos2 = doc.createPosition(doc.getLength() - 3);
pane.putClientProperty(DocumentView.END_POSITION_PROPERTY, endPos2);
pane.modelToView(0); // Force rebuild of VH
doc.remove(endPos2.getOffset() - 2, 3);
pane.putClientProperty(DocumentView.START_POSITION_PROPERTY, null);
pane.modelToView(0); // Force rebuild of VH
}
示例12: testEmptyCustomBounds
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testEmptyCustomBounds() throws Exception {
loggingOn();
JEditorPane pane = ViewUpdatesTesting.createPane();
Document doc = pane.getDocument();
pane.modelToView(0);
doc.insertString(0, "hello\nworld\ngood\nmorning", null);
Position startPos = doc.createPosition(3);
pane.putClientProperty(DocumentView.START_POSITION_PROPERTY, startPos);
pane.putClientProperty(DocumentView.END_POSITION_PROPERTY, startPos);
pane.modelToView(0); // Force rebuild of VH
Position endPos = doc.createPosition(2);
pane.putClientProperty(DocumentView.END_POSITION_PROPERTY, endPos);
pane.modelToView(0); // Force rebuild of VH
}
示例13: Fold
import javax.swing.text.Document; //导入方法依赖的package包/类
Fold(FoldOperationImpl operation,
FoldType type, String description, boolean collapsed,
Document doc, int startOffset, int endOffset,
int startGuardedLength, int endGuardedLength,
Object extraInfo)
throws BadLocationException {
if (startGuardedLength < 0) {
throw new IllegalArgumentException("startGuardedLength=" // NOI18N
+ startGuardedLength + " < 0"); // NOI18N
}
if (endGuardedLength < 0) {
throw new IllegalArgumentException("endGuardedLength=" // NOI18N
+ endGuardedLength + " < 0"); // NOI18N
}
if (startOffset >= endOffset) {
throw new IllegalArgumentException("startOffset=" + startOffset + " >= endOffset=" + endOffset); // NOI18N
}
if ((endOffset - startOffset) < (startGuardedLength + endGuardedLength)) {
throw new IllegalArgumentException("(endOffset=" + endOffset // NOI18N
+ " - startOffset=" + startOffset + ") < " // NOI18N
+ "(startGuardedLength=" + startGuardedLength // NOI18N
+ " + endGuardedLength=" + endGuardedLength + ")" // NOI18N
); // NOI18N
}
this.operation = operation;
this.type = type;
this.flags = collapsed ? FLAG_COLLAPSED : 0;
this.description = description;
this.startPos = doc.createPosition(startOffset);
this.endPos = doc.createPosition(endOffset);
this.startGuardedLength = startGuardedLength;
this.endGuardedLength = endGuardedLength;
this.extraInfo = extraInfo;
}
示例14: setStartOffset
import javax.swing.text.Document; //导入方法依赖的package包/类
void setStartOffset(Document doc, int startOffset)
throws BadLocationException {
if (isRootFold()) {
throw new IllegalStateException("Cannot set endOffset of root fold"); // NOI18N
} else {
this.startPos = doc.createPosition(startOffset);
}
}
示例15: actionPerformed
import javax.swing.text.Document; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent evt, JTextComponent target) {
if (target != null) {
Caret caret = target.getCaret();
if (!Utilities.isSelectionShowing(caret)) {
try {
int[] identifierBlock = Utilities.getIdentifierBlock((BaseDocument) target.getDocument(), caret.getDot());
if (identifierBlock != null) {
caret.setDot(identifierBlock[0]);
caret.moveDot(identifierBlock[1]);
}
} catch (BadLocationException e) {
LOGGER.log(Level.WARNING, null, e);
}
}
EditorFindSupport findSupport = EditorFindSupport.getInstance();
HashMap<String, Object> props = new HashMap<>(findSupport.createDefaultFindProperties());
String searchWord = target.getSelectedText();
if (searchWord != null) {
int n = searchWord.indexOf('\n');
if (n >= 0) {
searchWord = searchWord.substring(0, n);
}
props.put(EditorFindSupport.FIND_WHAT, searchWord);
Document doc = target.getDocument();
EditorUI eui = org.netbeans.editor.Utilities.getEditorUI(target);
if (eui.getComponent().getClientProperty("AsTextField") == null) { //NOI18N
findSupport.setFocusedTextComponent(eui.getComponent());
}
findSupport.putFindProperties(props);
findSupport.find(null, false);
if (caret instanceof EditorCaret) {
EditorCaret editorCaret = (EditorCaret) caret;
try {
int[] blocks = findSupport.getBlocks(new int[]{-1, -1}, doc, 0, doc.getLength());
if (blocks[0] >= 0 && blocks.length % 2 == 0) {
List<Position> newCarets = new ArrayList<>();
for (int i = 0; i < blocks.length; i += 2) {
int start = blocks[i];
int end = blocks[i + 1];
if (start == -1 || end == -1) {
break;
}
Position startPos = doc.createPosition(start);
Position endPos = doc.createPosition(end);
newCarets.add(endPos);
newCarets.add(startPos);
}
editorCaret.replaceCarets(newCarets, null); // TODO handle biases
}
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
}
}
}
}