本文整理汇总了Java中javax.swing.text.Caret类的典型用法代码示例。如果您正苦于以下问题:Java Caret类的具体用法?Java Caret怎么用?Java Caret使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Caret类属于javax.swing.text包,在下文中一共展示了Caret类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: actionPerformed
import javax.swing.text.Caret; //导入依赖的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();
}
}
}
}
}
示例2: insertIntoDocument
import javax.swing.text.Caret; //导入依赖的package包/类
private int insertIntoDocument(String s, JEditorPane target)
throws BadLocationException {
Document doc = target.getDocument();
if (s == null) {
s = "";
}
if (doc == null) {
return -1;
}
int start = -1;
try {
Caret caret = target.getCaret();
int p0 = Math.min(caret.getDot(), caret.getMark());
int p1 = Math.max(caret.getDot(), caret.getMark());
doc.remove(p0, p1 - p0);
start = caret.getDot();
doc.insertString(start, s + ";\n", null); // NOI18N
} catch (BadLocationException ble) {
LOGGER.log(Level.WARNING, org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "LBL_InsertAtLocationError") + ble);
}
return start;
}
示例3: substituteText
import javax.swing.text.Caret; //导入依赖的package包/类
/**
* If called with <code>SHIFT_MASK</code> modified it createa a start tag and
* end tag pair and place caret between them.
*/
public boolean substituteText( JTextComponent c, int offset, int len, int modifiers ){
String replacementText = getReplacementText(modifiers);
replaceText(c, replacementText, offset, len);
boolean shift = (modifiers & java.awt.event.InputEvent.SHIFT_MASK) != 0;
if (shift && startElement) {
Caret caret = c.getCaret(); // it is at the end of replacement
int dot = caret.getDot();
int rlen = replacementText.length();
if (empty) {
caret.setDot((dot - rlen) + replacementText.indexOf('/'));
}
}
return false;
}
示例4: releaseUI
import javax.swing.text.Caret; //导入依赖的package包/类
/**
* Ends the method selection mode, clears all used ui elements. Notifies each registered
* {@link ReleaseListener}.
*
* @param performAction <code>true</code> indicates that the current selection should
* be used to perform an action, <code>false</code> means that the selection mode
* has beencancelled
*/
public synchronized void releaseUI(boolean performAction) {
if (!isInSelectMode) {
return; // do nothing
}
getHighlightsBag(doc).clear();
editorPane.removeKeyListener(mainListener);
editorPane.removeMouseListener(mainListener);
editorPane.removeMouseMotionListener(mainListener);
editorPane.removeFocusListener(mainListener);
editorPane.putClientProperty(MethodChooser.class, null);
editorPane.setCursor(originalCursor);
Caret caret = editorPane.getCaret();
if (caret instanceof BaseCaret) {
((BaseCaret)caret).setVisible(true);
}
if (hintText != null && hintText.trim().length() > 0) {
Utilities.clearStatusText(editorPane);
}
isInSelectMode = false;
for (ReleaseListener listener : releaseListeners) {
listener.released(performAction);
}
}
示例5: actionPerformed
import javax.swing.text.Caret; //导入依赖的package包/类
public void actionPerformed(final ActionEvent evt, final JTextComponent target) {
if (target != null) {
final Caret caret = target.getCaret();
final BaseDocument doc = (BaseDocument)target.getDocument();
doc.render(new Runnable () {
public void run () {
DocumentUtilities.setTypingModification(doc, true);
try {
int dotPos = caret.getDot();
int bolPos = Utilities.getRowStart(target, dotPos);
int eolPos = Utilities.getRowEnd(target, dotPos);
eolPos = Math.min(eolPos + 1, doc.getLength()); // include '\n'
caret.setDot(bolPos);
caret.moveDot(eolPos);
} catch (BadLocationException e) {
target.getToolkit().beep();
} finally {
DocumentUtilities.setTypingModification(doc, false);
}
}
});
}
}
示例6: actionPerformed
import javax.swing.text.Caret; //导入依赖的package包/类
@Override
public void actionPerformed(ActionEvent evt, JTextComponent target) {
if (target != null) {
Caret caret = target.getCaret();
BookmarkList bookmarkList = BookmarkList.get(target.getDocument());
int dotOffset = caret.getDot();
Bookmark bookmark = gotoNext
? bookmarkList.getNextBookmark(dotOffset, true) // next (wrap)
: bookmarkList.getPreviousBookmark(dotOffset, true); // previous (wrap)
if (bookmark != null) {
if (select) {
caret.moveDot(bookmark.getOffset());
} else {
caret.setDot(bookmark.getOffset());
}
}
}
}
示例7: actionPerformed
import javax.swing.text.Caret; //导入依赖的package包/类
public void actionPerformed(ActionEvent evt, JTextComponent target) {
if (target != null) {
Caret caret = target.getCaret();
try {
int pos = Utilities.getRowFirstNonWhite((BaseDocument)target.getDocument(),
caret.getDot());
if (pos >= 0) {
boolean select = BaseKit.selectionFirstNonWhiteAction.equals(getValue(Action.NAME));
if (select) {
caret.moveDot(pos);
} else {
caret.setDot(pos);
}
}
} catch (BadLocationException e) {
target.getToolkit().beep();
}
}
}
示例8: isDragPossible
import javax.swing.text.Caret; //导入依赖的package包/类
/**
* Determines if the following are true:
* <ul>
* <li>the press event is located over a selection
* <li>the dragEnabled property is true
* <li>A TranferHandler is installed
* </ul>
* <p>
* This is implemented to check for a TransferHandler.
* Subclasses should perform the remaining conditions.
*/
protected boolean isDragPossible(MouseEvent e) {
JComponent comp = getEventComponent(e);
boolean possible = (comp == null) ? false : (comp.getTransferHandler() != null);
if (possible) {
JTextComponent c = (JTextComponent) getEventComponent(e);
if (c.getDragEnabled()) {
Caret caret = c.getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
if (dot != mark) {
Point p = new Point(e.getX(), e.getY());
int pos = c.viewToModel(p);
int p0 = Math.min(dot, mark);
int p1 = Math.max(dot, mark);
if ((pos >= p0) && (pos < p1)) {
return true;
}
}
}
}
return false;
}
示例9: getSelectionOrIdentifier
import javax.swing.text.Caret; //导入依赖的package包/类
/** Get the selection if there's any or get the identifier around
* the position if there's no selection.
*/
public static String getSelectionOrIdentifier(JTextComponent c, int offset)
throws BadLocationException {
Document doc = c.getDocument();
Caret caret = c.getCaret();
String ret;
if (Utilities.isSelectionShowing(caret)) {
ret = c.getSelectedText();
if (ret != null) return ret;
}
if (doc instanceof BaseDocument){
ret = getIdentifier((BaseDocument) doc, offset);
} else {
ret = getWord(c, offset);
}
return ret;
}
示例10: gotoDeclaration
import javax.swing.text.Caret; //导入依赖的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;
}
示例11: actionPerformed
import javax.swing.text.Caret; //导入依赖的package包/类
public @Override void actionPerformed(ActionEvent evt, JTextComponent target) {
String cmd = evt.getActionCommand();
int mod = evt.getModifiers();
// Dirty fix for Completion shortcut on Unix !!!
if (cmd != null && cmd.equals(" ") && (mod == ActionEvent.CTRL_MASK)) { // NOI18N
// Ctrl + SPACE
} else {
Caret caret = target.getCaret();
if (caret instanceof ExtCaret) {
((ExtCaret)caret).requestMatchBraceUpdateSync(); // synced bracket update
}
super.actionPerformed(evt, target);
}
if ((target != null) && (evt != null)) {
if ((cmd != null) && (cmd.length() == 1)) {
// Check whether char that should reindent the line was inserted
checkIndentHotChars(target, cmd);
// Check the completion
checkCompletion(target, cmd);
}
}
}
示例12: getMostRecentLine
import javax.swing.text.Caret; //导入依赖的package包/类
/**
* Get the line of the caret in the most recent editor.
* This returns the current line in the current editor if there's one,
* or a line of the caret in the editor, that was most recently active.
* @return the line or <code>null</code> when there was no recent active editor.
*/
public Line getMostRecentLine() {
EditorCookie e = getMostRecentEditorCookie ();
if (e == null) return null;
JEditorPane ep = getMostRecentEditor ();
if (ep == null) return null;
StyledDocument d = e.getDocument ();
if (d == null) return null;
Caret caret = ep.getCaret ();
if (caret == null) return null;
int lineNumber = NbDocument.findLineNumber(d, caret.getDot());
Line.Set lineSet = e.getLineSet();
try {
return lineSet.getCurrent(lineNumber);
} catch (IndexOutOfBoundsException ex) {
return null;
}
}
示例13: updateActiveCaret
import javax.swing.text.Caret; //导入依赖的package包/类
private void updateActiveCaret() {
if (visible) {
setVisible(false);
}
if (editorCaret != null) {
editorCaret.removeEditorCaretListener(weakEditorCaretListener);
weakEditorCaretListener = null;
editorCaret = null;
docText = null;
}
Caret caret = component.getCaret();
if (caret instanceof EditorCaret) { // Only work for editor caret
editorCaret = (EditorCaret) caret;
}
if (editorCaret != null) {
updateDocText();
weakEditorCaretListener = WeakListeners.create(EditorCaretListener.class, this, editorCaret);
editorCaret.addEditorCaretListener(weakEditorCaretListener);
}
}
示例14: handleTransfer
import javax.swing.text.Caret; //导入依赖的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;
}
示例15: computeSelIndex
import javax.swing.text.Caret; //导入依赖的package包/类
private int computeSelIndex(boolean inner) {
Caret caret = target.getCaret();
if (selectionInfos != null && caret != null && caret.getDot() != caret.getMark()) {
int dot = caret.getDot();
int mark = caret.getMark();
int start = Math.min(dot,mark);
//int end = Math.max(dot,mark);
for (int i = 0; i < selectionInfos.length; i++) {
if (selectionInfos[i].getStartOffset() == start) {
// TODO - check end offset too
return i;
}
}
// No exact match - look at the editor selection and find the range
// that most closely surround the selection (if inner is true, go
// for the inner one, otherwise the outer)
for (int i = selectionInfos.length-2; i >= 0; i--) {
if (selectionInfos[i].getStartOffset() > start &&
selectionInfos[i+1].getStartOffset() < start) {
return inner ? i : i-1;
}
}
}
return selIndex;
}