本文整理汇总了Java中javax.swing.text.JTextComponent.modelToView方法的典型用法代码示例。如果您正苦于以下问题:Java JTextComponent.modelToView方法的具体用法?Java JTextComponent.modelToView怎么用?Java JTextComponent.modelToView使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.JTextComponent
的用法示例。
在下文中一共展示了JTextComponent.modelToView方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getOffsetForPoint
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private static int getOffsetForPoint(Point p, JTextComponent c, BaseDocument doc) throws BadLocationException {
if (p.x >= 0 && p.y >= 0) {
int offset = c.viewToModel(p);
Rectangle r = c.modelToView(offset);
EditorUI eui = Utilities.getEditorUI(c);
// Check that p is on a line with text and not completely below text,
// ie. behind EOF.
int relY = p.y - r.y;
if (eui != null && relY < eui.getLineHeight()) {
// Check that p is on a line with text before its EOL.
if (offset < Utilities.getRowEnd(doc, offset)) {
return offset;
}
}
}
return -1;
}
示例2: scrollToText
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Scrolls the specified text component so the text between the starting and ending index are visible.
*/
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
try {
Rectangle startingRectangle = textComponent.modelToView(startingIndex);
Rectangle endDingRectangle = textComponent.modelToView(endingIndex);
Rectangle totalBounds = startingRectangle.union(endDingRectangle);
textComponent.scrollRectToVisible(totalBounds);
textComponent.repaint();
} catch (BadLocationException e) {
e.printStackTrace();
}
}
示例3: adjustRectangularSelectionMouseX
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private void adjustRectangularSelectionMouseX(int x, int y) {
if (!rectangularSelection) {
return;
}
JTextComponent c = component;
int offset = c.viewToModel(new Point(x, y));
Rectangle r = null;;
if (offset >= 0) {
try {
r = c.modelToView(offset);
} catch (BadLocationException ex) {
r = null;
}
}
if (r != null) {
float xDiff = x - r.x;
if (xDiff > 0) {
float charWidth;
LockedViewHierarchy lvh = ViewHierarchy.get(c).lock();
try {
charWidth = lvh.getDefaultCharWidth();
} finally {
lvh.unlock();
}
int n = (int) (xDiff / charWidth);
r.x += n * charWidth;
r.width = (int) charWidth;
}
rsDotRect.x = r.x;
rsDotRect.width = r.width;
updateRectangularSelectionPaintRect();
fireStateChanged();
}
}
示例4: updateRectangularUpDownSelection
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public void updateRectangularUpDownSelection() {
JTextComponent c = component;
int dotOffset = getDot();
try {
Rectangle r = c.modelToView(dotOffset);
rsDotRect.y = r.y;
rsDotRect.height = r.height;
} catch (BadLocationException ex) {
// Leave rsDotRect unchanged
}
}
示例5: updateRectangularUpDownSelection
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
*
*/
void updateRectangularUpDownSelection() {
JTextComponent c = component;
int dotOffset = getDot();
try {
Rectangle r = c.modelToView(dotOffset);
rsDotRect.y = r.y;
rsDotRect.height = r.height;
} catch (BadLocationException ex) {
// Leave rsDotRect unchanged
}
}
示例6: adjustRectangularSelectionMouseX
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private void adjustRectangularSelectionMouseX(int x, int y) {
if (!rectangularSelection) {
return;
}
JTextComponent c = component;
int offset = c.viewToModel(new Point(x, y));
Rectangle r = null;;
if (offset >= 0) {
try {
r = c.modelToView(offset);
} catch (BadLocationException ex) {
r = null;
}
}
if (r != null) {
float xDiff = x - r.x;
if (xDiff > 0) {
float charWidth;
LockedViewHierarchy lvh = ViewHierarchy.get(c).lock();
try {
charWidth = lvh.getDefaultCharWidth();
} finally {
lvh.unlock();
}
int n = (int) (xDiff / charWidth);
r.x += n * charWidth;
r.width = (int) charWidth;
}
rsDotRect.x = r.x;
rsDotRect.width = r.width;
updateRectangularSelectionPaintRect();
fireStateChanged(null);
}
}
示例7: ensureVisible
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Ensure that the given region will be visible in the view
* with the appropriate find insets.
*/
private void ensureVisible(JTextComponent c, int startOffset, int endOffset, Insets extraInsets) {
try {
Rectangle startBounds = c.modelToView(startOffset);
Rectangle endBounds = c.modelToView(endOffset);
if (startBounds != null && endBounds != null) {
startBounds.add(endBounds);
if (extraInsets != null) {
Rectangle visibleBounds = c.getVisibleRect();
int extraTop = (extraInsets.top < 0)
? -extraInsets.top * visibleBounds.height / 100 // percentage
: extraInsets.top * endBounds.height; // line count
startBounds.y -= extraTop;
startBounds.height += extraTop;
startBounds.height += (extraInsets.bottom < 0)
? -extraInsets.bottom * visibleBounds.height / 100 // percentage
: extraInsets.bottom * endBounds.height; // line count
int extraLeft = (extraInsets.left < 0)
? -extraInsets.left * visibleBounds.width / 100 // percentage
: extraInsets.left * endBounds.width; // char count
startBounds.x -= extraLeft;
startBounds.width += extraLeft;
startBounds.width += (extraInsets.right < 0)
? -extraInsets.right * visibleBounds.width / 100 // percentage
: extraInsets.right * endBounds.width; // char count
}
c.scrollRectToVisible(startBounds);
}
} catch (BadLocationException e) {
// do not scroll
}
}
示例8: activate
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Activates parameter completion support.
*
* @see #deactivate()
*/
public void activate() {
if (active) {
return;
}
active = true;
JTextComponent tc = ac.getTextComponent();
lastSelectedParam = -1;
if (pc.getShowParameterToolTip()) {
tip = new ParameterizedCompletionDescriptionToolTip(
parentWindow, this, ac, pc);
try {
int dot = tc.getCaretPosition();
Rectangle r = tc.modelToView(dot);
Point p = new Point(r.x, r.y);
SwingUtilities.convertPointToScreen(p, tc);
r.x = p.x;
r.y = p.y;
tip.setLocationRelativeTo(r);
tip.setVisible(true);
} catch (BadLocationException ble) { // Should never happen
UIManager.getLookAndFeel().provideErrorFeedback(tc);
ble.printStackTrace();
tip = null;
}
}
listener.install(tc);
// First time through, we'll need to create this window.
if (paramChoicesWindow==null) {
paramChoicesWindow = createParamChoicesWindow();
}
lastSelectedParam = getCurrentParameterIndex();
prepareParamChoicesWindow();
paramChoicesWindow.setVisible(true);
}
示例9: prepareParamChoicesWindow
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Updates the optional window listing likely completion choices,
*/
private void prepareParamChoicesWindow() {
// If this window was set to null, the user pressed Escape to hide it
if (paramChoicesWindow!=null) {
int offs = getCurrentParameterStartOffset();
if (offs==-1) {
paramChoicesWindow.setVisible(false);
return;
}
JTextComponent tc = ac.getTextComponent();
try {
Rectangle r = tc.modelToView(offs);
Point p = new Point(r.x, r.y);
SwingUtilities.convertPointToScreen(p, tc);
r.x = p.x;
r.y = p.y;
paramChoicesWindow.setLocationRelativeTo(r);
} catch (BadLocationException ble) { // Should never happen
UIManager.getLookAndFeel().provideErrorFeedback(tc);
ble.printStackTrace();
}
// Toggles visibility, if necessary.
paramChoicesWindow.setParameter(lastSelectedParam, paramPrefix);
}
}
示例10: invokeDefaultAction
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
boolean invokeDefaultAction(boolean onlyActive) {
JTextComponent comp = getComponent();
if (comp == null) {
Logger.getLogger(HintsUI.class.getName()).log(Level.WARNING, "HintsUI.invokeDefaultAction called, but comp == null");
return false;
}
Document doc = comp.getDocument();
cancel.set(false);
if (doc instanceof BaseDocument) {
try {
Rectangle carretRectangle = comp.modelToView(comp.getCaretPosition());
int line = Utilities.getLineOffset((BaseDocument) doc, comp.getCaretPosition());
FixData fixes;
String description;
if (!onlyActive) {
refresh(doc, comp.getCaretPosition());
AnnotationHolder holder = getAnnotationHolder(doc);
Pair<FixData, String> fixData = holder != null ? holder.buildUpFixDataForLine(line) : null;
if (fixData == null) return false;
fixes = fixData.first();
description = fixData.second();
} else {
AnnotationDesc activeAnnotation = ((BaseDocument) doc).getAnnotations().getActiveAnnotation(line);
if (activeAnnotation == null) {
return false;
}
String type = activeAnnotation.getAnnotationType();
if (!fixableAnnotations.contains(type) && onlyActive) {
return false;
}
if (onlyActive) {
refresh(doc, comp.getCaretPosition());
}
Annotations annotations = ((BaseDocument) doc).getAnnotations();
AnnotationDesc desc = annotations.getAnnotation(line, type);
ParseErrorAnnotation annotation = null;
if (desc != null) {
annotations.frontAnnotation(desc);
annotation = findAnnotation(doc, desc, line);
}
if (annotation == null) {
return false;
}
fixes = annotation.getFixes();
description = annotation.getDescription();
}
Point p = comp.modelToView(Utilities.getRowStartFromLineOffset((BaseDocument) doc, line)).getLocation();
p.y += carretRectangle.height;
if(comp.getParent() instanceof JViewport) {
p.x += ((JViewport)comp.getParent()).getViewPosition().x;
}
if(comp.getParent() instanceof JLayeredPane &&
comp.getParent().getParent() instanceof JViewport) {
p.x += ((JViewport)comp.getParent().getParent()).getViewPosition().x;
}
showPopup(fixes, description, comp, p);
return true;
} catch (BadLocationException ex) {
ErrorManager.getDefault().notify(ex);
}
}
return false;
}
示例11: getNextVisualPositionFrom
import javax.swing.text.JTextComponent; //导入方法依赖的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;
}
示例12: moveDot
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/** Makes selection by moving dot but leaving mark.
*
* <p>
* <b>Note:</b> This method is deprecated and the present implementation
* ignores values of scrollRect and scrollPolicy parameters.
*
* @param offset offset in the document to which the caret should be positioned.
* @param scrollRect rectangle to which the editor window should be scrolled.
* @param scrollPolicy the way how scrolling should be done.
* One of <code>EditorUI.SCROLL_*</code> constants.
*
* @deprecated use #setDot(int) preceded by <code>JComponent.scrollRectToVisible()</code>.
*/
public void moveDot(int offset, Rectangle scrollRect, int scrollPolicy) {
if (LOG_EDT.isLoggable(Level.FINE)) { // Only permit operations in EDT
if (!SwingUtilities.isEventDispatchThread()) {
throw new IllegalStateException("BaseCaret.moveDot() not in EDT: offset=" + offset); // NOI18N
}
}
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("moveDot: offset=" + offset); //NOI18N
}
JTextComponent c = component;
if (c != null) {
BaseDocument doc = (BaseDocument)c.getDocument();
if (doc != null && offset >= 0 && offset <= doc.getLength()) {
doc.readLock();
try {
int oldCaretPos = getDot();
if (offset == oldCaretPos) { // no change
return;
}
caretPos = doc.createPosition(offset);
if (selectionVisible) { // selection already visible
Utilities.getEditorUI(c).repaintBlock(oldCaretPos, offset);
}
if (rectangularSelection) {
Rectangle r = c.modelToView(offset);
if (rsDotRect != null) {
rsDotRect.y = r.y;
rsDotRect.height = r.height;
} else {
rsDotRect = r;
}
updateRectangularSelectionPaintRect();
}
} catch (BadLocationException e) {
throw new IllegalStateException(e.toString());
// position is incorrect
} finally {
doc.readUnlock();
}
}
fireStateChanged();
dispatchUpdate(true);
}
}
示例13: extendRectangularSelection
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Extend rectangular selection either by char in a specified selection
* or by word (if ctrl is pressed).
*
* @param toRight true for right or false for left.
* @param ctrl
*/
public void extendRectangularSelection(boolean toRight, boolean ctrl) {
JTextComponent c = component;
Document doc = c.getDocument();
int dotOffset = getDot();
Element lineRoot = doc.getDefaultRootElement();
int lineIndex = lineRoot.getElementIndex(dotOffset);
Element lineElement = lineRoot.getElement(lineIndex);
float charWidth;
LockedViewHierarchy lvh = ViewHierarchy.get(c).lock();
try {
charWidth = lvh.getDefaultCharWidth();
} finally {
lvh.unlock();
}
int newDotOffset = -1;
try {
int newlineOffset = lineElement.getEndOffset() - 1;
Rectangle newlineRect = c.modelToView(newlineOffset);
if (!ctrl) {
if (toRight) {
if (rsDotRect.x < newlineRect.x) {
newDotOffset = dotOffset + 1;
} else {
rsDotRect.x += charWidth;
}
} else { // toLeft
if (rsDotRect.x > newlineRect.x) {
rsDotRect.x -= charWidth;
if (rsDotRect.x < newlineRect.x) { // Fix on rsDotRect
newDotOffset = newlineOffset;
}
} else {
newDotOffset = Math.max(dotOffset - 1, lineElement.getStartOffset());
}
}
} else { // With Ctrl
int numVirtualChars = 8; // Number of virtual characters per one Ctrl+Shift+Arrow press
if (toRight) {
if (rsDotRect.x < newlineRect.x) {
newDotOffset = Math.min(Utilities.getNextWord(c, dotOffset), lineElement.getEndOffset() - 1);
} else { // Extend virtually
rsDotRect.x += numVirtualChars * charWidth;
}
} else { // toLeft
if (rsDotRect.x > newlineRect.x) { // Virtually extended
rsDotRect.x -= numVirtualChars * charWidth;
if (rsDotRect.x < newlineRect.x) {
newDotOffset = newlineOffset;
}
} else {
newDotOffset = Math.max(Utilities.getPreviousWord(c, dotOffset), lineElement.getStartOffset());
}
}
}
if (newDotOffset != -1) {
rsDotRect = c.modelToView(newDotOffset);
moveDot(newDotOffset); // updates rs and fires state change
} else {
updateRectangularSelectionPaintRect();
fireStateChanged();
}
} catch (BadLocationException ex) {
// Leave selection as is
}
}
示例14: extendRectangularSelection
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Extend rectangular selection either by char in a specified selection
* or by word (if ctrl is pressed).
*
* @param toRight true for right or false for left.
* @param ctrl true for ctrl pressed.
*/
void extendRectangularSelection(boolean toRight, boolean ctrl) {
JTextComponent c = component;
Document doc = c.getDocument();
int dotOffset = getDot();
Element lineRoot = doc.getDefaultRootElement();
int lineIndex = lineRoot.getElementIndex(dotOffset);
Element lineElement = lineRoot.getElement(lineIndex);
float charWidth;
LockedViewHierarchy lvh = ViewHierarchy.get(c).lock();
try {
charWidth = lvh.getDefaultCharWidth();
} finally {
lvh.unlock();
}
int newDotOffset = -1;
try {
int newlineOffset = lineElement.getEndOffset() - 1;
Rectangle newlineRect = c.modelToView(newlineOffset);
if (!ctrl) {
if (toRight) {
if (rsDotRect.x < newlineRect.x) {
newDotOffset = dotOffset + 1;
} else {
rsDotRect.x += charWidth;
}
} else { // toLeft
if (rsDotRect.x > newlineRect.x) {
rsDotRect.x -= charWidth;
if (rsDotRect.x < newlineRect.x) { // Fix on rsDotRect
newDotOffset = newlineOffset;
}
} else {
newDotOffset = Math.max(dotOffset - 1, lineElement.getStartOffset());
}
}
} else { // With Ctrl
LineDocument lineDoc = LineDocumentUtils.asRequired(doc, LineDocument.class);
int numVirtualChars = 8; // Number of virtual characters per one Ctrl+Shift+Arrow press
if (toRight) {
if (rsDotRect.x < newlineRect.x) {
newDotOffset = Math.min(LineDocumentUtils.getNextWordStart(lineDoc, dotOffset), lineElement.getEndOffset() - 1);
} else { // Extend virtually
rsDotRect.x += numVirtualChars * charWidth;
}
} else { // toLeft
if (rsDotRect.x > newlineRect.x) { // Virtually extended
rsDotRect.x -= numVirtualChars * charWidth;
if (rsDotRect.x < newlineRect.x) {
newDotOffset = newlineOffset;
}
} else {
newDotOffset = Math.max(LineDocumentUtils.getPreviousWordStart(lineDoc, dotOffset), lineElement.getStartOffset());
}
}
}
if (newDotOffset != -1) {
rsDotRect = c.modelToView(newDotOffset);
moveDot(newDotOffset); // updates rs and fires state change
} else {
updateRectangularSelectionPaintRect();
fireStateChanged(null);
}
} catch (BadLocationException ex) {
// Leave selection as is
}
}
示例15: chooseAlternatives
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public static boolean chooseAlternatives(Document doc, int offset, String caption, List<AlternativeLocation> alternatives) {
Collections.sort(alternatives);
// Prune results a bit
int MAX_COUNT = 30; // Don't show more items than this
String previous = "";
GsfHtmlFormatter formatter = new GsfHtmlFormatter();
int count = 0;
List<AlternativeLocation> pruned = new ArrayList<AlternativeLocation>(alternatives.size());
for (AlternativeLocation alt : alternatives) {
String s = alt.getDisplayHtml(formatter);
if (!s.equals(previous)) {
pruned.add(alt);
previous = s;
count++;
if (count == MAX_COUNT) {
break;
}
}
}
alternatives = pruned;
if (alternatives.size() <= 1) {
return false;
}
JTextComponent target = findEditor(doc);
if (target != null) {
try {
Rectangle rectangle = target.modelToView(offset);
Point point = new Point(rectangle.x, rectangle.y+rectangle.height);
SwingUtilities.convertPointToScreen(point, target);
PopupUtil.showPopup(new DeclarationPopup(caption, alternatives), caption, point.x, point.y, true, 0);
return true;
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
}
return false;
}