本文整理汇总了Java中javax.swing.text.Position.getOffset方法的典型用法代码示例。如果您正苦于以下问题:Java Position.getOffset方法的具体用法?Java Position.getOffset怎么用?Java Position.getOffset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.Position
的用法示例。
在下文中一共展示了Position.getOffset方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: flush
import javax.swing.text.Position; //导入方法依赖的package包/类
@Override
public void flush() throws IOException {
Reformat reformat = Reformat.get(doc);
reformat.lock();
try {
String text = buffer.toString();
if (text.length() > 0 && offset <= doc.getLength()) {
try {
doc.insertString(offset, text, null);
Position endPos = doc.createPosition(offset + text.length());
reformat.reformat(offset, endPos.getOffset());
int len = endPos.getOffset() - offset;
String reformattedText = doc.getText(offset, len);
doc.remove(offset, len);
writer.write(reformattedText.substring(writtenLen));
writtenLen = len;
} catch (BadLocationException e) {
}
}
} finally {
reformat.unlock();
}
}
示例2: testGetEndPosition
import javax.swing.text.Position; //导入方法依赖的package包/类
public void testGetEndPosition() {
System.out.println("testGetEndPosition");
OutWriter ow = new OutWriter ();
OutputDocument doc = new OutputDocument (ow);
String first = "This is the first string";
String second = "This is the second string, ain't it?";
String third = "This is the third string";
ow.println(first);
ow.println (second);
ow.flush();
Position pos = doc.getEndPosition();
int offset = pos.getOffset();
assertTrue ("End offset should match number of characters written", offset == ow.getLines().getCharCount());
ow.println (third);
ow.flush();
assertTrue ("Document end offset should change after writing more data", offset != pos.getOffset());
assertTrue ("End offset should match number of characters written", pos.getOffset() == ow.getLines().getCharCount());
}
示例3: AnnotationDescDelegate
import javax.swing.text.Position; //导入方法依赖的package包/类
AnnotationDescDelegate(BaseDocument doc, Position pos, int length, Annotation anno) {
super(pos.getOffset(),length);
this.pos = pos;
this.delegate = anno;
this.doc = doc;
// update AnnotationDesc.type member
updateAnnotationType();
// forward property changes to AnnotationDesc property changes
l = new PropertyChangeListener() {
public void propertyChange (PropertyChangeEvent evt) {
if (evt.getPropertyName() == null || Annotation.PROP_SHORT_DESCRIPTION.equals(evt.getPropertyName())) {
firePropertyChange(AnnotationDesc.PROP_SHORT_DESCRIPTION, null, null);
}
if (evt.getPropertyName() == null || Annotation.PROP_MOVE_TO_FRONT.equals(evt.getPropertyName())) {
firePropertyChange(AnnotationDesc.PROP_MOVE_TO_FRONT, null, null);
}
if (evt.getPropertyName() == null || Annotation.PROP_ANNOTATION_TYPE.equals(evt.getPropertyName())) {
updateAnnotationType();
firePropertyChange(AnnotationDesc.PROP_ANNOTATION_TYPE, null, null);
}
}
};
delegate.addPropertyChangeListener(l);
}
示例4: generateJavadoc
import javax.swing.text.Position; //导入方法依赖的package包/类
private void generateJavadoc(Document doc, Descriptor desc, Indent ie) throws BadLocationException {
// skip javadoc header /** and tail */ since they were already generated
String content = desc.javadoc;
if(content.endsWith("\n")) {
content = content.substring(0, content.length() - "\n".length());
}
if (content.length() == 0) {
return;
}
Position pos = desc.caret;
int startOffset = pos.getOffset();
doc.insertString(startOffset, content, null);
if (startOffset != pos.getOffset()) {
ie.reindent(startOffset + 1, pos.getOffset());
}
}
示例5: documentInsertAtZeroOffset
import javax.swing.text.Position; //导入方法依赖的package包/类
void documentInsertAtZeroOffset(int insertEndOffset) {
// Nested insert inside active transaction for caret moving
// Since carets may already be moved - do the operations with CaretItems directly
Position insertEndPos = null;
for (CaretItem caretItem : editorCaret.getSortedCaretItems()) {
Position dotPos = caretItem.getDotPosition();
Position.Bias dotBias = caretItem.getDotBias();
boolean modifyDot = (dotPos == null || dotPos.getOffset() == 0);
Position markPos = caretItem.getMarkPosition();
Position.Bias markBias = caretItem.getMarkBias();
boolean modifyMark = (markPos == null || markPos.getOffset() == 0);
if (modifyDot || modifyMark) {
if (insertEndPos == null) {
try {
insertEndPos = doc.createPosition(insertEndOffset);
} catch (BadLocationException ex) {
// Should never happen
return;
}
}
if (modifyDot) {
dotPos = insertEndPos;
// current impl retains dotBias
}
if (modifyMark) {
markPos = insertEndPos;
// current impl retains markBias
}
setDotAndMark(caretItem, dotPos, dotBias, markPos, markBias);
}
// Do not break the loop when caret's pos is above zero offset
// since the carets may be already moved during the transaction
// - possibly to offset zero. But there could be optimization
// at least scan position of only the caret items that were modified and not others.
}
if (insertEndPos != null) {
updateAffectedOffsets(0, insertEndOffset); // TODO isn't this extra work that setDotAndMark() already did??
fullResort = true;
}
}
示例6: addAllHighlightsImpl
import javax.swing.text.Position; //导入方法依赖的package包/类
private int [] addAllHighlightsImpl(PositionsBag bag) {
int changeStart = Integer.MAX_VALUE;
int changeEnd = Integer.MIN_VALUE;
GapList<Position> newMarks = bag.getMarks();
GapList<AttributeSet> newAttributes = bag.getAttributes();
for (int i = 0 ; i + 1 < newMarks.size(); i++) {
Position mark1 = newMarks.get(i);
Position mark2 = newMarks.get(i + 1);
AttributeSet attrSet = newAttributes.get(i);
if (attrSet == null) {
// skip empty highlight
continue;
}
addHighlightImpl(mark1, mark2, attrSet);
if (changeStart == Integer.MAX_VALUE) {
changeStart = mark1.getOffset();
}
changeEnd = mark2.getOffset();
}
if (changeStart != Integer.MAX_VALUE && changeEnd != Integer.MIN_VALUE) {
return new int [] { changeStart, changeEnd };
} else {
return null;
}
}
示例7: testCustomBounds
import javax.swing.text.Position; //导入方法依赖的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
}
示例8: contains
import javax.swing.text.Position; //导入方法依赖的package包/类
public boolean contains(Position pos, boolean allowHoles) {
if (!allowHoles) {
return header.getBegin().getOffset() <= pos.getOffset() &&
footer.getEnd().getOffset() >= pos.getOffset();
} else {
if (header.getBegin().getOffset() <= pos.getOffset() &&
header.getEnd().getOffset() >= pos.getOffset()) {
return true;
}
return footer.getBegin().getOffset() <= pos.getOffset() &&
footer.getEnd().getOffset() >= pos.getOffset();
}
}
示例9: findTooltipRange
import javax.swing.text.Position; //导入方法依赖的package包/类
private static Position[] findTooltipRange(Position[] origin, Position[] matches) {
if (origin == null || matches == null) {
return null;
}
int start = Integer.MAX_VALUE;
int end = -1;
Position sp = null;
Position ep = null;
// See issue #219683: in if-then-elif-else constructs, only the 2 initial pairs
// (start and end of the initial tag/construct) are interesting. For finer control,
// a language SPI has to be created.
for (int i = 0; i < Math.min(matches.length, 4); i += 2) {
Position s = matches[i];
Position e = matches[i+1];
int s2 = s.getOffset();
if (s2 < start) {
sp = s;
start = s2;
}
int e2 = e.getOffset();
if (e2 > end) {
ep = e;
end = e2;
}
}
return new Position[] { sp, ep };
}
示例10: actionPerformed
import javax.swing.text.Position; //导入方法依赖的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) {
}
}
}
示例11: TestAnnotationDesc2
import javax.swing.text.Position; //导入方法依赖的package包/类
public TestAnnotationDesc2(BaseDocument bd, Position position) {
super(position.getOffset(), -1);
this.doc = bd;
this.position = position;
}
示例12: TestAnnotationDesc
import javax.swing.text.Position; //导入方法依赖的package包/类
public TestAnnotationDesc (BaseDocument bd, Position position, String type) {
super(position.getOffset(), -1);
this.doc = bd;
this.position = position;
this.type = type;
}
示例13: checkConsistency
import javax.swing.text.Position; //导入方法依赖的package包/类
private void checkConsistency() {
try {
int docLength = masterDoc.getLength();
assertEquals(docLength, testDoc.getLength());
String masterText = masterDoc.getText(0, docLength);
String testText = testDoc.getText(0, docLength);
assertEquals(masterText, testText);
Element lineRoot = masterDoc.getDefaultRootElement();
Element testLineRoot = testDoc.getDefaultRootElement();
int lineCount = lineRoot.getElementCount();
if (lineCount != testLineRoot.getElementCount()) {
fail("Line count " + testLineRoot.getElementCount()
+ " != " + lineCount);
}
// Compare line boundaries
for (int i = 0; i < lineCount; i++) {
Element masterLine = lineRoot.getElement(i);
Element testLine = testLineRoot.getElement(i);
if (masterLine.getStartOffset() != testLine.getStartOffset()) {
fail("Start of line " + i + ": Offset " + testLine.getStartOffset()
+ " != " + masterLine.getStartOffset());
}
if (masterLine.getEndOffset() != testLine.getEndOffset()) {
fail("End of line " + i + ": Offset " + testLine.getEndOffset()
+ " != " + masterLine.getEndOffset());
}
}
int positionCount = masterPositions.size();
for (int i = 0; i < positionCount; i++) {
Position masterPos = (Position)masterPositions.get(i);
Position testPos = (Position)testPositions.get(i);
if (masterPos.getOffset() != testPos.getOffset()) {
fail("Tested position " + (i + 1) + " of " + positionCount
+ ": " + testPos.getOffset()
+ " != " + masterPos.getOffset());
}
}
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
}
示例14: update
import javax.swing.text.Position; //导入方法依赖的package包/类
void update(CaretItem caret) {
this.caretItem = caret;
Position dotPos = caret.getDotPosition();
if (dotPos != null) {
dotOffset = dotPos.getOffset();
dotShift = ComplexPositions.getSplitOffset(dotPos);
Position markPos = caret.getMarkPosition();
if (markPos != null && markPos != dotPos) { // Still they may be equal which means no selection
int markOffset = markPos.getOffset();
int markShift = ComplexPositions.getSplitOffset(markPos);
if (markOffset < dotOffset || (markOffset == dotOffset && markShift < dotShift)) {
startPos = markPos;
startBias = caret.getMarkBias();
endPos = dotPos;
endBias = caret.getDotBias();
startOffset = markOffset;
startShift = markShift;
endOffset = dotOffset;
endShift = dotShift;
dotAtStart = false;
selection = true;
} else {
if (markOffset == dotOffset && markShift == dotShift) { // No selection
startPos = markPos;
startBias = caret.getMarkBias();
endPos = dotPos;
endBias = caret.getDotBias();
startOffset = markOffset;
startShift = markShift;
endOffset = dotOffset;
endShift = dotShift;
dotAtStart = false;
selection = false;
} else {
startPos = dotPos;
startBias = caret.getDotBias();
endPos = markPos;
endBias = caret.getMarkBias();
startOffset = dotOffset;
startShift = dotShift;
endOffset = markOffset;
endShift = markShift;
dotAtStart = true;
selection = true;
}
}
} else {
startPos = endPos = dotPos;
startBias = endBias = caret.getDotBias();
startOffset = endOffset = dotOffset;
startShift = startShift = dotShift;
dotAtStart = false;
selection = false;
}
} else {
clear();
}
}
示例15: getBlocks
import javax.swing.text.Position; //导入方法依赖的package包/类
/**
* <p><b>IMPORTANT:</b> This method is public only for keeping backwards
* compatibility of the {@link org.netbeans.editor.FindSupport} class.
*/
public synchronized int[] getBlocks(final int[] blocks, final Document doc,
int startOffset, int endOffset) throws BadLocationException {
final Map<String, Object> props = getValidFindProperties(null);
String newCacheKey = calculateCacheKey(doc, startOffset, endOffset, props);
if (cachekey.equals(newCacheKey)) {
return Arrays.copyOf(cacheContent, cacheContent.length);
}
boolean blockSearch = Boolean.TRUE.equals(props.get(FIND_BLOCK_SEARCH));
Position blockSearchStartPos = (Position) props.get(FIND_BLOCK_SEARCH_START);
Position blockSearchEndPos = (Position) props.get(FIND_BLOCK_SEARCH_END);
if (blockSearch && blockSearchStartPos != null && blockSearchEndPos != null){
if (endOffset >= blockSearchStartPos.getOffset() &&
startOffset <= blockSearchEndPos.getOffset())
{
startOffset = Math.max(blockSearchStartPos.getOffset(), startOffset);
endOffset = Math.min(blockSearchEndPos.getOffset(), endOffset);
} else {
return blocks;
}
}
final int so = startOffset;
final int eo = endOffset;
currentResult = null;
try {
executor.submit(new Runnable() {
@Override
public void run() {
try {
currentResult = DocumentFinder.findBlocks(doc, so, eo, props, blocks);
cacheContent = currentResult.getFoundPositions();
} catch (BadLocationException ble) {
cacheContent = Arrays.copyOf(blocks, blocks.length);
LOG.log(Level.INFO, ble.getMessage(), ble);
}
}
}).get(TIME_LIMIT, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
cacheContent = Arrays.copyOf(blocks, blocks.length);
org.netbeans.editor.Utilities.setStatusBoldText(getFocusedTextComponent(), NbBundle.getMessage(EditorFindSupport.class, "slow-search"));
LOG.log(Level.INFO, ex.getMessage(), ex);
}
if (currentResult != null && currentResult.hasErrorMsg()) {
org.netbeans.editor.Utilities.setStatusBoldText(getFocusedTextComponent(), currentResult.getErrorMsg());
}
cachekey = newCacheKey;
return Arrays.copyOf(cacheContent, cacheContent.length);
}