本文整理汇总了Java中javax.swing.text.JTextComponent.getCaretPosition方法的典型用法代码示例。如果您正苦于以下问题:Java JTextComponent.getCaretPosition方法的具体用法?Java JTextComponent.getCaretPosition怎么用?Java JTextComponent.getCaretPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.JTextComponent
的用法示例。
在下文中一共展示了JTextComponent.getCaretPosition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCurrentParameterHighlight
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Returns the highlight of the current parameter.
*
* @return The current parameter's highlight, or <code>null</code> if
* the caret is not in a parameter's bounds.
* @see #getCurrentParameterStartOffset()
*/
private Highlight getCurrentParameterHighlight() {
JTextComponent tc = ac.getTextComponent();
int dot = tc.getCaretPosition();
if (dot>0) {
dot--; // Workaround for Java Highlight issues
}
List<Highlight> paramHighlights = getParameterHighlights();
for (Highlight h : paramHighlights) {
if (dot>=h.getStartOffset() && dot<h.getEndOffset()) {
return h;
}
}
return null;
}
示例2: reindent
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private void reindent(JTextComponent component) {
final BaseDocument doc = (BaseDocument) component.getDocument();
final int dotPos = component.getCaretPosition();
final Indent indent = Indent.get(doc);
indent.lock();
try {
doc.runAtomic(new Runnable() {
@Override
public void run() {
try {
int startOffset = Utilities.getRowStart(doc, dotPos);
int endOffset = Utilities.getRowEnd(doc, dotPos);
indent.reindent(startOffset, endOffset);
} catch (BadLocationException ex) {
//ignore
}
}
});
} finally {
indent.unlock();
}
}
示例3: TrailingWhitespaceRemoveProcessor
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public TrailingWhitespaceRemoveProcessor(Document doc, boolean removeFromModifiedLinesOnly, AtomicBoolean canceled) {
this.doc = doc;
this.removeFromModifiedLinesOnly = removeFromModifiedLinesOnly;
this.canceled = canceled;
this.docText = DocumentUtilities.getText(doc); // Persists for doc's lifetime
lineRootElement = DocumentUtilities.getParagraphRootElement(doc);
modRootElement = ModRootElement.get(doc);
JTextComponent lastFocusedComponent = EditorRegistry.lastFocusedComponent();
if (lastFocusedComponent != null && lastFocusedComponent.getDocument() == doc && !REMOVE_WHITESPACE_ON_CURRENT_LINE) {
int caretOffset = lastFocusedComponent.getCaretPosition();
caretLineIndex = lineRootElement.getElementIndex(caretOffset);
// Assign the relativeCaretOffset since the subsequent modifications
// done by physical whitespace removal would make the absolute offsets unusable.
caretRelativeOffset = caretOffset - lineRootElement.getElement(caretLineIndex).getStartOffset();
} else {
caretLineIndex = -1;
caretRelativeOffset = 0;
}
}
示例4: isEnabled
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public boolean isEnabled() {
JTextComponent comp = getTextComponent(null);
if (comp == null)
return false;
ASTNode node = getASTNode(comp);
if (node == null)
return false;
int position = comp.getCaretPosition();
ASTPath path = node.findPath(position);
if (path == null)
return false;
DatabaseContext root = DatabaseManager.getRoot((ASTNode) path.getRoot());
if (root == null)
return false;
DatabaseItem item = root.getDatabaseItem (path.getLeaf ().getOffset ());
return item != null;
}
示例5: reindent
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private void reindent(JTextComponent component) {
final BaseDocument doc = (BaseDocument) component.getDocument();
final int dotPos = component.getCaretPosition();
final Indent indent = Indent.get(doc);
indent.lock();
try {
doc.runAtomic(new Runnable() {
@Override
public void run() {
try {
int startOffset = Utilities.getRowStart(doc, dotPos);
int endOffset = Utilities.getRowEnd(doc, dotPos);
indent.reindent(startOffset, endOffset);
} catch (BadLocationException ex) {
//ignore
}
}
});
} finally {
indent.unlock();
}
}
示例6: previousCamelCasePosition
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
static int previousCamelCasePosition(JTextComponent textComponent) {
int offset = textComponent.getCaretPosition();
// Are we at the beginning of the document?
if (offset == 0) {
return -1;
}
final Document doc = textComponent.getDocument();
KeystrokeHandler bc = UiUtils.getBracketCompletion(doc, offset);
if (bc != null) {
int nextOffset = bc.getNextWordOffset(
doc, offset, true);
if (nextOffset != -1) {
return nextOffset;
}
}
try {
return Utilities.getPreviousWord(textComponent, offset);
} catch (BadLocationException ble) {
ErrorManager.getDefault().notify(ble);
}
return -1;
}
示例7: defaultAction
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
public void defaultAction(JTextComponent component) {
if (component == null) {
return;
}
Completion.get().hideAll();
Document doc = component.getDocument();
if (doc == null) {
return;
}
int caret = component.getCaretPosition();
int l = caret - fromOffset;
try {
doc.insertString(fromOffset, "/" + command + " ", null); // NOI18N
doc.remove(fromOffset + command.length() + 2, l);
} catch (BadLocationException ex) {
// ignore
}
}
示例8: delegates
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
protected boolean delegates(JTextComponent target) {
ConsoleModel model = ConsoleModel.get(target.getDocument());
if (model == null) {
return true;
}
ConsoleSection input = model.getInputSection();
if (input == null) {
return true;
}
int offset = target.getCaretPosition();
if (offset < input.getPartBegin() || offset > input.getEnd()) {
return true;
}
Document doc = target.getDocument();
LineDocument ld = LineDocumentUtils.as(doc, LineDocument.class);
if (ld == null) {
return true;
}
try {
int end = LineDocumentUtils.getLineEnd(ld, input.getPartBegin());
return offset > end;
} catch (BadLocationException ex) {
return true;
}
}
示例9: isFirstJavaLine
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
static int isFirstJavaLine(JTextComponent component) {
ShellSession s = ShellSession.get(component.getDocument());
if (s == null) {
return -1;
}
ConsoleSection sec = s.getModel().getInputSection();
if (sec == null) {
return -1;
}
LineDocument ld = LineDocumentUtils.as(component.getDocument(), LineDocument.class);
if (ld == null) {
return -1;
}
int off = sec.getStart();
int caret = component.getCaretPosition();
int s1 = LineDocumentUtils.getLineStart(ld, caret);
int s2 = LineDocumentUtils.getLineStart(ld, off);
try {
return s1 == s2 ?
component.getDocument().getText(sec.getPartBegin(), sec.getPartLen()).trim().length()
: -1;
} catch (BadLocationException ex) {
return 0;
}
}
示例10: enterAction
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
boolean enterAction() {
TextSync textSync = activeTextSync();
if (textSync != null) {
TextRegion<?> master = textSync.validMasterRegion();
JTextComponent component = component();
if (master.startOffset() <= component.getCaretPosition() && component.getCaretPosition() <= master.endOffset()) {
TextSyncGroup<?> group = textSync.group();
activateTextSync(null, group,
findEditableTextSyncIndex(group, group.activeTextSyncIndex() + 1, +1, false, false),
true);
return true;
}
releaseLastGroups(1);
} else {
// #145443 - I'm not sure why this is called when there is no active
// TextSync, but apparently it happens under certain circumstances. So
// let's try closing all active groups, which will terminate the special editing mode.
releaseLastGroups(editGroups.size());
}
return false;
}
示例11: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
public void actionPerformed(ActionEvent e) {
JTextComponent tc = ac.getTextComponent();
int dot = tc.getCaretPosition();
char end = pc.getProvider().getParameterListEnd();
// Are they at or past the end of the parameters?
if (dot>=maxPos.getOffset()-2) { // ">=" for overwrite mode
// Try to decide if we're closing a paren that is a part
// of the (last) arg being typed.
String text = getArgumentText(dot);
if (text!=null) {
char start = pc.getProvider().getParameterListStart();
int startCount = getCount(text, start);
int endCount = getCount(text, end);
if (startCount>endCount) { // Just closing a paren
tc.replaceSelection(Character.toString(end));
return;
}
}
//tc.setCaretPosition(maxPos.getOffset());
tc.setCaretPosition(Math.min(tc.getCaretPosition()+1,
tc.getDocument().getLength()));
deactivate();
}
// If not (in the middle of parameters), just insert the paren.
else {
tc.replaceSelection(Character.toString(end));
}
}
示例12: resetRectangularSelection
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public static void resetRectangularSelection(JTextComponent c) {
c.getCaretPosition();
c.putClientProperty(RECTANGULAR_SELECTION_REGIONS_PROPERTY, new ArrayList<Position>());
boolean value = !isRectangularSelection(c);
RectangularSelectionUtils.setRectangularSelection(c, Boolean.valueOf(value) );
RectangularSelectionUtils.setRectangularSelection(c, Boolean.valueOf(!value));
}
示例13: navigateToOccurence
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private static void navigateToOccurence(boolean next, JTextComponent txt) {
if (txt != null && txt.getDocument() != null) {
Document doc = txt.getDocument();
int position = txt.getCaretPosition();
int goTo = findOccurrencePosition(next, doc, position);
if (goTo > 0) {
txt.setCaretPosition(goTo);
doc.putProperty(markedOccurence, new long[] {DocumentUtilities.getDocumentVersion(doc), goTo});
} else {
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(GoToMarkOccurrencesAction.class, "java-no-marked-occurrence"));
doc.putProperty(markedOccurence, null);
}
}
}
示例14: getLineNumberForIndex
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
static int getLineNumberForIndex(final Accessible a, int index) {
final Accessible sa = CAccessible.getSwingAccessible(a);
if (!(sa instanceof JTextComponent)) return -1;
final JTextComponent jc = (JTextComponent) sa;
final Element root = jc.getDocument().getDefaultRootElement();
// treat -1 special, returns the current caret position
if (index == -1) index = jc.getCaretPosition();
// Determine line number (can be -1)
return root.getElementIndex(index);
}
示例15: navigateToOccurence
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private static void navigateToOccurence(boolean next, JTextComponent txt) {
if (txt != null && txt.getDocument() != null) {
Document doc = txt.getDocument();
int position = txt.getCaretPosition();
int goTo = findOccurrencePosition(next, doc, position);
if (goTo > 0) {
txt.setCaretPosition(goTo);
} else {
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(GoToMarkOccurrencesAction.class, "csl-no-marked-occurrence"));
}
}
}