本文整理汇总了Java中javax.swing.text.JTextComponent.getCaret方法的典型用法代码示例。如果您正苦于以下问题:Java JTextComponent.getCaret方法的具体用法?Java JTextComponent.getCaret怎么用?Java JTextComponent.getCaret使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.JTextComponent
的用法示例。
在下文中一共展示了JTextComponent.getCaret方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
public void actionPerformed(ActionEvent evt) {
NavigationHistory history = NavigationHistory.getNavigations();
if (null == history.getCurrentWaypoint()) {
// Haven't navigated back yet
JTextComponent c = componentRef.get();
JTextComponent target = c != null ? c : getTextComponent(evt);
if (target != null && target.getCaret() != null) {
try {
history.markWaypoint(target, target.getCaret().getDot(), true, false);
} catch (BadLocationException ble) {
LOG.log(Level.WARNING, "Can't mark current position", ble); //NOI18N
}
}
}
NavigationHistory.Waypoint wpt = waypoint != null ?
history.navigateTo(waypoint) : history.navigateBack();
if (wpt != null) {
show(wpt);
}
}
示例2: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public void actionPerformed(ActionEvent evt, JTextComponent target) {
if (target != null) {
Caret caret = target.getCaret();
try {
int newDotOffset = getNextWordOffset(target);
boolean select = selectionNextWordAction.equals(getValue(Action.NAME));
if (select) {
if (caret instanceof EditorCaret && RectangularSelectionUtils.isRectangularSelection(target)) {
RectangularSelectionCaretAccessor.get().extendRectangularSelection((EditorCaret)caret, true, true);
} else {
caret.moveDot(newDotOffset);
}
} else {
caret.setDot(newDotOffset);
}
} catch (BadLocationException ex) {
target.getToolkit().beep();
}
}
}
示例3: gotoDeclaration
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public boolean gotoDeclaration(JTextComponent target) {
BaseDocument doc = Utilities.getDocument(target);
if (doc == null)
return false;
try {
Caret caret = target.getCaret();
int dotPos = caret.getDot();
int[] idBlk = Utilities.getIdentifierBlock(doc, dotPos);
ExtSyntaxSupport extSup = (ExtSyntaxSupport)doc.getSyntaxSupport();
if (idBlk != null) {
int decPos = extSup.findDeclarationPosition(doc.getText(idBlk), idBlk[1]);
if (decPos >= 0) {
caret.setDot(decPos);
return true;
}
}
} catch (BadLocationException e) {
}
return false;
}
示例4: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
public void actionPerformed(ActionEvent e) {
if (isVisible()) {
JTextComponent comp = ac.getTextComponent();
Caret c = comp.getCaret();
int dot = c.getDot();
if (dot < comp.getDocument().getLength()) {
c.setDot(++dot);
// Ensure moving right hasn't moved us up a line, thus
// hiding the popup window.
if (comp.isVisible()) {
if (lastLine!=-1) {
doAutocomplete();
}
}
}
}
}
示例5: handleTransfer
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public boolean handleTransfer(JTextComponent targetComponent) {
if (targetComponent == null)
return false;
try {
Document doc = targetComponent.getDocument();
Caret caret = targetComponent.getCaret();
int p0 = Math.min(caret.getDot(), caret.getMark());
int p1 = Math.max(caret.getDot(), caret.getMark());
doc.remove(p0, p1 - p0);
//replace selected text by the inserted one
int start = caret.getDot();
doc.insertString(start, body, null);
}
catch (BadLocationException ble) {
return false;
}
return true;
}
示例6: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public void actionPerformed(ActionEvent evt, JTextComponent target) {
if (target == null) return;
if (!target.isEditable() || !target.isEnabled()) {
problem(null);
return;
}
Caret caret = target.getCaret();
BaseDocument doc = (BaseDocument)target.getDocument();
try {
doc.dump(System.out);
if (target == null) throw new BadLocationException(null,0); // folish compiler
} catch (BadLocationException e) {
problem(null);
}
}
示例7: moveToNewOffset
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
protected void moveToNewOffset(JTextComponent target, int offset, int length) throws BadLocationException {
Caret caret = target.getCaret();
if (caret instanceof BaseCaret && RectangularSelectionUtils.isRectangularSelection(target)) {
((BaseCaret) caret).extendRectangularSelection(false, true);
} else {
target.getCaret().moveDot(offset);
}
}
示例8: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent evt, JTextComponent target) {
if (target != null) {
Caret caret = target.getCaret();
if (Utilities.isSelectionShowing(caret)) {
EditorFindSupport findSupport = EditorFindSupport.getInstance();
HashMap<String, Object> props = new HashMap<>(findSupport.createDefaultFindProperties());
String searchWord = target.getSelectedText();
int n = searchWord.indexOf('\n');
if (n >= 0) {
searchWord = searchWord.substring(0, n);
}
props.put(EditorFindSupport.FIND_WHAT, searchWord);
props.put(EditorFindSupport.ADD_MULTICARET, Boolean.TRUE);
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);
props.put(EditorFindSupport.ADD_MULTICARET, Boolean.FALSE);
findSupport.putFindProperties(props);
} else {
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);
}
}
}
}
示例9: getSelectionOrIdentifierBlock
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/** Get the selection if there's any or get the identifier around
* the position if there's no selection.
* @param c component to work with
* @param offset position in document - usually the caret.getDot()
* @return the block (starting and ending position) enclosing the identifier
* or null if no identifier was found
*/
public static int[] getSelectionOrIdentifierBlock(JTextComponent c, int offset)
throws BadLocationException {
Document doc = c.getDocument();
Caret caret = c.getCaret();
int[] ret;
if (Utilities.isSelectionShowing(caret)) {
ret = new int[] { c.getSelectionStart(), c.getSelectionEnd() };
} else if (doc instanceof BaseDocument){
ret = getIdentifierBlock((BaseDocument)doc, offset);
} else {
ret = getIdentifierBlock(c, offset);
}
return ret;
}
示例10: isAbbrevDisabled
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Test whether the abbreviation expansion is disabled
* at the caret position in the given component's document.
*
* @param component non-null text component.
* @return true if the abbreviation can be expanded or false if not.
*/
public static boolean isAbbrevDisabled(JTextComponent component) {
Document doc = component.getDocument();
if (doc instanceof BaseDocument) {
BaseDocument bdoc = (BaseDocument)doc;
SyntaxSupport sup = bdoc.getSyntaxSupport();
if (sup != null) {
Caret caret = component.getCaret();
return sup.isAbbrevDisabled(caret != null ? caret.getDot() : 0);
}
}
return false; // abbrev not disabled
}
示例11: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private static void actionPerformed(JTextComponent target) {
if (target != null) {
if (org.netbeans.editor.Utilities.getEditorUI(target).isGlyphGutterVisible()) {
Caret caret = target.getCaret();
BookmarkList bookmarkList = BookmarkList.get(target.getDocument());
bookmarkList.toggleLineBookmark(caret.getDot());
} else { // Glyph gutter not visible -> just beep
target.getToolkit().beep();
}
}
}
示例12: createSchedulerEvent
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
protected SchedulerEvent createSchedulerEvent (SourceModificationEvent event) {
final JTextComponent ce = currentEditor;
final Caret caret = ce != null ? ce.getCaret() : null;
final Source s = getSource();
if (event.getModifiedSource() == s && caret != null) {
return new CursorMovedSchedulerEvent(this, caret.getDot(), caret.getMark()) { };
}
return null;
}
示例13: getMagicX
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
static double getMagicX(DocumentView docView, EditorView view, int offset, Bias bias, Shape alloc) {
JTextComponent textComponent = docView.getTextComponent();
if (textComponent == null) {
return 0d;
}
Caret caret = textComponent.getCaret();
Point magicCaretPoint = null;
if(caret != null) {
if(caret instanceof EditorCaret) {
EditorCaret editorCaret = (EditorCaret) caret;
CaretInfo info = editorCaret.getCaretAt(offset);
magicCaretPoint = (info != null) ? info.getMagicCaretPosition() : null;
} else {
magicCaretPoint = caret.getMagicCaretPosition();
}
}
double x;
if (magicCaretPoint == null) {
Shape offsetBounds = view.modelToViewChecked(offset, alloc, bias);
if (offsetBounds == null) {
x = 0d;
} else {
x = offsetBounds.getBounds2D().getX();
}
} else {
x = magicCaretPoint.x;
}
return x;
}
示例14: setDropLocation
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public void setDropLocation(JTextComponent targetComponent) {
Caret caret = targetComponent.getCaret();
setStartPosition(Math.min(caret.getDot(), caret.getMark()));
setEndPosition(Math.max(caret.getDot(), caret.getMark()));
}
示例15: mouseDragged
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public @Override void mouseDragged(MouseEvent e) {
EditorUI eui = editorUI;
if (eui == null) {
return;
}
JTextComponent component = eui.getComponent();
BaseTextUI textUI = (BaseTextUI)component.getUI();
AbstractDocument aDoc = (AbstractDocument)component.getDocument();
aDoc.readLock();
try {
// The drag must be extended to a next line in order to perform any selection
int lineStartOffset = textUI.getPosFromY(e.getY());
boolean updateDragEndOffset = false;
if (dragStartOffset == -1) { // Drag starts now
dragStartOffset = lineStartOffset;
dragEndOffset = lineStartOffset;
} else if (dragStartOffset == dragEndOffset) {
if (lineStartOffset != dragStartOffset) {
updateDragEndOffset = true;
}
} else {
updateDragEndOffset = true;
}
if (updateDragEndOffset) {
// Extend selection to active line's end or begining depending on dragStartOffset
Caret caret = component.getCaret();
if (lineStartOffset >= dragStartOffset) {
if (caret.getMark() != dragStartOffset) {
caret.setDot(dragStartOffset);
}
// Check if the sele
// Extend to next line's begining
dragEndOffset = Math.min(Utilities.getRowEnd((BaseDocument) aDoc, lineStartOffset) + 1, aDoc.getLength());
} else { // Backward selection
// Check if the selection is already reverted i.e. it starts at dragStartOffset's line end
if (caret.getMark() == dragStartOffset) {
caret.setDot(Utilities.getRowEnd((BaseDocument)aDoc, dragStartOffset) + 1);
}
dragEndOffset = lineStartOffset;
}
component.moveCaretPosition(dragEndOffset);
}
} catch (BadLocationException ble) {
// Ignore rather than notify
} finally {
aDoc.readUnlock();
}
}