Java Code Examples for javax.swing.JEditorPane#scrollRectToVisible()

The following examples show how to use javax.swing.JEditorPane#scrollRectToVisible() . 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: JEditorPaneTagJavaElement.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
@Override
public Object _makeVisible() {
    JEditorPane editor = (JEditorPane) parent.getComponent();
    Iterator iterator = findTag((HTMLDocument) editor.getDocument());
    int startOffset = iterator.getStartOffset();
    int endOffset = iterator.getEndOffset();
    try {
        Rectangle bounds = editor.modelToView(startOffset + (endOffset - startOffset) / 2);
        if (bounds != null) {
            bounds.height = editor.getVisibleRect().height;
            editor.scrollRectToVisible(bounds);
        }
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Unable to get text for tag " + tag + " in document with index " + index, e);
    }
    return null;
}
 
Example 2
Source File: OpenAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void performOpen(JEditorPane[] panes) {
    if (panes == null || panes.length == 0) {
        StatusDisplayer.getDefault().setStatusText(Bundle.ERR_ShellConsoleClosed());
        return;
    }
    JEditorPane p = panes[0];
    Rng[] fragments = theHandle.getFragments();
    
    int to = fragments[0].start;
    p.requestFocus();
    int pos = Math.min(p.getDocument().getLength() - 1, to);
    p.setCaretPosition(pos);
    try {
        Rectangle r = p.modelToView(pos);
        p.scrollRectToVisible(r);
    } catch (BadLocationException ex) {
    }
}
 
Example 3
Source File: JEditorPanePosJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@Override
public Object _makeVisible() {
    JEditorPane editor = (JEditorPane) parent.getComponent();
    try {
        Rectangle bounds = editor.modelToView(pos);
        if (bounds != null) {
            bounds.height = editor.getVisibleRect().height;
            editor.scrollRectToVisible(bounds);
        }
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Invalid position " + pos + "(" + e.getMessage() + ")", e);
    }
    return null;
}
 
Example 4
Source File: DownloadMapsWindow.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
private static JList<String> newGameSelectionList(
    final DownloadFileDescription selectedMap,
    final List<DownloadFileDescription> maps,
    final JEditorPane descriptionPanel,
    final DefaultListModel<String> model) {
  final JList<String> gamesList = new JList<>(model);
  final int selectedIndex = maps.indexOf(selectedMap);
  gamesList.setSelectedIndex(selectedIndex);

  final String text = maps.get(selectedIndex).toHtmlString();
  descriptionPanel.setText(text);
  descriptionPanel.scrollRectToVisible(new Rectangle(0, 0, 0, 0));
  return gamesList;
}
 
Example 5
Source File: DownloadMapsWindow.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
private static ListSelectionListener newDescriptionPanelUpdatingSelectionListener(
    final JEditorPane descriptionPanel,
    final JList<String> gamesList,
    final List<DownloadFileDescription> maps,
    final MapAction action,
    final JLabel mapSizeLabelToUpdate) {
  return e -> {
    if (!e.getValueIsAdjusting()) {
      final int index = gamesList.getSelectedIndex();

      final boolean somethingIsSelected = index >= 0;
      if (somethingIsSelected) {
        final String mapName = gamesList.getModel().getElementAt(index);

        // find the map description by map name and update the map download detail panel
        final Optional<DownloadFileDescription> map =
            maps.stream()
                .filter(mapDescription -> mapDescription.getMapName().equals(mapName))
                .findFirst();
        if (map.isPresent()) {
          final String text = map.get().toHtmlString();
          descriptionPanel.setText(text);
          descriptionPanel.scrollRectToVisible(new Rectangle(0, 0, 0, 0));
          updateMapUrlAndSizeLabel(map.get(), action, mapSizeLabelToUpdate);
        }
      }
    }
  };
}
 
Example 6
Source File: CloneableEditorSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Forcibly create one editor component. Then set the caret
* to the given position.
* @param pos where to place the caret
* @param column where to place the caret
* @param reuse if true, the infrastructure tries to reuse other, already opened editor
 * for the purpose of opening this file/line. 
* @return always non-<code>null</code> editor
*/
private final Pane openAtImpl(final PositionRef pos, final int column, boolean reuse) {
    CloneableEditorSupport redirect = CloneableEditorSupportRedirector.findRedirect(this);
    if (redirect != null) {
        return redirect.openAtImpl(pos, column, reuse);
    }
    final Pane e = openPane(reuse);
    final Task t = prepareDocument();
    e.ensureVisible();
    class Selector implements TaskListener, Runnable {
        private boolean documentLocked = false;

        public void taskFinished(org.openide.util.Task t2) {
            javax.swing.SwingUtilities.invokeLater(this);
            t2.removeTaskListener(this);
        }

        public void run() {
            // #25435. Pane can be null.
            JEditorPane ePane = e.getEditorPane();

            if (ePane == null) {
                return;
            }

            StyledDocument doc = getDocument();

            if (doc == null) {
                return; // already closed or error loading
            }

            if (!documentLocked) {
                documentLocked = true;
                doc.render(this);
            } else {
                Caret caret = ePane.getCaret();

                if (caret == null) {
                    return;
                }

                int offset;

                // Pane's document may differ - see #204980
                Document paneDoc = ePane.getDocument();
                if (paneDoc instanceof StyledDocument && paneDoc != doc) {
                    if (ERR.isLoggable(Level.FINE)) {
                        ERR.fine("paneDoc=" + paneDoc + "\n !=\ndoc=" + doc); // NOI18N
                    }
                    doc = (StyledDocument) paneDoc;
                }

                javax.swing.text.Element el = NbDocument.findLineRootElement(doc);
                el = el.getElement(el.getElementIndex(pos.getOffset()));
                offset = el.getStartOffset() + Math.max(0, column);

                if (offset > el.getEndOffset()) {
                    offset = Math.max(el.getStartOffset(), el.getEndOffset() - 1);
                }

                caret.setDot(offset);

                try { // scroll to show reasonable part of the document
                    Rectangle r = ePane.modelToView(offset);
                    if (r != null) {
                        r.height *= 5;
                        ePane.scrollRectToVisible(r);
                    }
                } catch (BadLocationException ex) {
                    ERR.log(Level.WARNING, "Can't scroll to text: pos.getOffset=" + pos.getOffset() //NOI18N
                        + ", column=" + column + ", offset=" + offset //NOI18N
                        + ", doc.getLength=" + doc.getLength(), ex); //NOI18N
                }
            }
        }
    }
    t.addTaskListener(new Selector());

    return e;
}
 
Example 7
Source File: FmtOptions.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void refreshPreview() {
    JEditorPane pane = (JEditorPane) getPreviewComponent();
    try {
        int rm = previewPrefs.getInt(rightMargin, provider.getDefaultAsInt(rightMargin));
        pane.putClientProperty("TextLimitLine", rm); //NOI18N
    }
    catch( NumberFormatException e ) {
        // Ignore it
    }

    Rectangle visibleRectangle = pane.getVisibleRect();
    pane.setText(previewText);
    pane.setIgnoreRepaint(true);

    final Document doc = pane.getDocument();
    if (doc instanceof BaseDocument) {
        final Reformat reformat = Reformat.get(doc);
        reformat.lock();
        try {
            ((BaseDocument) doc).runAtomic(new Runnable() {
                @Override
                public void run() {

                    try {
                        reformat.reformat(0, doc.getLength());
                    } catch (BadLocationException ble) {
                        LOGGER.log(Level.WARNING, null, ble);
                    }
                }
            });
        } finally {
            reformat.unlock();
        }
    } else {
        LOGGER.warning(String.format("Can't format %s; it's not BaseDocument.", doc)); //NOI18N
    }
    pane.setIgnoreRepaint(false);
    pane.scrollRectToVisible(visibleRectangle);
    pane.repaint(100);

}
 
Example 8
Source File: FmtOptions.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void refreshPreview() {
    JEditorPane pane = (JEditorPane) getPreviewComponent();
    try {
        int rm = previewPrefs.getInt(RIGHT_MARGIN, getDefaultAsInt(RIGHT_MARGIN));
        pane.putClientProperty("TextLimitLine", rm); //NOI18N
    } catch (NumberFormatException e) {
        // Ignore it
    }

    Rectangle visibleRectangle = pane.getVisibleRect();
    pane.setText(previewText);
    pane.setIgnoreRepaint(true);

    final Document doc = pane.getDocument();
    if (doc instanceof BaseDocument) {
        final Reformat reformat = Reformat.get(doc);
        reformat.lock();
        try {
            ((BaseDocument) doc).runAtomic(new Runnable() {
                @Override
                public void run() {

                    try {
                        reformat.reformat(0, doc.getLength());
                    } catch (BadLocationException ble) {
                        LOGGER.log(Level.WARNING, null, ble);
                    }
                }
            });
        } finally {
            reformat.unlock();
        }
    } else {
        LOGGER.warning(String.format("Can't format %s; it's not BaseDocument.", doc)); //NOI18N
    }
    pane.setIgnoreRepaint(false);
    pane.scrollRectToVisible(visibleRectangle);
    pane.repaint(100);

}
 
Example 9
Source File: FmtOptions.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void refreshPreview() {
    JEditorPane pane = (JEditorPane) getPreviewComponent();
    try {
        int rm = previewPrefs.getInt(rightMargin, getDefaultAsInt(rightMargin));
        pane.putClientProperty("TextLimitLine", rm); //NOI18N
    }
    catch( NumberFormatException e ) {
        // Ignore it
    }

    Rectangle visibleRectangle = pane.getVisibleRect();
    pane.setText(previewText);
    pane.setIgnoreRepaint(true);

    final Document doc = pane.getDocument();
    if (doc instanceof BaseDocument) {
        final Reformat reformat = Reformat.get(doc);
        reformat.lock();
        try {
            ((BaseDocument) doc).runAtomic(new Runnable() {
                @Override
                public void run() {

                    try {
                        reformat.reformat(0, doc.getLength());
                    } catch (BadLocationException ble) {
                        LOGGER.log(Level.WARNING, null, ble);
                    }
                }
            });
        } finally {
            reformat.unlock();
        }
    } else {
        LOGGER.warning(String.format("Can't format %s; it's not BaseDocument.", doc)); //NOI18N
    }
    pane.setIgnoreRepaint(false);
    pane.scrollRectToVisible(visibleRectangle);
    pane.repaint(100);

}