当前位置: 首页>>代码示例>>Java>>正文


Java Caret.getMagicCaretPosition方法代码示例

本文整理汇总了Java中javax.swing.text.Caret.getMagicCaretPosition方法的典型用法代码示例。如果您正苦于以下问题:Java Caret.getMagicCaretPosition方法的具体用法?Java Caret.getMagicCaretPosition怎么用?Java Caret.getMagicCaretPosition使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.swing.text.Caret的用法示例。


在下文中一共展示了Caret.getMagicCaretPosition方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getMagicX

import javax.swing.text.Caret; //导入方法依赖的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;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:30,代码来源:HighlightsViewUtils.java

示例2: getNextVisualPositionFrom

import javax.swing.text.Caret; //导入方法依赖的package包/类
public @Override int getNextVisualPositionFrom(
       int pos, Position.Bias b, Shape a, 
       int direction, Position.Bias[] biasRet
   ) throws BadLocationException {
biasRet[0] = Position.Bias.Forward;
switch (direction) {
case NORTH:
case SOUTH:
{
    JTextComponent target = (JTextComponent) getContainer();
    Caret c = (target != null) ? target.getCaret() : null;
    // YECK! Ideally, the x location from the magic caret position
    // would be passed in.
    Point mcp;
    if (c != null) {
	mcp = c.getMagicCaretPosition();
    }
    else {
	mcp = null;
    }
    int x;
    if (mcp == null) {
	Rectangle loc = target.modelToView(pos);
	x = (loc == null) ? 0 : loc.x;
    }
    else {
	x = mcp.x;
    }
    if (direction == NORTH) {
	pos = Utilities.getPositionAbove(target, pos, x);
    }
    else {
	pos = Utilities.getPositionBelow(target, pos, x);
    }
}
    break;
case WEST:
    if(pos == -1) {
	pos = Math.max(0, getStartOffset());
    }
    else {
               if (b == Position.Bias.Backward){
                   pos = Math.max(0, getStartOffset());
               }else{
                   pos = Math.max(0, getStartOffset() - 1);
               }
    }
    break;
case EAST:
    if(pos == -1) {
	pos = getStartOffset();
    }
    else {
	pos = Math.min(getEndOffset(), getDocument().getLength());
               //JTextComponent target = (JTextComponent) getContainer();
               //if (target!=null && Utilities.getRowEnd(target, pos) == pos) pos = Math.min(pos+1, getDocument().getLength());
    }
    break;
default:
    throw new IllegalArgumentException("Bad direction: " + direction); // NOI18N
}
return pos;
   }
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:64,代码来源:CollapsedView.java

示例3: filterOutOfSection

import javax.swing.text.Caret; //导入方法依赖的package包/类
private boolean filterOutOfSection(ConsoleSection s, FilterBypass fb, int dot, Position.Bias bias, boolean setOrMove) throws BadLocationException {
    Caret c = fb.getCaret();
    Point magPosition = c.getMagicCaretPosition();
    int curPos = c.getDot();
    if (magPosition == null) {
        // some other action that move-up / move-down, i.e. start of document / end document, navigation
        if (curPos == s.getPartBegin()|| curPos == s.getEnd()) {
            return true;
        }
        int nDot;
        if (dot < s.getPartBegin()&& curPos != s.getStart()) {
            nDot = s.getPartBegin();
        } else if (dot > s.getEnd() && curPos != s.getEnd()) {
            nDot = s.getEnd();
        } else {
            return true;
        }
        setMoveDot(fb, nDot, bias, setOrMove);
        return false;
    }
    
    // magicPosition is set, so the move is accross the lines (in Y axis)
    LineDocument ld = LineDocumentUtils.as(lastPane.getDocument(), LineDocument.class);
    if (ld == null) {
        return true;
    }
    int curLine =  LineDocumentUtils.getLineStart(ld, c.getDot());
    int ref;
    if (dot < s.getStart()) {
        // measure Y; get X from magicPosition.
        ref  = LineDocumentUtils.getLineStart(ld, s.getStart());
    } else if (dot >= s.getEnd()) {
        ref  = LineDocumentUtils.getLineStart(ld, s.getEnd());
    } else {
        return true;
    }
    if (curLine == ref) {
        return true;
    }
    Rectangle rect = lastPane.getUI().modelToView(lastPane, ref);
    rect.x = magPosition.x;
    int pos = lastPane.getUI().viewToModel(lastPane, rect.getLocation());
    if (pos < 0) {
        return true;
    }
    // I want to avoid positioning into the prompts:
    for (Rng range : s.getPartRanges()) {
        if (range.start > pos) {
            pos = range.start;
            break;
        }
        if (range.end >= pos) {
            break;
        }
    }
    setMoveDot(fb, pos, bias, setOrMove);
    return false;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:59,代码来源:ConsoleEditor.java

示例4: actionPerformedImpl

import javax.swing.text.Caret; //导入方法依赖的package包/类
@Override
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

	Rectangle visible = textArea.getVisibleRect();
	Rectangle newVis = new Rectangle(visible);
	int selectedIndex = textArea.getCaretPosition();
	int scrollAmount = textArea.getScrollableBlockIncrement(
					visible, SwingConstants.VERTICAL, direction);
	int initialY = visible.y;
	Caret caret = textArea.getCaret();
	Point magicPosition = caret.getMagicCaretPosition();
	int yOffset;

	if (selectedIndex!=-1) {

		try {

			Rectangle dotBounds = textArea.modelToView(selectedIndex);
			int x = (magicPosition != null) ? magicPosition.x :
										dotBounds.x;
			int h = dotBounds.height;
			yOffset = direction *
					((int)Math.ceil(scrollAmount/(double)h)-1)*h;
			newVis.y = constrainY(textArea, initialY+yOffset, yOffset, visible.height);
			int newIndex;

			if (visible.contains(dotBounds.x, dotBounds.y)) {
				// Dot is currently visible, base the new
				// location off the old, or
				newIndex = textArea.viewToModel(
							new Point(x, constrainY(textArea,
								dotBounds.y + yOffset, 0, 0)));
								}
			else {
				// Dot isn't visible, choose the top or the bottom
				// for the new location.
				if (direction == -1) {
					newIndex = textArea.viewToModel(new Point(
											x, newVis.y));
				}
				else {
					newIndex = textArea.viewToModel(new Point(
							x, newVis.y + visible.height));
				}
			}
			newIndex = constrainOffset(textArea, newIndex);
			if (newIndex != selectedIndex) {
				// Make sure the new visible location contains
				// the location of dot, otherwise Caret will
				// cause an additional scroll.
				adjustScrollIfNecessary(textArea, newVis, initialY,
									newIndex);
				if (select) {
					textArea.moveCaretPosition(newIndex);
				}
				else {
					textArea.setCaretPosition(newIndex);
				}
			}

		} catch (BadLocationException ble) { }

	} // End of if (selectedIndex!=-1).

	else {
		yOffset = direction * scrollAmount;
		newVis.y = constrainY(textArea, initialY + yOffset, yOffset, visible.height);
	}

	if (magicPosition != null) {
		caret.setMagicCaretPosition(magicPosition);
	}

	textArea.scrollRectToVisible(newVis);
}
 
开发者ID:Thecarisma,项目名称:powertext,代码行数:76,代码来源:RTextAreaEditorKit.java


注:本文中的javax.swing.text.Caret.getMagicCaretPosition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。