Java Code Examples for javax.swing.text.JTextComponent#modelToView()
The following examples show how to use
javax.swing.text.JTextComponent#modelToView() .
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: GuiUtil.java From CQL with GNU Affero General Public License v3.0 | 6 votes |
public static void centerLineInScrollPane(JTextComponent component) { Container container = SwingUtilities.getAncestorOfClass(JViewport.class, component); if (container == null) return; try { Rectangle r = component.modelToView(component.getCaretPosition()); // Rectangle2D r = component.modelToView2D(component.getCaretPosition()); JViewport viewport = (JViewport) container; if (r == null) { // || viewport == null) { return; } int extentHeight = viewport.getExtentSize().height; int viewHeight = viewport.getViewSize().height; int y = Math.max(0, r.y - ((extentHeight - r.height) / 2)); // double y = Math.max(0, r.getY() - ((extentHeight - r.getHeight()) / 2)); y = Math.min(y, viewHeight - extentHeight); viewport.setViewPosition(new Point(0, y)); // viewport.setViewPosition(new Point(0, (int) y)); } catch (BadLocationException ble) { } }
Example 2
Source File: NbToolTip.java From netbeans with Apache License 2.0 | 6 votes |
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 3
Source File: GoToDecoratorAtCaretAction.java From netbeans with Apache License 2.0 | 6 votes |
private void showPopup( LinkedHashSet<TypeElement> elements , CompilationController controller, MetadataModel<WebBeansModel> model ,JTextComponent target ) { List<ElementHandle<? extends Element>> handles = new ArrayList< ElementHandle<? extends Element>>(elements.size()); for (TypeElement element : elements) { handles.add( ElementHandle.create( element )); } StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage( InjectablesModel.class, "LBL_WaitNode")); try { Rectangle rectangle = target.modelToView(target.getCaret().getDot()); Point point = new Point(rectangle.x, rectangle.y + rectangle.height); SwingUtilities.convertPointToScreen(point, target); String title = NbBundle.getMessage( GoToInjectableAtCaretAction.class, "LBL_ChooseDecorator"); PopupUtil.showPopup(new InjectablesPopup(title, handles, controller), title, point.x, point.y); } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } }
Example 4
Source File: ReflectCompletionDialog.java From jpexs-decompiler with GNU General Public License v3.0 | 6 votes |
/** * Display the dialog. * @param target text component (its Window will be the parent) */ public void displayFor(JTextComponent target) { try { int dot = target.getSelectionStart(); Window window = SwingUtilities.getWindowAncestor(target); Rectangle rt = target.modelToView(dot); Point loc = new Point(rt.x, rt.y); // convert the location from Text Componet coordinates to // Frame coordinates... loc = SwingUtilities.convertPoint(target, loc, window); // and then to Screen coordinates SwingUtilities.convertPointToScreen(loc, window); setLocationRelativeTo(window); setLocation(loc); } catch (BadLocationException ex) { Logger.getLogger(ReflectCompletionDialog.class.getName()).log(Level.SEVERE, null, ex); } finally { setFonts(target.getFont()); updateItems(); jTxtItem.setText(target.getSelectedText()); setVisible(true); } }
Example 5
Source File: CurrentLineHighlighter.java From nextreports-designer with Apache License 2.0 | 6 votes |
/** * Fetches the previous caret location, stores the current caret location, * If the caret is on another line, repaint the previous line and the current line * * @param c the text component */ private static void currentLineChanged(JTextComponent c) { try { int previousCaret = ((Integer) c.getClientProperty(PREVIOUS_CARET)).intValue(); Rectangle prev = c.modelToView(previousCaret); Rectangle r = c.modelToView(c.getCaretPosition()); c.putClientProperty(PREVIOUS_CARET, new Integer(c.getCaretPosition())); if ((prev != null) && (prev.y != r.y)) { c.repaint(0, prev.y, c.getWidth(), r.height); c.repaint(0, r.y, c.getWidth(), r.height); } } catch (BadLocationException e) { // ignore } }
Example 6
Source File: LineHighlighter.java From darklaf with MIT License | 5 votes |
public void paint(final Graphics g, final int p0, final int p1, final Shape bounds, final JTextComponent c) { try { Rectangle r = c.modelToView(c.getCaretPosition()); g.setColor(color); g.fillRect(0, r.y, c.getWidth(), r.height); if (lastView == null) { lastView = r; } } catch (BadLocationException ble) { ble.printStackTrace(); } }
Example 7
Source File: MainPanel.java From java-swing-tips with MIT License | 5 votes |
private static void scrollToCenter(JTextComponent tc, int pos) throws BadLocationException { Rectangle rect = tc.modelToView(pos); // Java 9: Rectangle rect = tc.modelToView2D(pos).getBounds(); Container c = SwingUtilities.getAncestorOfClass(JViewport.class, tc); if (Objects.nonNull(rect) && c instanceof JViewport) { rect.x = Math.round(rect.x - c.getWidth() / 2f); rect.width = c.getWidth(); rect.height = Math.round(c.getHeight() / 2f); tc.scrollRectToVisible(rect); } }
Example 8
Source File: JavaOverviewSummary.java From visualvm with GNU General Public License v2.0 | 5 votes |
protected JViewport createViewport() { return new JViewport() { public void scrollRectToVisible(Rectangle aRect) { if (getView() instanceof JTextComponent) { try { JTextComponent tc = (JTextComponent)getView(); Caret caret = tc.getCaret(); Rectangle selStart = tc.modelToView(Math.min(caret.getDot(), caret.getMark())); Rectangle selEnd = tc.modelToView(Math.max(caret.getDot(), caret.getMark())); int x = Math.min(selStart.x, selEnd.x); int xx = Math.max(selStart.x + selStart.width, selEnd.x + selEnd.width); int y = Math.min(selStart.y, selEnd.y); int yy = Math.max(selStart.y + selStart.height, selEnd.y + selEnd.height); Rectangle r = new Rectangle(x, y, xx - x, yy - y); super.scrollRectToVisible(SwingUtilities.convertRectangle(tc, r, this)); } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } } else { super.scrollRectToVisible(aRect); } aRect = SwingUtilities.convertRectangle(this, aRect, getParent()); ((JComponent)getParent()).scrollRectToVisible(aRect); } }; }
Example 9
Source File: EditorFindSupport.java From netbeans with Apache License 2.0 | 5 votes |
/** * 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 } }
Example 10
Source File: EditorCaret.java From netbeans with Apache License 2.0 | 5 votes |
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 11
Source File: EditorCaret.java From netbeans with Apache License 2.0 | 5 votes |
/** * */ 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 } }
Example 12
Source File: BaseCaret.java From netbeans with Apache License 2.0 | 5 votes |
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 13
Source File: Utility.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
/** * 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(); } }
Example 14
Source File: CurrentLineHighlighter.java From nextreports-designer with Apache License 2.0 | 5 votes |
public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) { try { Rectangle r = c.modelToView(c.getCaretPosition()); // if (c.getSelectedText() == null) { // no selection g.setColor(color); g.fillRect(0, r.y, c.getWidth(), r.height); // } else { // selection // c.repaint(0, r.y, c.getWidth(), r.height); // } } catch (BadLocationException e) { // ignore } }
Example 15
Source File: Utility.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
/** * 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(); } }
Example 16
Source File: Utility.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
/** * 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(); } }
Example 17
Source File: ComboCompletionAction.java From visualvm with GNU General Public License v2.0 | 4 votes |
public void actionPerformed(ActionEvent e) { JTextComponent target = getTextComponent(e); if (target != null && target.getDocument() instanceof SyntaxDocument) { SyntaxDocument sDoc = (SyntaxDocument) target.getDocument(); int dot = target.getCaretPosition(); Token token = sDoc.getTokenAt(dot); String abbrev = ""; try { if (token != null) { abbrev = token.getText(sDoc); while (CLOSING.contains(abbrev)) { token = sDoc.getTokenAt(token.start - 1); abbrev = token.getText(sDoc); } if (MEMBER_SEPARATOR.equals(abbrev)) { abbrev = "[" + ActionUtils.getTokenStringAt(sDoc, token.start - 1) + "]" + abbrev; } else { Token prev = sDoc.getTokenAt(token.start - 1); if (prev != null && MEMBER_SEPARATOR.equals(prev.getText(sDoc))) { abbrev = "[" + ActionUtils.getTokenStringAt(sDoc, prev.start - 1) + "]" + MEMBER_SEPARATOR + abbrev; } } sDoc.remove(token.start, token.length); dot = token.start; } Frame frame = ActionUtils.getFrameFor(target); if (dlg == null) { dlg = new ComboCompletionDialog(frame, true, items); } dlg.setLocationRelativeTo(frame); Point p = frame.getLocation(); // Get location of Dot in rt Rectangle rt = target.modelToView(dot); Point loc = new Point(rt.x, rt.y); // convert the location from Text Componet coordinates to // Frame coordinates... loc = SwingUtilities.convertPoint(target, loc, frame); // and then to Screen coordinates SwingUtilities.convertPointToScreen(loc, frame); dlg.setLocation(loc); dlg.setFonts(target.getFont()); dlg.setText(abbrev); dlg.setVisible(true); String res = dlg.getResult(); ActionUtils.insertMagicString(target, dot, res); } catch (BadLocationException ex) { Logger.getLogger(ComboCompletionAction.class.getName()).log(Level.SEVERE, null, ex); } } }
Example 18
Source File: UnderlineHighlightPainter.java From pumpernickel with MIT License | 4 votes |
@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 19
Source File: GoToSupport.java From netbeans with Apache License 2.0 | 4 votes |
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; }
Example 20
Source File: OverrideEditorActions.java From netbeans with Apache License 2.0 | 4 votes |
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { Caret caret = target.getCaret(); BaseDocument doc = (BaseDocument)target.getDocument(); try { int dot = caret.getDot(); // #232675: if bounds are defined, use them rather than line start/end Object o = target.getClientProperty(PROP_NAVIGATE_BOUNDARIES); PositionRegion bounds = null; int lineStartPos = Utilities.getRowStart(target, dot); if (o instanceof PositionRegion) { bounds = (PositionRegion)o; int start = bounds.getStartOffset(); int end = bounds.getEndOffset(); int boundLineStart = Utilities.getRowStart(target, start); // refinement: only use the boundaries if the caret is at the same line // as boundary start; otherwise ignore the boundary and use document lines. if (boundLineStart == lineStartPos && dot > start && dot <= end) { // move to the region start dot = start; } else { bounds = null; } } if (bounds == null) { if (homeKeyColumnOne) { // to first column dot = lineStartPos; } else { // either to line start or text start int textStartPos = Utilities.getRowFirstNonWhite(doc, lineStartPos); if (textStartPos < 0) { // no text on the line textStartPos = Utilities.getRowEnd(target, lineStartPos); } if (dot == lineStartPos) { // go to the text start pos dot = textStartPos; } else if (dot <= textStartPos) { dot = lineStartPos; } else { dot = textStartPos; } } } // For partial view hierarchy check bounds dot = Math.max(dot, target.getUI().getRootView(target).getStartOffset()); String actionName = (String) getValue(Action.NAME); boolean select = selectionBeginLineAction.equals(actionName) || selectionLineFirstColumnAction.equals(actionName); // If possible scroll the view to its begining horizontally // to ease user's orientation in the code. Rectangle r = target.modelToView(dot); Rectangle visRect = target.getVisibleRect(); if (r.getMaxX() < visRect.getWidth()) { r.x = 0; target.scrollRectToVisible(r); } target.putClientProperty("navigational.action", SwingConstants.WEST); if (select) { caret.moveDot(dot); } else { caret.setDot(dot); } } catch (BadLocationException e) { target.getToolkit().beep(); } } }