javax.swing.text.JTextComponent#viewToModel ( )源码实例Demo

下面列出了javax.swing.text.JTextComponent#viewToModel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: netbeans   文件: CaretFoldExpanderImpl.java
@Override
public boolean checkExpandFold(JTextComponent c, Point p) {
    FoldHierarchy foldHierarchy = FoldHierarchy.get(c);
    foldHierarchy.lock();
    try {
        int offset = c.viewToModel(p);
        Iterator collapsedFoldIterator = FoldUtilities.collapsedFoldIterator(foldHierarchy, offset, offset);
        if (collapsedFoldIterator.hasNext()) {
            Fold fold = (Fold) collapsedFoldIterator.next();
            // Expand even if the offset is at fold's begining/end because that's what viewToModel() will return
            if (offset >= fold.getStartOffset() && offset <= fold.getEndOffset()) {
                foldHierarchy.expand(fold);
                return true;
            }
        }
        return false;
    } finally {
        foldHierarchy.unlock();
    }
}
 
源代码2 项目: netbeans   文件: BaseCaret.java
/**
 * Translates mouse event to text offset
 */
int mouse2Offset(MouseEvent evt) {
    JTextComponent c = component;
    int offset = 0;
    if (c != null) {
        int y = evt.getY();
        if (y < 0) {
            offset = 0;
        } else if (y > c.getSize().getHeight()) {
            offset = c.getDocument().getLength();
        } else {
            offset = c.viewToModel(new Point(evt.getX(), evt.getY()));
        }
    }
    return offset;
}
 
源代码3 项目: netbeans   文件: BaseCaret.java
/**
 * 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;
}
 
源代码4 项目: netbeans   文件: EditorCaret.java
/**
 * Translates mouse event to text offset
 */
int mouse2Offset(MouseEvent evt) {
    JTextComponent c = component;
    int offset = 0;
    if (c != null) {
        int y = evt.getY();
        if (y < 0) {
            offset = 0;
        } else if (y > c.getSize().getHeight()) {
            offset = c.getDocument().getLength();
        } else {
            offset = c.viewToModel(new Point(evt.getX(), evt.getY()));
        }
    }
    return offset;
}
 
源代码5 项目: netbeans   文件: NbToolTip.java
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;
}
 
源代码6 项目: netbeans   文件: BaseCaret.java
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();
    }
}
 
源代码7 项目: netbeans   文件: EditorCaret.java
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);
    }
}
 
源代码8 项目: netbeans   文件: EditorCaret.java
/**
    * 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;
   }
 
源代码9 项目: visualvm   文件: ActionUtils.java
/**
 * Get the closest position within the document of the component that
 * has given line and column.  
 * @param editor
 * @param line
 * @param column
 * @return the closest positon for the text component at given line and
 * column
 */
public static int getDocumentPosition(JTextComponent editor, int line,
        int column) {
    int lineHeight = editor.getFontMetrics(editor.getFont()).getHeight();
    int charWidth = editor.getFontMetrics(editor.getFont()).charWidth('m');
    int y = line * lineHeight;
    int x = column * charWidth;
    Point pt = new Point(x, y);
    int pos = editor.viewToModel(pt);
    return pos;
}
 
源代码10 项目: jpexs-decompiler   文件: ActionUtils.java
/**
 * Gets the column number at given position of editor.  The first column is
 * ZERO
 * @param editor
 * @param pos
 * @return the 0 based column number
 * @throws javax.swing.text.BadLocationException
 */
public static int getColumnNumber(JTextComponent editor, int pos)
	throws BadLocationException {
	// speedup if the pos is 0
	if(pos == 0) {
		return 0;
	}
	Rectangle r = editor.modelToView(pos);
	int start = editor.viewToModel(new Point(0, r.y));
	int column = pos - start;
	return column;
}
 
源代码11 项目: jpexs-decompiler   文件: ActionUtils.java
/**
 * Get the closest position within the document of the component that
 * has given line and column.
 * @param editor
 * @param line the first being 1
 * @param column the first being 1
 * @return the closest positon for the text component at given line and
 * column
 */
public static int getDocumentPosition(JTextComponent editor, int line,
	int column) {
	int lineHeight = editor.getFontMetrics(editor.getFont()).getHeight();
	int charWidth = editor.getFontMetrics(editor.getFont()).charWidth('m');
	int y = line * lineHeight;
	int x = column * charWidth;
	Point pt = new Point(x, y);
	int pos = editor.viewToModel(pt);
	return pos;
}
 
源代码12 项目: pumpernickel   文件: UnderlineHighlightPainter.java
@Override
public void paint(Graphics g0, int p0, int p1, Shape bounds,
		JTextComponent c) {
	Graphics2D g = (Graphics2D) g0.create();
	try {
		int length = c.getDocument().getLength();
		p0 = Math.min(Math.max(0, p0), length);
		p1 = Math.min(Math.max(0, p1), length);
		Rectangle rect0 = c.modelToView(p0);
		Rectangle rect1 = c.modelToView(p1);
		if (rect0.y + rect0.height == rect1.y + rect1.height) {
			drawLine(g, p0, p1, rect0.x, rect1.x + rect1.width, rect0.y
					+ rect0.height, squiggle);
		} else {
			int currentY = rect0.y + rect0.height;
			int startX = rect0.x;

			Rectangle r = rect0;
			Point p = new Point(1000000, 0);
			int lastPos = p0;
			drawLines: while (true) {
				p.y = r.y + r.height / 2;
				int pos = c.viewToModel(p);
				if (p1 <= pos) {
					drawLine(g, lastPos, p1, startX, rect1.x + rect1.width,
							currentY, squiggle);
					break drawLines;
				} else {
					r = c.modelToView(Math.min(pos, p1));
					drawLine(g, lastPos, pos, startX, r.x + r.width,
							currentY, squiggle);

					lastPos = pos + 1;
					r = c.getUI().modelToView(c, lastPos, Bias.Forward);
					startX = r.x;
					currentY = r.y + r.height;
				}
			}
		}
	} catch (BadLocationException e) {
		throw new RuntimeException(e);
	} finally {
		g.dispose();
	}
}
 
源代码13 项目: visualvm   文件: ActionUtils.java
/**
 * Gets the column number at given position of editor.  The first column is
 * ZERO
 * @param editor
 * @param pos
 * @return the 0 based column number
 * @throws javax.swing.text.BadLocationException
 */
public static int getColumnNumber(JTextComponent editor, int pos)
        throws BadLocationException {
    Rectangle r = editor.modelToView(pos);
    int start = editor.viewToModel(new Point(0, r.y));
    int column = pos - start;
    return column;
}