本文整理汇总了Java中javax.swing.text.Document.getLength方法的典型用法代码示例。如果您正苦于以下问题:Java Document.getLength方法的具体用法?Java Document.getLength怎么用?Java Document.getLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.Document
的用法示例。
在下文中一共展示了Document.getLength方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findLineNumber
import javax.swing.text.Document; //导入方法依赖的package包/类
private static int findLineNumber(Document doc, int offset) {
int length = doc.getLength();
String text;
int count = 0;
try {
text = doc.getText(0, length);
} catch (BadLocationException ex) { // should not happen
Exceptions.printStackTrace(ex);
return -1;
}
for (int i=0; i < length; i++) {
if (i == offset) {
return count;
}
if (text.charAt(i) == '\n') {
count++;
}
}
return offset==length ? count : -1;
}
示例2: nextCamelCasePosition
import javax.swing.text.Document; //导入方法依赖的package包/类
static int nextCamelCasePosition(JTextComponent textComponent) {
int offset = textComponent.getCaretPosition();
Document doc = textComponent.getDocument();
// Are we at the end of the document?
if (offset == doc.getLength()) {
return -1;
}
KeystrokeHandler bc = UiUtils.getBracketCompletion(doc, offset);
if (bc != null) {
int nextOffset = bc.getNextWordOffset(doc, offset, false);
if (nextOffset != -1) {
return nextOffset;
}
}
try {
return Utilities.getNextWord(textComponent, offset);
} catch (BadLocationException ble) {
// something went wrong :(
ErrorManager.getDefault().notify(ble);
}
return -1;
}
示例3: actionPerformed
import javax.swing.text.Document; //导入方法依赖的package包/类
@Override
public void actionPerformed(ActionEvent e) {
JTextComponent target = getTextComponent(e);
boolean beep = true;
if ((target != null) && (target.isEditable())) {
try {
Document doc = target.getDocument();
int ss = 0;
int se = doc.getLength();
if (ss != se) {
doc.remove(ss, se - ss);
beep = false;
}
} catch (BadLocationException bl) {
}
}
if (beep) {
UIManager.getLookAndFeel().provideErrorFeedback(target);
}
}
示例4: run
import javax.swing.text.Document; //导入方法依赖的package包/类
@Override
protected void run(Context context) throws Exception {
Document expectedDoc = getExpectedDocument(context);
int docLen = expectedDoc.getLength();
if (docLen == 0)
return; // Nothing to possibly remove
Random random = context.container().random();
int length;
if (REMOVE_CHAR.equals(name())) {
length = 1;
} else if (REMOVE_TEXT.equals(name())) {
Integer maxLength = (Integer) context.getPropertyOrNull(REMOVE_TEXT_MAX_LENGTH);
if (maxLength == null)
maxLength = Integer.valueOf(10);
if (maxLength > docLen)
maxLength = Integer.valueOf(docLen);
length = random.nextInt(maxLength) + 1;
} else {
throw new IllegalStateException("Unexpected op name=" + name());
}
int offset = random.nextInt(docLen - length + 1);
remove(context, offset, length);
}
示例5: getVisualColumn
import javax.swing.text.Document; //导入方法依赖的package包/类
/** Return visual column (with expanded tabs) on the line.
* @param doc document to operate on
* @param offset position in document for which the visual column should be found
* @return visual column on the line determined by position
*/
public static int getVisualColumn(Document doc, int offset) throws BadLocationException {
int docLen = doc.getLength();
if (offset == docLen + 1) { // at ending extra '\n' => make docLen to proceed without BLE
offset = docLen;
}
// TODO: fix this, do not use reflection
try {
Method m = findDeclaredMethod(doc.getClass(), "getVisColFromPos", Integer.TYPE); //NOI18N
m.setAccessible(true);
int col = (Integer) m.invoke(doc, offset);
return col;
// return doc.getVisColFromPos(offset);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
示例6: testOffsetInsideBackward
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testOffsetInsideBackward() throws Exception {
Document doc = new PlainDocument();
WordMatch wordMatch = WordMatch.get(doc);
// 012345678901234567890123456789
doc.insertString(0, "abc abc dab ab a xyz ab abd", null);
int docLen = doc.getLength();
int offset = 15;
wordMatch.matchWord(offset, false);
compareText(doc, offset, "aba", docLen + 2);
wordMatch.matchWord(docLen, true);
compareText(doc, offset, "a ", docLen);
wordMatch.matchWord(docLen, true);
compareText(doc, offset, "aa ", docLen + 1);
wordMatch.matchWord(docLen, true);
compareText(doc, offset, "xyza", docLen + 3);
wordMatch.matchWord(docLen, true);
compareText(doc, offset, "abda", docLen + 3);
wordMatch.matchWord(docLen, true);
compareText(doc, offset, "abca", docLen + 3);
}
示例7: testOffset0Forward
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testOffset0Forward() throws Exception {
Document doc = new PlainDocument();
WordMatch wordMatch = WordMatch.get(doc);
// 012345678901234567890123456789
doc.insertString(0, "abc abc dab ab a abab+c", null);
int docLen = doc.getLength();
wordMatch.matchWord(0, true);
compareText(doc, 0, "abca", docLen + 3);
wordMatch.matchWord(0, true);
compareText(doc, 0, "daba", docLen + 3);
wordMatch.matchWord(0, true);
compareText(doc, 0, "aba", docLen + 2);
wordMatch.matchWord(0, true);
compareText(doc, 0, "aa", docLen + 1);
wordMatch.matchWord(0, true);
compareText(doc, 0, "ababa", docLen + 4);
wordMatch.matchWord(0, true);
compareText(doc, 0, "ca", docLen + 1);
wordMatch.matchWord(0, true);
compareText(doc, 0, "ca", docLen + 1);
wordMatch.matchWord(0, false);
compareText(doc, 0, "ababa", docLen + 4);
}
示例8: charBackspaced
import javax.swing.text.Document; //导入方法依赖的package包/类
@Override
public boolean charBackspaced(Document doc, int dotPos, JTextComponent target, char ch) throws BadLocationException {
if (ch == '%' && dotPos > 0 && dotPos <= doc.getLength() - 2) {
String s = doc.getText(dotPos - 1, 3);
if ("<%>".equals(s)) { // NOI18N
doc.remove(dotPos, 2);
return true;
}
}
return false;
}
示例9: testMultiDocs
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testMultiDocs() throws Exception {
JEditorPane pane = new JEditorPane();
pane.setText("abc ax ec ajo");
JFrame frame = new JFrame();
frame.getContentPane().add(pane);
frame.pack(); // Allows to EditorRegistry.register() to make an item in the component list
EditorApiPackageAccessor.get().setIgnoredAncestorClass(JEditorPane.class);
EditorApiPackageAccessor.get().register(pane);
Document doc = new PlainDocument();
WordMatch wordMatch = WordMatch.get(doc);
// 012345678901234567890123456789
doc.insertString(0, "abc a x ahoj", null);
int docLen = doc.getLength();
int offset = 5;
wordMatch.matchWord(offset, false);
compareText(doc, offset - 1, "abc ", docLen + 2);
wordMatch.matchWord(offset, false);
compareText(doc, offset - 1, "ahoj ", docLen + 3);
wordMatch.matchWord(offset, false);
compareText(doc, offset - 1, "ax ", docLen + 1);
wordMatch.matchWord(offset, false);
compareText(doc, offset - 1, "ajo ", docLen + 2);
wordMatch.matchWord(offset, false);
compareText(doc, offset - 1, "ajo ", docLen + 2);
pane.setText(""); // Ensure this doc would affect WordMatch for other docs
}
示例10: getContents
import javax.swing.text.Document; //导入方法依赖的package包/类
private String getContents(Document d, int startFrom) {
try {
int l = d.getLength() + 1;
if (startFrom > l) {
startFrom = l;
}
if (hasMoreParts()) {
StringBuilder sb = new StringBuilder();
for (Rng r : partOffsets) {
if (startFrom >= r.end) {
continue;
} else if (startFrom > r.start) {
sb.append(d.getText(startFrom, Math.min(l, r.end) - startFrom));
} else {
startFrom = Math.min(l, r.start);
sb.append(d.getText(r.start, Math.min(l, r.end) - startFrom));
}
}
return sb.toString();
} else if (startFrom >= getEnd()) {
return ""; // NOI18N
} else if (startFrom > contentStart) {
int e = Math.min(l, contentStart + getPartLen());
return d.getText(startFrom, e - startFrom);
} else {
startFrom = Math.min(l, contentStart);
return d.getText(startFrom, Math.min(l - startFrom, getPartLen()));
}
} catch (BadLocationException ex) {
ex.printStackTrace();
}
return "";
}
示例11: getComponent
import javax.swing.text.Document; //导入方法依赖的package包/类
@Override
protected JComponent getComponent() {
JTextComponent text = getTextComponent();
if (text == null) {
return null;
}
if (Config.getDefault().isAsEditor()) {
return getEditorComponent(text);
}
Document document = myEditor.getDocument();
if (document == null) {
return null;
}
int start;
int end;
if (Config.getDefault().isSelection()) {
start = text.getSelectionStart();
end = text.getSelectionEnd();
}
else {
start = 0;
end = document.getLength();
}
AttributedCharacterIterator[] iterators = getIterators(document, start, end);
//out();
//out("iterators: " + iterators);
//out();
if (iterators != null) {
return new ComponentDocument(iterators);
}
try {
return new ComponentDocument(text.getText(start, end - start));
}
catch (BadLocationException e) {
return null;
}
}
示例12: logOp
import javax.swing.text.Document; //导入方法依赖的package包/类
private void logOp(Context context, Document doc, String msg) throws Exception {
if (Boolean.TRUE.equals(context.round().getPropertyOrNull(RandomTestContainer.LOG_OP))) {
StringBuilder sb = new StringBuilder(doc.getLength() + 50);
dumpLines(sb, doc);
sb.append("\n");
context.container().logger().info(sb.toString());
}
}
示例13: lineIndex2Offset
import javax.swing.text.Document; //导入方法依赖的package包/类
public static int lineIndex2Offset(Document doc, int lineIndex) {
javax.swing.text.Element lineRoot = doc.getDefaultRootElement();
int offset = (lineIndex < lineRoot.getElementCount())
? lineRoot.getElement(lineIndex).getStartOffset()
: doc.getLength();
return offset;
}
示例14: bindComponentToDocument
import javax.swing.text.Document; //导入方法依赖的package包/类
/**
* Bind given component and given document together.
*
* @param document to bind
* @param offset position at which content of the component will be virtually placed
* @param length how many characters replace from the original document
* @param component component to bind
*/
public static void bindComponentToDocument(Document document, int offset, int length, JTextComponent component) {
Parameters.notNull("document", document); //NOI18N
Parameters.notNull("component", component); //NOI18N
if (offset < 0 || offset > document.getLength()) {
throw new IllegalArgumentException("Invalid offset=" + offset + "; file.size=" + document.getLength()); //NOI18N
}
if (length < 0 || offset + length > document.getLength()) {
throw new IllegalArgumentException("Invalid lenght=" + length + "; offset=" + offset + ", file.size=" + document.getLength()); //NOI18N
}
bind(component, document, null, offset, -1, -1, length, (String)document.getProperty("mimeType")); //NOI18N
}
示例15: paintComponentWrapped
import javax.swing.text.Document; //导入方法依赖的package包/类
/**
* Paints icons when line wrapping is enabled. Note that this does not
* override the parent class's implementation to avoid this version being
* called when line wrapping is disabled.
*/
private void paintComponentWrapped(Graphics g) {
// The variables we use are as follows:
// - visibleRect is the "visible" area of the text area; e.g.
// [0,100, 300,100+(lineCount*cellHeight)-1].
// actualTop.y is the topmost-pixel in the first logical line we
// paint. Note that we may well not paint this part of the logical
// line, as it may be broken into many physical lines, with the first
// few physical lines scrolled past. Note also that this is NOT the
// visible rect of this line number list; this line number list has
// visible rect == [0,0, insets.left-1,visibleRect.height-1].
// We avoid using modelToView/viewToModel where possible, as these
// methods trigger a parsing of the line into syntax tokens, which is
// costly. It's cheaper to just grab the child views' bounds.
RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
// boolean currentLineHighlighted = textArea.getHighlightCurrentLine();
Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
int topPosition = textArea.viewToModel(
new Point(visibleRect.x,visibleRect.y));
int topLine = root.getElementIndex(topPosition);
int topY = visibleRect.y;
int bottomY = visibleRect.y + visibleRect.height;
int cellHeight = textArea.getLineHeight();
// Paint icons
if (trackingIcons!=null) {
int lastLine = textArea.getLineCount() - 1;
for (int i=trackingIcons.size()-1; i>=0; i--) { // Last to first
GutterIconInfo ti = getTrackingIcon(i);
Icon icon = ti.getIcon();
if (icon!=null) {
int iconH = icon.getIconHeight();
int offs = ti.getMarkedOffset();
if (offs>=0 && offs<=doc.getLength()) {
int line = root.getElementIndex(offs);
if (line<=lastLine && line>=topLine) {
try {
int lineY = rsta.yForLine(line);
if (lineY<=bottomY && (lineY+iconH>=topY)) {
int y2 = lineY + (cellHeight-iconH)/2;
ti.getIcon().paintIcon(this, g, 0, y2);
lastLine = line-1; // Paint only 1 icon per line
}
} catch (BadLocationException ble) {
ble.printStackTrace(); // Never happens
}
}
else if (line<topLine) {
break; // All other lines are above us, so quit now
}
}
}
}
}
}