本文整理汇总了Java中javax.swing.text.Caret.getMark方法的典型用法代码示例。如果您正苦于以下问题:Java Caret.getMark方法的具体用法?Java Caret.getMark怎么用?Java Caret.getMark使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.Caret
的用法示例。
在下文中一共展示了Caret.getMark方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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;
}
示例3: 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.
*/
private boolean isDragPossible(MouseEvent e) {
Object src = e.getSource();
if (src instanceof JComponent) {
JComponent comp = (JComponent) src;
boolean possible = (comp == null) ? false : (comp.getTransferHandler() != null);
if (possible && comp instanceof JTextComponent) {
JTextComponent c = (JTextComponent) comp;
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;
}
示例4: createSchedulerEvent
import javax.swing.text.Caret; //导入方法依赖的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;
}
示例5: copy
import javax.swing.text.Caret; //导入方法依赖的package包/类
private void copy() {
Caret caret = view.getCaret();
if (caret.getDot() != caret.getMark()) {
view.copy();
} else {
editorComponent.copy();
}
}
示例6: setRoundedSelectionEdges
import javax.swing.text.Caret; //导入方法依赖的package包/类
/**
* Sets whether the edges of selections are rounded in this text area.
* This method fires a property change of type
* {@link #ROUNDED_SELECTION_PROPERTY}.
*
* @param rounded Whether selection edges should be rounded.
* @see #getRoundedSelectionEdges()
*/
public void setRoundedSelectionEdges(boolean rounded) {
if (roundedSelectionEdges!=rounded) {
roundedSelectionEdges = rounded;
Caret c = getCaret();
if (c instanceof ConfigurableCaret) {
((ConfigurableCaret)c).setRoundedSelectionEdges(rounded);
if (c.getDot()!=c.getMark()) { // e.g., there's is a selection
repaint();
}
}
firePropertyChange(ROUNDED_SELECTION_PROPERTY, !rounded,
rounded);
}
}
示例7: mouseDragged
import javax.swing.text.Caret; //导入方法依赖的package包/类
@Override
public void mouseDragged(MouseEvent e) {
// WORKAROUND: Since JTextComponent only updates the caret
// location on mouse clicked and released, we'll do it on dragged
// events when the left mouse button is clicked.
if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
Caret caret = getCaret();
dot = caret.getDot();
mark = caret.getMark();
fireCaretUpdate(this);
}
}
示例8: mousePressed
import javax.swing.text.Caret; //导入方法依赖的package包/类
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) { // OS X popup triggers are on pressed
showPopup(e);
}
else if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
Caret caret = getCaret();
dot = caret.getDot();
mark = caret.getMark();
fireCaretUpdate(this);
}
}
示例9: actionPerformedImpl
import javax.swing.text.Caret; //导入方法依赖的package包/类
@Override
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
boolean beep = true;
if (textArea != null && textArea.isEditable()) {
try {
Document doc = textArea.getDocument();
Caret caret = textArea.getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
if (dot != mark) {
doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
beep = false;
}
else if (dot < doc.getLength()) {
int delChars = 1;
if (dot < doc.getLength() - 1) {
String dotChars = doc.getText(dot, 2);
char c0 = dotChars.charAt(0);
char c1 = dotChars.charAt(1);
if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
c1 >= '\uDC00' && c1 <= '\uDFFF') {
delChars = 2;
}
}
doc.remove(dot, delChars);
beep = false;
}
} catch (BadLocationException bl) {
}
}
if (beep) {
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
}
if (textArea != null) {
textArea.requestFocusInWindow();
}
}
示例10: invoke
import javax.swing.text.Caret; //导入方法依赖的package包/类
/**
* Invokes this code template. The changes are made to the given text
* area.
*
* @param textArea The text area to operate on.
* @throws BadLocationException If something bad happens.
*/
@Override
public void invoke(RSyntaxTextArea textArea) throws BadLocationException {
Caret c = textArea.getCaret();
int dot = c.getDot();
int mark = c.getMark();
int p0 = Math.min(dot, mark);
int p1 = Math.max(dot, mark);
RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument();
Element map = doc.getDefaultRootElement();
int lineNum = map.getElementIndex(dot);
Element line = map.getElement(lineNum);
int start = line.getStartOffset();
int end = line.getEndOffset()-1; // Why always "-1"?
String s = textArea.getText(start,end-start);
int len = s.length();
// endWS is the end of the leading whitespace
// of the current line.
int endWS = 0;
while (endWS<len && RSyntaxUtilities.isWhitespace(s.charAt(endWS))) {
endWS++;
}
s = s.substring(0, endWS);
p0 -= getID().length();
String beforeText = getBeforeTextIndented(s);
String afterText = getAfterTextIndented(s);
doc.replace(p0,p1-p0, beforeText+afterText, null);
textArea.setCaretPosition(p0+beforeText.length());
}
示例11: mouseDragged
import javax.swing.text.Caret; //导入方法依赖的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();
}
}
示例12: actionPerformedImpl
import javax.swing.text.Caret; //导入方法依赖的package包/类
@Override
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
if (!textArea.isEditable() || !textArea.isEnabled()) {
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
return;
}
RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
RSyntaxDocument doc = (RSyntaxDocument)rsta.getDocument();
Caret c = rsta.getCaret();
boolean selection = c.getDot()!=c.getMark();
rsta.replaceSelection("/");
// Don't automatically complete a tag if there was a selection
int dot = c.getDot();
if (doc.getLanguageIsMarkup() &&
doc.getCompleteMarkupCloseTags() &&
!selection && rsta.getCloseMarkupTags() && dot>1) {
try {
// Check actual char before token type, since it's quicker
char ch = doc.charAt(dot-2);
if (ch=='<' || ch=='[') {
Token t = doc.getTokenListForLine(
rsta.getCaretLineNumber());
t = RSyntaxUtilities.getTokenAtOffset(t, dot-1);
if (t!=null && t.getType()==Token.MARKUP_TAG_DELIMITER) {
//System.out.println("Huzzah - closing tag!");
String tagName = discoverTagName(doc, dot);
if (tagName!=null) {
rsta.replaceSelection(tagName + (char)(ch+2));
}
}
}
} catch (BadLocationException ble) { // Never happens
UIManager.getLookAndFeel().provideErrorFeedback(rsta);
ble.printStackTrace();
}
}
}
示例13: replaceImpl
import javax.swing.text.Caret; //导入方法依赖的package包/类
boolean replaceImpl(Map<String, Object> props, boolean oppositeDir, JTextComponent c) throws BadLocationException {
props = getValidFindProperties(props);
boolean back = Boolean.TRUE.equals(props.get(FIND_BACKWARD_SEARCH));
if (oppositeDir) {
back = !back;
}
boolean blockSearch = Boolean.TRUE.equals(props.get(FIND_BLOCK_SEARCH));
Position blockSearchStartPos = (Position) props.get(FIND_BLOCK_SEARCH_START);
int blockSearchStartOffset = (blockSearchStartPos != null) ? blockSearchStartPos.getOffset() : -1;
if (c != null) {
String s = (String)props.get(FIND_REPLACE_WITH);
Caret caret = c.getCaret();
if (caret.isSelectionVisible() && caret.getDot() != caret.getMark()){
Object dp = props.get(FIND_BACKWARD_SEARCH);
boolean direction = (dp != null) ? ((Boolean)dp).booleanValue() : false;
int dotPos = (oppositeDir ^ direction ? c.getSelectionEnd() : c.getSelectionStart());
c.setCaretPosition(dotPos);
}
FindReplaceResult result = findReplaceImpl(s, props, oppositeDir, c);
if (result!=null){
s = result.getReplacedString();
} else {
return false;
}
Document doc = c.getDocument();
int startOffset = c.getSelectionStart();
int len = c.getSelectionEnd() - startOffset;
DocUtils.atomicLock(doc);
try {
if (len > 0) {
doc.remove(startOffset, len);
}
if (s != null && s.length() > 0) {
try {
NavigationHistory.getEdits().markWaypoint(c, startOffset, false, true);
} catch (BadLocationException e) {
LOG.log(Level.WARNING, "Can't add position to the history of edits.", e); //NOI18N
}
doc.insertString(startOffset, s, null);
if (startOffset == blockSearchStartOffset) { // Replaced at begining of block
blockSearchStartPos = doc.createPosition(startOffset);
props.put(EditorFindSupport.FIND_BLOCK_SEARCH_START, blockSearchStartPos);
}
}
} finally {
DocUtils.atomicUnlock(doc);
if (blockSearch){
setBlockSearchHighlight(blockSearchStartOffset, getBlockEndOffset());
}
}
// adjust caret pos after replace operation
int adjustedCaretPos = (back || s == null) ? startOffset : startOffset + s.length();
caret.setDot(adjustedCaretPos);
}
return true;
}
示例14: navigateAreas
import javax.swing.text.Caret; //导入方法依赖的package包/类
private static void navigateAreas(
int [] origin,
int [] matches,
int caretOffset,
Object caretBias,
Caret caret,
boolean select
) {
if (matches != null && matches.length >= 2) {
int newDotBackwardIdx = -1;
int newDotForwardIdx = -1;
for(int i = 0; i < matches.length / 2; i++) {
if (matches[i * 2] <= origin[0] &&
(newDotBackwardIdx == -1 || matches[i * 2] > matches[newDotBackwardIdx * 2])
) {
newDotBackwardIdx = i;
}
if (matches[i * 2] >= origin[1] &&
(newDotForwardIdx == -1 || matches[i * 2] < matches[newDotForwardIdx * 2])
) {
newDotForwardIdx = i;
}
}
if (newDotBackwardIdx != -1) {
if (select) {
int set, move;
if (caretOffset < origin[1]) {
set = origin[0];
move = matches[2 * newDotBackwardIdx + 1];
} else {
set = origin[1];
move = matches[2 * newDotBackwardIdx];
}
if (caret.getDot() == caret.getMark()) { // || (move <= caret.getMark() && caret.getMark() <= set)
caret.setDot(set);
}
caret.moveDot(move);
} else {
if (B_BACKWARD.equalsIgnoreCase(caretBias.toString())) {
caret.setDot(matches[2 * newDotBackwardIdx + 1]);
} else {
caret.setDot(matches[2 * newDotBackwardIdx]);
}
}
} else if (newDotForwardIdx != -1) {
if (select) {
int set, move;
if (caretOffset > origin[0]) {
set = origin[1];
move = matches[2 * newDotForwardIdx];
} else {
set = origin[0];
move = matches[2 * newDotForwardIdx + 1];
}
if (caret.getDot() == caret.getMark()) { // || (set <= caret.getMark() && caret.getMark() <= move)
caret.setDot(set);
}
caret.moveDot(move);
} else {
if (B_BACKWARD.equalsIgnoreCase(caretBias.toString())) {
caret.setDot(matches[2 * newDotForwardIdx + 1]);
} else {
caret.setDot(matches[2 * newDotForwardIdx]);
}
}
}
}
}
示例15: computeCompletions
import javax.swing.text.Caret; //导入方法依赖的package包/类
private LinkedList<String> computeCompletions() {
LinkedList<String> result = new LinkedList<>();
Caret caret = getEditorComponent().getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
int min = Math.min(dot, mark);
int max = Math.max(dot, mark);
String content;
try {
// only do completion if the selection runs up to the end of a label
Document document = getDocument();
if (max < document.getLength()
&& Character.isLetterOrDigit(document.getText(max, 1).charAt(0))) {
return result;
}
content = document.getText(0, min);
} catch (BadLocationException exc) {
throw new IllegalStateException(String.format("Impossible error: %s", exc));
}
// Find where the label starts
int start = min;
while (start > 0 && Character.isLetterOrDigit(content.charAt(start - 1))) {
start--;
}
if (start < min) {
// Identify the root of the word to be completed
String root = content.substring(start);
SortedSet<String> tailSet = RealCellEditor.this.labels.tailSet(root);
if (!tailSet.isEmpty()) {
Iterator<String> iter = tailSet.iterator();
String nextCompletion = iter.next();
while (nextCompletion.startsWith(root)) {
result.add(nextCompletion.substring(min - start));
nextCompletion = iter.next();
}
}
}
return result;
}