Java Code Examples for javax.swing.text.JTextComponent#viewToModel()

The following examples show how to use javax.swing.text.JTextComponent#viewToModel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CaretFoldExpanderImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example 2
Source File: BaseCaret.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: BaseCaret.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: EditorCaret.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: NbToolTip.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 6
Source File: BaseCaret.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 7
Source File: EditorCaret.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 8
Source File: EditorCaret.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
    * 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;
   }
 
Example 9
Source File: ActionUtils.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source File: ActionUtils.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: ActionUtils.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: UnderlineHighlightPainter.java    From pumpernickel with MIT License 4 votes vote down vote up
@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();
	}
}
 
Example 13
Source File: ActionUtils.java    From visualvm with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}